1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
<?php
declare(strict_types=1);
namespace WebThumbnailer\Application;
use PHPUnit\Framework\TestCase;
use WebThumbnailer\Utils\FileUtils;
/**
* Test the configuration manager.
*/
class ConfigManagerTest extends TestCase
{
/**
* Before each test method.
*/
public function setUp(): void
{
ConfigManager::$configFiles = [];
ConfigManager::reload();
}
/**
* Load simple config file and get nested values.
*/
public function testLoadConfig(): void
{
ConfigManager::$configFiles = [FileUtils::getPath(__DIR__, '..', 'resources') . 'settingsok.json'];
$value = ConfigManager::get('nested.setting.1.top');
$this->assertEquals('value', $value);
$value = ConfigManager::get('nested.setting');
$this->assertEquals(2, count($value));
}
/**
* Load config file and read non existing keys.
*/
public function testLoadConfigNotFound(): void
{
ConfigManager::$configFiles = [FileUtils::getPath(__DIR__, '..', 'resources') . 'empty.json'];
$value = ConfigManager::get('');
$this->assertEmpty($value);
$value = ConfigManager::get('nope');
$this->assertEmpty($value);
$value = ConfigManager::get('nope.nope');
$this->assertEmpty($value);
$value = ConfigManager::get('nope', false);
$this->assertEquals(false, $value);
}
/**
* Load multiple config files with overriding value.
*/
public function testLoadConfigMultiFiles(): void
{
ConfigManager::$configFiles = [
FileUtils::getPath(__DIR__, '..', 'resources') . 'settingsok.json',
FileUtils::getPath(__DIR__, '..', 'resources') . 'settings-multiple.json',
];
$value = ConfigManager::get('nested.setting.1.top');
$this->assertEquals('value', $value);
$value = ConfigManager::get('key');
$this->assertEquals('value2', $value);
}
/**
* Add a second config file, with overriding setting.
*/
public function testLoadConfigMultiFilesReloaded(): void
{
ConfigManager::$configFiles = [FileUtils::getPath(__DIR__, '..', 'resources') . 'settingsok.json'];
$value = ConfigManager::get('nested.setting.1.top');
$this->assertEquals('value', $value);
$this->assertEquals('foo', ConfigManager::get('key'));
ConfigManager::$configFiles[] = FileUtils::getPath(__DIR__, '..', 'resources') . 'settings-multiple.json';
ConfigManager::reload();
$value = ConfigManager::get('key');
$this->assertEquals('value2', $value);
}
/**
* Add a second config file, with overriding setting (using addFile()).
*/
public function testLoadConfigAddFile(): void
{
ConfigManager::$configFiles = [FileUtils::getPath(__DIR__, '..', 'resources') . 'settingsok.json'];
$value = ConfigManager::get('nested.setting.1.top');
$this->assertEquals('value', $value);
$this->assertEquals('foo', ConfigManager::get('key'));
ConfigManager::addFile(FileUtils::getPath(__DIR__, '..', 'resources') . 'settings-multiple.json');
$value = ConfigManager::get('key');
$this->assertEquals('value2', $value);
}
}
|