移动 tests 到 thinkphp 目录

This commit is contained in:
7IN0SAN9
2016-01-19 22:34:27 +08:00
parent c46e8cd7b6
commit 00c101abf6
35 changed files with 2413 additions and 0 deletions

163
tests/README.md Normal file
View File

@@ -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
<?php
// 测试入口文件
define('IN_UNIT_TEST', true);
$_SERVER['REQUEST_METHOD'] = 'GET';
// 定义项目路径
define('APP_PATH', __DIR__ . '/../application/');
// 开启调试模式
define('APP_DEBUG', true);
// 加载框架引导文件
require __DIR__ . '/../thinkphp/start.php';
```
###主要测试流程###
thinkphp5的测试的主要流程是跟think的系统流程是相似的大体的流程为
1. 引用bootstrap文件加载mock里的小框架文件加载所需文件
2. 根据文件目录,添加测试文件
3. 执行单元测试,输出结果
###测试举例###
例如测试thinkphp里的apc缓存将分为以下几个过程
1. 在tests/framework文件夹下创建apcTest.php文件该文件应与apc.php目录路径(thinkphp/library/think/cache/driver)一致,并引用 `PHPUnit_Framework_TestCase`。
```php
<?php
namespace think\cache\driver;
class apcTest extends \PHPUnit_Framework_TestCase
{
//设定基境
public function setUp()
{
}
}
```
2. 编写测试文件
- 引用app、config和cache
```php
use think\app;
use think\cache;
use think\config;
```
- 在setUp函数中设定require条件
```php
if(!extension_loaded('apc')){
$this->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|||

1
tests/conf/apc.ini Normal file
View File

@@ -0,0 +1 @@
apc.enable_cli=1

2
tests/conf/apcu_bc.ini Normal file
View File

@@ -0,0 +1,2 @@
extension=apcu.so
extension=apc.so

1
tests/fixtures/config.ini vendored Normal file
View File

@@ -0,0 +1 @@
inifile=1

26
tests/fixtures/config.php vendored Normal file
View File

@@ -0,0 +1,26 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
// $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' => [],
],
];

6
tests/fixtures/config.xml vendored Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0"?>
<document>
<xmlfile>
<istrue>1</istrue>
</xmlfile>
</document>

2
tests/fixtures/runtime/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
*
!.gitignore

28
tests/mock.php Normal file
View File

@@ -0,0 +1,28 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
// 测试入口文件
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);

19
tests/script/install.sh Executable file
View File

@@ -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

View File

@@ -0,0 +1,61 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Haotong Lin <lofanmi@gmail.com>
// +----------------------------------------------------------------------
/**
* 保证运行环境正常
*/
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));
}
}

View File

@@ -0,0 +1,172 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 大漠 <zhylninc@gmail.com>
// +----------------------------------------------------------------------
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))));
}
}
}

View File

@@ -0,0 +1,165 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2015 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Haotong Lin <lofanmi@gmail.com>
// +----------------------------------------------------------------------
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&lt;&gt;', 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&lt;&gt;', 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&lt;&gt;',
'c' => 'trim htmlspecialchars&lt;&gt;',
'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' => '<b\\ar />'];
$filters = ['trim'];
$this->assertEquals('test&lt;&gt;', Input::getData('a', $input, $filters));
$filters = ['trim', false];
$this->assertEquals('test<>', Input::getData('a', $input, $filters));
$filters = 'stripslashes';
$this->assertEquals("&lt;bar /&gt;", Input::getData('b', $input, $filters));
$filters = 'stripslashes,0';
$this->assertEquals("<bar />", Input::getData('b', $input, $filters));
}
}

View File

@@ -0,0 +1,315 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 大漠 <zhylninc@gmail.com>
// +----------------------------------------------------------------------
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());
}
}

View File

@@ -0,0 +1,303 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 大漠 <zhylninc@gmail.com>
// +----------------------------------------------------------------------
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']);
}
}

View File

@@ -0,0 +1,140 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2015 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: mahuan <mahuan@d1web.top>
// +----------------------------------------------------------------------
/**
* view测试
* @author mahuan <mahuan@d1web.top>
*/
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));
}
}

View File

