DispatcherTest.php 9.7 KB

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