ActionDispatcherTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 3.3.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Http;
  17. use Cake\Http\ActionDispatcher;
  18. use Cake\Http\Response;
  19. use Cake\Http\ServerRequest;
  20. use Cake\Routing\Router;
  21. use Cake\TestSuite\TestCase;
  22. use ReflectionProperty;
  23. /**
  24. * Test case for the ActionDispatcher.
  25. */
  26. class ActionDispatcherTest extends TestCase
  27. {
  28. /**
  29. * Setup
  30. *
  31. * @return void
  32. */
  33. public function setUp(): void
  34. {
  35. parent::setUp();
  36. Router::reload();
  37. static::setAppNamespace();
  38. $this->dispatcher = new ActionDispatcher();
  39. }
  40. /**
  41. * Ensure the constructor args end up on the right protected properties.
  42. *
  43. * @return void
  44. */
  45. public function testConstructorArgs()
  46. {
  47. $factory = $this->getMockBuilder('Cake\Http\ControllerFactory')->getMock();
  48. $dispatcher = new ActionDispatcher($factory);
  49. $reflect = new ReflectionProperty($dispatcher, 'factory');
  50. $reflect->setAccessible(true);
  51. $this->assertSame($factory, $reflect->getValue($dispatcher));
  52. }
  53. /**
  54. * Test that dispatching sets the Router request state.
  55. *
  56. * @return void
  57. */
  58. public function testDispatchSetsRequestContext()
  59. {
  60. $this->assertNull(Router::getRequest());
  61. $req = new ServerRequest([
  62. 'url' => '/cakes',
  63. 'params' => [
  64. 'plugin' => null,
  65. 'controller' => 'Cakes',
  66. 'action' => 'index',
  67. 'pass' => [],
  68. 'return' => true,
  69. ],
  70. ]);
  71. $res = new Response();
  72. $this->dispatcher->dispatch($req, $res);
  73. $this->assertSame($req, Router::getRequest(true));
  74. }
  75. /**
  76. * Test dispatch with autorender
  77. *
  78. * @return void
  79. */
  80. public function testDispatchAutoRender()
  81. {
  82. $request = new ServerRequest([
  83. 'url' => 'posts',
  84. 'params' => [
  85. 'controller' => 'Posts',
  86. 'action' => 'index',
  87. 'pass' => [],
  88. ],
  89. ]);
  90. $response = new Response();
  91. $result = $this->dispatcher->dispatch($request, $response);
  92. $this->assertInstanceOf('Cake\Http\Response', $result);
  93. $this->assertStringContainsString('posts index', (string)$result->getBody());
  94. }
  95. /**
  96. * Test dispatch with autorender=false
  97. *
  98. * @return void
  99. */
  100. public function testDispatchAutoRenderFalse()
  101. {
  102. $request = new ServerRequest([
  103. 'url' => 'posts',
  104. 'params' => [
  105. 'controller' => 'Cakes',
  106. 'action' => 'noRender',
  107. 'pass' => [],
  108. ],
  109. ]);
  110. $response = new Response();
  111. $result = $this->dispatcher->dispatch($request, $response);
  112. $this->assertInstanceOf('Cake\Http\Response', $result);
  113. $this->assertStringContainsString('autoRender false body', (string)$result->getBody());
  114. }
  115. /**
  116. * testMissingController method
  117. *
  118. * @return void
  119. */
  120. public function testMissingController()
  121. {
  122. $this->expectException(\Cake\Routing\Exception\MissingControllerException::class);
  123. $this->expectExceptionMessage('Controller class SomeController could not be found.');
  124. $request = new ServerRequest([
  125. 'url' => 'some_controller/home',
  126. 'params' => [
  127. 'controller' => 'SomeController',
  128. 'action' => 'home',
  129. ],
  130. ]);
  131. $response = $this->getMockBuilder('Cake\Http\Response')->getMock();
  132. $this->dispatcher->dispatch($request, $response);
  133. }
  134. /**
  135. * testMissingControllerInterface method
  136. *
  137. * @return void
  138. */
  139. public function testMissingControllerInterface()
  140. {
  141. $this->expectException(\Cake\Routing\Exception\MissingControllerException::class);
  142. $this->expectExceptionMessage('Controller class Interface could not be found.');
  143. $request = new ServerRequest([
  144. 'url' => 'interface/index',
  145. 'params' => [
  146. 'controller' => 'Interface',
  147. 'action' => 'index',
  148. ],
  149. ]);
  150. $response = $this->getMockBuilder('Cake\Http\Response')->getMock();
  151. $this->dispatcher->dispatch($request, $response);
  152. }
  153. /**
  154. * testMissingControllerInterface method
  155. *
  156. * @return void
  157. */
  158. public function testMissingControllerAbstract()
  159. {
  160. $this->expectException(\Cake\Routing\Exception\MissingControllerException::class);
  161. $this->expectExceptionMessage('Controller class Abstract could not be found.');
  162. $request = new ServerRequest([
  163. 'url' => 'abstract/index',
  164. 'params' => [
  165. 'controller' => 'Abstract',
  166. 'action' => 'index',
  167. ],
  168. ]);
  169. $response = $this->getMockBuilder('Cake\Http\Response')->getMock();
  170. $this->dispatcher->dispatch($request, $response);
  171. }
  172. /**
  173. * Test that lowercase controller names result in missing controller errors.
  174. *
  175. * In case-insensitive file systems, lowercase controller names will kind of work.
  176. * This causes annoying deployment issues for lots of folks.
  177. *
  178. * @return void
  179. */
  180. public function testMissingControllerLowercase()
  181. {
  182. $this->expectException(\Cake\Routing\Exception\MissingControllerException::class);
  183. $this->expectExceptionMessage('Controller class somepages could not be found.');
  184. $request = new ServerRequest([
  185. 'url' => 'pages/home',
  186. 'params' => [
  187. 'plugin' => null,
  188. 'controller' => 'somepages',
  189. 'action' => 'display',
  190. 'pass' => ['home'],
  191. ],
  192. ]);
  193. $response = $this->getMockBuilder('Cake\Http\Response')->getMock();
  194. $this->dispatcher->dispatch($request, $response);
  195. }
  196. /**
  197. * Ensure that a controller's startup event can stop the request.
  198. *
  199. * @return void
  200. */
  201. public function testStartupProcessAbort()
  202. {
  203. $request = new ServerRequest([
  204. 'url' => 'cakes/index',
  205. 'params' => [
  206. 'plugin' => null,
  207. 'controller' => 'Cakes',
  208. 'action' => 'index',
  209. 'stop' => 'startup',
  210. 'pass' => [],
  211. ],
  212. ]);
  213. $response = new Response();
  214. $result = $this->dispatcher->dispatch($request, $response);
  215. $this->assertSame('startup stop', (string)$result->getBody());
  216. }
  217. /**
  218. * Ensure that a controllers startup process can emit a response
  219. *
  220. * @return void
  221. */
  222. public function testShutdownProcessResponse()
  223. {
  224. $request = new ServerRequest([
  225. 'url' => 'cakes/index',
  226. 'params' => [
  227. 'plugin' => null,
  228. 'controller' => 'Cakes',
  229. 'action' => 'index',
  230. 'stop' => 'shutdown',
  231. 'pass' => [],
  232. ],
  233. ]);
  234. $response = new Response();
  235. $result = $this->dispatcher->dispatch($request, $response);
  236. $this->assertSame('shutdown stop', (string)$result->getBody());
  237. }
  238. }