@@ -0,0 +1,22 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Haotong Lin <lofanmi@gmail.com>
// +----------------------------------------------------------------------
namespace think;
class appTest extends \PHPUnit_Framework_TestCase
{
public function testRun()
{
\think\App::run();
$this->expectOutputString('<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} body{ background: #fff; font-family: "微软雅黑"; color: #333;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.8em; font-size: 36px }</style><div style="padding: 24px 48px;"> <h1>:)</h1><p>欢迎使用 <b>ThinkPHP5</b></p></div><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script><script type="text/javascript" src="http://ad.topthink.com/Public/static/client.js"></script><thinkad id="ad_bd568ce7058a1091"></thinkad>');
// todo...
}
}

View File

@@ -0,0 +1,135 @@
<?php
namespace tests\thinkphp\library\think\cache\driver;
use think\cache;
/**
* 缓存抽象类,提供一些测试
* @author simon <mahuan@d1web.top>
*/
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'));
}
}

View File

@@ -0,0 +1,50 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
/**
* Apc缓存驱动测试
* @author mahuan <mahuan@d1web.top>
*/
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()
{
}
}

View File

@@ -0,0 +1,42 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
/**
* 数据库缓存驱动测试
* @author mahuan <mahuan@d1web.top>
*/
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;
}
}

View File

@@ -0,0 +1,34 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
/**
* Ini配置测试
*
* @author 7IN0SAN9 <me@7in0.me>
*/
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();
}
}

View File

@@ -0,0 +1,34 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
/**
* Xml配置测试
*
* @author 7IN0SAN9 <me@7in0.me>
*/
namespace think\config\driver;
use think\config;
class xmlTest extends \PHPUnit_Framework_TestCase
{
public function testParse()
{
Config::parse('<?xml version="1.0"?><document><xmlstring>1</xmlstring></document>', '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();
}
}

View File

@@ -0,0 +1,170 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Haotong Lin <lofanmi@gmail.com>
// +----------------------------------------------------------------------
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);
}
}

View File

@@ -0,0 +1,2 @@
*
!.gitignore

View File

@@ -0,0 +1,99 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Haotong Lin <lofanmi@gmail.com>
// +----------------------------------------------------------------------
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);
}
}

View File

@@ -0,0 +1,146 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Haotong Lin <lofanmi@gmail.com>
// +----------------------------------------------------------------------
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);
}
}

View File

@@ -0,0 +1,2 @@
*
!.gitignore

View File

@@ -0,0 +1,2 @@
*
!.gitignore

View File

@@ -0,0 +1,2 @@
*
!.gitignore

View File

@@ -0,0 +1,2 @@
*
!.gitignore

View File

@@ -0,0 +1,2 @@
*
!.gitignore

View File

@@ -0,0 +1,2 @@
*
!.gitignore

View File

