Skip to content

Commit cc380a2

Browse files
committed
test: Command transmission fallback handling
refs #950
1 parent 06da58c commit cc380a2

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

Diff for: test/php/Lib/StrikingCommandTransport.php

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Tests\Icinga\Module\Icingadb\Lib;
4+
5+
use Icinga\Application\Config;
6+
use Icinga\Data\ConfigObject;
7+
use Icinga\Module\Icingadb\Command\IcingaApiCommand;
8+
use Icinga\Module\Icingadb\Command\Transport\ApiCommandTransport;
9+
use Icinga\Module\Icingadb\Command\Transport\CommandTransport;
10+
use Icinga\Module\Icingadb\Command\Transport\CommandTransportException;
11+
12+
class StrikingCommandTransport extends CommandTransport
13+
{
14+
public static function getConfig(): Config
15+
{
16+
return Config::fromArray(['endpoint1' => ['host' => 'endpointA'], 'endpoint2' => ['host' => 'endpointB']]);
17+
}
18+
19+
public static function createTransport(ConfigObject $config): ApiCommandTransport
20+
{
21+
return (new class extends ApiCommandTransport {
22+
protected function sendCommand(IcingaApiCommand $command)
23+
{
24+
throw new CommandTransportException(sprintf('%s strikes!', $this->getHost()));
25+
}
26+
})->setHost($config->host);
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Tests\Icinga\Module\Icingadb\Command\Transport;
4+
5+
use Icinga\Module\Icingadb\Command\Object\AddCommentCommand;
6+
use Icinga\Module\Icingadb\Command\Transport\CommandTransportException;
7+
use Icinga\Module\Icingadb\Model\Host;
8+
use PHPUnit\Framework\TestCase;
9+
use Tests\Icinga\Module\Icingadb\Lib\StrikingCommandTransport;
10+
11+
class CommandTransportTest extends TestCase
12+
{
13+
public function testFallbackHandling()
14+
{
15+
$this->expectException(CommandTransportException::class);
16+
$this->expectExceptionMessage('endpointB strikes!');
17+
18+
(new StrikingCommandTransport())->send(
19+
(new AddCommentCommand())
20+
->setExpireTime(42)
21+
->setAuthor('GLaDOS')
22+
->setComment('The cake is a lie')
23+
->setObjects(new \CallbackFilterIterator(new \ArrayIterator([
24+
(new Host())->setProperties(['name' => 'host1']),
25+
(new Host())->setProperties(['name' => 'host2']),
26+
]), function ($host) {
27+
return $host->name === 'host2';
28+
}))
29+
);
30+
}
31+
32+
public function testGeneratorsAreNotSupported()
33+
{
34+
$this->expectException(\InvalidArgumentException::class);
35+
$this->expectExceptionMessage('Generators are not supported');
36+
37+
(new StrikingCommandTransport())->send(
38+
(new AddCommentCommand())
39+
->setExpireTime(42)
40+
->setAuthor('GLaDOS')
41+
->setComment('The cake is a lie')
42+
->setObjects((function () {
43+
yield (new Host())->setProperties(['name' => 'host1']);
44+
yield (new Host())->setProperties(['name' => 'host2']);
45+
})())
46+
);
47+
}
48+
}

0 commit comments

Comments
 (0)