refactor config test (#199)

This commit is contained in:
sunxyw
2022-12-24 15:46:13 +08:00
committed by GitHub
parent a44a7e86d7
commit b41871a2a2
3 changed files with 33 additions and 53 deletions

View File

@@ -11,8 +11,8 @@ trait HasVirtualFileSystem
{ {
private vfsStreamDirectory $vfs; private vfsStreamDirectory $vfs;
private function setUpVfs(string $dir = 'root'): void private function setUpVfs(string $dir = 'root', array $structure = []): void
{ {
$this->vfs = vfsStream::setup($dir); $this->vfs = vfsStream::setup($dir, structure: $structure);
} }
} }

View File

@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Tests\ZM\Config; namespace Tests\ZM\Config;
use Tests\TestCase; use Tests\TestCase;
use Tests\Trait\HasVirtualFileSystem;
use ZM\Config\ZMConfig; use ZM\Config\ZMConfig;
use ZM\Exception\ConfigException; use ZM\Exception\ConfigException;
use ZM\Utils\ReflectionUtil; use ZM\Utils\ReflectionUtil;
@@ -14,15 +15,13 @@ use ZM\Utils\ReflectionUtil;
*/ */
class ZMConfigTest extends TestCase class ZMConfigTest extends TestCase
{ {
private static ZMConfig $config; use HasVirtualFileSystem;
public static function setUpBeforeClass(): void private ZMConfig $config;
protected function setUp(): void
{ {
$mock_dir = __DIR__ . '/config_mock'; parent::setUp();
if (!is_dir($mock_dir)) {
mkdir($mock_dir, 0755, true);
}
$test_config = [ $test_config = [
'foo' => 'bar', 'foo' => 'bar',
'bar' => 'baz', 'bar' => 'baz',
@@ -49,52 +48,33 @@ class ZMConfigTest extends TestCase
'foo', 'bar', 'foo', 'bar',
], ],
]; ];
$this->setUpVfs('config', [
// 下方测试需要临时写入的文件 'test.php' => '<?php return ' . var_export($test_config, true) . ';',
file_put_contents($mock_dir . '/test.php', '<?php return ' . var_export($test_config, true) . ';'); 'test.development.php' => '<?php return ["environment" => "yes", "env" => "development"];',
file_put_contents( 'test.production.php' => '<?php return ["environment" => "yes", "env" => "production"];',
$mock_dir . '/test.development.php', 'test.patch.php' => '<?php return ["patch" => "yes", "another array" => ["far", "baz"]];',
'<?php return ["environment" => "yes", "env" => "development"];' ]);
);
file_put_contents(
$mock_dir . '/test.production.php',
'<?php return ["environment" => "yes", "env" => "production"];'
);
file_put_contents(
$mock_dir . '/test.patch.php',
'<?php return ["patch" => "yes", "another array" => ["far", "baz"]];'
);
try { try {
$config = new ZMConfig([ $config = new ZMConfig([
__DIR__ . '/config_mock', $this->vfs->url(),
], 'development'); ], 'development');
} catch (ConfigException $e) { } catch (ConfigException $e) {
self::fail($e->getMessage()); $this->fail($e->getMessage());
} }
self::$config = $config; $this->config = $config;
}
public static function tearDownAfterClass(): void
{
foreach (scandir(__DIR__ . '/config_mock') as $file) {
if ($file !== '.' && $file !== '..') {
unlink(__DIR__ . '/config_mock/' . $file);
}
}
rmdir(__DIR__ . '/config_mock');
} }
public function testGetValueWhenKeyContainsDot(): void public function testGetValueWhenKeyContainsDot(): void
{ {
$this->markTestSkipped('should it be supported?'); $this->markTestSkipped('should it be supported?');
// $this->assertEquals('c', self::$config->get('test.a.b')); // $this->assertEquals('c', $this->config->get('test.a.b'));
// $this->assertEquals('d', self::$config->get('test.a.b.c')); // $this->assertEquals('d', $this->config->get('test.a.b.c'));
} }
public function testGetBooleanValue(): void public function testGetBooleanValue(): void
{ {
$this->assertTrue(self::$config->get('test.boolean')); $this->assertTrue($this->config->get('test.boolean'));
} }
/** /**
@@ -102,7 +82,7 @@ class ZMConfigTest extends TestCase
*/ */
public function testGetValue(string $key, mixed $expected): void public function testGetValue(string $key, mixed $expected): void
{ {
$this->assertSame($expected, self::$config->get($key)); $this->assertSame($expected, $this->config->get($key));
} }
public function providerTestGetValue(): array public function providerTestGetValue(): array
@@ -118,31 +98,31 @@ class ZMConfigTest extends TestCase
public function testGetWithDefault(): void public function testGetWithDefault(): void
{ {
$this->assertSame('default', self::$config->get('not_exist', 'default')); $this->assertSame('default', $this->config->get('not_exist', 'default'));
} }
public function testSetValue(): void public function testSetValue(): void
{ {
self::$config->set('key', 'value'); $this->config->set('key', 'value');
$this->assertSame('value', self::$config->get('key')); $this->assertSame('value', $this->config->get('key'));
} }
public function testSetArrayValue(): void public function testSetArrayValue(): void
{ {
self::$config->set('array', ['a', 'b']); $this->config->set('array', ['a', 'b']);
$this->assertSame(['a', 'b'], self::$config->get('array')); $this->assertSame(['a', 'b'], $this->config->get('array'));
$this->assertSame('a', self::$config->get('array.0')); $this->assertSame('a', $this->config->get('array.0'));
} }
public function testGetEnvironmentSpecifiedValue(): void public function testGetEnvironmentSpecifiedValue(): void
{ {
$this->assertSame('yes', self::$config->get('test.environment')); $this->assertSame('yes', $this->config->get('test.environment'));
$this->assertSame('development', self::$config->get('test.env')); $this->assertSame('development', $this->config->get('test.env'));
} }
public function testGetPatchSpecifiedValue(): void public function testGetPatchSpecifiedValue(): void
{ {
$this->assertSame('yes', self::$config->get('test.patch')); $this->assertSame('yes', $this->config->get('test.patch'));
} }
/** /**
@@ -151,7 +131,7 @@ class ZMConfigTest extends TestCase
public function testGetFileLoadType(string $name, string $type): void public function testGetFileLoadType(string $name, string $type): void
{ {
$method = ReflectionUtil::getMethod(ZMConfig::class, 'getFileLoadType'); $method = ReflectionUtil::getMethod(ZMConfig::class, 'getFileLoadType');
$actual = $method->invokeArgs(self::$config, [$name]); $actual = $method->invokeArgs($this->config, [$name]);
$this->assertSame($type, $actual); $this->assertSame($type, $actual);
} }
@@ -170,6 +150,6 @@ class ZMConfigTest extends TestCase
{ {
// using of space inside config key is not an officially supported feature, // 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. // it may be removed in the future, please avoid using it in your project.
$this->assertSame(['far', 'baz'], self::$config->get('test.another array')); $this->assertSame(['far', 'baz'], $this->config->get('test.another array'));
} }
} }

View File

@@ -75,12 +75,12 @@ class FileSystemTest extends TestCase
'Qux' => [ 'Qux' => [
'Quux.php' => '<?php namespace Bar\Qux; class Quux {}', 'Quux.php' => '<?php namespace Bar\Qux; class Quux {}',
], ],
'Baz.php.ignore' => '',
], ],
'Chore' => [ 'Chore' => [
'global.php' => '<?php function global_function() {}', 'global.php' => '<?php function global_function() {}',
'global_classes.php' => '<?php class GlobalClass {}', 'global_classes.php' => '<?php class GlobalClass {}',
], ],
'Baz.php.ignore' => '',
], $this->vfs); ], $this->vfs);
$classes = FileSystem::getClassesPsr4($this->vfs->url(), ''); $classes = FileSystem::getClassesPsr4($this->vfs->url(), '');
$this->assertSame([ $this->assertSame([