From 00c101abf65db86f899d8d6eacc51d4508770bfc Mon Sep 17 00:00:00 2001 From: 7IN0SAN9 Date: Tue, 19 Jan 2016 22:34:27 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E5=8A=A8=20tests=20=E5=88=B0=20thinkp?= =?UTF-8?q?hp=20=E7=9B=AE=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/README.md | 163 +++++++++ tests/conf/apc.ini | 1 + tests/conf/apcu_bc.ini | 2 + tests/fixtures/config.ini | 1 + tests/fixtures/config.php | 26 ++ tests/fixtures/config.xml | 6 + tests/fixtures/runtime/.gitignore | 2 + tests/mock.php | 28 ++ tests/script/install.sh | 19 ++ tests/thinkphp/baseTest.php | 61 ++++ tests/thinkphp/library/think/DebugTest.php | 172 ++++++++++ tests/thinkphp/library/think/InputTest.php | 165 +++++++++ tests/thinkphp/library/think/ResponseTest.php | 315 ++++++++++++++++++ tests/thinkphp/library/think/SessionTest.php | 303 +++++++++++++++++ tests/thinkphp/library/think/ViewTest.php | 140 ++++++++ tests/thinkphp/library/think/appTest.php | 22 ++ .../think/cache/driver/CacheTestCase.php | 135 ++++++++ .../library/think/cache/driver/apcTest.php | 50 +++ .../library/think/cache/driver/dbTest.php | 42 +++ .../library/think/config/driver/iniTest.php | 34 ++ .../library/think/config/driver/xmlTest.php | 34 ++ tests/thinkphp/library/think/configTest.php | 170 ++++++++++ .../library/think/controller/.gitignore | 2 + .../thinkphp/library/think/controllerTest.php | 99 ++++++ tests/thinkphp/library/think/cookieTest.php | 146 ++++++++ .../library/think/db/driver/.gitignore | 2 + .../library/think/log/driver/.gitignore | 2 + tests/thinkphp/library/think/model/.gitignore | 2 + .../thinkphp/library/think/session/.gitignore | 2 + .../library/think/template/driver/.gitignore | 2 + .../library/think/template/taglib/.gitignore | 2 + tests/thinkphp/library/think/templateTest.php | 257 ++++++++++++++ .../library/think/view/driver/.gitignore | 2 + .../library/traits/controller/.gitignore | 2 + .../thinkphp/library/traits/model/.gitignore | 2 + 35 files changed, 2413 insertions(+) create mode 100644 tests/README.md create mode 100644 tests/conf/apc.ini create mode 100644 tests/conf/apcu_bc.ini create mode 100644 tests/fixtures/config.ini create mode 100644 tests/fixtures/config.php create mode 100644 tests/fixtures/config.xml create mode 100644 tests/fixtures/runtime/.gitignore create mode 100644 tests/mock.php create mode 100755 tests/script/install.sh create mode 100644 tests/thinkphp/baseTest.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 create mode 100644 tests/thinkphp/library/think/appTest.php create mode 100644 tests/thinkphp/library/think/cache/driver/CacheTestCase.php create mode 100644 tests/thinkphp/library/think/cache/driver/apcTest.php create mode 100644 tests/thinkphp/library/think/cache/driver/dbTest.php create mode 100644 tests/thinkphp/library/think/config/driver/iniTest.php create mode 100644 tests/thinkphp/library/think/config/driver/xmlTest.php create mode 100644 tests/thinkphp/library/think/configTest.php create mode 100644 tests/thinkphp/library/think/controller/.gitignore create mode 100644 tests/thinkphp/library/think/controllerTest.php create mode 100644 tests/thinkphp/library/think/cookieTest.php create mode 100644 tests/thinkphp/library/think/db/driver/.gitignore create mode 100644 tests/thinkphp/library/think/log/driver/.gitignore create mode 100644 tests/thinkphp/library/think/model/.gitignore create mode 100644 tests/thinkphp/library/think/session/.gitignore create mode 100644 tests/thinkphp/library/think/template/driver/.gitignore create mode 100644 tests/thinkphp/library/think/template/taglib/.gitignore create mode 100644 tests/thinkphp/library/think/templateTest.php create mode 100644 tests/thinkphp/library/think/view/driver/.gitignore create mode 100644 tests/thinkphp/library/traits/controller/.gitignore create mode 100644 tests/thinkphp/library/traits/model/.gitignore diff --git a/tests/README.md b/tests/README.md new file mode 100644 index 00000000..cc14c255 --- /dev/null +++ b/tests/README.md @@ -0,0 +1,163 @@ +###测试目录结构### +测试文件主要在tests文件下面,主要有以下几个文件夹 +- mock 小核心文件,用于做文件加载,引入框架等操作。 +- framework 真正的测试文件,和项目文件夹机构一致,但是里面都是测试文件 + - think + - application + - public + - tests + - thinkphp + - 其他文件 + +###准备工作### +1. 判断渲染,若果是测试环境则不执行渲染,主要在library/think/app.php文件中,修改其中的第116行: + + ```php + if (defined('IN_UNIT_TEST')) { + return $data; + } else { + Response::returnData($data, Config::get('default_return_type'), Config::get('response_exit')); + } + ``` + 和129行 + + ```php + if (defined('IN_UNIT_TEST')) { + return $data; + } else { + Response::returnData($data, Config::get('default_return_type'), Config::get('response_exit')); + } + ``` + +2. 修改config.php,添加重置配置函数: + + ```php + // 重置配置参数 + public static function reset($name, $value = null, $range = ''){ + self::$config = []; + } + ``` + +3. 添加mock文件,在文件中添加单元测试定义,以及请求定义,否则报错。 + + ```php + markTestSkipped('apc扩展不可用!'); + }; + ``` + + - 编写测试用例 + + *具体写法参照 [PHPUnit 官方文档](https://phpunit.de/manual/4.8/zh_cn/index.html)* + + ```php + public function testGet() + { + App::run(); + $this->assertInstanceOf( + '\think\cache\driver\Apc', + Cache::connect(['type' => 'apc', 'expire' => 1]) + ); + $this->assertTrue(Cache::set('key', 'value')); + $this->assertEquals('value', Cache::get('key')); + $this->assertTrue(Cache::rm('key')); + $this->assertFalse(Cache::get('key')); + $this->assertTrue(Cache::clear('key')); + Config::reset(); + } + ``` + +3. 回到根目录执行单元测试命令: + ```bash + $ phpunit + ``` + 若想看到所有结果,请添加-v参数 + ```bash + $ phpunit -v + ``` + +4. 输出结果 + +###相关文档### +[各个部分单元测试说明](http://www.kancloud.cn/brother_simon/tp5_test/96971 "各部分单元测试说明") + +###大家一起来### +单元测试的内容会跟框架同步,测试内容方方面面,是一个相对复杂的模块,同时也是一个值得重视的部分。希望大家能够多多提出意见,多多参与。如果你有任何问题或想法,可以随时提issue,我们期待着收到听大家的质疑和讨论。 + +## 任务进度 +单元测试任务进度,请大家认领模块 + +|模块|认领人|进度| +|---|---|---| +|Base||| +|App||| +|Build||| +|Config||| +|Cache||| +|Controller||| +|Cookie||| +|Db||| +|Debug|大漠|| +|Error|大漠|| +|Hook||| +|Input||| +|Lang||| +|Loader||| +|Log||| +|Model||| +|Response|大漠|√| +|Route||| +|Session|大漠|√| +|Template||| +|Url||| +|View||| diff --git a/tests/conf/apc.ini b/tests/conf/apc.ini new file mode 100644 index 00000000..008bcabf --- /dev/null +++ b/tests/conf/apc.ini @@ -0,0 +1 @@ +apc.enable_cli=1 diff --git a/tests/conf/apcu_bc.ini b/tests/conf/apcu_bc.ini new file mode 100644 index 00000000..acb9c7e6 --- /dev/null +++ b/tests/conf/apcu_bc.ini @@ -0,0 +1,2 @@ +extension=apcu.so +extension=apc.so diff --git a/tests/fixtures/config.ini b/tests/fixtures/config.ini new file mode 100644 index 00000000..2a17cc0d --- /dev/null +++ b/tests/fixtures/config.ini @@ -0,0 +1 @@ +inifile=1 \ No newline at end of file diff --git a/tests/fixtures/config.php b/tests/fixtures/config.php new file mode 100644 index 00000000..eb4fb446 --- /dev/null +++ b/tests/fixtures/config.php @@ -0,0 +1,26 @@ + +// +---------------------------------------------------------------------- +// $Id$ + +return [ + 'url_route_on' => true, + 'slog' => [ + 'host' => '111.202.76.133', + //是否显示利于优化的参数,如果允许时间,消耗内存等 + 'optimize' => true, + 'show_included_files' => true, + 'error_handler' => true, + //日志强制记录到配置的client_id + 'force_client_id' => '', + //限制允许读取日志的client_id + 'allow_client_ids' => [], + ], +]; diff --git a/tests/fixtures/config.xml b/tests/fixtures/config.xml new file mode 100644 index 00000000..3c3266a1 --- /dev/null +++ b/tests/fixtures/config.xml @@ -0,0 +1,6 @@ + + + + 1 + + \ No newline at end of file diff --git a/tests/fixtures/runtime/.gitignore b/tests/fixtures/runtime/.gitignore new file mode 100644 index 00000000..a3a0c8b5 --- /dev/null +++ b/tests/fixtures/runtime/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore \ No newline at end of file diff --git a/tests/mock.php b/tests/mock.php new file mode 100644 index 00000000..2b270276 --- /dev/null +++ b/tests/mock.php @@ -0,0 +1,28 @@ + +// +---------------------------------------------------------------------- + +// 测试入口文件 + +define('IN_UNIT_TEST', true); + +$_SERVER['REQUEST_METHOD'] = 'GET'; + +// 定义项目测试基础路径 +define('TEST_PATH', __DIR__ . '/'); + +// 定义项目路径 +define('APP_PATH', __DIR__ . '/fixtures/'); +// 开启调试模式 +define('APP_DEBUG', true); + +// 加载框架引导文件 +require __DIR__ . '/../start.php'; +\think\Loader::addNamespace('tests', TEST_PATH); diff --git a/tests/script/install.sh b/tests/script/install.sh new file mode 100755 index 00000000..d8c7284b --- /dev/null +++ b/tests/script/install.sh @@ -0,0 +1,19 @@ +#!/bin/bash + +if [ $(phpenv version-name) != "hhvm" ]; then + pecl channel-update pecl.php.net + + if [ $(phpenv version-name) = "7.0" ]; then + pear config-set php_ini '' + pecl config-set php_ini '' + echo "yes\nno\n" | pecl install apcu-5.1.2 + pecl install apcu_bc-beta + phpenv config-add tests/conf/apcu_bc.ini + else + echo "yes\nno\n" | pecl install apcu-4.0.10 + fi + + phpenv config-add tests/conf/apc.ini +fi + +composer install --no-interaction --ignore-platform-reqs diff --git a/tests/thinkphp/baseTest.php b/tests/thinkphp/baseTest.php new file mode 100644 index 00000000..010a9ca9 --- /dev/null +++ b/tests/thinkphp/baseTest.php @@ -0,0 +1,61 @@ + +// +---------------------------------------------------------------------- + +/** + * 保证运行环境正常 + */ +class baseTest extends \PHPUnit_Framework_TestCase +{ + public function testConstants() + { + $this->assertNotEmpty(START_TIME); + $this->assertNotEmpty(START_MEM); + $this->assertNotEmpty(THINK_VERSION); + $this->assertNotEmpty(DS); + $this->assertNotEmpty(THINK_PATH); + $this->assertNotEmpty(LIB_PATH); + $this->assertNotEmpty(EXTEND_PATH); + $this->assertNotEmpty(MODE_PATH); + $this->assertNotEmpty(CORE_PATH); + $this->assertNotEmpty(ORG_PATH); + $this->assertNotEmpty(TRAIT_PATH); + $this->assertNotEmpty(APP_PATH); + $this->assertNotEmpty(APP_NAMESPACE); + $this->assertNotEmpty(COMMON_MODULE); + $this->assertNotEmpty(RUNTIME_PATH); + $this->assertNotEmpty(DATA_PATH); + $this->assertNotEmpty(LOG_PATH); + $this->assertNotEmpty(CACHE_PATH); + $this->assertNotEmpty(TEMP_PATH); + $this->assertNotEmpty(VENDOR_PATH); + $this->assertNotEmpty(EXT); + $this->assertNotEmpty(MODEL_LAYER); + $this->assertNotEmpty(VIEW_LAYER); + $this->assertNotEmpty(CONTROLLER_LAYER); + $this->assertTrue(is_bool(APP_DEBUG)); + $this->assertTrue(is_bool(APP_HOOK)); + $this->assertNotEmpty(ENV_PREFIX); + $this->assertTrue(is_bool(IS_API)); + $this->assertTrue(is_bool(IN_UNIT_TEST)); + $this->assertTrue(is_bool(APP_AUTO_BUILD)); + $this->assertNotEmpty(APP_MODE); + $this->assertTrue(!is_null(IS_CGI)); + $this->assertTrue(!is_null(IS_WIN)); + $this->assertTrue(!is_null(IS_CLI)); + $this->assertTrue(is_bool(IS_AJAX)); + $this->assertNotEmpty(NOW_TIME); + $this->assertNotEmpty(REQUEST_METHOD); + $this->assertTrue(is_bool(IS_GET)); + $this->assertTrue(is_bool(IS_POST)); + $this->assertTrue(is_bool(IS_PUT)); + $this->assertTrue(is_bool(IS_DELETE)); + } +} diff --git a/tests/thinkphp/library/think/DebugTest.php b/tests/thinkphp/library/think/DebugTest.php new file mode 100644 index 00000000..b51a8a7d --- /dev/null +++ b/tests/thinkphp/library/think/DebugTest.php @@ -0,0 +1,172 @@ + +// +---------------------------------------------------------------------- +namespace think; + +/** + * Generated by PHPUnit_SkeletonGenerator on 2016-01-15 at 07:00:38. + */ +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..4fce502b --- /dev/null +++ b/tests/thinkphp/library/think/InputTest.php @@ -0,0 +1,165 @@ + +// +---------------------------------------------------------------------- + +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..2929477f --- /dev/null +++ b/tests/thinkphp/library/think/ResponseTest.php @@ -0,0 +1,315 @@ + +// +---------------------------------------------------------------------- +namespace think; + +/** + * Generated by PHPUnit_SkeletonGenerator on 2016-01-14 at 21:32:07. + */ +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..a1e8f472 --- /dev/null +++ b/tests/thinkphp/library/think/SessionTest.php @@ -0,0 +1,303 @@ + +// +---------------------------------------------------------------------- +namespace think; + +/** + * Generated by PHPUnit_SkeletonGenerator on 2016-01-14 at 02:40:08. + */ +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..0d7f7844 --- /dev/null +++ b/tests/thinkphp/library/think/ViewTest.php @@ -0,0 +1,140 @@ + +// +---------------------------------------------------------------------- +/** + * view测试 + * @author mahuan + */ +namespace 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)); + } +} diff --git a/tests/thinkphp/library/think/appTest.php b/tests/thinkphp/library/think/appTest.php new file mode 100644 index 00000000..cff6b227 --- /dev/null +++ b/tests/thinkphp/library/think/appTest.php @@ -0,0 +1,22 @@ + +// +---------------------------------------------------------------------- + +namespace think; + +class appTest extends \PHPUnit_Framework_TestCase +{ + public function testRun() + { + \think\App::run(); + $this->expectOutputString('

