From 7c19368db58f16858fde7f1c5fe50c0d640c0482 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 13 Apr 2024 13:45:00 +0200 Subject: Adding upstream version 1.1.2. Signed-off-by: Daniel Baumann --- test/php/Lib/StrikingCommandTransport.php | 28 ++++ .../Command/Transport/CommandTransportTest.php | 48 ++++++ .../Model/Behavior/FlattenedObjectVarsTest.php | 161 +++++++++++++++++++++ 3 files changed, 237 insertions(+) create mode 100644 test/php/Lib/StrikingCommandTransport.php create mode 100644 test/php/library/Icingadb/Command/Transport/CommandTransportTest.php create mode 100644 test/php/library/Icingadb/Model/Behavior/FlattenedObjectVarsTest.php (limited to 'test/php') diff --git a/test/php/Lib/StrikingCommandTransport.php b/test/php/Lib/StrikingCommandTransport.php new file mode 100644 index 0000000..5e14ef9 --- /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 0000000..63a1b66 --- /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']); + })()) + ); + } +} diff --git a/test/php/library/Icingadb/Model/Behavior/FlattenedObjectVarsTest.php b/test/php/library/Icingadb/Model/Behavior/FlattenedObjectVarsTest.php new file mode 100644 index 0000000..7cfd97a --- /dev/null +++ b/test/php/library/Icingadb/Model/Behavior/FlattenedObjectVarsTest.php @@ -0,0 +1,161 @@ += ?)) OR host.id IS NULL) +ORDER BY host.id +SQL; + + private const DOUBLE_UNEQUAL_RESULT = <<<'SQL' +SELECT host.id +FROM host +WHERE (host.id NOT IN ((SELECT sub_customvar_flat_host.id AS sub_customvar_flat_host_id + FROM customvar_flat sub_customvar_flat + INNER JOIN host_customvar sub_customvar_flat_host_customvar + ON sub_customvar_flat_host_customvar.customvar_id = + sub_customvar_flat.customvar_id + INNER JOIN host sub_customvar_flat_host + ON sub_customvar_flat_host.id = sub_customvar_flat_host_customvar.host_id + WHERE (((sub_customvar_flat.flatname = ?) AND (sub_customvar_flat.flatvalue = ?)) OR + ((sub_customvar_flat.flatname = ?) AND (sub_customvar_flat.flatvalue = ?))) + AND (sub_customvar_flat_host.id IS NOT NULL) + GROUP BY sub_customvar_flat_host.id + HAVING COUNT(DISTINCT sub_customvar_flat.id) >= ?)) OR host.id IS NULL) +ORDER BY host.id +SQL; + + private const EQUAL_UNEQUAL_RESULT = <<<'SQL' +SELECT host.id +FROM host +WHERE ((host.id NOT IN ((SELECT sub_customvar_flat_host.id AS sub_customvar_flat_host_id + FROM customvar_flat sub_customvar_flat + INNER JOIN host_customvar sub_customvar_flat_host_customvar + ON sub_customvar_flat_host_customvar.customvar_id = + sub_customvar_flat.customvar_id + INNER JOIN host sub_customvar_flat_host + ON sub_customvar_flat_host.id = sub_customvar_flat_host_customvar.host_id + WHERE ((sub_customvar_flat.flatname = ?) AND (sub_customvar_flat.flatvalue = ?)) + AND (sub_customvar_flat_host.id IS NOT NULL) + GROUP BY sub_customvar_flat_host.id + HAVING COUNT(DISTINCT sub_customvar_flat.id) >= ?)) OR host.id IS NULL)) + AND (host.id IN ((SELECT sub_customvar_flat_host.id AS sub_customvar_flat_host_id + FROM customvar_flat sub_customvar_flat + INNER JOIN host_customvar sub_customvar_flat_host_customvar + ON sub_customvar_flat_host_customvar.customvar_id = + sub_customvar_flat.customvar_id + INNER JOIN host sub_customvar_flat_host + ON sub_customvar_flat_host.id = sub_customvar_flat_host_customvar.host_id + WHERE (sub_customvar_flat.flatname = ?) + AND (sub_customvar_flat.flatvalue = ?) + GROUP BY sub_customvar_flat_host.id + HAVING COUNT(DISTINCT sub_customvar_flat.id) >= ?))) +ORDER BY host.id +SQL; + + private const DOUBLE_EQUAL_RESULT = <<<'SQL' +SELECT host.id +FROM host +WHERE host.id IN ((SELECT sub_customvar_flat_host.id AS sub_customvar_flat_host_id + FROM customvar_flat sub_customvar_flat + INNER JOIN host_customvar sub_customvar_flat_host_customvar + ON sub_customvar_flat_host_customvar.customvar_id = + sub_customvar_flat.customvar_id + INNER JOIN host sub_customvar_flat_host + ON sub_customvar_flat_host.id = sub_customvar_flat_host_customvar.host_id + WHERE ((sub_customvar_flat.flatname = ?) AND (sub_customvar_flat.flatvalue = ?)) + OR ((sub_customvar_flat.flatname = ?) AND (sub_customvar_flat.flatvalue = ?)) + GROUP BY sub_customvar_flat_host.id + HAVING COUNT(DISTINCT sub_customvar_flat.id) >= ?)) +ORDER BY host.id +SQL; + + /** @var Connection */ + private $connection; + + public function setUp(): void + { + $this->connection = new TestConnection(); + $this->setUpSqlAssertions(); + } + + public function testSingleUnequalCondition() + { + $query = Host::on($this->connection) + ->columns('host.id') + ->orderBy('host.id') + ->filter(Filter::unequal('host.vars.in.valid', 'foo')); + + $this->assertSql(self::SINGLE_UNEQUAL_RESULT, $query->assembleSelect(), ['in.valid', 'foo', 1]); + } + + public function testDoubleUnequalCondition() + { + $query = Host::on($this->connection) + ->columns('host.id') + ->orderBy('host.id') + ->filter(Filter::unequal('host.vars.in.valid', 'foo')) + ->filter(Filter::unequal('host.vars.missing', 'bar')); + + $this->assertSql( + self::DOUBLE_UNEQUAL_RESULT, + $query->assembleSelect(), + ['in.valid', 'foo', 'missing', 'bar', 1] + ); + } + + public function testEqualAndUnequalCondition() + { + $query = Host::on($this->connection) + ->columns('host.id') + ->orderBy('host.id') + ->filter(Filter::unequal('host.vars.in.valid', 'bar')) + ->filter(Filter::equal('host.vars.env', 'foo')); + + $this->assertSql( + self::EQUAL_UNEQUAL_RESULT, + $query->assembleSelect(), + ['in.valid', 'bar', 1, 'env', 'foo', 1] + ); + } + + public function testDoubleEqualCondition() + { + $query = Host::on($this->connection) + ->columns('host.id') + ->orderBy('host.id') + ->filter(Filter::equal('host.vars.env', 'foo')) + ->filter(Filter::equal('host.vars.os', 'bar')); + + $this->assertSql( + self::DOUBLE_EQUAL_RESULT, + $query->assembleSelect(), + ['env', 'foo', 'os', 'bar', 2] + ); + } +} -- cgit v1.2.3