DispatcherTest.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. <?php
  2. /**
  3. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice
  8. *
  9. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  11. * @since 1.2.0
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Test\TestCase\Routing;
  15. use Cake\Controller\Controller;
  16. use Cake\Core\App;
  17. use Cake\Core\Configure;
  18. use Cake\Core\Plugin;
  19. use Cake\Error;
  20. use Cake\Event\Event;
  21. use Cake\Network\Request;
  22. use Cake\Network\Response;
  23. use Cake\Network\Session;
  24. use Cake\Routing\Dispatcher;
  25. use Cake\Routing\Filter\ControllerFactoryFilter;
  26. use Cake\Routing\Router;
  27. use Cake\TestSuite\TestCase;
  28. use Cake\Utility\Inflector;
  29. /**
  30. * A testing stub that doesn't send headers.
  31. */
  32. class DispatcherMockResponse extends Response {
  33. protected function _sendHeader($name, $value = null) {
  34. return $name . ' ' . $value;
  35. }
  36. }
  37. /**
  38. * TestDispatcher class
  39. */
  40. class TestDispatcher extends Dispatcher {
  41. /**
  42. * Controller instance, made publicly available for testing
  43. *
  44. * @var Controller
  45. */
  46. public $controller;
  47. /**
  48. * invoke method
  49. *
  50. * @param \Cake\Controller\Controller $controller
  51. * @return \Cake\Network\Response $response
  52. */
  53. protected function _invoke(Controller $controller) {
  54. $this->controller = $controller;
  55. return parent::_invoke($controller);
  56. }
  57. }
  58. /**
  59. * MyPluginAppController class
  60. *
  61. */
  62. class MyPluginAppController extends Controller {
  63. }
  64. interface DispatcherTestInterfaceController {
  65. public function index();
  66. }
  67. /**
  68. * MyPluginController class
  69. *
  70. */
  71. class MyPluginController extends MyPluginAppController {
  72. /**
  73. * name property
  74. *
  75. * @var string
  76. */
  77. public $name = 'MyPlugin';
  78. /**
  79. * index method
  80. *
  81. * @return void
  82. */
  83. public function index() {
  84. return true;
  85. }
  86. /**
  87. * add method
  88. *
  89. * @return void
  90. */
  91. public function add() {
  92. return true;
  93. }
  94. /**
  95. * admin_add method
  96. *
  97. * @param mixed $id
  98. * @return void
  99. */
  100. public function admin_add($id = null) {
  101. return $id;
  102. }
  103. }
  104. /**
  105. * OtherPagesController class
  106. *
  107. */
  108. class OtherPagesController extends MyPluginAppController {
  109. /**
  110. * name property
  111. *
  112. * @var string
  113. */
  114. public $name = 'OtherPages';
  115. /**
  116. * display method
  117. *
  118. * @param string $page
  119. * @return void
  120. */
  121. public function display($page = null) {
  122. return $page;
  123. }
  124. /**
  125. * index method
  126. *
  127. * @return void
  128. */
  129. public function index() {
  130. return true;
  131. }
  132. }
  133. /**
  134. * ArticlesTestAppController class
  135. *
  136. */
  137. class ArticlesTestAppController extends Controller {
  138. }
  139. /**
  140. * ArticlesTestController class
  141. *
  142. */
  143. class ArticlesTestController extends ArticlesTestAppController {
  144. /**
  145. * name property
  146. *
  147. * @var string
  148. */
  149. public $name = 'ArticlesTest';
  150. /**
  151. * admin_index method
  152. *
  153. * @return void
  154. */
  155. public function admin_index() {
  156. return true;
  157. }
  158. /**
  159. * fake index method.
  160. *
  161. * @return void
  162. */
  163. public function index() {
  164. return true;
  165. }
  166. }
  167. /**
  168. * DispatcherTest class
  169. *
  170. */
  171. class DispatcherTest extends TestCase {
  172. /**
  173. * setUp method
  174. *
  175. * @return void
  176. */
  177. public function setUp() {
  178. parent::setUp();
  179. $_GET = [];
  180. Configure::write('App.base', false);
  181. Configure::write('App.baseUrl', false);
  182. Configure::write('App.dir', 'app');
  183. Configure::write('App.webroot', 'webroot');
  184. Configure::write('App.namespace', 'TestApp');
  185. $this->dispatcher = new TestDispatcher();
  186. $this->dispatcher->addFilter(new ControllerFactoryFilter());
  187. }
  188. /**
  189. * tearDown method
  190. *
  191. * @return void
  192. */
  193. public function tearDown() {
  194. parent::tearDown();
  195. Plugin::unload();
  196. }
  197. /**
  198. * testMissingController method
  199. *
  200. * @expectedException \Cake\Routing\Error\MissingControllerException
  201. * @expectedExceptionMessage Controller class SomeController could not be found.
  202. * @return void
  203. */
  204. public function testMissingController() {
  205. $request = new Request([
  206. 'url' => 'some_controller/home',
  207. 'params' => [
  208. 'controller' => 'SomeController',
  209. 'action' => 'home',
  210. ]
  211. ]);
  212. $response = $this->getMock('Cake\Network\Response');
  213. $this->dispatcher->dispatch($request, $response, array('return' => 1));
  214. }
  215. /**
  216. * testMissingControllerInterface method
  217. *
  218. * @expectedException \Cake\Routing\Error\MissingControllerException
  219. * @expectedExceptionMessage Controller class DispatcherTestInterface could not be found.
  220. * @return void
  221. */
  222. public function testMissingControllerInterface() {
  223. $request = new Request([
  224. 'url' => 'dispatcher_test_interface/index',
  225. 'params' => [
  226. 'controller' => 'DispatcherTestInterface',
  227. 'action' => 'index',
  228. ]
  229. ]);
  230. $url = new Request('dispatcher_test_interface/index');
  231. $response = $this->getMock('Cake\Network\Response');
  232. $this->dispatcher->dispatch($request, $response, array('return' => 1));
  233. }
  234. /**
  235. * testMissingControllerInterface method
  236. *
  237. * @expectedException \Cake\Routing\Error\MissingControllerException
  238. * @expectedExceptionMessage Controller class Abstract could not be found.
  239. * @return void
  240. */
  241. public function testMissingControllerAbstract() {
  242. $request = new Request([
  243. 'url' => 'abstract/index',
  244. 'params' => [
  245. 'controller' => 'Abstract',
  246. 'action' => 'index',
  247. ]
  248. ]);
  249. $response = $this->getMock('Cake\Network\Response');
  250. $this->dispatcher->dispatch($request, $response, array('return' => 1));
  251. }
  252. /**
  253. * testDispatch method
  254. *
  255. * @return void
  256. */
  257. public function testDispatchBasic() {
  258. $url = new Request([
  259. 'url' => 'pages/home',
  260. 'params' => [
  261. 'controller' => 'Pages',
  262. 'action' => 'display',
  263. 'pass' => ['extract'],
  264. 'return' => 1
  265. ]
  266. ]);
  267. $response = $this->getMock('Cake\Network\Response');
  268. $this->dispatcher->dispatch($url, $response);
  269. $expected = 'Pages';
  270. $this->assertEquals($expected, $this->dispatcher->controller->name);
  271. }
  272. /**
  273. * Test that Dispatcher handles actions that return response objects.
  274. *
  275. * @return void
  276. */
  277. public function testDispatchActionReturnsResponse() {
  278. $request = new Request([
  279. 'url' => 'some_pages/responseGenerator',
  280. 'params' => [
  281. 'controller' => 'SomePages',
  282. 'action' => 'responseGenerator',
  283. 'pass' => []
  284. ]
  285. ]);
  286. $response = $this->getMock('Cake\Network\Response', array('_sendHeader'));
  287. ob_start();
  288. $this->dispatcher->dispatch($request, $response);
  289. $result = ob_get_clean();
  290. $this->assertEquals('new response', $result);
  291. }
  292. /**
  293. * testPrefixDispatch method
  294. *
  295. * @return void
  296. */
  297. public function testPrefixDispatch() {
  298. $request = new Request([
  299. 'url' => 'admin/posts/index',
  300. 'params' => [
  301. 'prefix' => 'Admin',
  302. 'controller' => 'Posts',
  303. 'action' => 'index',
  304. 'pass' => [],
  305. 'return' => 1
  306. ]
  307. ]);
  308. $response = $this->getMock('Cake\Network\Response');
  309. $this->dispatcher->dispatch($request, $response);
  310. $this->assertInstanceOf(
  311. 'TestApp\Controller\Admin\PostsController',
  312. $this->dispatcher->controller
  313. );
  314. $expected = '/admin/posts/index';
  315. $this->assertSame($expected, $request->here);
  316. }
  317. /**
  318. * test prefix dispatching in a plugin.
  319. *
  320. * @return void
  321. */
  322. public function testPrefixDispatchPlugin() {
  323. Plugin::load('TestPlugin');
  324. $request = new Request([
  325. 'url' => 'admin/test_plugin/comments/index',
  326. 'params' => [
  327. 'plugin' => 'TestPlugin',
  328. 'prefix' => 'Admin',
  329. 'controller' => 'Comments',
  330. 'action' => 'index',
  331. 'pass' => [],
  332. 'return' => 1
  333. ]
  334. ]);
  335. $response = $this->getMock('Cake\Network\Response');
  336. $this->dispatcher->dispatch($request, $response);
  337. $this->assertInstanceOf(
  338. 'TestPlugin\Controller\Admin\CommentsController',
  339. $this->dispatcher->controller
  340. );
  341. }
  342. /**
  343. * Test dispatcher filters being called.
  344. *
  345. * @return void
  346. */
  347. public function testDispatcherFilter() {
  348. $filter = $this->getMock(
  349. 'Cake\Routing\DispatcherFilter',
  350. ['beforeDispatch', 'afterDispatch']
  351. );
  352. $filter->expects($this->at(0))
  353. ->method('beforeDispatch');
  354. $filter->expects($this->at(1))
  355. ->method('afterDispatch');
  356. $this->dispatcher->addFilter($filter);
  357. $request = new Request([
  358. 'url' => '/',
  359. 'params' => [
  360. 'controller' => 'pages',
  361. 'action' => 'display',
  362. 'home',
  363. 'pass' => []
  364. ]
  365. ]);
  366. $response = $this->getMock('Cake\Network\Response', ['send']);
  367. $this->dispatcher->dispatch($request, $response);
  368. }
  369. /**
  370. * Test dispatcher filters being called and changing the response.
  371. *
  372. * @return void
  373. */
  374. public function testBeforeDispatchAbortDispatch() {
  375. $response = $this->getMock('Cake\Network\Response', ['send']);
  376. $response->expects($this->once())
  377. ->method('send');
  378. $filter = $this->getMock(
  379. 'Cake\Routing\DispatcherFilter',
  380. ['beforeDispatch', 'afterDispatch']);
  381. $filter->expects($this->once())
  382. ->method('beforeDispatch')
  383. ->will($this->returnValue($response));
  384. $filter->expects($this->never())
  385. ->method('afterDispatch');
  386. $request = new Request();
  387. $res = new Response();
  388. $this->dispatcher->addFilter($filter);
  389. $this->dispatcher->dispatch($request, $res);
  390. }
  391. /**
  392. * Test dispatcher filters being called and changing the response.
  393. *
  394. * @return void
  395. */
  396. public function testAfterDispatchReplaceResponse() {
  397. $response = $this->getMock('Cake\Network\Response', ['_sendHeader', 'send']);
  398. $response->expects($this->once())
  399. ->method('send');
  400. $filter = $this->getMock(
  401. 'Cake\Routing\DispatcherFilter',
  402. ['beforeDispatch', 'afterDispatch']);
  403. $filter->expects($this->once())
  404. ->method('afterDispatch')
  405. ->will($this->returnValue($response));
  406. $request = new Request([
  407. 'url' => '/posts',
  408. 'params' => [
  409. 'plugin' => null,
  410. 'controller' => 'Posts',
  411. 'action' => 'index',
  412. 'pass' => [],
  413. ],
  414. 'session' => new Session
  415. ]);
  416. $this->dispatcher->addFilter($filter);
  417. $this->dispatcher->dispatch($request, $response);
  418. }
  419. }