:)

欢迎使用 ThinkPHP5

'); + // todo... + } +} 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..aa718e55 --- /dev/null +++ b/tests/thinkphp/library/think/cache/driver/CacheTestCase.php @@ -0,0 +1,135 @@ + + */ +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/cache/driver/apcTest.php b/tests/thinkphp/library/think/cache/driver/apcTest.php new file mode 100644 index 00000000..24e00c62 --- /dev/null +++ b/tests/thinkphp/library/think/cache/driver/apcTest.php @@ -0,0 +1,50 @@ + +// +---------------------------------------------------------------------- +/** + * Apc缓存驱动测试 + * @author mahuan + */ +namespace tests\thinkphp\library\think\cache\driver; + +class apcTest extends CacheTestCase +{ + private $_cacheInstance = null; + /** + * 基境缓存类型 + */ + protected function setUp() + { + \think\Cache::connect(array('type' => 'apc', 'expire' => 2)); + } + /** + * @return ApcCache + */ + protected function getCacheInstance() + { + if (!extension_loaded("apc")) { + $this->markTestSkipped("APC没有安装,已跳过测试!"); + } elseif ('cli' === PHP_SAPI && !ini_get('apc.enable_cli')) { + $this->markTestSkipped("APC模块没有开启,已跳过测试!"); + } + if (null === $this->_cacheInstance) { + $this->_cacheInstance = new \think\cache\driver\Apc(); + } + return $this->_cacheInstance; + } + /** + * 缓存过期测试《提出来测试,因为目前看通不过缓存过期测试,所以还需研究》 + * @return mixed + * @access public + */ + public function testExpire() + { + } +} diff --git a/tests/thinkphp/library/think/cache/driver/dbTest.php b/tests/thinkphp/library/think/cache/driver/dbTest.php new file mode 100644 index 00000000..8702fd7c --- /dev/null +++ b/tests/thinkphp/library/think/cache/driver/dbTest.php @@ -0,0 +1,42 @@ + +// +---------------------------------------------------------------------- + +/** + * 数据库缓存驱动测试 + * @author mahuan + */ +namespace tests\thinkphp\library\think\cache\driver; + +class dbTest extends CacheTestCase +{ + private $_cacheInstance = null; + + /** + * 基境缓存类型 + */ + protected function setUp() + { + //数据库缓存测试因为缺少数据库单元测试所以暂时跳过 + $this->markTestSkipped("暂时跳过测试。"); + \think\Cache::connect(array('type' => 'db', 'expire' => 2)); + } + + /** + * @return DbCache + */ + protected function getCacheInstance() + { + if (null === $this->_cacheInstance) { + $this->_cacheInstance = new \think\cache\driver\Db(); + } + return $this->_cacheInstance; + } +} diff --git a/tests/thinkphp/library/think/config/driver/iniTest.php b/tests/thinkphp/library/think/config/driver/iniTest.php new file mode 100644 index 00000000..f8d27d4c --- /dev/null +++ b/tests/thinkphp/library/think/config/driver/iniTest.php @@ -0,0 +1,34 @@ + +// +---------------------------------------------------------------------- + +/** + * Ini配置测试 + * + * @author 7IN0SAN9 + */ + +namespace think\config\driver; + +use think\config; + +class iniTest extends \PHPUnit_Framework_TestCase +{ + public function testParse() + { + Config::parse('inistring=1', 'ini'); + $this->assertEquals(1, Config::get('inistring')); + Config::reset(); + Config::parse('thinkphp/tests/fixtures/config.ini'); + $this->assertTrue(Config::has('inifile')); + $this->assertEquals(1, Config::get('inifile')); + Config::reset(); + } +} diff --git a/tests/thinkphp/library/think/config/driver/xmlTest.php b/tests/thinkphp/library/think/config/driver/xmlTest.php new file mode 100644 index 00000000..9332796c --- /dev/null +++ b/tests/thinkphp/library/think/config/driver/xmlTest.php @@ -0,0 +1,34 @@ + +// +---------------------------------------------------------------------- + +/** + * Xml配置测试 + * + * @author 7IN0SAN9 + */ + +namespace think\config\driver; + +use think\config; + +class xmlTest extends \PHPUnit_Framework_TestCase +{ + public function testParse() + { + Config::parse('1', 'xml'); + $this->assertEquals(1, Config::get('xmlstring')); + Config::reset(); + Config::parse('thinkphp/tests/fixtures/config.xml'); + $this->assertTrue(Config::has('xmlfile.istrue')); + $this->assertEquals(1, Config::get('xmlfile.istrue')); + Config::reset(); + } +} diff --git a/tests/thinkphp/library/think/configTest.php b/tests/thinkphp/library/think/configTest.php new file mode 100644 index 00000000..b55d0e6e --- /dev/null +++ b/tests/thinkphp/library/think/configTest.php @@ -0,0 +1,170 @@ + +// +---------------------------------------------------------------------- + +namespace think; + +use ReflectionClass; + +class configTest extends \PHPUnit_Framework_TestCase +{ + 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 + $this->assertEquals('_sys_', $reflectedPropertyRange->getValue()); + $config = $reflectedPropertyConfig->getValue(); + $this->assertTrue(is_array($config)); + // test range initialization + Config::range('_test_'); + $this->assertEquals('_test_', $reflectedPropertyRange->getValue()); + $config = $reflectedPropertyConfig->getValue(); + $this->assertEquals([], $config['_test_']); + } + + // public function testParse() + // { + // see \think\config\driver\...Test.php + // } + + public function testLoad() + { + $file = 'config'; + $config = array_change_key_case(include APP_PATH . $file . EXT); + $name = '_name_'; + $range = '_test_'; + + $reflectedClass = new ReflectionClass('\think\config'); + $reflectedPropertyConfig = $reflectedClass->getProperty('config'); + $reflectedPropertyConfig->setAccessible(true); + $reflectedPropertyConfig->setValue([]); + + $this->assertEquals($config, \think\config::load($file, $name, $range)); + $this->assertNotEquals(null, \think\config::load($file, $name, $range)); + } + + public function testHas() + { + $range = '_test_'; + $this->assertFalse(\think\config::has('abcd', $range)); + $reflectedClass = new ReflectionClass('\think\config'); + $reflectedPropertyConfig = $reflectedClass->getProperty('config'); + $reflectedPropertyConfig->setAccessible(true); + + // if (!strpos($name, '.')): + $reflectedPropertyConfig->setValue([ + $range => ['abcd' => 'value'], + ]); + $this->assertTrue(\think\config::has('abcd', $range)); + + // else ... + $this->assertFalse(\think\config::has('abcd.efg', $range)); + + $reflectedPropertyConfig->setValue([ + $range => ['abcd' => ['efg' => 'value']], + ]); + $this->assertTrue(\think\config::has('abcd.efg', $range)); + } + + public function testGet() + { + $range = '_test_'; + $reflectedClass = new ReflectionClass('\think\config'); + $reflectedPropertyConfig = $reflectedClass->getProperty('config'); + $reflectedPropertyConfig->setAccessible(true); + // test all configurations + $reflectedPropertyConfig->setValue([$range => []]); + $this->assertEquals([], \think\config::get(null, $range)); + $this->assertEquals(null, \think\config::get(null, 'does_not_exist')); + // test $_ENV configuration + defined('ENV_PREFIX') or define('ENV_PREFIX', '_TEST_'); + $name = 'test_name'; + $value = 'value'; + $_ENV[ENV_PREFIX . $name] = $value; + $this->assertEquals($value, \think\config::get($name, $range)); + // test getting configuration + $reflectedPropertyConfig->setValue([$range => ['abcd' => 'efg']]); + $this->assertEquals('efg', \think\config::get('abcd', $range)); + $this->assertEquals(null, \think\config::get('does_not_exist', $range)); + $this->assertEquals(null, \think\config::get('abcd', 'does_not_exist')); + // test $_ENV configuration with dot syntax + $this->assertEquals($value, \think\config::get('test.name', $range)); + // test getting configuration with dot syntax + $reflectedPropertyConfig->setValue([$range => [ + 'one' => ['two' => $value], + ]]); + $this->assertEquals($value, \think\config::get('one.two', $range)); + $this->assertEquals(null, \think\config::get('one.does_not_exist', $range)); + $this->assertEquals(null, \think\config::get('one.two', 'does_not_exist')); + } + + public function testSet() + { + $range = '_test_'; + $reflectedClass = new ReflectionClass('\think\config'); + $reflectedPropertyConfig = $reflectedClass->getProperty('config'); + $reflectedPropertyConfig->setAccessible(true); + $reflectedPropertyConfig->setValue([]); + // if (is_string($name)): + // without dot syntax + $name = 'name'; + $value = 'value'; + \think\config::set($name, $value, $range); + $config = $reflectedPropertyConfig->getValue(); + $this->assertEquals($value, $config[$range][$name]); + // with dot syntax + $name = 'one.two'; + $value = 'dot value'; + \think\config::set($name, $value, $range); + $config = $reflectedPropertyConfig->getValue(); + $this->assertEquals($value, $config[$range]['one']['two']); + // if (is_array($name)): + // see testLoad() + // ... + // test getting all configurations...? + // return self::$config[$range]; ?? + $value = ['all' => 'configuration']; + $reflectedPropertyConfig->setValue([$range => $value]); + $this->assertEquals($value, \think\config::set(null, null, $range)); + $this->assertNotEquals(null, \think\config::set(null, null, $range)); + } + + public function testReset() + { + $range = '_test_'; + $reflectedClass = new ReflectionClass('\think\config'); + $reflectedPropertyConfig = $reflectedClass->getProperty('config'); + $reflectedPropertyConfig->setAccessible(true); + $reflectedPropertyConfig->setValue([$range => ['abcd' => 'efg']]); + + // clear all configurations + \think\config::reset(true); + $config = $reflectedPropertyConfig->getValue(); + $this->assertEquals([], $config); + // clear the configuration in range of parameter. + $reflectedPropertyConfig->setValue([ + $range => [ + 'abcd' => 'efg', + 'hijk' => 'lmn', + ], + 'a' => 'b', + ]); + \think\config::reset($range); + $config = $reflectedPropertyConfig->getValue(); + $this->assertEquals([ + $range => [], + 'a' => 'b', + ], $config); + } +} diff --git a/tests/thinkphp/library/think/controller/.gitignore b/tests/thinkphp/library/think/controller/.gitignore new file mode 100644 index 00000000..a3a0c8b5 --- /dev/null +++ b/tests/thinkphp/library/think/controller/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore \ No newline at end of file diff --git a/tests/thinkphp/library/think/controllerTest.php b/tests/thinkphp/library/think/controllerTest.php new file mode 100644 index 00000000..12dcc6e4 --- /dev/null +++ b/tests/thinkphp/library/think/controllerTest.php @@ -0,0 +1,99 @@ + +// +---------------------------------------------------------------------- + +namespace think; + +require_once CORE_PATH . '../../helper.php'; + +class Foo extends \think\Controller +{ + public $test = 'test'; + + public function _initialize() + { + $this->test = 'abcd'; + } +} + +class Bar extends \think\Controller +{ + public $test = 1; + + public $beforeActionList = ['action1', 'action2']; + + public function action1() + { + $this->test += 2; + return 'action1'; + } + + public function action2() + { + $this->test += 4; + return 'action2'; + } +} + +class Baz extends \think\Controller +{ + public $test = 1; + + public $beforeActionList = [ + 'action1' => ['only' => ['index']], + 'action2' => ['except' => ['index']], + 'action3' => ['only' => ['abcd']], + 'action4' => ['except' => ['abcd']], + ]; + + public function action1() + { + $this->test += 2; + return 'action1'; + } + + public function action2() + { + $this->test += 4; + return 'action2'; + } + + public function action3() + { + $this->test += 8; + return 'action2'; + } + + public function action4() + { + $this->test += 16; + return 'action2'; + } +} + +define('ACTION_NAME', 'index'); + +class controllerTest extends \PHPUnit_Framework_TestCase +{ + public function testInitialize() + { + $foo = new Foo; + $this->assertEquals('abcd', $foo->test); + } + + public function testBeforeAction() + { + $obj = new Bar; + $this->assertEquals(7, $obj->test); + + $obj = new Baz; + $this->assertEquals(19, $obj->test); + } +} diff --git a/tests/thinkphp/library/think/cookieTest.php b/tests/thinkphp/library/think/cookieTest.php new file mode 100644 index 00000000..2ed56bce --- /dev/null +++ b/tests/thinkphp/library/think/cookieTest.php @@ -0,0 +1,146 @@ + +// +---------------------------------------------------------------------- + +namespace think; + +use ReflectionClass; + +class cookieTest extends \PHPUnit_Framework_TestCase +{ + protected $ref; + + protected $default = [ + // cookie 名称前缀 + 'prefix' => '', + // cookie 保存时间 + 'expire' => 0, + // cookie 保存路径 + 'path' => '/', + // cookie 有效域名 + 'domain' => '', + // cookie 启用安全传输 + 'secure' => false, + // httponly设置 + 'httponly' => '', + ]; + + protected function setUp() + { + $reflectedClass = new ReflectionClass('\think\Cookie'); + $reflectedPropertyConfig = $reflectedClass->getProperty('config'); + $reflectedPropertyConfig->setAccessible(true); + $reflectedPropertyConfig->setValue($this->default); + $this->ref = $reflectedPropertyConfig; + } + + public function testInit() + { + $config = [ + // cookie 名称前缀 + 'prefix' => 'think_', + // cookie 保存时间 + 'expire' => 0, + // cookie 保存路径 + 'path' => '/path/to/test/', + // cookie 有效域名 + 'domain' => '.thinkphp.cn', + // cookie 启用安全传输 + 'secure' => true, + // httponly设置 + 'httponly' => '1', + ]; + \think\Cookie::init($config); + + $this->assertEquals( + array_merge($this->default, array_change_key_case($config)), + $this->ref->getValue() + ); + } + + public function testPrefix() + { + $this->assertEquals($this->default['prefix'], \think\Cookie::prefix()); + + $prefix = '_test_'; + $this->assertNotEquals($prefix, \think\Cookie::prefix()); + \think\Cookie::prefix($prefix); + + $config = $this->ref->getValue(); + $this->assertEquals($prefix, $config['prefix']); + } + + public function testSet() + { + $value = 'value'; + + $name = 'name1'; + \think\Cookie::set($name, $value, 10); + $this->assertEquals($value, $_COOKIE[$this->default['prefix'] . $name]); + + $name = 'name2'; + \think\Cookie::set($name, $value, null); + $this->assertEquals($value, $_COOKIE[$this->default['prefix'] . $name]); + + $name = 'name3'; + \think\Cookie::set($name, $value, 'expire=100&prefix=pre_'); + $this->assertEquals($value, $_COOKIE['pre_' . $name]); + + $name = 'name4'; + $value = ['_test_中文_']; + \think\Cookie::set($name, $value); + $this->assertEquals('think:' . json_encode([urlencode('_test_中文_')]), $_COOKIE[$name]); + } + + public function testGet() + { + $_COOKIE = [ + 'a' => 'b', + 'pre_abc' => 'c', + 'd' => 'think:' . json_encode([urlencode('_test_中文_')]), + ]; + $this->assertEquals('b', \think\Cookie::get('a')); + $this->assertEquals(null, \think\Cookie::get('does_not_exist')); + $this->assertEquals('c', \think\Cookie::get('abc', 'pre_')); + $this->assertEquals(['_test_中文_'], \think\Cookie::get('d')); + } + + public function testDelete() + { + $_COOKIE = [ + 'a' => 'b', + 'pre_abc' => 'c', + ]; + $this->assertEquals('b', \think\Cookie::get('a')); + \think\Cookie::delete('a'); + $this->assertEquals(null, \think\Cookie::get('a')); + + $this->assertEquals('c', \think\Cookie::get('abc', 'pre_')); + \think\Cookie::delete('abc', 'pre_'); + $this->assertEquals(null, \think\Cookie::get('abc', 'pre_')); + } + + public function testClear() + { + $_COOKIE = []; + $this->assertEquals(null, \think\Cookie::clear()); + + $_COOKIE = ['a' => 'b']; + \think\Cookie::clear(); + $this->assertEquals(null, $_COOKIE); + + $_COOKIE = [ + 'a' => 'b', + 'pre_abc' => 'c', + ]; + \think\Cookie::clear('pre_'); + $this->assertEquals(['a' => 'b'], $_COOKIE); + } +} diff --git a/tests/thinkphp/library/think/db/driver/.gitignore b/tests/thinkphp/library/think/db/driver/.gitignore new file mode 100644 index 00000000..a3a0c8b5 --- /dev/null +++ b/tests/thinkphp/library/think/db/driver/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore \ No newline at end of file diff --git a/tests/thinkphp/library/think/log/driver/.gitignore b/tests/thinkphp/library/think/log/driver/.gitignore new file mode 100644 index 00000000..a3a0c8b5 --- /dev/null +++ b/tests/thinkphp/library/think/log/driver/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore \ No newline at end of file diff --git a/tests/thinkphp/library/think/model/.gitignore b/tests/thinkphp/library/think/model/.gitignore new file mode 100644 index 00000000..a3a0c8b5 --- /dev/null +++ b/tests/thinkphp/library/think/model/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore \ No newline at end of file diff --git a/tests/thinkphp/library/think/session/.gitignore b/tests/thinkphp/library/think/session/.gitignore new file mode 100644 index 00000000..a3a0c8b5 --- /dev/null +++ b/tests/thinkphp/library/think/session/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore \ No newline at end of file diff --git a/tests/thinkphp/library/think/template/driver/.gitignore b/tests/thinkphp/library/think/template/driver/.gitignore new file mode 100644 index 00000000..a3a0c8b5 --- /dev/null +++ b/tests/thinkphp/library/think/template/driver/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore \ No newline at end of file diff --git a/tests/thinkphp/library/think/template/taglib/.gitignore b/tests/thinkphp/library/think/template/taglib/.gitignore new file mode 100644 index 00000000..a3a0c8b5 --- /dev/null +++ b/tests/thinkphp/library/think/template/taglib/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore \ No newline at end of file diff --git a/tests/thinkphp/library/think/templateTest.php b/tests/thinkphp/library/think/templateTest.php new file mode 100644 index 00000000..69dbd7b3 --- /dev/null +++ b/tests/thinkphp/library/think/templateTest.php @@ -0,0 +1,257 @@ + +// +---------------------------------------------------------------------- + +use think\Template; + +class TemplateTest extends \PHPUnit_Framework_TestCase +{ + public function testVar() + { + $template = new Template(); + + $content = << +EOF; + + $template->parse($content); + $this->assertEquals($content, $data); + + $content = << +EOF; + + $template->parse($content); + $this->assertEquals($content, $data); + + $content = << +EOF; + + $template->parse($content); + $this->assertEquals($content, $data); + + $content = << +EOF; + + $template->parse($content); + $this->assertEquals($content, $data); + + $content = << +EOF; + + $template->parse($content); + $this->assertEquals($content, $data); + + $content = << +EOF; + + $template->parse($content); + $this->assertEquals($content, $data); + + $content = << +EOF; + + $template->parse($content); + $this->assertEquals($content, $data); + + $content = << +EOF; + + $template->parse($content); + $this->assertEquals($content, $data); + + $content = << +EOF; + + $template->parse($content); + $this->assertEquals($content, $data); + + $content = << +EOF; + + $template->parse($content); + $this->assertEquals($content, $data); + + $content = << +EOF; + + $template->parse($content); + $this->assertEquals($content, $data); + + $content = <<parse($content); + $this->assertEquals($content, $data); + + } + + public function testVarIdentify() + { + $config['tpl_begin'] = '<#'; + $config['tpl_end'] = '#>'; + $config['tpl_var_identify'] = ''; + $template = new Template($config); + + $content = << +EOF; + $data = <<a) ? (is_array(\$info)?\$info['a']:\$info->a) : 'test'; ?> +EOF; + + $template->parse($content); + $this->assertEquals($content, $data); + + $content = << +EOF; + $data = <<a)==(is_array(\$info)?\$info['b']:\$info->b)) echo 'test'; ?> +EOF; + + $template->parse($content); + $this->assertEquals($content, $data); + + $content = << +EOF; + $data = <<a) !== ''?(is_array(\$info)?\$info['a']:\$info->a):'test')?'yes':'no'; ?> +EOF; + + $template->parse($content); + $this->assertEquals($content, $data); + } + + public function testTag() + { + $template = new Template(); + + $content = << +one + +two + +default + +EOF; + $template->parse($content); + $this->assertEquals($content, $data); + + $content = << + +a + + +b + + +default + +EOF; + $template->parse($content); + $this->assertEquals($content, $data); + + $content = <<\$val} + +{/foreach} +EOF; + $data = <<\$val): ?> + + +EOF; + $template->parse($content); + $this->assertEquals($content, $data); + + $content = <<\$val): ?> + + +EOF; + $template->parse($content); + $this->assertEquals($content, $data); + + } +} diff --git a/tests/thinkphp/library/think/view/driver/.gitignore b/tests/thinkphp/library/think/view/driver/.gitignore new file mode 100644 index 00000000..a3a0c8b5 --- /dev/null +++ b/tests/thinkphp/library/think/view/driver/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore \ No newline at end of file diff --git a/tests/thinkphp/library/traits/controller/.gitignore b/tests/thinkphp/library/traits/controller/.gitignore new file mode 100644 index 00000000..a3a0c8b5 --- /dev/null +++ b/tests/thinkphp/library/traits/controller/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore \ No newline at end of file diff --git a/tests/thinkphp/library/traits/model/.gitignore b/tests/thinkphp/library/traits/model/.gitignore new file mode 100644 index 00000000..a3a0c8b5 --- /dev/null +++ b/tests/thinkphp/library/traits/model/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore \ No newline at end of file