From a0bb146790cc2c6b1615e8a50c6b19becd963250 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Mon, 18 Mar 2024 13:34:14 +0100 Subject: [PATCH] test: Command transmission fallback handling refs #950 --- test/php/Lib/StrikingCommandTransport.php | 28 +++++++++++ .../Transport/CommandTransportTest.php | 48 +++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 test/php/Lib/StrikingCommandTransport.php create mode 100644 test/php/library/Icingadb/Command/Transport/CommandTransportTest.php diff --git a/test/php/Lib/StrikingCommandTransport.php b/test/php/Lib/StrikingCommandTransport.php new file mode 100644 index 00000000..5e14ef9d --- /dev/null +++ b/test/php/Lib/StrikingCommandTransport.php @@ -0,0 +1,28 @@ + ['host' => 'endpointA'], 'endpoint2' => ['host' => 'endpointB']]); + } + + public static function createTransport(ConfigObject $config): ApiCommandTransport + { + return (new class extends ApiCommandTransport { + protected function sendCommand(IcingaApiCommand $command) + { + throw new CommandTransportException(sprintf('%s strikes!', $this->getHost())); + } + })->setHost($config->host); + } +} diff --git a/test/php/library/Icingadb/Command/Transport/CommandTransportTest.php b/test/php/library/Icingadb/Command/Transport/CommandTransportTest.php new file mode 100644 index 00000000..63a1b668 --- /dev/null +++ b/test/php/library/Icingadb/Command/Transport/CommandTransportTest.php @@ -0,0 +1,48 @@ +expectException(CommandTransportException::class); + $this->expectExceptionMessage('endpointB strikes!'); + + (new StrikingCommandTransport())->send( + (new AddCommentCommand()) + ->setExpireTime(42) + ->setAuthor('GLaDOS') + ->setComment('The cake is a lie') + ->setObjects(new \CallbackFilterIterator(new \ArrayIterator([ + (new Host())->setProperties(['name' => 'host1']), + (new Host())->setProperties(['name' => 'host2']), + ]), function ($host) { + return $host->name === 'host2'; + })) + ); + } + + public function testGeneratorsAreNotSupported() + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Generators are not supported'); + + (new StrikingCommandTransport())->send( + (new AddCommentCommand()) + ->setExpireTime(42) + ->setAuthor('GLaDOS') + ->setComment('The cake is a lie') + ->setObjects((function () { + yield (new Host())->setProperties(['name' => 'host1']); + yield (new Host())->setProperties(['name' => 'host2']); + })()) + ); + } +}