Merge pull request #340 from Lofanmi/master

完善单元测试
This commit is contained in:
ThinkPHP
2016-03-03 20:30:50 +08:00
2 changed files with 65 additions and 11 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,13 @@
namespace tests\thinkphp\library\think; namespace tests\thinkphp\library\think;
use ReflectionClass;
use think\Controller;
use think\View;
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 +32,7 @@ class Foo extends \think\Controller
} }
} }
class Bar extends \think\Controller class Bar extends Controller
{ {
public $test = 1; public $test = 1;
@@ -47,15 +51,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 +105,54 @@ 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)
{
$view = new View();
$rc = new ReflectionClass(get_class($controller));
$property = $rc->getProperty('view');
$property->setAccessible(true);
$property->setValue($controller, $view);
return $view;
}
public function testFetch()
{
$controller = new Foo;
$view = $this->getView($controller);
$template = dirname(__FILE__) . '/display.html';
$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 = dirname(__FILE__) . '/display.html';
$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);
}
} }