icingaweb2/test/php/library/Icinga/Data/DataArray/ArrayDatasourceTest.php
Eric Lippmann 662de28f85 License source files as GPL-3.0-or-later
Add SPDX license headers and mark source files as GPL-3.0-or-later to
preserve the option to relicense under later GPL versions.
2026-03-26 17:49:26 +01:00

164 lines
4.4 KiB
PHP

<?php
// SPDX-FileCopyrightText: 2018 Icinga GmbH <https://icinga.com>
// SPDX-License-Identifier: GPL-3.0-or-later
namespace Tests\Icinga\Data;
use Icinga\Test\BaseTestCase;
use Icinga\Data\DataArray\ArrayDatasource;
class ArrayDatasourceTest extends BaseTestCase
{
private $query;
public function setUp(): void
{
parent::setUp();
$this->query = (new ArrayDatasource([
(object) [
'host' => 'a host',
'problem' => '1',
'state' => '2',
'handled' => '1'
],
(object) [
'host' => 'b host',
'problem' => '1',
'state' => '0',
'handled' => '0'
],
(object) [
'host' => 'c host',
'problem' => '1',
'state' => '1',
'handled' => '0'
]
]))->select();
}
public function testSelectFactory()
{
$this->assertInstanceOf('Icinga\\Data\\SimpleQuery', $this->query);
}
public function testOrderWithOneRuleIsCorrect()
{
$result = $this->query
->order('host', 'desc')
->fetchAll();
$this->assertEquals(
[
(object) [
'host' => 'c host',
'problem' => '1',
'state' => '1',
'handled' => '0'
],
(object) [
'host' => 'b host',
'problem' => '1',
'state' => '0',
'handled' => '0'
],
(object) [
'host' => 'a host',
'problem' => '1',
'state' => '2',
'handled' => '1'
]
],
$result,
'ArrayDatasource does not sort queries correctly'
);
}
public function testOrderWithTwoRulesIsCorrect()
{
$result = $this->query
->order('handled', 'asc')
->order('host', 'asc')
->fetchAll();
$this->assertEquals(
[
(object) [
'host' => 'b host',
'problem' => '1',
'state' => '0',
'handled' => '0'
],
(object) [
'host' => 'c host',
'problem' => '1',
'state' => '1',
'handled' => '0'
],
(object) [
'host' => 'a host',
'problem' => '1',
'state' => '2',
'handled' => '1'
]
],
$result,
'ArrayDatasource does not sort queries correctly'
);
}
public function testOrderIsCorrectWithLimitAndOffset()
{
$result = $this->query
->order('handled', 'asc')
->order('host', 'asc')
->limit(2)
->fetchAll();
$this->assertEquals(
[
(object) [
'host' => 'b host',
'problem' => '1',
'state' => '0',
'handled' => '0'
],
(object) [
'host' => 'c host',
'problem' => '1',
'state' => '1',
'handled' => '0'
]
],
$result,
'ArrayDatasource does not sort limited queries correctly'
);
}
public function testOrderByKeyColumnIsCorrect()
{
$result = (new ArrayDatasource([
'a' => (object) [
'foo' => 'bar',
'baz' => 'qux'
],
'b' => (object) [
'foo' => 'bar',
'baz' => 'qux'
],
'c' => (object) [
'foo' => 'bar',
'baz' => 'qux'
]
]))->setKeyColumn('name')
->select()
->order('name', 'desc')
->fetchAll();
$this->assertSame(
['c', 'b', 'a'],
array_keys($result),
'ArrayDatasource does not sort queries correctly by key column'
);
}
}