完善测试文件

This commit is contained in:
thinkphp
2016-02-29 21:53:27 +08:00
parent c447e900a7
commit a61e05b20f
6 changed files with 180 additions and 179 deletions

View File

@@ -23,7 +23,7 @@ class configTest extends \PHPUnit_Framework_TestCase
{ {
public function testRange() public function testRange()
{ {
$reflectedClass = new ReflectionClass('\think\config'); $reflectedClass = new ReflectionClass('\think\Config');
$reflectedPropertyRange = $reflectedClass->getProperty('range'); $reflectedPropertyRange = $reflectedClass->getProperty('range');
$reflectedPropertyRange->setAccessible(true); $reflectedPropertyRange->setAccessible(true);
$reflectedPropertyConfig = $reflectedClass->getProperty('config'); $reflectedPropertyConfig = $reflectedClass->getProperty('config');
@@ -51,20 +51,20 @@ class configTest extends \PHPUnit_Framework_TestCase
$name = '_name_'; $name = '_name_';
$range = '_test_'; $range = '_test_';
$reflectedClass = new ReflectionClass('\think\config'); $reflectedClass = new ReflectionClass('\think\Config');
$reflectedPropertyConfig = $reflectedClass->getProperty('config'); $reflectedPropertyConfig = $reflectedClass->getProperty('config');
$reflectedPropertyConfig->setAccessible(true); $reflectedPropertyConfig->setAccessible(true);
$reflectedPropertyConfig->setValue([]); $reflectedPropertyConfig->setValue([]);
$this->assertEquals($config, \think\config::load($file, $name, $range)); $this->assertEquals($config, Config::load($file, $name, $range));
$this->assertNotEquals(null, \think\config::load($file, $name, $range)); $this->assertNotEquals(null, Config::load($file, $name, $range));
} }
public function testHas() public function testHas()
{ {
$range = '_test_'; $range = '_test_';
$this->assertFalse(\think\config::has('abcd', $range)); $this->assertFalse(Config::has('abcd', $range));
$reflectedClass = new ReflectionClass('\think\config'); $reflectedClass = new ReflectionClass('\think\Config');
$reflectedPropertyConfig = $reflectedClass->getProperty('config'); $reflectedPropertyConfig = $reflectedClass->getProperty('config');
$reflectedPropertyConfig->setAccessible(true); $reflectedPropertyConfig->setAccessible(true);
@@ -72,53 +72,53 @@ class configTest extends \PHPUnit_Framework_TestCase
$reflectedPropertyConfig->setValue([ $reflectedPropertyConfig->setValue([
$range => ['abcd' => 'value'], $range => ['abcd' => 'value'],
]); ]);
$this->assertTrue(\think\config::has('abcd', $range)); $this->assertTrue(Config::has('abcd', $range));
// else ... // else ...
$this->assertFalse(\think\config::has('abcd.efg', $range)); $this->assertFalse(Config::has('abcd.efg', $range));
$reflectedPropertyConfig->setValue([ $reflectedPropertyConfig->setValue([
$range => ['abcd' => ['efg' => 'value']], $range => ['abcd' => ['efg' => 'value']],
]); ]);
$this->assertTrue(\think\config::has('abcd.efg', $range)); $this->assertTrue(Config::has('abcd.efg', $range));
} }
public function testGet() public function testGet()
{ {
$range = '_test_'; $range = '_test_';
$reflectedClass = new ReflectionClass('\think\config'); $reflectedClass = new ReflectionClass('\think\Config');
$reflectedPropertyConfig = $reflectedClass->getProperty('config'); $reflectedPropertyConfig = $reflectedClass->getProperty('config');
$reflectedPropertyConfig->setAccessible(true); $reflectedPropertyConfig->setAccessible(true);
// test all configurations // test all configurations
$reflectedPropertyConfig->setValue([$range => []]); $reflectedPropertyConfig->setValue([$range => []]);
$this->assertEquals([], \think\config::get(null, $range)); $this->assertEquals([], Config::get(null, $range));
$this->assertEquals(null, \think\config::get(null, 'does_not_exist')); $this->assertEquals(null, Config::get(null, 'does_not_exist'));
// test $_ENV configuration // test $_ENV configuration
defined('ENV_PREFIX') or define('ENV_PREFIX', '_TEST_'); defined('ENV_PREFIX') or define('ENV_PREFIX', '_TEST_');
$name = 'test_name'; $name = 'test_name';
$value = 'value'; $value = 'value';
$_ENV[ENV_PREFIX . $name] = $value; $_ENV[ENV_PREFIX . $name] = $value;
$this->assertEquals($value, \think\config::get($name, $range)); $this->assertEquals($value, Config::get($name, $range));
// test getting configuration // test getting configuration
$reflectedPropertyConfig->setValue([$range => ['abcd' => 'efg']]); $reflectedPropertyConfig->setValue([$range => ['abcd' => 'efg']]);
$this->assertEquals('efg', \think\config::get('abcd', $range)); $this->assertEquals('efg', Config::get('abcd', $range));
$this->assertEquals(null, \think\config::get('does_not_exist', $range)); $this->assertEquals(null, Config::get('does_not_exist', $range));
$this->assertEquals(null, \think\config::get('abcd', 'does_not_exist')); $this->assertEquals(null, Config::get('abcd', 'does_not_exist'));
// test $_ENV configuration with dot syntax // test $_ENV configuration with dot syntax
$this->assertEquals($value, \think\config::get('test.name', $range)); $this->assertEquals($value, Config::get('test.name', $range));
// test getting configuration with dot syntax // test getting configuration with dot syntax
$reflectedPropertyConfig->setValue([$range => [ $reflectedPropertyConfig->setValue([$range => [
'one' => ['two' => $value], 'one' => ['two' => $value],
]]); ]]);
$this->assertEquals($value, \think\config::get('one.two', $range)); $this->assertEquals($value, Config::get('one.two', $range));
$this->assertEquals(null, \think\config::get('one.does_not_exist', $range)); $this->assertEquals(null, Config::get('one.does_not_exist', $range));
$this->assertEquals(null, \think\config::get('one.two', 'does_not_exist')); $this->assertEquals(null, Config::get('one.two', 'does_not_exist'));
} }
public function testSet() public function testSet()
{ {
$range = '_test_'; $range = '_test_';
$reflectedClass = new ReflectionClass('\think\config'); $reflectedClass = new ReflectionClass('\think\Config');
$reflectedPropertyConfig = $reflectedClass->getProperty('config'); $reflectedPropertyConfig = $reflectedClass->getProperty('config');
$reflectedPropertyConfig->setAccessible(true); $reflectedPropertyConfig->setAccessible(true);
$reflectedPropertyConfig->setValue([]); $reflectedPropertyConfig->setValue([]);
@@ -126,13 +126,13 @@ class configTest extends \PHPUnit_Framework_TestCase
// without dot syntax // without dot syntax
$name = 'name'; $name = 'name';
$value = 'value'; $value = 'value';
\think\config::set($name, $value, $range); Config::set($name, $value, $range);
$config = $reflectedPropertyConfig->getValue(); $config = $reflectedPropertyConfig->getValue();
$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';
\think\config::set($name, $value, $range); Config::set($name, $value, $range);
$config = $reflectedPropertyConfig->getValue(); $config = $reflectedPropertyConfig->getValue();
$this->assertEquals($value, $config[$range]['one']['two']); $this->assertEquals($value, $config[$range]['one']['two']);
// if (is_array($name)): // if (is_array($name)):
@@ -142,20 +142,20 @@ class configTest extends \PHPUnit_Framework_TestCase
// return self::$config[$range]; ?? // return self::$config[$range]; ??
$value = ['all' => 'configuration']; $value = ['all' => 'configuration'];
$reflectedPropertyConfig->setValue([$range => $value]); $reflectedPropertyConfig->setValue([$range => $value]);
$this->assertEquals($value, \think\config::set(null, null, $range)); $this->assertEquals($value, Config::set(null, null, $range));
$this->assertNotEquals(null, \think\config::set(null, null, $range)); $this->assertNotEquals(null, Config::set(null, null, $range));
} }
public function testReset() public function testReset()
{ {
$range = '_test_'; $range = '_test_';
$reflectedClass = new ReflectionClass('\think\config'); $reflectedClass = new ReflectionClass('\think\Config');
$reflectedPropertyConfig = $reflectedClass->getProperty('config'); $reflectedPropertyConfig = $reflectedClass->getProperty('config');
$reflectedPropertyConfig->setAccessible(true); $reflectedPropertyConfig->setAccessible(true);
$reflectedPropertyConfig->setValue([$range => ['abcd' => 'efg']]); $reflectedPropertyConfig->setValue([$range => ['abcd' => 'efg']]);
// clear all configurations // clear all configurations
\think\config::reset(true); Config::reset(true);
$config = $reflectedPropertyConfig->getValue(); $config = $reflectedPropertyConfig->getValue();
$this->assertEquals([], $config); $this->assertEquals([], $config);
// clear the configuration in range of parameter. // clear the configuration in range of parameter.
@@ -166,7 +166,7 @@ class configTest extends \PHPUnit_Framework_TestCase
], ],
'a' => 'b', 'a' => 'b',
]); ]);
\think\config::reset($range); Config::reset($range);
$config = $reflectedPropertyConfig->getValue(); $config = $reflectedPropertyConfig->getValue();
$this->assertEquals([ $this->assertEquals([
$range => [], $range => [],

View File

@@ -17,6 +17,7 @@
namespace tests\thinkphp\library\think; namespace tests\thinkphp\library\think;
use ReflectionClass; use ReflectionClass;
use think\Cookie;
class cookieTest extends \PHPUnit_Framework_TestCase class cookieTest extends \PHPUnit_Framework_TestCase
{ {
@@ -64,7 +65,7 @@ class cookieTest extends \PHPUnit_Framework_TestCase
// httponly设置 // httponly设置
'httponly' => '1', 'httponly' => '1',
]; ];
\think\Cookie::init($config); Cookie::init($config);
$this->assertEquals( $this->assertEquals(
array_merge($this->default, array_change_key_case($config)), array_merge($this->default, array_change_key_case($config)),
@@ -74,11 +75,11 @@ class cookieTest extends \PHPUnit_Framework_TestCase
public function testPrefix() public function testPrefix()
{ {
$this->assertEquals($this->default['prefix'], \think\Cookie::prefix()); $this->assertEquals($this->default['prefix'], Cookie::prefix());
$prefix = '_test_'; $prefix = '_test_';
$this->assertNotEquals($prefix, \think\Cookie::prefix()); $this->assertNotEquals($prefix, Cookie::prefix());
\think\Cookie::prefix($prefix); Cookie::prefix($prefix);
$config = $this->ref->getValue(); $config = $this->ref->getValue();
$this->assertEquals($prefix, $config['prefix']); $this->assertEquals($prefix, $config['prefix']);
@@ -89,20 +90,20 @@ class cookieTest extends \PHPUnit_Framework_TestCase
$value = 'value'; $value = 'value';
$name = 'name1'; $name = 'name1';
\think\Cookie::set($name, $value, 10); Cookie::set($name, $value, 10);
$this->assertEquals($value, $_COOKIE[$this->default['prefix'] . $name]); $this->assertEquals($value, $_COOKIE[$this->default['prefix'] . $name]);
$name = 'name2'; $name = 'name2';
\think\Cookie::set($name, $value, null); Cookie::set($name, $value, null);
$this->assertEquals($value, $_COOKIE[$this->default['prefix'] . $name]); $this->assertEquals($value, $_COOKIE[$this->default['prefix'] . $name]);
$name = 'name3'; $name = 'name3';
\think\Cookie::set($name, $value, 'expire=100&prefix=pre_'); Cookie::set($name, $value, 'expire=100&prefix=pre_');
$this->assertEquals($value, $_COOKIE['pre_' . $name]); $this->assertEquals($value, $_COOKIE['pre_' . $name]);
$name = 'name4'; $name = 'name4';
$value = ['_test_中文_']; $value = ['_test_中文_'];
\think\Cookie::set($name, $value); Cookie::set($name, $value);
$this->assertEquals('think:' . json_encode([urlencode('_test_中文_')]), $_COOKIE[$name]); $this->assertEquals('think:' . json_encode([urlencode('_test_中文_')]), $_COOKIE[$name]);
} }
@@ -113,10 +114,10 @@ class cookieTest extends \PHPUnit_Framework_TestCase
'pre_abc' => 'c', 'pre_abc' => 'c',
'd' => 'think:' . json_encode([urlencode('_test_中文_')]), 'd' => 'think:' . json_encode([urlencode('_test_中文_')]),
]; ];
$this->assertEquals('b', \think\Cookie::get('a')); $this->assertEquals('b', Cookie::get('a'));
$this->assertEquals(null, \think\Cookie::get('does_not_exist')); $this->assertEquals(null, Cookie::get('does_not_exist'));
$this->assertEquals('c', \think\Cookie::get('abc', 'pre_')); $this->assertEquals('c', Cookie::get('abc', 'pre_'));
$this->assertEquals(['_test_中文_'], \think\Cookie::get('d')); $this->assertEquals(['_test_中文_'], Cookie::get('d'));
} }
public function testDelete() public function testDelete()
@@ -125,30 +126,25 @@ class cookieTest extends \PHPUnit_Framework_TestCase
'a' => 'b', 'a' => 'b',
'pre_abc' => 'c', 'pre_abc' => 'c',
]; ];
$this->assertEquals('b', \think\Cookie::get('a')); $this->assertEquals('b', Cookie::get('a'));
\think\Cookie::delete('a'); Cookie::delete('a');
$this->assertEquals(null, \think\Cookie::get('a')); $this->assertEquals(null, Cookie::get('a'));
$this->assertEquals('c', \think\Cookie::get('abc', 'pre_')); $this->assertEquals('c', Cookie::get('abc', 'pre_'));
\think\Cookie::delete('abc', 'pre_'); Cookie::delete('abc', 'pre_');
$this->assertEquals(null, \think\Cookie::get('abc', 'pre_')); $this->assertEquals(null, Cookie::get('abc', 'pre_'));
} }
public function testClear() public function testClear()
{ {
$_COOKIE = []; $_COOKIE = [];
$this->assertEquals(null, \think\Cookie::clear()); $this->assertEquals(null, Cookie::clear());
/*
$_COOKIE = ['a' => 'b'];
\think\Cookie::clear();
$this->assertEquals(null, $_COOKIE);*/
$_COOKIE = [ $_COOKIE = [
'a' => 'b', 'a' => 'b',
'pre_abc' => 'c', 'pre_abc' => 'c',
]; ];
\think\Cookie::clear('pre_'); Cookie::clear('pre_');
$this->assertEquals(['a' => 'b'], $_COOKIE); $this->assertEquals(['a' => 'b'], $_COOKIE);
} }
} }

