icingaweb2/test/php/library/Icinga/Data/ConfigObjectTest.php
jrauh01 3b9d663bcf
Replace array() with [] (#5509)
Replace the outdated `array()` spelling with the more modern `[]`. This
is applied throughout the whole project, also including phpdocs, tests,
view scripts and core modules.
2026-07-01 14:19:02 +02:00

213 lines
7.2 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\ConfigObject;
class ConfigObjectTest extends BaseTestCase
{
public function testWhetherInitializingAConfigWithAssociativeArraysCreatesHierarchicalConfigObjects()
{
$config = new ConfigObject([
'a' => 'b',
'c' => 'd',
'e' => [
'f' => 'g',
'h' => 'i',
'j' => [
'k' => 'l',
'm' => 'n'
]
]
]);
$this->assertInstanceOf(
get_class($config),
$config->e,
'ConfigObject::__construct() does not accept two dimensional arrays'
);
$this->assertInstanceOf(
get_class($config),
$config->e->j,
'ConfigObject::__construct() does not accept multi dimensional arrays'
);
}
/**
* @depends testWhetherInitializingAConfigWithAssociativeArraysCreatesHierarchicalConfigObjects
*/
public function testWhetherItIsPossibleToCloneConfigObjects()
{
$config = new ConfigObject([
'a' => 'b',
'c' => [
'd' => 'e'
]
]);
$newConfig = clone $config;
$this->assertNotSame(
$config,
$newConfig,
'Shallow cloning objects of type ConfigObject does not seem to work properly'
);
$this->assertNotSame(
$config->c,
$newConfig->c,
'Deep cloning objects of type ConfigObject does not seem to work properly'
);
}
public function testWhetherConfigObjectsAreTraversable()
{
$config = new ConfigObject(['a' => 'b', 'c' => 'd']);
$config->e = 'f';
$this->assertInstanceOf('Iterator', $config, 'ConfigObject objects do not implement interface `Iterator\'');
$actual = [];
foreach ($config as $key => $value) {
$actual[$key] = $value;
}
$this->assertEquals(
['a' => 'b', 'c' => 'd', 'e' => 'f'],
$actual,
'ConfigObject objects do not iterate properly in the order their values were inserted'
);
}
public function testWhetherOneCanCheckWhetherConfigObjectsHaveACertainPropertyOrSection()
{
$config = new ConfigObject(['a' => 'b', 'c' => ['d' => 'e']]);
$this->assertTrue(isset($config->a), 'ConfigObjects do not seem to implement __isset() properly');
$this->assertTrue(isset($config->c->d), 'ConfigObjects do not seem to implement __isset() properly');
$this->assertFalse(isset($config->d), 'ConfigObjects do not seem to implement __isset() properly');
$this->assertFalse(isset($config->c->e), 'ConfigObjects do not seem to implement __isset() properly');
$this->assertTrue(isset($config['a']), 'ConfigObject do not seem to implement offsetExists() properly');
$this->assertFalse(isset($config['d']), 'ConfigObject do not seem to implement offsetExists() properly');
}
public function testWhetherItIsPossibleToAccessProperties()
{
$config = new ConfigObject(['a' => 'b', 'c' => null]);
$this->assertEquals('b', $config->a, 'ConfigObjects do not allow property access');
$this->assertNull($config['c'], 'ConfigObjects do not allow offset access');
$this->assertNull($config->d, 'ConfigObjects do not return NULL as default');
}
public function testWhetherItIsPossibleToSetPropertiesAndSections()
{
$config = new ConfigObject();
$config->a = 'b';
$config['c'] = ['d' => 'e'];
$this->assertTrue(isset($config->a), 'ConfigObjects do not allow to set properties');
$this->assertTrue(isset($config->c), 'ConfigObjects do not allow to set offsets');
$this->assertInstanceOf(
get_class($config),
$config->c,
'ConfigObjects do not convert arrays to config objects when set'
);
}
public function testWhetherItIsNotPossibleToAppendProperties()
{
$this->expectException(\Icinga\Exception\ProgrammingError::class);
$config = new ConfigObject();
$config[] = 'test';
}
public function testWhetherItIsPossibleToUnsetPropertiesAndSections()
{
$config = new ConfigObject(['a' => 'b', 'c' => ['d' => 'e']]);
unset($config->a);
unset($config['c']);
$this->assertFalse(isset($config->a), 'ConfigObjects do not allow to unset properties');
$this->assertFalse(isset($config->c), 'ConfigObjects do not allow to unset sections');
}
public function testWhetherOneCanCheckIfAConfigObjectHasAnyPropertiesOrSections()
{
$config = new ConfigObject();
$this->assertTrue($config->isEmpty(), 'ConfigObjects do not report that they are empty');
$config->test = 'test';
$this->assertFalse($config->isEmpty(), 'ConfigObjects do report that they are empty although they are not');
}
/**
* @depends testWhetherItIsPossibleToAccessProperties
*/
public function testWhetherItIsPossibleToRetrieveDefaultValuesForNonExistentPropertiesOrSections()
{
$config = new ConfigObject(['a' => 'b']);
$this->assertEquals(
'b',
$config->get('a'),
'ConfigObjects do not return the actual value of existing properties'
);
$this->assertNull(
$config->get('b'),
'ConfigObjects do not return NULL as default for non-existent properties'
);
$this->assertEquals(
'test',
$config->get('test', 'test'),
'ConfigObjects do not allow to define the default value to return for non-existent properties'
);
}
public function testWhetherItIsPossibleToRetrieveAllPropertyAndSectionNames()
{
$config = new ConfigObject(['a' => 'b', 'c' => ['d' => 'e']]);
$this->assertEquals(
['a', 'c'],
$config->keys(),
'ConfigObjects do not list property and section names correctly'
);
}
public function testWhetherConfigObjectsCanBeConvertedToArrays()
{
$config = new ConfigObject(['a' => 'b', 'c' => ['d' => 'e']]);
$this->assertEquals(
['a' => 'b', 'c' => ['d' => 'e']],
$config->toArray(),
'ConfigObjects cannot be correctly converted to arrays'
);
}
/**
* @depends testWhetherConfigObjectsCanBeConvertedToArrays
*/
public function testWhetherItIsPossibleToMergeConfigObjects()
{
$config = new ConfigObject(['a' => 'b']);
$config->merge(['a' => 'bb', 'c' => 'd', 'e' => ['f' => 'g']]);
$this->assertEquals(
['a' => 'bb', 'c' => 'd', 'e' => ['f' => 'g']],
$config->toArray(),
'ConfigObjects cannot be extended with arrays'
);
$config->merge(new ConfigObject(['c' => ['d' => 'ee'], 'e' => ['h' => 'i']]));
$this->assertEquals(
['a' => 'bb', 'c' => ['d' => 'ee'], 'e' => ['f' => 'g', 'h' => 'i']],
$config->toArray(),
'ConfigObjects cannot be extended with other ConfigObjects'
);
}
}