summaryrefslogtreecommitdiffstats
path: root/test/php
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-13 11:45:00 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-13 11:45:00 +0000
commit7c19368db58f16858fde7f1c5fe50c0d640c0482 (patch)
treeb416482e821607bcef8656c462d39ff995d65db0 /test/php
parentAdding upstream version 1.1.1. (diff)
downloadicingadb-web-7c19368db58f16858fde7f1c5fe50c0d640c0482.tar.xz
icingadb-web-7c19368db58f16858fde7f1c5fe50c0d640c0482.zip
Adding upstream version 1.1.2.upstream/1.1.2upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'test/php')
-rw-r--r--test/php/Lib/StrikingCommandTransport.php28
-rw-r--r--test/php/library/Icingadb/Command/Transport/CommandTransportTest.php48
-rw-r--r--test/php/library/Icingadb/Model/Behavior/FlattenedObjectVarsTest.php161
3 files changed, 237 insertions, 0 deletions
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 @@
+<?php
+
+namespace Tests\Icinga\Module\Icingadb\Lib;
+
+use Icinga\Application\Config;
+use Icinga\Data\ConfigObject;
+use Icinga\Module\Icingadb\Command\IcingaApiCommand;
+use Icinga\Module\Icingadb\Command\Transport\ApiCommandTransport;
+use Icinga\Module\Icingadb\Command\Transport\CommandTransport;
+use Icinga\Module\Icingadb\Command\Transport\CommandTransportException;
+
+class StrikingCommandTransport extends CommandTransport
+{
+ public static function getConfig(): Config
+ {
+ return Config::fromArray(['endpoint1' => ['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 @@
+<?php
+
+namespace Tests\Icinga\Module\Icingadb\Command\Transport;
+
+use Icinga\Module\Icingadb\Command\Object\AddCommentCommand;
+use Icinga\Module\Icingadb\Command\Transport\CommandTransportException;
+use Icinga\Module\Icingadb\Model\Host;
+use PHPUnit\Framework\TestCase;
+use Tests\Icinga\Module\Icingadb\Lib\StrikingCommandTransport;
+
+class CommandTransportTest extends TestCase
+{
+ public function testFallbackHandling()
+ {
+ $this->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 @@
+<?php
+
+/* Icinga DB Web | (c) 2024 Icinga GmbH | GPLv2 */
+
+namespace Tests\Icinga\Modules\Icingadb\Model\Behavior;
+
+use Icinga\Module\Icingadb\Model\Host;
+use ipl\Sql\Connection;
+use ipl\Sql\Test\SqlAssertions;
+use ipl\Sql\Test\TestConnection;
+use ipl\Stdlib\Filter;
+use PHPUnit\Framework\TestCase;
+
+class FlattenedObjectVarsTest extends TestCase
+{
+ use SqlAssertions;
+
+ private const SINGLE_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)
+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]
+ );
+ }
+}