@@ -0,0 +1,257 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2015 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Haotong Lin <lofanmi@gmail.com>
// +----------------------------------------------------------------------
use think\Template;
class TemplateTest extends \PHPUnit_Framework_TestCase
{
public function testVar()
{
$template = new Template();
$content = <<<EOF
{\$name.a.b|default='test'}
EOF;
$data = <<<EOF
<?php echo (isset(\$name['a']['b']) && (\$name['a']['b'] !== '')?\$name['a']['b']:'test'); ?>
EOF;
$template->parse($content);
$this->assertEquals($content, $data);
$content = <<<EOF
{\$name.a??'test'}
EOF;
$data = <<<EOF
<?php echo isset(\$name['a']) ? \$name['a'] : 'test'; ?>
EOF;
$template->parse($content);
$this->assertEquals($content, $data);
$content = <<<EOF
{\$name.a?='test'}
EOF;
$data = <<<EOF
<?php if(!empty(\$name['a'])) echo 'test'; ?>
EOF;
$template->parse($content);
$this->assertEquals($content, $data);
$content = <<<EOF
{\$name.a?:'test'}
EOF;
$data = <<<EOF
<?php echo !empty(\$name['a'])?\$name['a']:'test'; ?>
EOF;
$template->parse($content);
$this->assertEquals($content, $data);
$content = <<<EOF
{\$name.a?\$name.b:'no'}
EOF;
$data = <<<EOF
<?php echo !empty(\$name['a'])?\$name['b']:'no'; ?>
EOF;
$template->parse($content);
$this->assertEquals($content, $data);
$content = <<<EOF
{\$name.a==\$name.b?='test'}
EOF;
$data = <<<EOF
<?php if(\$name['a']==\$name['b']) echo 'test'; ?>
EOF;
$template->parse($content);
$this->assertEquals($content, $data);
$content = <<<EOF
{\$name.a==\$name.b?'a':'b'}
EOF;
$data = <<<EOF
<?php echo (\$name['a']==\$name['b'])?'a':'b'; ?>
EOF;
$template->parse($content);
$this->assertEquals($content, $data);
$content = <<<EOF
{\$name.a|default='test'==\$name.b?'a':'b'}
EOF;
$data = <<<EOF
<?php echo ((isset(\$name['a']) && (\$name['a'] !== '')?\$name['a']:'test')==\$name['b'])?'a':'b'; ?>
EOF;
$template->parse($content);
$this->assertEquals($content, $data);
$content = <<<EOF
{:ltrim(rtrim(\$name.a))}
EOF;
$data = <<<EOF
<?php echo ltrim(rtrim(\$name['a'])); ?>
EOF;
$template->parse($content);
$this->assertEquals($content, $data);
$content = <<<EOF
{~echo(trim(\$name.a))}
EOF;
$data = <<<EOF
<?php echo(trim(\$name['a'])); ?>
EOF;
$template->parse($content);
$this->assertEquals($content, $data);
$content = <<<EOF
{+\$name.a}
EOF;
$data = <<<EOF
<?php echo +\$name['a']; ?>
EOF;
$template->parse($content);
$this->assertEquals($content, $data);
$content = <<<EOF
{/*\$name*/}
EOF;
$data = '';
$template->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
<#\$info.a??'test'#>
EOF;
$data = <<<EOF
<?php echo (is_array(\$info)?\$info['a']:\$info->a) ? (is_array(\$info)?\$info['a']:\$info->a) : 'test'; ?>
EOF;
$template->parse($content);
$this->assertEquals($content, $data);
$content = <<<EOF
<#\$info.a==\$info.b?='test'#>
EOF;
$data = <<<EOF
<?php if((is_array(\$info)?\$info['a']:\$info->a)==(is_array(\$info)?\$info['b']:\$info->b)) echo 'test'; ?>
EOF;
$template->parse($content);
$this->assertEquals($content, $data);
$content = <<<EOF
<#\$info.a|default='test'?'yes':'no'#>
EOF;
$data = <<<EOF
<?php echo ((is_array(\$info)?\$info['a']:\$info->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 = <<<EOF
{if \$var.a==\$var.b}
one
{elseif !empty(\$var.a) /}
two
{else /}
default
{/if}
EOF;
$data = <<<EOF
<?php if(\$var['a']==\$var['b']): ?>
one
<?php elseif(!empty(\$var['a'])): ?>
two
<?php else: ?>
default
<?php endif; ?>
EOF;
$template->parse($content);
$this->assertEquals($content, $data);
$content = <<<EOF
{switch \$var}
{case \$a /}
a
{/case}
{case b}
b
{/case}
{default /}
default
{/switch}
EOF;
$data = <<<EOF
<?php switch(\$var): ?>
<?php case \$a: ?>
a
<?php break; ?>
<?php case "b": ?>
b
<?php break; ?>
<?php default: ?>
default
<?php endswitch; ?>
EOF;
$template->parse($content);
$this->assertEquals($content, $data);
$content = <<<EOF
{foreach \$list as \$key=>\$val}
{/foreach}
EOF;
$data = <<<EOF
<?php foreach(\$list as \$key=>\$val): ?>
<?php endforeach; ?>
EOF;
$template->parse($content);
$this->assertEquals($content, $data);
$content = <<<EOF
{foreach name="list" id="val" key="key"}
{/foreach}
EOF;
$data = <<<EOF
<?php if(is_array(\$list)): foreach(\$list as \$key=>\$val): ?>
<?php endforeach; endif; ?>
EOF;
$template->parse($content);
$this->assertEquals($content, $data);
}
}

View File

@@ -0,0 +1,2 @@
*
!.gitignore

View File

@@ -0,0 +1,2 @@
*
!.gitignore

View File

@@ -0,0 +1,2 @@
*
!.gitignore