add get file load type test

This commit is contained in:
sunxyw
2022-08-21 16:13:16 +08:00
parent 07b2f175f3
commit 74af1516ac
3 changed files with 43 additions and 1 deletions

View File

@@ -102,6 +102,7 @@ class RefactoredConfig
// 按照加载顺序加载配置文件
foreach (self::LOAD_ORDER as $load_type) {
foreach ($stages[$load_type] as $file_path) {
logger()->info('Loading config file: ' . $file_path);
$this->loadConfigFromPath($file_path);
}
}
@@ -189,12 +190,13 @@ class RefactoredConfig
/**
* 获取文件加载类型
*
* @param string $name 文件名
* @param string $name 文件名,不带扩展名
*
* @return string 可能为global, environment, patch
*/
private function getFileLoadType(string $name): string
{
// TODO: 对于多段名称的处理,如 test.patch.development
// 传入此处的 name 参数有三种可能的格式:
// 1. 纯文件名:如 test此时加载类型为 global
// 2. 文件名.环境:如 test.development此时加载类型为 environment

View File

@@ -120,4 +120,21 @@ class ReflectionUtil
? new ReflectionMethod($callback[0], $callback[1])
: new ReflectionFunction($callback);
}
/**
* 获取传入的类方法,并确保其可访问
*
* 请不要滥用此方法!!!
*
* @param string $class 类名
* @param string $method 方法名
* @throws ReflectionException
*/
public static function getMethod(string $class, string $method): ReflectionMethod
{
$class = new \ReflectionClass($class);
$method = $class->getMethod($method);
$method->setAccessible(true);
return $method;
}
}

View File

@@ -6,6 +6,7 @@ namespace Tests\ZM\Config;
use PHPUnit\Framework\TestCase;
use ZM\Config\RefactoredConfig;
use ZM\Utils\ReflectionUtil;
/**
* @internal
@@ -87,6 +88,7 @@ class RefactoredConfigTest extends TestCase
/**
* @dataProvider providerTestGetValue
* @param mixed $expected
*/
public function testGetValue(string $key, $expected): void
{
@@ -127,4 +129,25 @@ class RefactoredConfigTest extends TestCase
$this->assertSame('environment', self::$config->get('test.from'));
$this->assertSame('development', self::$config->get('test.env'));
}
/**
* @dataProvider providerTestGetFileLoadType
*/
public function testGetFileLoadType(string $name, string $type): void
{
$method = ReflectionUtil::getMethod(RefactoredConfig::class, 'getFileLoadType');
$actual = $method->invokeArgs(self::$config, [$name]);
$this->assertSame($type, $actual);
}
public function providerTestGetFileLoadType(): array
{
return [
'global' => ['test', 'global'],
'environment' => ['test.development', 'environment'],
'undefined' => ['test.dev.inv', 'undefined'],
'patch' => ['test.patch', 'patch'],
// 'complex' => ['test.patch.development', 'patch'],
];
}
}