From b4e83c96a4ea4c3263359bee1ed7f1c47508d366 Mon Sep 17 00:00:00 2001 From: 7IN0SAN9 Date: Wed, 20 Jan 2016 11:20:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=90=8D=E5=A4=A7=E5=B0=8F?= =?UTF-8?q?=E5=86=99=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../think/cache/driver/cacheTestCase.php | 145 ++++++++ tests/thinkphp/library/think/debugTest.php | 177 ++++++++++ tests/thinkphp/library/think/inputTest.php | 172 ++++++++++ tests/thinkphp/library/think/responseTest.php | 318 ++++++++++++++++++ tests/thinkphp/library/think/sessionTest.php | 306 +++++++++++++++++ tests/thinkphp/library/think/viewTest.php | 142 ++++++++ 6 files changed, 1260 insertions(+) create mode 100644 tests/thinkphp/library/think/cache/driver/cacheTestCase.php create mode 100644 tests/thinkphp/library/think/debugTest.php create mode 100644 tests/thinkphp/library/think/inputTest.php create mode 100644 tests/thinkphp/library/think/responseTest.php create mode 100644 tests/thinkphp/library/think/sessionTest.php create mode 100644 tests/thinkphp/library/think/viewTest.php diff --git a/tests/thinkphp/library/think/cache/driver/cacheTestCase.php b/tests/thinkphp/library/think/cache/driver/cacheTestCase.php new file mode 100644 index 00000000..2bf87f58 --- /dev/null +++ b/tests/thinkphp/library/think/cache/driver/cacheTestCase.php @@ -0,0 +1,145 @@ + +// +---------------------------------------------------------------------- + +/** + * 缓存抽象类,提供一些测试 + * @author simon + */ + +namespace tests\thinkphp\library\think\cache\driver; + +use think\cache; + +abstract class cacheTestCase extends \PHPUnit_Framework_TestCase +{ + + /** + * 获取缓存句柄,子类必须有 + * @access protected + */ + abstract protected function getCacheInstance(); + /** + * tearDown函数 + */ + protected function tearDown() + { + } + /** + * 设定一组测试值,包括测试字符串、整数、数组和对象 + * @return mixed + * @access public + */ + public function prepare() + { + $cache = $this->getCacheInstance(); + $cache->clear(); + $cache->set('string_test', 'string_test'); + $cache->set('number_test', 11); + $cache->set('array_test', ['array_test' => 'array_test']); + return $cache; + } + /** + * 测试缓存设置,包括测试字符串、整数、数组和对象 + * @return mixed + * @access public + */ + public function testSet() + { + $cache = $this->getCacheInstance(); + $this->assertTrue($cache->set('string_test', 'string_test')); + $this->assertTrue($cache->set('number_test', 11)); + $this->assertTrue($cache->set('array_test', ['array_test' => 'array_test'])); + } + /** + * 测试缓存读取,包括测试字符串、整数、数组和对象 + * @return mixed + * @access public + */ + public function testGet() + { + $cache = $this->prepare(); + $this->assertEquals('string_test', $cache->get('string_test')); + $this->assertEquals(11, $cache->get('number_test')); + $array = $cache->get('array_test'); + $this->assertArrayHasKey('array_test', $array); + $this->assertEquals('array_test', $array['array_test']); + } + /** + * 测试缓存存在情况,包括测试字符串、整数、数组和对象 + * @return mixed + * @access public + */ + public function testExists() + { + $cache = $this->prepare(); + $this->assertNotEmpty($cache->get('string_test')); + $this->assertNotEmpty($cache->get('number_test')); + $this->assertFalse($cache->get('not_exists')); + } + /** + * 测试缓存不存在情况,包括测试字符串、整数、数组和对象 + * @return mixed + * @access public + */ + public function testGetNonExistent() + { + $cache = $this->getCacheInstance(); + $this->assertFalse($cache->get('non_existent_key')); + } + /** + * 测试特殊值缓存,包括测试字符串、整数、数组和对象 + * @return mixed + * @access public + */ + public function testStoreSpecialValues() + { + $cache = $this->getCacheInstance(); + $cache->set('null_value', null); + //清空缓存后,返回null而不是false + $this->assertTrue(is_null($cache->get('null_value'))); + } + /** + * 缓存过期测试 + * @return mixed + * @access public + */ + public function testExpire() + { + $cache = $this->getCacheInstance(); + $this->assertTrue($cache->set('expire_test', 'expire_test', 2)); + usleep(500000); + $this->assertEquals('expire_test', $cache->get('expire_test')); + usleep(2500000); + $this->assertFalse($cache->get('expire_test')); + } + /** + * 删除缓存测试 + * @return mixed + * @access public + */ + public function testDelete() + { + $cache = $this->prepare(); + $this->assertNotNull($cache->rm('number_test')); + $this->assertFalse($cache->get('number_test')); + } + /** + * 清空缓存测试 + * @return mixed + * @access public + */ + public function testClear() + { + $cache = $this->prepare(); + $this->assertTrue($cache->clear()); + $this->assertFalse($cache->get('number_test')); + } +} diff --git a/tests/thinkphp/library/think/debugTest.php b/tests/thinkphp/library/think/debugTest.php new file mode 100644 index 00000000..7a792d8f --- /dev/null +++ b/tests/thinkphp/library/think/debugTest.php @@ -0,0 +1,177 @@ + +// +---------------------------------------------------------------------- + +/** + * Debug测试 + * @author 大漠 + */ + +namespace tests\thinkphp\library\think; + +use think\Debug; + +class debugTest extends \PHPUnit_Framework_TestCase +{ + + /** + * + * @var Debug + */ + protected $object; + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + */ + protected function setUp() + { + $this->object = new Debug(); + } + + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + */ + protected function tearDown() + {} + + /** + * @covers think\Debug::remark + * @todo Implement testRemark(). + */ + public function testRemark() + { + $name = "testremarkkey"; + $value = "testremarkval"; + \think\Debug::remark($name); + } + + /** + * @covers think\Debug::getRangeTime + * @todo Implement testGetRangeTime(). + */ + public function testGetRangeTime() + { + $start = "testGetRangeTimeStart"; + $end = "testGetRangeTimeEnd"; + \think\Debug::remark($start); + usleep(20000); + // \think\Debug::remark($end); + + $time = \think\Debug::getRangeTime($start, $end); + $this->assertLessThan(0.03, $time); + //$this->assertEquals(0.03, ceil($time)); + } + + /** + * @covers think\Debug::getUseTime + * @todo Implement testGetUseTime(). + */ + public function testGetUseTime() + { + $time = \think\Debug::getUseTime(); + $this->assertLessThan(2.5, $time); + } + + /** + * @covers think\Debug::getThroughputRate + * @todo Implement testGetThroughputRate(). + */ + public function testGetThroughputRate() + { + usleep(100000); + $throughputRate = \think\Debug::getThroughputRate(); + $this->assertLessThan(10, $throughputRate); + } + + /** + * @covers think\Debug::getRangeMem + * @todo Implement testGetRangeMem(). + */ + public function testGetRangeMem() + { + $start = "testGetRangeMemStart"; + $end = "testGetRangeMemEnd"; + \think\Debug::remark($start); + $str = ""; + for ($i = 0; $i < 10000; $i++) { + $str .= "mem"; + } + + $rangeMem = \think\Debug::getRangeMem($start, $end); + + $this->assertLessThan(33, explode(" ", $rangeMem)[0]); + } + + /** + * @covers think\Debug::getUseMem + * @todo Implement testGetUseMem(). + */ + public function testGetUseMem() + { + $useMem = \think\Debug::getUseMem(); + + $this->assertLessThan(13, explode(" ", $useMem)[0]); + } + + /** + * @covers think\Debug::getMemPeak + * @todo Implement testGetMemPeak(). + */ + public function testGetMemPeak() + { + $start = "testGetMemPeakStart"; + $end = "testGetMemPeakEnd"; + \think\Debug::remark($start); + $str = ""; + for ($i = 0; $i < 100000; $i++) { + $str .= "mem"; + } + $memPeak = \think\Debug::getMemPeak($start, $end); + + // echo "\r\n" . $memPeak . "\r\n"; + + $this->assertLessThan(238, explode(" ", $memPeak)[0]); + } + + /** + * @covers think\Debug::getFile + * @todo Implement testGetFile(). + */ + public function testGetFile() + { + $count = \think\Debug::getFile(); + + $this->assertEquals(count(get_included_files()), $count); + + $info = \think\Debug::getFile(true); + $this->assertEquals(count(get_included_files()), count($info)); + + $this->assertContains("KB", $info[0]); + } + + /** + * @covers think\Debug::dump + * @todo Implement testDump(). + */ + public function testDump() + { + $var = array(); + $var["key"] = "val"; + $output = \think\Debug::dump($var, false, $label = "label"); + + if (IS_WIN) { + $this->assertEquals("(1) {\\n 'key' =>\\n string(3) \\\"val\\\"\\n}\\n\\r\\n\"", end(explode("array", json_encode($output)))); + } else { + $this->assertEquals("(1) {\\n 'key' =>\\n string(3) \\\"val\\\"\\n}\\n\\n\"", end(explode("array", json_encode($output)))); + } + } +} diff --git a/tests/thinkphp/library/think/inputTest.php b/tests/thinkphp/library/think/inputTest.php new file mode 100644 index 00000000..52740409 --- /dev/null +++ b/tests/thinkphp/library/think/inputTest.php @@ -0,0 +1,172 @@ + +// +---------------------------------------------------------------------- + +/** + * Input测试 + * @author Haotong Lin + */ + +namespace tests\thinkphp\library\think; + +use think\Input; + +class inputTest extends \PHPUnit_Framework_TestCase +{ + public function testEmptyStringName() + { + $input = ['a' => 'test']; + $this->assertEquals($input, Input::getData('', $input, 'trim')); + } + + public function testInputName() + { + $input = ['a' => 'test']; + $this->assertEquals($input['a'], Input::getData('a', $input)); + } + + public function testDefaultValue() + { + $input = ['a' => 'test']; + $default = 'default'; + $this->assertEquals($default, Input::getData('foo', $input, null, $default)); + } + + public function testStringFilter() + { + $input = ['a' => ' test ', 'b' => ' test<> ']; + $filters = 'trim'; + $this->assertEquals('test', Input::getData('a', $input, $filters)); + $filters = 'trim,htmlspecialchars'; + $this->assertEquals('test<>', Input::getData('b', $input, $filters)); + } + + public function testArrayFilter() + { + $input = ['a' => ' test ', 'b' => ' test<> ']; + $filters = ['trim']; + $this->assertEquals('test', Input::getData('a', $input, $filters)); + $filters = ['trim', 'htmlspecialchars']; + $this->assertEquals('test<>', Input::getData('b', $input, $filters)); + } + + public function testFilterExp() + { + $src = 'EXP|NEQ|GT|EGT|LT|ELT|OR|XOR|LIKE|NOTLIKE|NOT BETWEEN|NOTBETWEEN|BETWEEN|NOTIN|NOT IN|IN'; + $regexs = explode('|', $src); + $data = Input::getData('', $regexs); + foreach ($regexs as $key => $value) { + $expected = $value . ' '; + $this->assertEquals($expected, $data[$key]); + } + } + + public function testFiltrateWithRegex() + { + $input = ['a' => 'test1', 'b' => '_test2']; + $filters = '/^test/'; + $this->assertEquals('test1', Input::getData('a', $input, $filters)); + $default = 'default value'; + $this->assertEquals($default, Input::getData('b', $input, $filters, $default)); + } + + public function testFiltrateWithFilterVar() + { + $email = 'abc@gmail.com'; + $error = 'not email'; + $default = false; + $input = ['a' => $email, 'b' => $error]; + $filters = FILTER_VALIDATE_EMAIL; + $this->assertEquals($email, Input::getData('a', $input, $filters)); + $this->assertFalse(Input::getData('b', $input, $filters, $default)); + $filters = 'validate_email'; + $this->assertFalse(Input::getData('b', $input, $filters, $default)); + } + + public function testAllInput() + { + $input = [ + 'a' => ' trim ', + 'b' => 'htmlspecialchars<>', + 'c' => ' trim htmlspecialchars<> ', + 'd' => 'eXp', + 'e' => 'NEQ', + 'f' => 'gt', + ]; + $filters = 'htmlspecialchars,trim'; + $excepted = [ + 'a' => 'trim', + 'b' => 'htmlspecialchars<>', + 'c' => 'trim htmlspecialchars<>', + 'd' => 'eXp ', + 'e' => 'NEQ ', + 'f' => 'gt ', + ]; + $this->assertEquals($excepted, Input::getData('', $input, $filters)); + } + + public function testTypeCast() + { + $input = [ + 'a' => [1, 2, 3], + 'b' => '1000', + 'c' => '3.14', + 'd' => 'test boolean', + ]; + $this->assertEquals([1, 2, 3], Input::getData('a/a', $input)); + $this->assertEquals(1000, Input::getData('b/d', $input)); + $this->assertEquals(3.14, Input::getData('c/f', $input)); + $this->assertEquals(true, Input::getData('d/b', $input)); + } + + public function testSuperglobals() + { + Input::setFilter('trim'); + $_GET['get'] = 'get value '; + $this->assertEquals('get value', Input::get('get')); + $_POST['post'] = 'post value '; + $this->assertEquals('post value', Input::post('post')); + + $_SERVER['REQUEST_METHOD'] = 'POST'; + $this->assertEquals('post value', Input::param('post')); + $this->assertEquals(null, Input::param('get')); + $_SERVER['REQUEST_METHOD'] = 'GET'; + $this->assertEquals('get value', Input::param('get')); + $this->assertEquals(null, Input::param('post')); + + session_start(); + $_SESSION['test'] = 'session value '; + $this->assertEquals('session value', Input::session('test')); + session_destroy(); + + $_COOKIE['cookie'] = 'cookie value '; + $this->assertEquals('cookie value', Input::cookie('cookie')); + + $_SERVER['REQUEST_METHOD'] = 'GET '; + $this->assertEquals('GET', Input::server('REQUEST_METHOD')); + + $this->assertEquals('testing', Input::env('APP_ENV')); + } + + public function testFilterCover() + { + Input::setFilter('htmlspecialchars'); + $input = ['a' => ' test<> ', 'b' => '']; + $filters = ['trim']; + $this->assertEquals('test<>', Input::getData('a', $input, $filters)); + $filters = ['trim', false]; + $this->assertEquals('test<>', Input::getData('a', $input, $filters)); + $filters = 'stripslashes'; + $this->assertEquals("<bar />", Input::getData('b', $input, $filters)); + $filters = 'stripslashes,0'; + $this->assertEquals("", Input::getData('b', $input, $filters)); + } + +} diff --git a/tests/thinkphp/library/think/responseTest.php b/tests/thinkphp/library/think/responseTest.php new file mode 100644 index 00000000..b4b6da06 --- /dev/null +++ b/tests/thinkphp/library/think/responseTest.php @@ -0,0 +1,318 @@ + +// +---------------------------------------------------------------------- + +/** + * Response测试 + * @author 大漠 + */ + +namespace tests\thinkphp\library\think; + +class responseTest extends \PHPUnit_Framework_TestCase +{ + + /** + * + * @var Response + */ + protected $object; + + protected $default_return_type; + + protected $default_ajax_return; + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + */ + protected function setUp() + { + // 1. + // restore_error_handler(); + // Warning: Cannot modify header information - headers already sent by (output started at PHPUnit\Util\Printer.php:173) + // more see in https://www.analysisandsolutions.com/blog/html/writing-phpunit-tests-for-wordpress-plugins-wp-redirect-and-continuing-after-php-errors.htm + + // 2. + // the Symfony used the HeaderMock.php + + // 3. + // not run the eclipse will held, and travis-ci.org Searching for coverage reports + // **> Python coverage not found + // **> No coverage report found. + // add the + // /** + // * @runInSeparateProcess + // */ + if (!$this->default_return_type) { + $this->default_return_type = \think\Config::get('default_return_type'); + } + if (!$this->default_ajax_return) { + $this->default_ajax_return = \think\Config::get('default_ajax_return'); + } + } + + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + */ + protected function tearDown() + { + \think\Config::set('default_ajax_return', $this->default_ajax_return); + \think\Config::set('default_return_type', $this->default_return_type); + \think\Response::type(\think\Config::get('default_return_type')); // 会影响其他测试 + } + + /** + * @covers think\Response::send + * @todo Implement testSend(). + */ + public function testSend() + { + $dataArr = array(); + $dataArr["key"] = "value"; + $dataArr->key = "val"; + + $result = \think\Response::send($dataArr, "", true); + $this->assertArrayHasKey("key", $result); + + $result = \think\Response::send($dataArr, "json", true); + $this->assertEquals('{"key":"value"}', $result); + + $handler = "callback"; + $_GET[\think\Config::get('var_jsonp_handler')] = $handler; + $result = \think\Response::send($dataArr, "jsonp", true); + $this->assertEquals('callback({"key":"value"});', $result); + + \think\Response::tramsform(function () { + + return "callbackreturndata"; + }); + + $result = \think\Response::send($dataArr, "", true); + $this->assertEquals("callbackreturndata", $result); + $_GET[\think\Config::get('var_jsonp_handler')] = ""; + } + + /** + * @covers think\Response::tramsform + * @todo Implement testTramsform(). + */ + public function testTramsform() + { + \think\Response::tramsform(function () { + + return "callbackreturndata"; + }); + + $result = \think\Response::send($dataArr, "", true); + $this->assertEquals("callbackreturndata", $result); + + \think\Response::tramsform(null); + } + + /** + * @covers think\Response::type + * @todo Implement testType(). + */ + public function testType() + { + $type = "json"; + \think\Response::type($type); + + $result = \think\Response::type(); + $this->assertEquals($type, $result); + \think\Response::type($type); + } + + /** + * @covers think\Response::data + * @todo Implement testData(). + */ + public function testData() + { + $data = "data"; + \think\Response::data($data); + \think\Response::data(null); + } + + /** + * @covers think\Response::isExit + * @todo Implement testIsExit(). + */ + public function testIsExit() + { + $isExit = true; + \think\Response::isExit($isExit); + + $result = \think\Response::isExit(); + $this->assertTrue($isExit, $result); + \think\Response::isExit(false); + } + + /** + * @covers think\Response::result + * @todo Implement testResult(). + */ + public function testResult() + { + $data = "data"; + $code = "1001"; + $msg = "the msg"; + $type = "json"; + $result = \think\Response::result($data, $code, $msg, $type); + + $this->assertEquals($code, $result["code"]); + $this->assertEquals($msg, $result["msg"]); + $this->assertEquals($data, $result["data"]); + $this->assertEquals($_SERVER['REQUEST_TIME_FLOAT'], $result["time"]); + $this->assertEquals($type, \think\Response::type()); + } + + /** + * @covers think\Response::success + * @todo Implement testSuccess(). + */ + public function testSuccess() + { + // round 1 + $msg = 1001; + $data = "data"; + + $url = "www.HTTP_REFERER.com"; + $HTTP_REFERER = $_SERVER["HTTP_REFERER"]; + $_SERVER["HTTP_REFERER"] = $url; + \think\Config::set('default_return_type', "json"); + + $result = \think\Response::success($msg, $data); + + $this->assertEquals($msg, $result["code"]); + + $this->assertEquals($data, $result["data"]); + $this->assertEquals($url, $result["url"]); + $this->assertEquals("json", \think\Response::type()); + $this->assertEquals(3, $result["wait"]); + + // round 2 + $msg = "the msg"; + $url = "www.thinkphptestsucess.com"; + + $result = \think\Response::success($msg, $data, $url); + + $this->assertEquals($msg, $result["msg"]); + $this->assertEquals($url, $result["url"]); + + // round 3 异常在travis-ci中未能重现 + // $this->setExpectedException('\think\Exception'); + // FIXME 静态方法mock + // $oMockView = $this->getMockBuilder('\think\View')->setMethods(array( + // 'fetch' + // ))->getMock(); + + // $oMockView->expects($this->any())->method('fetch')->will($this->returnValue('content')); + + // \think\Config::set('default_return_type', "html"); + // $result = \think\Response::success($msg, $data, $url); + + // FIXME 静态方法mock + // $this->assertEquals('content', $result); + + $_SERVER["HTTP_REFERER"] = $HTTP_REFERER; + } + + /** + * @covers think\Response::error + * @todo Implement testError(). + */ + public function testError() + { + // round 1 + $msg = 1001; + $data = "data"; + + \think\Config::set('default_return_type', "json"); + + $result = \think\Response::error($msg, $data); + + $this->assertEquals($msg, $result["code"]); + $this->assertEquals($data, $result["data"]); + $this->assertEquals('javascript:history.back(-1);', $result["url"]); + $this->assertEquals("json", \think\Response::type()); + $this->assertEquals(3, $result["wait"]); + + // round 2 + $msg = "the msg"; + $url = "www.thinkphptesterror.com"; + + $result = \think\Response::error($msg, $data, $url); + + $this->assertEquals($msg, $result["msg"]); + $this->assertEquals($url, $result["url"]); + + // round 3 异常在travis-ci中未能重现 + // $this->setExpectedException('\think\Exception'); + // FIXME 静态方法mock + // $oMockView = $this->getMockBuilder('\think\View')->setMethods(array( + // 'fetch' + // ))->getMock(); + + // $oMockView->expects($this->any())->method('fetch')->will($this->returnValue('content')); + + // \think\Config::set('default_return_type', "html"); + + // $result = \think\Response::error($msg, $data, $url); + + // FIXME 静态方法mock + // $this->assertEquals('content', $result); + } + + /** + * @#runInSeparateProcess + * @covers think\Response::redirect + * @todo Implement testRedirect(). + */ + public function testRedirect() + { + // $url = "http://www.testredirect.com"; + // $params = array(); + // $params[] = 301; + + // // FIXME 静态方法mock Url::build + // // echo "\r\n" . json_encode(xdebug_get_headers()) . "\r\n"; + // \think\Response::redirect($url, $params); + + // $this->assertContains('Location: ' . $url, xdebug_get_headers()); + } + + /** + * @#runInSeparateProcess + * @covers think\Response::header + * @todo Implement testHeader(). + */ + public function testHeader() + { + // $name = "Location"; + // $url = "http://www.testheader.com/"; + // \think\Response::header($name, $url); + // $this->assertContains($name . ': ' . $url, xdebug_get_headers()); + } + + /** + * @#runInSeparateProcess + * @covers think\Response::sendHttpStatus + * @todo Implement testSendHttpStatus(). + */ + public function testSendHttpStatus() + { + // \think\Response::sendHttpStatus(416); + // $this->assertContains('HTTP/1.1 ' . ': ' . $status, xdebug_get_headers()); + // $this->assertContains('Status:' . ': ' . $status, xdebug_get_headers()); + } +} diff --git a/tests/thinkphp/library/think/sessionTest.php b/tests/thinkphp/library/think/sessionTest.php new file mode 100644 index 00000000..65b42906 --- /dev/null +++ b/tests/thinkphp/library/think/sessionTest.php @@ -0,0 +1,306 @@ + +// +---------------------------------------------------------------------- + +/** + * Session测试 + * @author 大漠 + */ + +namespace tests\thinkphp\library\think; + +class sessionTest extends \PHPUnit_Framework_TestCase +{ + + /** + * + * @var Session + */ + protected $object; + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + */ + protected function setUp() + { + // $this->object = new Session (); + // register_shutdown_function ( function () { + // } ); // 此功能无法取消,需要回调函数配合。 + set_exception_handler(function () {}); + set_error_handler(function () {}); + } + + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + */ + protected function tearDown() + { + register_shutdown_function('think\Error::appShutdown'); + set_error_handler('think\Error::appError'); + set_exception_handler('think\Error::appException'); + } + + /** + * @covers think\Session::prefix + * + * @todo Implement testPrefix(). + */ + public function testPrefix() + { + \think\Session::prefix(null); + \think\Session::prefix('think_'); + + $this->assertEquals('think_', \think\Session::prefix()); + } + + /** + * @covers think\Session::init + * + * @todo Implement testInit(). + */ + public function testInit() + { + \think\Session::prefix(null); + $config = [ + // cookie 名称前缀 + 'prefix' => 'think_', + // cookie 保存时间 + 'expire' => 60, + // cookie 保存路径 + 'path' => '/path/to/test/session/', + // cookie 有效域名 + 'domain' => '.thinkphp.cn', + 'var_session_id' => 'sessionidtest', + 'id' => 'sess_8fhgkjuakhatbeg2fa14lo84q1', + 'name' => 'session_name', + 'use_trans_sid' => '1', + 'use_cookies' => '1', + 'cache_limiter' => '60', + 'cache_expire' => '60', + 'type' => '', // memcache + 'namespace' => '\\think\\session\\driver\\', // ? + 'auto_start' => '1', + ]; + + $_REQUEST[$config['var_session_id']] = $config['id']; + \think\Session::init($config); + + // 开始断言 + $this->assertEquals($config['prefix'], \think\Session::prefix()); + $this->assertEquals($config['id'], $_REQUEST[$config['var_session_id']]); + $this->assertEquals($config['name'], session_name()); + + $this->assertEquals($config['path'], session_save_path()); + $this->assertEquals($config['use_cookies'], ini_get('session.use_cookies')); + $this->assertEquals($config['domain'], ini_get('session.cookie_domain')); + $this->assertEquals($config['expire'], ini_get('session.gc_maxlifetime')); + $this->assertEquals($config['expire'], ini_get('session.cookie_lifetime')); + + $this->assertEquals($config['cache_limiter'], session_cache_limiter($config['cache_limiter'])); + $this->assertEquals($config['cache_expire'], session_cache_expire($config['cache_expire'])); + + // 检测分支 + $_REQUEST[$config['var_session_id']] = null; + session_write_close(); + session_destroy(); + + \think\Session::init($config); + + // 测试auto_start + // PHP_SESSION_DISABLED + // PHP_SESSION_NONE + // PHP_SESSION_ACTIVE + // session_status() + $this->assertEquals(0, ini_get('session.auto_start')); + + $this->assertEquals($config['use_trans_sid'], ini_get('session.use_trans_sid')); + + \think\Session::init($config); + $this->assertEquals($config['id'], session_id()); + } + + /** + * 单独重现异常 + * @expectedException \think\Exception + */ + public function testException() + { + $config = [ + // cookie 名称前缀 + 'prefix' => 'think_', + // cookie 保存时间 + 'expire' => 0, + // cookie 保存路径 + 'path' => '/path/to/test/session/', + // cookie 有效域名 + 'domain' => '.thinkphp.cn', + 'var_session_id' => 'sessionidtest', + 'id' => 'sess_8fhgkjuakhatbeg2fa14lo84q1', + 'name' => 'session_name', + 'use_trans_sid' => '1', + 'use_cookies' => '1', + 'cache_limiter' => '60', + 'cache_expire' => '60', + 'type' => 'memcache', // + 'namespace' => '\\think\\session\\driver\\', // ? + 'auto_start' => '1', + ]; + + // 测试session驱动是否存在 + // @expectedException 异常类名 + $this->setExpectedException('\think\Exception', 'error session handler', 11700); + + \think\Session::init($config); + } + + /** + * @covers think\Session::set + * + * @todo Implement testSet(). + */ + public function testSet() + { + \think\Session::prefix(null); + \think\Session::set('sessionname', 'sessionvalue'); + $this->assertEquals('sessionvalue', $_SESSION['sessionname']); + + \think\Session::set('sessionnamearr.subname', 'sessionvalue'); + $this->assertEquals('sessionvalue', $_SESSION['sessionnamearr']['subname']); + + \think\Session::set('sessionnameper', 'sessionvalue', 'think_'); + $this->assertEquals('sessionvalue', $_SESSION['think_']['sessionnameper']); + + \think\Session::set('sessionnamearrper.subname', 'sessionvalue', 'think_'); + $this->assertEquals('sessionvalue', $_SESSION['think_']['sessionnamearrper']['subname']); + } + + /** + * @covers think\Session::get + * + * @todo Implement testGet(). + */ + public function testGet() + { + \think\Session::prefix(null); + + \think\Session::set('sessionnameget', 'sessionvalue'); + $this->assertEquals(\think\Session::get('sessionnameget'), $_SESSION['sessionnameget']); + + \think\Session::set('sessionnamegetarr.subname', 'sessionvalue'); + $this->assertEquals(\think\Session::get('sessionnamegetarr.subname'), $_SESSION['sessionnamegetarr']['subname']); + + \think\Session::set('sessionnamegetarrperall', 'sessionvalue', 'think_'); + $this->assertEquals(\think\Session::get('', 'think_')['sessionnamegetarrperall'], $_SESSION['think_']['sessionnamegetarrperall']); + + \think\Session::set('sessionnamegetper', 'sessionvalue', 'think_'); + $this->assertEquals(\think\Session::get('sessionnamegetper', 'think_'), $_SESSION['think_']['sessionnamegetper']); + + \think\Session::set('sessionnamegetarrper.subname', 'sessionvalue', 'think_'); + $this->assertEquals(\think\Session::get('sessionnamegetarrper.subname', 'think_'), $_SESSION['think_']['sessionnamegetarrper']['subname']); + } + + /** + * @covers think\Session::delete + * + * @todo Implement testDelete(). + */ + public function testDelete() + { + \think\Session::prefix(null); + \think\Session::set('sessionnamedel', 'sessionvalue'); + \think\Session::delete('sessionnamedel'); + $this->assertEmpty($_SESSION['sessionnamedel']); + + \think\Session::set('sessionnamedelarr.subname', 'sessionvalue'); + \think\Session::delete('sessionnamedelarr.subname'); + $this->assertEmpty($_SESSION['sessionnamedelarr']['subname']); + + \think\Session::set('sessionnamedelper', 'sessionvalue', 'think_'); + \think\Session::delete('sessionnamedelper', 'think_'); + $this->assertEmpty($_SESSION['think_']['sessionnamedelper']); + + \think\Session::set('sessionnamedelperarr.subname', 'sessionvalue', 'think_'); + \think\Session::delete('sessionnamedelperarr.subname', 'think_'); + $this->assertEmpty($_SESSION['think_']['sessionnamedelperarr']['subname']); + } + + /** + * @covers think\Session::clear + * + * @todo Implement testClear(). + */ + public function testClear() + { + \think\Session::prefix(null); + + \think\Session::set('sessionnameclsper', 'sessionvalue1', 'think_'); + \think\Session::clear('think_'); + $this->assertNull($_SESSION['think_']); + + \think\Session::set('sessionnameclsper', 'sessionvalue1', 'think_'); + \think\Session::clear(); + $this->assertEmpty($_SESSION); + } + + /** + * @covers think\Session::has + * + * @todo Implement testHas(). + */ + public function testHas() + { + \think\Session::prefix(null); + \think\Session::set('sessionnamehas', 'sessionvalue'); + $this->assertTrue(\think\Session::has('sessionnamehas')); + + \think\Session::set('sessionnamehasarr.subname', 'sessionvalue'); + $this->assertTrue(\think\Session::has('sessionnamehasarr.subname')); + + \think\Session::set('sessionnamehasper', 'sessionvalue', 'think_'); + $this->assertTrue(\think\Session::has('sessionnamehasper', 'think_')); + + \think\Session::set('sessionnamehasarrper.subname', 'sessionvalue', 'think_'); + $this->assertTrue(\think\Session::has('sessionnamehasarrper.subname', 'think_')); + } + + /** + * @covers think\Session::pause + * + * @todo Implement testPause(). + */ + public function testPause() + { + \think\Session::pause(); + } + + /** + * @covers think\Session::start + * + * @todo Implement testStart(). + */ + public function testStart() + { + \think\Session::start(); + } + + /** + * @covers think\Session::destroy + * + * @todo Implement testDestroy(). + */ + public function testDestroy() + { + \think\Session::set('sessionnamedestroy', 'sessionvalue'); + \think\Session::destroy(); + $this->assertEmpty($_SESSION['sessionnamedestroy']); + } +} diff --git a/tests/thinkphp/library/think/viewTest.php b/tests/thinkphp/library/think/viewTest.php new file mode 100644 index 00000000..e925285f --- /dev/null +++ b/tests/thinkphp/library/think/viewTest.php @@ -0,0 +1,142 @@ + +// +---------------------------------------------------------------------- + +/** + * view测试 + * @author mahuan + */ + +namespace tests\thinkphp\library\think; + +class viewTest extends \PHPUnit_Framework_TestCase +{ + + /** + * 句柄测试 + * @return mixed + * @access public + */ + public function testGetInstance() + { + \think\Cookie::get('a'); + $view_instance = \think\View::instance(); + $this->assertInstanceOf('\think\view', $view_instance, 'instance方法返回错误'); + } + + /** + * 测试变量赋值 + * @return mixed + * @access public + */ + public function testAssign() + { + $view_instance = \think\View::instance(); + $data = $view_instance->assign(array('key' => 'value')); + $data = $view_instance->assign('key2', 'value2'); + //测试私有属性 + $expect_data = array('key' => 'value', 'key2' => 'value2'); + $this->assertAttributeEquals($expect_data, 'data', $view_instance); + } + + /** + * 测试配置 + * @return mixed + * @access public + */ + public function testConfig() + { + $view_instance = \think\View::instance(); + $data = $view_instance->config('key2', 'value2'); + $data = $view_instance->config('key3', 'value3'); + $data = $view_instance->config('key3', 'value_cover'); + //不应包含value + $data = $view_instance->config(array('key' => 'value')); + //基础配置替换 + $data = $view_instance->config(array('view_path' => 'view_path')); + //目标结果 + $this->assertAttributeContains('value2', "config", $view_instance); + $this->assertAttributeContains('value_cover', "config", $view_instance); + $this->assertAttributeNotContains('value', "config", $view_instance); + $this->assertAttributeContains('view_path', "config", $view_instance); + } + + /** + * 测试引擎设置 + * @return mixed + * @access public + */ + public function testEngine() + { + $view_instance = \think\View::instance(); + $data = $view_instance->engine('php'); + $this->assertAttributeEquals('php', 'engine', $view_instance); + //测试模板引擎驱动 + $data = $view_instance->engine('think'); + $think_engine = new \think\view\driver\Think; + $this->assertAttributeEquals($think_engine, 'engine', $view_instance); + } + + /** + * 测试引擎设置 + * @return mixed + * @access public + */ + public function testTheme() + { + $view_instance = \think\View::instance(); + $data = $view_instance->theme(true); + //反射类取出私有属性的值 + $reflection = new \ReflectionClass('\think\View'); + $property = $reflection->getProperty('config'); + $property->setAccessible(true); + $config_value = $property->getValue($view_instance); + + $this->assertTrue($config_value['theme_on']); + $this->assertTrue($config_value['auto_detect_theme']); + + //关闭主题测试 + $data = $view_instance->theme(false); + $config_value = $property->getValue($view_instance); + $this->assertFalse($config_value['theme_on']); + + //指定主题测试 + $data = $view_instance->theme('theme_name'); + $config_value = $property->getValue($view_instance); + $this->assertTrue($config_value['theme_on']); + $this->assertAttributeEquals('theme_name', 'theme', $view_instance); + } + + /** + * 测试引擎设置 + * @return mixed + * @access public + */ + public function testParseTemplate() + { + $view_instance = \think\View::instance(); + $method = new \ReflectionMethod('\think\View', 'ParseTemplate'); + $method->setAccessible(true); + $this->assertEquals('/theme_name/CONTROLLER_NAME/template_name.html', $method->invoke($view_instance, 'template_name')); + } + + /** + * 测试引擎设置 + * @return mixed + * @access public + */ + public function testGetThemePath() + { + $view_instance = \think\View::instance(); + $method = new \ReflectionMethod('\think\View', 'getThemePath'); + $method->setAccessible(true); + $this->assertEquals('/theme_name/', $method->invoke($view_instance)); + } +}