controllerTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. /**
  12. * 控制器测试
  13. * @author Haotong Lin <lofanmi@gmail.com>
  14. */
  15. namespace tests\thinkphp\library\think;
  16. use ReflectionClass;
  17. use think\Controller;
  18. use think\Request;
  19. use think\View;
  20. require_once CORE_PATH . '../../helper.php';
  21. class Foo extends Controller
  22. {
  23. public $test = 'test';
  24. public function _initialize()
  25. {
  26. $this->test = 'abcd';
  27. }
  28. public function assignTest()
  29. {
  30. $this->assign('abcd', 'dcba');
  31. $this->assign(['key1' => 'value1', 'key2' => 'value2']);
  32. }
  33. public function fetchTest()
  34. {
  35. $template = APP_PATH . 'views' . DS .'display.html';
  36. return $this->fetch($template, ['name' => 'ThinkPHP']);
  37. }
  38. public function displayTest()
  39. {
  40. $template = APP_PATH . 'views' . DS .'display.html';
  41. return $this->display($template, ['name' => 'ThinkPHP']);
  42. }
  43. public function test()
  44. {
  45. $data = [
  46. 'username' => 'username',
  47. 'nickname' => 'nickname',
  48. 'password' => '123456',
  49. 'repassword' => '123456',
  50. 'email' => 'abc@abc.com',
  51. 'sex' => '0',
  52. 'age' => '20',
  53. 'code' => '1234',
  54. ];
  55. $validate = [
  56. ['username', 'length:5,15', '用户名长度为5到15个字符'],
  57. ['nickname', 'require', '请填昵称'],
  58. ['password', '[\w-]{6,15}', '密码长度为6到15个字符'],
  59. ['repassword', 'confirm:password', '两次密码不一到致'],
  60. ['email', 'filter:validate_email', '邮箱格式错误'],
  61. ['sex', 'in:0,1', '性别只能为为男或女'],
  62. ['age', 'between:1,80', '年龄只能在10-80之间'],
  63. ];
  64. return $this->validate($data, $validate);
  65. }
  66. }
  67. class Bar extends Controller
  68. {
  69. public $test = 1;
  70. public $beforeActionList = ['action1', 'action2'];
  71. public function action1()
  72. {
  73. $this->test += 2;
  74. return 'action1';
  75. }
  76. public function action2()
  77. {
  78. $this->test += 4;
  79. return 'action2';
  80. }
  81. }
  82. class Baz extends Controller
  83. {
  84. public $test = 1;
  85. public $beforeActionList = [
  86. 'action1' => ['only' => 'index'],
  87. 'action2' => ['except' => 'index'],
  88. 'action3' => ['only' => 'abcd'],
  89. 'action4' => ['except' => 'abcd'],
  90. ];
  91. public function action1()
  92. {
  93. $this->test += 2;
  94. return 'action1';
  95. }
  96. public function action2()
  97. {
  98. $this->test += 4;
  99. return 'action2';
  100. }
  101. public function action3()
  102. {
  103. $this->test += 8;
  104. return 'action2';
  105. }
  106. public function action4()
  107. {
  108. $this->test += 16;
  109. return 'action2';
  110. }
  111. }
  112. class controllerTest extends \PHPUnit_Framework_TestCase
  113. {
  114. public function testInitialize()
  115. {
  116. $foo = new Foo(Request::instance());
  117. $this->assertEquals('abcd', $foo->test);
  118. }
  119. public function testBeforeAction()
  120. {
  121. $obj = new Bar(Request::instance());
  122. $this->assertEquals(7, $obj->test);
  123. $obj = new Baz(Request::instance());
  124. $this->assertEquals(19, $obj->test);
  125. }
  126. private function getView($controller)
  127. {
  128. $view = new View();
  129. $rc = new ReflectionClass(get_class($controller));
  130. $property = $rc->getProperty('view');
  131. $property->setAccessible(true);
  132. $property->setValue($controller, $view);
  133. return $view;
  134. }
  135. public function testFetch()
  136. {
  137. $controller = new Foo(Request::instance());
  138. $view = $this->getView($controller);
  139. $template = APP_PATH . 'views' . DS .'display.html';
  140. $viewFetch = $view->fetch($template, ['name' => 'ThinkPHP']);
  141. $this->assertEquals($controller->fetchTest(), $viewFetch);
  142. }
  143. public function testDisplay()
  144. {
  145. $controller = new Foo;
  146. $view = $this->getView($controller);
  147. $template = APP_PATH . 'views' . DS .'display.html';
  148. $viewFetch = $view->display($template, ['name' => 'ThinkPHP']);
  149. $this->assertEquals($controller->displayTest(), $viewFetch);
  150. }
  151. public function testAssign()
  152. {
  153. $controller = new Foo(Request::instance());
  154. $view = $this->getView($controller);
  155. $controller->assignTest();
  156. $expect = ['abcd' => 'dcba', 'key1' => 'value1', 'key2' => 'value2'];
  157. $this->assertAttributeEquals($expect, 'data', $view);
  158. }
  159. public function testValidate()
  160. {
  161. $controller = new Foo(Request::instance());
  162. $result = $controller->test();
  163. $this->assertTrue($result);
  164. }
  165. }