完善单元测试

This commit is contained in:
Haotong Lin
2016-03-03 00:02:55 +08:00
parent 0e6d985601
commit 19a446c7de
3 changed files with 63 additions and 12 deletions

View File

@@ -110,15 +110,15 @@ thinkphp5 的测试的主要流程是跟 thinkphp 的系统流程是相似的,
|Base||| |Base|||
|App|Haotong Lin|| |App|Haotong Lin||
|Build|刘志淳|| |Build|刘志淳||
|Config|Haotong Lin|| |Config|Haotong Lin||
|Cache||| |Cache|||
|Controller|Haotong Lin|| |Controller|Haotong Lin||
|Cookie|Haotong Lin|| |Cookie|Haotong Lin||
|Db||| |Db|||
|Debug|大漠|√| |Debug|大漠|√|
|Error|大漠|| |Error|大漠||
|Hook|流年|√| |Hook|流年|√|
|Input|Haotong Lin|| |Input|Haotong Lin||
|Lang|流年|√| |Lang|流年|√|
|Loader|流年|| |Loader|流年||
|Log||| |Log|||

View File

@@ -16,9 +16,12 @@
namespace tests\thinkphp\library\think; namespace tests\thinkphp\library\think;
use ReflectionClass;
use think\Controller;
require_once CORE_PATH . '../../helper.php'; require_once CORE_PATH . '../../helper.php';
class Foo extends \think\Controller class Foo extends Controller
{ {
public $test = 'test'; public $test = 'test';
@@ -28,7 +31,7 @@ class Foo extends \think\Controller
} }
} }
class Bar extends \think\Controller class Bar extends Controller
{ {
public $test = 1; public $test = 1;
@@ -47,15 +50,15 @@ class Bar extends \think\Controller
} }
} }
class Baz extends \think\Controller class Baz extends Controller
{ {
public $test = 1; public $test = 1;
public $beforeActionList = [ public $beforeActionList = [
'action1' => ['only' => ['index']], 'action1' => ['only' => 'index'],
'action2' => ['except' => ['index']], 'action2' => ['except' => 'index'],
'action3' => ['only' => ['abcd']], 'action3' => ['only' => 'abcd'],
'action4' => ['except' => ['abcd']], 'action4' => ['except' => 'abcd'],
]; ];
public function action1() public function action1()
@@ -101,4 +104,52 @@ class controllerTest extends \PHPUnit_Framework_TestCase
$obj = new Baz; $obj = new Baz;
$this->assertEquals(19, $obj->test); $this->assertEquals(19, $obj->test);
} }
private function getView($controller)
{
$rc = new ReflectionClass(get_class($controller));
$property = $rc->getProperty('view');
$property->setAccessible(true);
return $property->getValue($controller);
}
public function testFetch()
{
$controller = new Foo;
$view = $this->getView($controller);
$template = __DIR__ . '/display';
$viewFetch = $view->fetch($template, ['name' => 'ThinkPHP']);
$controllerFetch = $controller->fetch($template, ['name' => 'ThinkPHP']);
$this->assertEquals($controllerFetch, $viewFetch);
}
public function testShow()
{
$controller = new Foo;
$view = $this->getView($controller);
$template = __DIR__ . '/display';
$viewFetch = $view->show($template, ['name' => 'ThinkPHP']);
$controllerFetch = $controller->show($template, ['name' => 'ThinkPHP']);
$this->assertEquals($controllerFetch, $viewFetch);
}
public function testAssign()
{
$controller = new Foo;
$view = $this->getView($controller);
$controller->assign('abcd', 'dcba');
$controller->assign(['key1' => 'value1', 'key2' => 'value2']);
$expect = ['abcd' => 'dcba', 'key1' => 'value1', 'key2' => 'value2'];
$this->assertAttributeEquals($expect, 'data', $view);
}
public function testEngine()
{
$controller = new Foo;
$view = $this->getView($controller);
$view->engine = null;
$this->assertEquals(null, $view->engine);
$controller->engine('php');
$this->assertEquals('php', $view->engine);
}
} }

View File

@@ -37,7 +37,7 @@ class cookieTest extends \PHPUnit_Framework_TestCase
// httponly设置 // httponly设置
'httponly' => '', 'httponly' => '',
// 是否使用 setcookie // 是否使用 setcookie
'setcookie' => false, 'setcookie' => true,
]; ];
protected function setUp() protected function setUp()