View File

@@ -50,7 +50,7 @@ class debugTest extends \PHPUnit_Framework_TestCase
public function testRemark() public function testRemark()
{ {
$name = "testremarkkey"; $name = "testremarkkey";
\think\Debug::remark($name); Debug::remark($name);
} }
/** /**
@@ -61,11 +61,11 @@ class debugTest extends \PHPUnit_Framework_TestCase
{ {
$start = "testGetRangeTimeStart"; $start = "testGetRangeTimeStart";
$end = "testGetRangeTimeEnd"; $end = "testGetRangeTimeEnd";
\think\Debug::remark($start); Debug::remark($start);
usleep(20000); usleep(20000);
// \think\Debug::remark($end); // \think\Debug::remark($end);
$time = \think\Debug::getRangeTime($start, $end); $time = Debug::getRangeTime($start, $end);
$this->assertLessThan(0.03, $time); $this->assertLessThan(0.03, $time);
//$this->assertEquals(0.03, ceil($time)); //$this->assertEquals(0.03, ceil($time));
} }
@@ -76,7 +76,7 @@ class debugTest extends \PHPUnit_Framework_TestCase
*/ */
public function testGetUseTime() public function testGetUseTime()
{ {
$time = \think\Debug::getUseTime(); $time = Debug::getUseTime();
$this->assertLessThan(5.5, $time); $this->assertLessThan(5.5, $time);
} }
@@ -87,7 +87,7 @@ class debugTest extends \PHPUnit_Framework_TestCase
public function testGetThroughputRate() public function testGetThroughputRate()
{ {
usleep(100000); usleep(100000);
$throughputRate = \think\Debug::getThroughputRate(); $throughputRate = Debug::getThroughputRate();
$this->assertLessThan(10, $throughputRate); $this->assertLessThan(10, $throughputRate);
} }
@@ -99,13 +99,13 @@ class debugTest extends \PHPUnit_Framework_TestCase
{ {
$start = "testGetRangeMemStart"; $start = "testGetRangeMemStart";
$end = "testGetRangeMemEnd"; $end = "testGetRangeMemEnd";
\think\Debug::remark($start); Debug::remark($start);
$str = ""; $str = "";
for ($i = 0; $i < 10000; $i++) { for ($i = 0; $i < 10000; $i++) {
$str .= "mem"; $str .= "mem";
} }
$rangeMem = \think\Debug::getRangeMem($start, $end); $rangeMem = Debug::getRangeMem($start, $end);
$this->assertLessThan(33, explode(" ", $rangeMem)[0]); $this->assertLessThan(33, explode(" ", $rangeMem)[0]);
} }
@@ -116,7 +116,7 @@ class debugTest extends \PHPUnit_Framework_TestCase
*/ */
public function testGetUseMem() public function testGetUseMem()
{ {
$useMem = \think\Debug::getUseMem(); $useMem = Debug::getUseMem();
$this->assertLessThan(15, explode(" ", $useMem)[0]); $this->assertLessThan(15, explode(" ", $useMem)[0]);
} }
@@ -129,12 +129,12 @@ class debugTest extends \PHPUnit_Framework_TestCase
{ {
$start = "testGetMemPeakStart"; $start = "testGetMemPeakStart";
$end = "testGetMemPeakEnd"; $end = "testGetMemPeakEnd";
\think\Debug::remark($start); Debug::remark($start);
$str = ""; $str = "";
for ($i = 0; $i < 100000; $i++) { for ($i = 0; $i < 100000; $i++) {
$str .= "mem"; $str .= "mem";
} }
$memPeak = \think\Debug::getMemPeak($start, $end); $memPeak = Debug::getMemPeak($start, $end);
$this->assertLessThan(355, explode(" ", $memPeak)[0]); $this->assertLessThan(355, explode(" ", $memPeak)[0]);
} }
@@ -144,11 +144,11 @@ class debugTest extends \PHPUnit_Framework_TestCase
*/ */
public function testGetFile() public function testGetFile()
{ {
$count = \think\Debug::getFile(); $count = Debug::getFile();
$this->assertEquals(count(get_included_files()), $count); $this->assertEquals(count(get_included_files()), $count);
$info = \think\Debug::getFile(true); $info = Debug::getFile(true);
$this->assertEquals(count(get_included_files()), count($info)); $this->assertEquals(count(get_included_files()), count($info));
$this->assertContains("KB", $info[0]); $this->assertContains("KB", $info[0]);
@@ -160,9 +160,9 @@ class debugTest extends \PHPUnit_Framework_TestCase
*/ */
public function testDump() public function testDump()
{ {
$var = array(); $var = [];
$var["key"] = "val"; $var["key"] = "val";
$output = \think\Debug::dump($var, false, $label = "label"); $output = Debug::dump($var, false, $label = "label");
$array = explode("array", json_encode($output)); $array = explode("array", json_encode($output));
if (IS_WIN) { if (IS_WIN) {
$this->assertEquals("(1) {\\n [\\\"key\\\"] => string(3) \\\"val\\\"\\n}\\n\\r\\n\"", end($array)); $this->assertEquals("(1) {\\n [\\\"key\\\"] => string(3) \\\"val\\\"\\n}\\n\\r\\n\"", end($array));

View File

@@ -16,6 +16,9 @@
namespace tests\thinkphp\library\think; namespace tests\thinkphp\library\think;
use think\Config;
use think\Response;
class responseTest extends \PHPUnit_Framework_TestCase class responseTest extends \PHPUnit_Framework_TestCase
{ {
@@ -52,10 +55,10 @@ class responseTest extends \PHPUnit_Framework_TestCase
// * @runInSeparateProcess // * @runInSeparateProcess
// */ // */
if (!$this->default_return_type) { if (!$this->default_return_type) {
$this->default_return_type = \think\Config::get('default_return_type'); $this->default_return_type = Config::get('default_return_type');
} }
if (!$this->default_ajax_return) { if (!$this->default_ajax_return) {
$this->default_ajax_return = \think\Config::get('default_ajax_return'); $this->default_ajax_return = Config::get('default_ajax_return');
} }
} }
@@ -65,9 +68,9 @@ class responseTest extends \PHPUnit_Framework_TestCase
*/ */
protected function tearDown() protected function tearDown()
{ {
\think\Config::set('default_ajax_return', $this->default_ajax_return); Config::set('default_ajax_return', $this->default_ajax_return);
\think\Config::set('default_return_type', $this->default_return_type); Config::set('default_return_type', $this->default_return_type);
\think\Response::type(\think\Config::get('default_return_type')); // 会影响其他测试 Response::type(Config::get('default_return_type')); // 会影响其他测试
} }
/** /**
@@ -80,25 +83,25 @@ class responseTest extends \PHPUnit_Framework_TestCase
$dataArr["key"] = "value"; $dataArr["key"] = "value";
//$dataArr->key = "val"; //$dataArr->key = "val";
$result = \think\Response::send($dataArr, "", true); $result = Response::send($dataArr, "", true);
$this->assertArrayHasKey("key", $result); $this->assertArrayHasKey("key", $result);
$result = \think\Response::send($dataArr, "json", true); $result = Response::send($dataArr, "json", true);
$this->assertEquals('{"key":"value"}', $result); $this->assertEquals('{"key":"value"}', $result);
$handler = "callback"; $handler = "callback";
$_GET[\think\Config::get('var_jsonp_handler')] = $handler; $_GET[Config::get('var_jsonp_handler')] = $handler;
$result = \think\Response::send($dataArr, "jsonp", true); $result = Response::send($dataArr, "jsonp", true);
$this->assertEquals('callback({"key":"value"});', $result); $this->assertEquals('callback({"key":"value"});', $result);
\think\Response::tramsform(function () { Response::tramsform(function () {
return "callbackreturndata"; return "callbackreturndata";
}); });
$result = \think\Response::send($dataArr, "", true); $result = Response::send($dataArr, "", true);
$this->assertEquals("callbackreturndata", $result); $this->assertEquals("callbackreturndata", $result);
$_GET[\think\Config::get('var_jsonp_handler')] = ""; $_GET[Config::get('var_jsonp_handler')] = "";
} }
/** /**
@@ -107,15 +110,15 @@ class responseTest extends \PHPUnit_Framework_TestCase
*/ */
public function testTramsform() public function testTramsform()
{ {
\think\Response::tramsform(function () { Response::tramsform(function () {
return "callbackreturndata"; return "callbackreturndata";
}); });
$dataArr = []; $dataArr = [];
$result = \think\Response::send($dataArr, "", true); $result = Response::send($dataArr, "", true);
$this->assertEquals("callbackreturndata", $result); $this->assertEquals("callbackreturndata", $result);
\think\Response::tramsform(null); Response::tramsform(null);
} }
/** /**
@@ -125,11 +128,11 @@ class responseTest extends \PHPUnit_Framework_TestCase
public function testType() public function testType()
{ {
$type = "json"; $type = "json";
\think\Response::type($type); Response::type($type);
$result = \think\Response::type(); $result = Response::type();
$this->assertEquals($type, $result); $this->assertEquals($type, $result);
\think\Response::type($type); Response::type($type);
} }
/** /**
@@ -139,8 +142,8 @@ class responseTest extends \PHPUnit_Framework_TestCase
public function testData() public function testData()
{ {
$data = "data"; $data = "data";
\think\Response::data($data); Response::data($data);
\think\Response::data(null); Response::data(null);
} }
/** /**
@@ -150,11 +153,11 @@ class responseTest extends \PHPUnit_Framework_TestCase
public function testIsExit() public function testIsExit()
{ {
$isExit = true; $isExit = true;
\think\Response::isExit($isExit); Response::isExit($isExit);
$result = \think\Response::isExit(); $result = Response::isExit();
$this->assertTrue($isExit, $result); $this->assertTrue($isExit, $result);
\think\Response::isExit(false); Response::isExit(false);
} }
/** /**
@@ -167,13 +170,13 @@ class responseTest extends \PHPUnit_Framework_TestCase
$code = "1001"; $code = "1001";
$msg = "the msg"; $msg = "the msg";
$type = "json"; $type = "json";
$result = \think\Response::result($data, $code, $msg, $type); $result = Response::result($data, $code, $msg, $type);
$this->assertEquals($code, $result["code"]); $this->assertEquals($code, $result["code"]);
$this->assertEquals($msg, $result["msg"]); $this->assertEquals($msg, $result["msg"]);
$this->assertEquals($data, $result["data"]); $this->assertEquals($data, $result["data"]);
$this->assertEquals($_SERVER['REQUEST_TIME'], $result["time"]); $this->assertEquals($_SERVER['REQUEST_TIME'], $result["time"]);
$this->assertEquals($type, \think\Response::type()); $this->assertEquals($type, Response::type());
} }
/** /**
@@ -191,22 +194,22 @@ class responseTest extends \PHPUnit_Framework_TestCase
$HTTP_REFERER = $_SERVER["HTTP_REFERER"]; $HTTP_REFERER = $_SERVER["HTTP_REFERER"];
} }
$_SERVER["HTTP_REFERER"] = $url; $_SERVER["HTTP_REFERER"] = $url;
\think\Config::set('default_return_type', "json"); Config::set('default_return_type', "json");
$result = \think\Response::success($msg, $data); $result = Response::success($msg, $data);
$this->assertEquals($msg, $result["code"]); $this->assertEquals($msg, $result["code"]);
$this->assertEquals($data, $result["data"]); $this->assertEquals($data, $result["data"]);
$this->assertEquals($url, $result["url"]); $this->assertEquals($url, $result["url"]);
$this->assertEquals("json", \think\Response::type()); $this->assertEquals("json", Response::type());
$this->assertEquals(3, $result["wait"]); $this->assertEquals(3, $result["wait"]);
// round 2 // round 2
$msg = "the msg"; $msg = "the msg";
$url = "www.thinkphptestsucess.com"; $url = "www.thinkphptestsucess.com";
$result = \think\Response::success($msg, $data, $url); $result = Response::success($msg, $data, $url);
$this->assertEquals($msg, $result["msg"]); $this->assertEquals($msg, $result["msg"]);
$this->assertEquals($url, $result["url"]); $this->assertEquals($url, $result["url"]);
@@ -220,8 +223,8 @@ class responseTest extends \PHPUnit_Framework_TestCase
// $oMockView->expects($this->any())->method('fetch')->will($this->returnValue('content')); // $oMockView->expects($this->any())->method('fetch')->will($this->returnValue('content'));
// \think\Config::set('default_return_type', "html"); // Config::set('default_return_type', "html");
// $result = \think\Response::success($msg, $data, $url); // $result = Response::success($msg, $data, $url);
// FIXME 静态方法mock // FIXME 静态方法mock
// $this->assertEquals('content', $result); // $this->assertEquals('content', $result);
@@ -241,21 +244,21 @@ class responseTest extends \PHPUnit_Framework_TestCase
$msg = 1001; $msg = 1001;
$data = "data"; $data = "data";
\think\Config::set('default_return_type', "json"); Config::set('default_return_type', "json");
$result = \think\Response::error($msg, $data); $result = Response::error($msg, $data);
$this->assertEquals($msg, $result["code"]); $this->assertEquals($msg, $result["code"]);
$this->assertEquals($data, $result["data"]); $this->assertEquals($data, $result["data"]);
$this->assertEquals('javascript:history.back(-1);', $result["url"]); $this->assertEquals('javascript:history.back(-1);', $result["url"]);
$this->assertEquals("json", \think\Response::type()); $this->assertEquals("json", Response::type());
$this->assertEquals(3, $result["wait"]); $this->assertEquals(3, $result["wait"]);
// round 2 // round 2
$msg = "the msg"; $msg = "the msg";
$url = "www.thinkphptesterror.com"; $url = "www.thinkphptesterror.com";
$result = \think\Response::error($msg, $data, $url); $result = Response::error($msg, $data, $url);
$this->assertEquals($msg, $result["msg"]); $this->assertEquals($msg, $result["msg"]);
$this->assertEquals($url, $result["url"]); $this->assertEquals($url, $result["url"]);
@@ -269,9 +272,9 @@ class responseTest extends \PHPUnit_Framework_TestCase
// $oMockView->expects($this->any())->method('fetch')->will($this->returnValue('content')); // $oMockView->expects($this->any())->method('fetch')->will($this->returnValue('content'));
// \think\Config::set('default_return_type', "html"); // Config::set('default_return_type', "html");
// $result = \think\Response::error($msg, $data, $url); // $result = Response::error($msg, $data, $url);
// FIXME 静态方法mock // FIXME 静态方法mock
// $this->assertEquals('content', $result); // $this->assertEquals('content', $result);
@@ -290,7 +293,7 @@ class responseTest extends \PHPUnit_Framework_TestCase
// // FIXME 静态方法mock Url::build // // FIXME 静态方法mock Url::build
// // echo "\r\n" . json_encode(xdebug_get_headers()) . "\r\n"; // // echo "\r\n" . json_encode(xdebug_get_headers()) . "\r\n";
// \think\Response::redirect($url, $params); // Response::redirect($url, $params);
// $this->assertContains('Location: ' . $url, xdebug_get_headers()); // $this->assertContains('Location: ' . $url, xdebug_get_headers());
} }
@@ -304,7 +307,7 @@ class responseTest extends \PHPUnit_Framework_TestCase
{ {
// $name = "Location"; // $name = "Location";
// $url = "http://www.testheader.com/"; // $url = "http://www.testheader.com/";
// \think\Response::header($name, $url); // Response::header($name, $url);
// $this->assertContains($name . ': ' . $url, xdebug_get_headers()); // $this->assertContains($name . ': ' . $url, xdebug_get_headers());
} }

View File

@@ -16,6 +16,8 @@
namespace tests\thinkphp\library\think; namespace tests\thinkphp\library\think;
use think\Session;
class sessionTest extends \PHPUnit_Framework_TestCase class sessionTest extends \PHPUnit_Framework_TestCase
{ {
@@ -56,10 +58,10 @@ class sessionTest extends \PHPUnit_Framework_TestCase
*/ */
public function testPrefix() public function testPrefix()
{ {
\think\Session::prefix(null); Session::prefix(null);
\think\Session::prefix('think_'); Session::prefix('think_');
$this->assertEquals('think_', \think\Session::prefix()); $this->assertEquals('think_', Session::prefix());
} }
/** /**
@@ -69,7 +71,7 @@ class sessionTest extends \PHPUnit_Framework_TestCase
*/ */
public function testInit() public function testInit()
{ {
\think\Session::prefix(null); Session::prefix(null);
$config = [ $config = [
// cookie 名称前缀 // cookie 名称前缀
'prefix' => 'think_', 'prefix' => 'think_',
@@ -92,10 +94,10 @@ class sessionTest extends \PHPUnit_Framework_TestCase
]; ];
$_REQUEST[$config['var_session_id']] = $config['id']; $_REQUEST[$config['var_session_id']] = $config['id'];
\think\Session::init($config); Session::init($config);
// 开始断言 // 开始断言
$this->assertEquals($config['prefix'], \think\Session::prefix()); $this->assertEquals($config['prefix'], Session::prefix());
$this->assertEquals($config['id'], $_REQUEST[$config['var_session_id']]); $this->assertEquals($config['id'], $_REQUEST[$config['var_session_id']]);
$this->assertEquals($config['name'], session_name()); $this->assertEquals($config['name'], session_name());
@@ -113,7 +115,7 @@ class sessionTest extends \PHPUnit_Framework_TestCase
session_write_close(); session_write_close();
session_destroy(); session_destroy();
\think\Session::init($config); Session::init($config);
// 测试auto_start // 测试auto_start
// PHP_SESSION_DISABLED // PHP_SESSION_DISABLED
@@ -124,7 +126,7 @@ class sessionTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($config['use_trans_sid'], ini_get('session.use_trans_sid')); $this->assertEquals($config['use_trans_sid'], ini_get('session.use_trans_sid'));
\think\Session::init($config); Session::init($config);
$this->assertEquals($config['id'], session_id()); $this->assertEquals($config['id'], session_id());
} }
@@ -159,7 +161,7 @@ class sessionTest extends \PHPUnit_Framework_TestCase
// @expectedException 异常类名 // @expectedException 异常类名
$this->setExpectedException('\think\Exception', 'error session handler', 11700); $this->setExpectedException('\think\Exception', 'error session handler', 11700);
\think\Session::init($config); Session::init($config);
} }
/** /**
@@ -169,17 +171,17 @@ class sessionTest extends \PHPUnit_Framework_TestCase
*/ */
public function testSet() public function testSet()
{ {
\think\Session::prefix(null); Session::prefix(null);
\think\Session::set('sessionname', 'sessionvalue'); Session::set('sessionname', 'sessionvalue');
$this->assertEquals('sessionvalue', $_SESSION['sessionname']); $this->assertEquals('sessionvalue', $_SESSION['sessionname']);
\think\Session::set('sessionnamearr.subname', 'sessionvalue'); Session::set('sessionnamearr.subname', 'sessionvalue');
$this->assertEquals('sessionvalue', $_SESSION['sessionnamearr']['subname']); $this->assertEquals('sessionvalue', $_SESSION['sessionnamearr']['subname']);
\think\Session::set('sessionnameper', 'sessionvalue', 'think_'); Session::set('sessionnameper', 'sessionvalue', 'think_');
$this->assertEquals('sessionvalue', $_SESSION['think_']['sessionnameper']); $this->assertEquals('sessionvalue', $_SESSION['think_']['sessionnameper']);
\think\Session::set('sessionnamearrper.subname', 'sessionvalue', 'think_'); Session::set('sessionnamearrper.subname', 'sessionvalue', 'think_');
$this->assertEquals('sessionvalue', $_SESSION['think_']['sessionnamearrper']['subname']); $this->assertEquals('sessionvalue', $_SESSION['think_']['sessionnamearrper']['subname']);
} }
@@ -190,22 +192,22 @@ class sessionTest extends \PHPUnit_Framework_TestCase
*/ */
public function testGet() public function testGet()
{ {
\think\Session::prefix(null); Session::prefix(null);
\think\Session::set('sessionnameget', 'sessionvalue'); Session::set('sessionnameget', 'sessionvalue');
$this->assertEquals(\think\Session::get('sessionnameget'), $_SESSION['sessionnameget']); $this->assertEquals(Session::get('sessionnameget'), $_SESSION['sessionnameget']);
\think\Session::set('sessionnamegetarr.subname', 'sessionvalue'); Session::set('sessionnamegetarr.subname', 'sessionvalue');
$this->assertEquals(\think\Session::get('sessionnamegetarr.subname'), $_SESSION['sessionnamegetarr']['subname']); $this->assertEquals(Session::get('sessionnamegetarr.subname'), $_SESSION['sessionnamegetarr']['subname']);
\think\Session::set('sessionnamegetarrperall', 'sessionvalue', 'think_'); Session::set('sessionnamegetarrperall', 'sessionvalue', 'think_');
$this->assertEquals(\think\Session::get('', 'think_')['sessionnamegetarrperall'], $_SESSION['think_']['sessionnamegetarrperall']); $this->assertEquals(Session::get('', 'think_')['sessionnamegetarrperall'], $_SESSION['think_']['sessionnamegetarrperall']);
\think\Session::set('sessionnamegetper', 'sessionvalue', 'think_'); Session::set('sessionnamegetper', 'sessionvalue', 'think_');
$this->assertEquals(\think\Session::get('sessionnamegetper', 'think_'), $_SESSION['think_']['sessionnamegetper']); $this->assertEquals(Session::get('sessionnamegetper', 'think_'), $_SESSION['think_']['sessionnamegetper']);
\think\Session::set('sessionnamegetarrper.subname', 'sessionvalue', 'think_'); Session::set('sessionnamegetarrper.subname', 'sessionvalue', 'think_');
$this->assertEquals(\think\Session::get('sessionnamegetarrper.subname', 'think_'), $_SESSION['think_']['sessionnamegetarrper']['subname']); $this->assertEquals(Session::get('sessionnamegetarrper.subname', 'think_'), $_SESSION['think_']['sessionnamegetarrper']['subname']);
} }
/** /**
@@ -215,21 +217,21 @@ class sessionTest extends \PHPUnit_Framework_TestCase
*/ */
public function testDelete() public function testDelete()
{ {
\think\Session::prefix(null); Session::prefix(null);
\think\Session::set('sessionnamedel', 'sessionvalue'); Session::set('sessionnamedel', 'sessionvalue');
\think\Session::delete('sessionnamedel'); Session::delete('sessionnamedel');
$this->assertEmpty($_SESSION['sessionnamedel']); $this->assertEmpty($_SESSION['sessionnamedel']);
\think\Session::set('sessionnamedelarr.subname', 'sessionvalue'); Session::set('sessionnamedelarr.subname', 'sessionvalue');
\think\Session::delete('sessionnamedelarr.subname'); Session::delete('sessionnamedelarr.subname');
$this->assertEmpty($_SESSION['sessionnamedelarr']['subname']); $this->assertEmpty($_SESSION['sessionnamedelarr']['subname']);
\think\Session::set('sessionnamedelper', 'sessionvalue', 'think_'); Session::set('sessionnamedelper', 'sessionvalue', 'think_');
\think\Session::delete('sessionnamedelper', 'think_'); Session::delete('sessionnamedelper', 'think_');
$this->assertEmpty($_SESSION['think_']['sessionnamedelper']); $this->assertEmpty($_SESSION['think_']['sessionnamedelper']);
\think\Session::set('sessionnamedelperarr.subname', 'sessionvalue', 'think_'); Session::set('sessionnamedelperarr.subname', 'sessionvalue', 'think_');
\think\Session::delete('sessionnamedelperarr.subname', 'think_'); Session::delete('sessionnamedelperarr.subname', 'think_');
$this->assertEmpty($_SESSION['think_']['sessionnamedelperarr']['subname']); $this->assertEmpty($_SESSION['think_']['sessionnamedelperarr']['subname']);
} }
@@ -240,14 +242,14 @@ class sessionTest extends \PHPUnit_Framework_TestCase
*/ */
public function testClear() public function testClear()
{ {
\think\Session::prefix(null); Session::prefix(null);
\think\Session::set('sessionnameclsper', 'sessionvalue1', 'think_'); Session::set('sessionnameclsper', 'sessionvalue1', 'think_');
\think\Session::clear('think_'); Session::clear('think_');
$this->assertNull($_SESSION['think_']); $this->assertNull($_SESSION['think_']);
\think\Session::set('sessionnameclsper', 'sessionvalue1', 'think_'); Session::set('sessionnameclsper', 'sessionvalue1', 'think_');
\think\Session::clear(); Session::clear();
$this->assertEmpty($_SESSION); $this->assertEmpty($_SESSION);
} }
@@ -258,18 +260,18 @@ class sessionTest extends \PHPUnit_Framework_TestCase
*/ */
public function testHas() public function testHas()
{ {
\think\Session::prefix(null); Session::prefix(null);
\think\Session::set('sessionnamehas', 'sessionvalue'); Session::set('sessionnamehas', 'sessionvalue');
$this->assertTrue(\think\Session::has('sessionnamehas')); $this->assertTrue(Session::has('sessionnamehas'));
\think\Session::set('sessionnamehasarr.subname', 'sessionvalue'); Session::set('sessionnamehasarr.subname', 'sessionvalue');
$this->assertTrue(\think\Session::has('sessionnamehasarr.subname')); $this->assertTrue(Session::has('sessionnamehasarr.subname'));
\think\Session::set('sessionnamehasper', 'sessionvalue', 'think_'); Session::set('sessionnamehasper', 'sessionvalue', 'think_');
$this->assertTrue(\think\Session::has('sessionnamehasper', 'think_')); $this->assertTrue(Session::has('sessionnamehasper', 'think_'));
\think\Session::set('sessionnamehasarrper.subname', 'sessionvalue', 'think_'); Session::set('sessionnamehasarrper.subname', 'sessionvalue', 'think_');
$this->assertTrue(\think\Session::has('sessionnamehasarrper.subname', 'think_')); $this->assertTrue(Session::has('sessionnamehasarrper.subname', 'think_'));
} }
/** /**
@@ -279,7 +281,7 @@ class sessionTest extends \PHPUnit_Framework_TestCase
*/ */
public function testPause() public function testPause()
{ {
\think\Session::pause(); Session::pause();
} }
/** /**
@@ -289,7 +291,7 @@ class sessionTest extends \PHPUnit_Framework_TestCase
*/ */
public function testStart() public function testStart()
{ {
\think\Session::start(); Session::start();
} }
/** /**
@@ -299,8 +301,8 @@ class sessionTest extends \PHPUnit_Framework_TestCase
*/ */
public function testDestroy() public function testDestroy()
{ {
\think\Session::set('sessionnamedestroy', 'sessionvalue'); Session::set('sessionnamedestroy', 'sessionvalue');
\think\Session::destroy(); Session::destroy();
$this->assertEmpty($_SESSION['sessionnamedestroy']); $this->assertEmpty($_SESSION['sessionnamedestroy']);
} }
} }