mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-08 15:42:48 +08:00
调整Config相关单元测试用例
This commit is contained in:
52
tests/thinkphp/library/think/config/ConfigInitTrait.php
Normal file
52
tests/thinkphp/library/think/config/ConfigInitTrait.php
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试用例在使用Config时(如reset),可能会影响其他测试用例
|
||||||
|
* 此Trait在每次执行测试用例后会对Config进行还原
|
||||||
|
*/
|
||||||
|
namespace tests\thinkphp\library\think\config;
|
||||||
|
|
||||||
|
use think\Config;
|
||||||
|
|
||||||
|
trait ConfigInitTrait
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var \Closure
|
||||||
|
*/
|
||||||
|
protected static $internalConfigFoo;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var \Closure
|
||||||
|
*/
|
||||||
|
protected static $internalRangeFoo;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var mixed
|
||||||
|
*/
|
||||||
|
protected static $originConfig;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected static $originRange;
|
||||||
|
|
||||||
|
public static function setUpBeforeClass()
|
||||||
|
{
|
||||||
|
self::$internalConfigFoo = \Closure::bind(function($value = null) {
|
||||||
|
return !is_null($value) ? Config::$config = $value : Config::$config;
|
||||||
|
}, null, Config::class);
|
||||||
|
|
||||||
|
self::$internalRangeFoo = \Closure::bind(function($value = null) {
|
||||||
|
return !is_null($value) ? Config::$range = $value : Config::$range;
|
||||||
|
}, null, Config::class);
|
||||||
|
|
||||||
|
self::$originConfig = call_user_func(self::$internalConfigFoo);
|
||||||
|
self::$originRange = call_user_func(self::$internalRangeFoo);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function tearDown()
|
||||||
|
{
|
||||||
|
call_user_func(self::$internalConfigFoo, self::$originConfig);
|
||||||
|
call_user_func(self::$internalRangeFoo, self::$originRange);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -16,10 +16,13 @@
|
|||||||
|
|
||||||
namespace tests\thinkphp\library\think\config\driver;
|
namespace tests\thinkphp\library\think\config\driver;
|
||||||
|
|
||||||
|
use tests\thinkphp\library\think\config\ConfigInitTrait;
|
||||||
use think\config;
|
use think\config;
|
||||||
|
|
||||||
class iniTest extends \PHPUnit_Framework_TestCase
|
class iniTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
|
use ConfigInitTrait;
|
||||||
|
|
||||||
public function testParse()
|
public function testParse()
|
||||||
{
|
{
|
||||||
Config::parse('inistring=1', 'ini');
|
Config::parse('inistring=1', 'ini');
|
||||||
|
|||||||
@@ -16,10 +16,13 @@
|
|||||||
|
|
||||||
namespace tests\thinkphp\library\think\config\driver;
|
namespace tests\thinkphp\library\think\config\driver;
|
||||||
|
|
||||||
|
use tests\thinkphp\library\think\config\ConfigInitTrait;
|
||||||
use think\config;
|
use think\config;
|
||||||
|
|
||||||
class jsonTest extends \PHPUnit_Framework_TestCase
|
class jsonTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
|
use ConfigInitTrait;
|
||||||
|
|
||||||
public function testParse()
|
public function testParse()
|
||||||
{
|
{
|
||||||
Config::parse('{"jsonstring":1}', 'json');
|
Config::parse('{"jsonstring":1}', 'json');
|
||||||
|
|||||||
@@ -16,10 +16,13 @@
|
|||||||
|
|
||||||
namespace tests\thinkphp\library\think\config\driver;
|
namespace tests\thinkphp\library\think\config\driver;
|
||||||
|
|
||||||
|
use tests\thinkphp\library\think\config\ConfigInitTrait;
|
||||||
use think\config;
|
use think\config;
|
||||||
|
|
||||||
class xmlTest extends \PHPUnit_Framework_TestCase
|
class xmlTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
|
use ConfigInitTrait;
|
||||||
|
|
||||||
public function testParse()
|
public function testParse()
|
||||||
{
|
{
|
||||||
Config::parse('<?xml version="1.0"?><document><xmlstring>1</xmlstring></document>', 'xml');
|
Config::parse('<?xml version="1.0"?><document><xmlstring>1</xmlstring></document>', 'xml');
|
||||||
|
|||||||
@@ -16,27 +16,23 @@
|
|||||||
|
|
||||||
namespace tests\thinkphp\library\think;
|
namespace tests\thinkphp\library\think;
|
||||||
|
|
||||||
use ReflectionClass;
|
use tests\thinkphp\library\think\config\ConfigInitTrait;
|
||||||
use think\Config;
|
use think\Config;
|
||||||
|
|
||||||
class configTest extends \PHPUnit_Framework_TestCase
|
class configTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
|
use ConfigInitTrait;
|
||||||
|
|
||||||
public function testRange()
|
public function testRange()
|
||||||
{
|
{
|
||||||
$reflectedClass = new ReflectionClass('\think\Config');
|
|
||||||
$reflectedPropertyRange = $reflectedClass->getProperty('range');
|
|
||||||
$reflectedPropertyRange->setAccessible(true);
|
|
||||||
$reflectedPropertyConfig = $reflectedClass->getProperty('config');
|
|
||||||
$reflectedPropertyConfig->setAccessible(true);
|
|
||||||
// test default range
|
// test default range
|
||||||
$this->assertEquals('_sys_', $reflectedPropertyRange->getValue());
|
$this->assertEquals('_sys_', call_user_func(self::$internalRangeFoo));
|
||||||
$config = $reflectedPropertyConfig->getValue();
|
|
||||||
$this->assertTrue(is_array($config));
|
$this->assertTrue(is_array(call_user_func(self::$internalConfigFoo)));
|
||||||
// test range initialization
|
// test range initialization
|
||||||
Config::range('_test_');
|
Config::range('_test_');
|
||||||
$this->assertEquals('_test_', $reflectedPropertyRange->getValue());
|
$this->assertEquals('_test_', call_user_func(self::$internalRangeFoo));
|
||||||
$config = $reflectedPropertyConfig->getValue();
|
$this->assertEquals([], call_user_func(self::$internalConfigFoo)['_test_']);
|
||||||
$this->assertEquals([], $config['_test_']);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// public function testParse()
|
// public function testParse()
|
||||||
@@ -51,11 +47,6 @@ class configTest extends \PHPUnit_Framework_TestCase
|
|||||||
$name = '_name_';
|
$name = '_name_';
|
||||||
$range = '_test_';
|
$range = '_test_';
|
||||||
|
|
||||||
$reflectedClass = new ReflectionClass('\think\Config');
|
|
||||||
$reflectedPropertyConfig = $reflectedClass->getProperty('config');
|
|
||||||
$reflectedPropertyConfig->setAccessible(true);
|
|
||||||
$reflectedPropertyConfig->setValue([]);
|
|
||||||
|
|
||||||
$this->assertEquals($config, Config::load($file, $name, $range));
|
$this->assertEquals($config, Config::load($file, $name, $range));
|
||||||
$this->assertNotEquals(null, Config::load($file, $name, $range));
|
$this->assertNotEquals(null, Config::load($file, $name, $range));
|
||||||
}
|
}
|
||||||
@@ -64,12 +55,8 @@ class configTest extends \PHPUnit_Framework_TestCase
|
|||||||
{
|
{
|
||||||
$range = '_test_';
|
$range = '_test_';
|
||||||
$this->assertFalse(Config::has('abcd', $range));
|
$this->assertFalse(Config::has('abcd', $range));
|
||||||
$reflectedClass = new ReflectionClass('\think\Config');
|
|
||||||
$reflectedPropertyConfig = $reflectedClass->getProperty('config');
|
|
||||||
$reflectedPropertyConfig->setAccessible(true);
|
|
||||||
|
|
||||||
// if (!strpos($name, '.')):
|
call_user_func(self::$internalConfigFoo, [
|
||||||
$reflectedPropertyConfig->setValue([
|
|
||||||
$range => ['abcd' => 'value'],
|
$range => ['abcd' => 'value'],
|
||||||
]);
|
]);
|
||||||
$this->assertTrue(Config::has('abcd', $range));
|
$this->assertTrue(Config::has('abcd', $range));
|
||||||
@@ -77,7 +64,7 @@ class configTest extends \PHPUnit_Framework_TestCase
|
|||||||
// else ...
|
// else ...
|
||||||
$this->assertFalse(Config::has('abcd.efg', $range));
|
$this->assertFalse(Config::has('abcd.efg', $range));
|
||||||
|
|
||||||
$reflectedPropertyConfig->setValue([
|
call_user_func(self::$internalConfigFoo, [
|
||||||
$range => ['abcd' => ['efg' => 'value']],
|
$range => ['abcd' => ['efg' => 'value']],
|
||||||
]);
|
]);
|
||||||
$this->assertTrue(Config::has('abcd.efg', $range));
|
$this->assertTrue(Config::has('abcd.efg', $range));
|
||||||
@@ -86,23 +73,23 @@ class configTest extends \PHPUnit_Framework_TestCase
|
|||||||
public function testGet()
|
public function testGet()
|
||||||
{
|
{
|
||||||
$range = '_test_';
|
$range = '_test_';
|
||||||
$reflectedClass = new ReflectionClass('\think\Config');
|
call_user_func(self::$internalConfigFoo, [
|
||||||
$reflectedPropertyConfig = $reflectedClass->getProperty('config');
|
$range => []
|
||||||
$reflectedPropertyConfig->setAccessible(true);
|
]);
|
||||||
// test all configurations
|
|
||||||
$reflectedPropertyConfig->setValue([$range => []]);
|
|
||||||
$this->assertEquals([], Config::get(null, $range));
|
$this->assertEquals([], Config::get(null, $range));
|
||||||
$this->assertEquals(null, Config::get(null, 'does_not_exist'));
|
$this->assertEquals(null, Config::get(null, 'does_not_exist'));
|
||||||
$value = 'value';
|
$value = 'value';
|
||||||
// test getting configuration
|
// test getting configuration
|
||||||
$reflectedPropertyConfig->setValue([$range => ['abcd' => 'efg']]);
|
call_user_func(self::$internalConfigFoo, [
|
||||||
|
$range => ['abcd' => 'efg']
|
||||||
|
]);
|
||||||
$this->assertEquals('efg', Config::get('abcd', $range));
|
$this->assertEquals('efg', Config::get('abcd', $range));
|
||||||
$this->assertEquals(null, Config::get('does_not_exist', $range));
|
$this->assertEquals(null, Config::get('does_not_exist', $range));
|
||||||
$this->assertEquals(null, Config::get('abcd', 'does_not_exist'));
|
$this->assertEquals(null, Config::get('abcd', 'does_not_exist'));
|
||||||
// test getting configuration with dot syntax
|
// test getting configuration with dot syntax
|
||||||
$reflectedPropertyConfig->setValue([$range => [
|
call_user_func(self::$internalConfigFoo, [
|
||||||
'one' => ['two' => $value],
|
$range => ['one' => ['two' => $value]]
|
||||||
]]);
|
]);
|
||||||
$this->assertEquals($value, Config::get('one.two', $range));
|
$this->assertEquals($value, Config::get('one.two', $range));
|
||||||
$this->assertEquals(null, Config::get('one.does_not_exist', $range));
|
$this->assertEquals(null, Config::get('one.does_not_exist', $range));
|
||||||
$this->assertEquals(null, Config::get('one.two', 'does_not_exist'));
|
$this->assertEquals(null, Config::get('one.two', 'does_not_exist'));
|
||||||
@@ -111,22 +98,18 @@ class configTest extends \PHPUnit_Framework_TestCase
|
|||||||
public function testSet()
|
public function testSet()
|
||||||
{
|
{
|
||||||
$range = '_test_';
|
$range = '_test_';
|
||||||
$reflectedClass = new ReflectionClass('\think\Config');
|
|
||||||
$reflectedPropertyConfig = $reflectedClass->getProperty('config');
|
|
||||||
$reflectedPropertyConfig->setAccessible(true);
|
|
||||||
$reflectedPropertyConfig->setValue([]);
|
|
||||||
// if (is_string($name)):
|
|
||||||
// without dot syntax
|
// without dot syntax
|
||||||
$name = 'name';
|
$name = 'name';
|
||||||
$value = 'value';
|
$value = 'value';
|
||||||
Config::set($name, $value, $range);
|
Config::set($name, $value, $range);
|
||||||
$config = $reflectedPropertyConfig->getValue();
|
$config = call_user_func(self::$internalConfigFoo);
|
||||||
$this->assertEquals($value, $config[$range][$name]);
|
$this->assertEquals($value, $config[$range][$name]);
|
||||||
// with dot syntax
|
// with dot syntax
|
||||||
$name = 'one.two';
|
$name = 'one.two';
|
||||||
$value = 'dot value';
|
$value = 'dot value';
|
||||||
Config::set($name, $value, $range);
|
Config::set($name, $value, $range);
|
||||||
$config = $reflectedPropertyConfig->getValue();
|
$config = call_user_func(self::$internalConfigFoo);
|
||||||
$this->assertEquals($value, $config[$range]['one']['two']);
|
$this->assertEquals($value, $config[$range]['one']['two']);
|
||||||
// if (is_array($name)):
|
// if (is_array($name)):
|
||||||
// see testLoad()
|
// see testLoad()
|
||||||
@@ -134,7 +117,7 @@ class configTest extends \PHPUnit_Framework_TestCase
|
|||||||
// test getting all configurations...?
|
// test getting all configurations...?
|
||||||
// return self::$config[$range]; ??
|
// return self::$config[$range]; ??
|
||||||
$value = ['all' => 'configuration'];
|
$value = ['all' => 'configuration'];
|
||||||
$reflectedPropertyConfig->setValue([$range => $value]);
|
call_user_func(self::$internalConfigFoo, [$range => $value]);
|
||||||
$this->assertEquals($value, Config::set(null, null, $range));
|
$this->assertEquals($value, Config::set(null, null, $range));
|
||||||
$this->assertNotEquals(null, Config::set(null, null, $range));
|
$this->assertNotEquals(null, Config::set(null, null, $range));
|
||||||
}
|
}
|
||||||
@@ -142,17 +125,14 @@ class configTest extends \PHPUnit_Framework_TestCase
|
|||||||
public function testReset()
|
public function testReset()
|
||||||
{
|
{
|
||||||
$range = '_test_';
|
$range = '_test_';
|
||||||
$reflectedClass = new ReflectionClass('\think\Config');
|
call_user_func(self::$internalConfigFoo, [$range => ['abcd' => 'efg']]);
|
||||||
$reflectedPropertyConfig = $reflectedClass->getProperty('config');
|
|
||||||
$reflectedPropertyConfig->setAccessible(true);
|
|
||||||
$reflectedPropertyConfig->setValue([$range => ['abcd' => 'efg']]);
|
|
||||||
|
|
||||||
// clear all configurations
|
// clear all configurations
|
||||||
Config::reset(true);
|
Config::reset(true);
|
||||||
$config = $reflectedPropertyConfig->getValue();
|
$config = call_user_func(self::$internalConfigFoo);
|
||||||
$this->assertEquals([], $config);
|
$this->assertEquals([], $config);
|
||||||
// clear the configuration in range of parameter.
|
// clear the configuration in range of parameter.
|
||||||
$reflectedPropertyConfig->setValue([
|
call_user_func(self::$internalConfigFoo, [
|
||||||
$range => [
|
$range => [
|
||||||
'abcd' => 'efg',
|
'abcd' => 'efg',
|
||||||
'hijk' => 'lmn',
|
'hijk' => 'lmn',
|
||||||
@@ -160,7 +140,7 @@ class configTest extends \PHPUnit_Framework_TestCase
|
|||||||
'a' => 'b',
|
'a' => 'b',
|
||||||
]);
|
]);
|
||||||
Config::reset($range);
|
Config::reset($range);
|
||||||
$config = $reflectedPropertyConfig->getValue();
|
$config = call_user_func(self::$internalConfigFoo);
|
||||||
$this->assertEquals([
|
$this->assertEquals([
|
||||||
$range => [],
|
$range => [],
|
||||||
'a' => 'b',
|
'a' => 'b',
|
||||||
|
|||||||
@@ -17,6 +17,7 @@
|
|||||||
namespace tests\thinkphp\library\think;
|
namespace tests\thinkphp\library\think;
|
||||||
|
|
||||||
use ReflectionClass;
|
use ReflectionClass;
|
||||||
|
use think\Config;
|
||||||
use think\Controller;
|
use think\Controller;
|
||||||
use think\Request;
|
use think\Request;
|
||||||
use think\View;
|
use think\View;
|
||||||
|
|||||||
Reference in New Issue
Block a user