zhamao-framework/tests/ZM/Config/ZMConfigTest.php

156 lines
4.6 KiB
PHP
Raw Normal View History

2022-08-20 17:43:06 +08:00
<?php
2022-08-20 18:40:54 +08:00
declare(strict_types=1);
2022-08-20 17:43:06 +08:00
namespace Tests\ZM\Config;
2022-12-23 17:16:08 +08:00
use Tests\TestCase;
2022-12-24 15:46:13 +08:00
use Tests\Trait\HasVirtualFileSystem;
2022-08-23 17:51:20 +08:00
use ZM\Config\ZMConfig;
2022-12-23 17:16:08 +08:00
use ZM\Exception\ConfigException;
2022-08-21 16:13:16 +08:00
use ZM\Utils\ReflectionUtil;
2022-08-20 17:43:06 +08:00
2022-08-20 18:40:54 +08:00
/**
* @internal
*/
2022-08-23 17:51:20 +08:00
class ZMConfigTest extends TestCase
2022-08-20 17:43:06 +08:00
{
2022-12-24 15:46:13 +08:00
use HasVirtualFileSystem;
2022-08-20 17:43:06 +08:00
2022-12-24 15:46:13 +08:00
private ZMConfig $config;
2022-08-20 17:43:06 +08:00
2022-12-24 15:46:13 +08:00
protected function setUp(): void
{
parent::setUp();
2022-08-20 17:43:06 +08:00
$test_config = [
'foo' => 'bar',
'bar' => 'baz',
'baz' => 'bat',
'null' => null,
'boolean' => true,
'associate' => [
'x' => 'xxx',
'y' => 'yyy',
],
'array' => [
'aaa',
'zzz',
],
'x' => [
'z' => 'zoo',
],
'a.b' => 'c',
'a' => [
'b.c' => 'd',
],
'default' => 'yes',
2022-08-23 15:37:31 +08:00
'another array' => [
'foo', 'bar',
],
2022-08-20 17:43:06 +08:00
];
2022-12-24 15:46:13 +08:00
$this->setUpVfs('config', [
'test.php' => '<?php return ' . var_export($test_config, true) . ';',
'test.development.php' => '<?php return ["environment" => "yes", "env" => "development"];',
'test.production.php' => '<?php return ["environment" => "yes", "env" => "production"];',
'test.patch.php' => '<?php return ["patch" => "yes", "another array" => ["far", "baz"]];',
]);
2022-08-20 17:43:06 +08:00
2022-12-23 17:16:08 +08:00
try {
2022-12-31 20:22:55 +08:00
$init_conf = require SOURCE_ROOT_DIR . '/config/config.php';
$init_conf['source']['paths'] = [$this->vfs->url()];
2023-02-24 17:00:40 +08:00
$config = new ZMConfig($init_conf);
2022-12-23 17:16:08 +08:00
} catch (ConfigException $e) {
2022-12-24 15:46:13 +08:00
$this->fail($e->getMessage());
2022-08-20 17:43:06 +08:00
}
2022-12-24 15:46:13 +08:00
$this->config = $config;
2022-08-20 17:43:06 +08:00
}
public function testGetValueWhenKeyContainsDot(): void
{
$this->markTestSkipped('should it be supported?');
2024-10-02 20:31:16 +08:00
// $this->assertEquals('c', $this->config->get('test.a.b'));
// $this->assertEquals('d', $this->config->get('test.a.b.c'));
2022-08-20 17:43:06 +08:00
}
public function testGetBooleanValue(): void
{
2022-12-24 15:46:13 +08:00
$this->assertTrue($this->config->get('test.boolean'));
2022-08-20 17:43:06 +08:00
}
2022-08-21 14:19:53 +08:00
/**
* @dataProvider providerTestGetValue
*/
public function testGetValue(string $key, mixed $expected): void
2022-08-20 17:43:06 +08:00
{
2022-12-24 15:46:13 +08:00
$this->assertSame($expected, $this->config->get($key));
2022-08-21 14:19:53 +08:00
}
public function providerTestGetValue(): array
{
return [
'null' => ['test.null', null],
'boolean' => ['test.boolean', true],
'associate' => ['test.associate', ['x' => 'xxx', 'y' => 'yyy']],
'array' => ['test.array', ['aaa', 'zzz']],
'dot access' => ['test.x.z', 'zoo'],
];
2022-08-20 17:43:06 +08:00
}
public function testGetWithDefault(): void
{
2022-12-24 15:46:13 +08:00
$this->assertSame('default', $this->config->get('not_exist', 'default'));
2022-08-20 17:43:06 +08:00
}
public function testSetValue(): void
{
2022-12-24 15:46:13 +08:00
$this->config->set('key', 'value');
$this->assertSame('value', $this->config->get('key'));
2022-08-20 17:43:06 +08:00
}
public function testSetArrayValue(): void
{
2022-12-24 15:46:13 +08:00
$this->config->set('array', ['a', 'b']);
$this->assertSame(['a', 'b'], $this->config->get('array'));
$this->assertSame('a', $this->config->get('array.0'));
2022-08-20 17:43:06 +08:00
}
2022-08-20 18:28:22 +08:00
public function testGetEnvironmentSpecifiedValue(): void
{
2022-12-24 15:46:13 +08:00
$this->assertSame('yes', $this->config->get('test.environment'));
$this->assertSame('development', $this->config->get('test.env'));
2022-08-20 18:28:22 +08:00
}
2022-08-21 16:13:16 +08:00
2022-08-22 17:02:50 +08:00
public function testGetPatchSpecifiedValue(): void
{
2022-12-24 15:46:13 +08:00
$this->assertSame('yes', $this->config->get('test.patch'));
2022-08-22 17:02:50 +08:00
}
2022-08-21 16:13:16 +08:00
/**
* @dataProvider providerTestGetFileLoadType
*/
public function testGetFileLoadType(string $name, string $type): void
{
2022-08-23 17:51:20 +08:00
$method = ReflectionUtil::getMethod(ZMConfig::class, 'getFileLoadType');
2022-12-24 15:46:13 +08:00
$actual = $method->invokeArgs($this->config, [$name]);
2022-08-21 16:13:16 +08:00
$this->assertSame($type, $actual);
}
public function providerTestGetFileLoadType(): array
{
return [
'default' => ['test', 'default'],
2022-08-21 16:13:16 +08:00
'environment' => ['test.development', 'environment'],
'patch' => ['test.patch', 'patch'],
2022-08-22 17:02:50 +08:00
// complex case are not supported yet
'invalid' => ['test.patch.development', 'undefined'],
2022-08-21 16:13:16 +08:00
];
}
2022-08-23 15:37:31 +08:00
public function testArrayReplaceInsteadOfMerge(): void
{
// using of space inside config key is not an officially supported feature,
// it may be removed in the future, please avoid using it in your project.
2022-12-24 15:46:13 +08:00
$this->assertSame(['far', 'baz'], $this->config->get('test.another array'));
2022-08-23 15:37:31 +08:00
}
2022-08-20 17:43:06 +08:00
}