From 74af1516aca909568707350bf09698737fdc3f54 Mon Sep 17 00:00:00 2001 From: sunxyw Date: Sun, 21 Aug 2022 16:13:16 +0800 Subject: [PATCH] add get file load type test --- src/ZM/Config/RefactoredConfig.php | 4 +++- src/ZM/Utils/ReflectionUtil.php | 17 +++++++++++++++++ tests/ZM/Config/RefactoredConfigTest.php | 23 +++++++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/ZM/Config/RefactoredConfig.php b/src/ZM/Config/RefactoredConfig.php index f8cced12..71e279a5 100644 --- a/src/ZM/Config/RefactoredConfig.php +++ b/src/ZM/Config/RefactoredConfig.php @@ -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 diff --git a/src/ZM/Utils/ReflectionUtil.php b/src/ZM/Utils/ReflectionUtil.php index 34a288a7..b3632e3f 100644 --- a/src/ZM/Utils/ReflectionUtil.php +++ b/src/ZM/Utils/ReflectionUtil.php @@ -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; + } } diff --git a/tests/ZM/Config/RefactoredConfigTest.php b/tests/ZM/Config/RefactoredConfigTest.php index 812facb4..053550a8 100644 --- a/tests/ZM/Config/RefactoredConfigTest.php +++ b/tests/ZM/Config/RefactoredConfigTest.php @@ -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'], + ]; + } }