ControllerFactoryTest.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.3.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Http;
  16. use Cake\Http\ControllerFactory;
  17. use Cake\Http\ServerRequest;
  18. use Cake\TestSuite\TestCase;
  19. /**
  20. * Test case for ControllerFactory.
  21. */
  22. class ControllerFactoryTest extends TestCase
  23. {
  24. /**
  25. * Setup
  26. *
  27. * @return void
  28. */
  29. public function setUp()
  30. {
  31. parent::setUp();
  32. static::setAppNamespace();
  33. $this->factory = new ControllerFactory();
  34. $this->response = $this->getMockBuilder('Cake\Http\Response')->getMock();
  35. }
  36. /**
  37. * Test building an application controller
  38. *
  39. * @return void
  40. */
  41. public function testApplicationController()
  42. {
  43. $request = new ServerRequest([
  44. 'url' => 'cakes/index',
  45. 'params' => [
  46. 'controller' => 'Cakes',
  47. 'action' => 'index',
  48. ]
  49. ]);
  50. $result = $this->factory->create($request, $this->response);
  51. $this->assertInstanceOf('TestApp\Controller\CakesController', $result);
  52. $this->assertSame($request, $result->request);
  53. $this->assertSame($this->response, $result->response);
  54. }
  55. /**
  56. * Test building a prefixed app controller.
  57. *
  58. * @return void
  59. */
  60. public function testPrefixedAppController()
  61. {
  62. $request = new ServerRequest([
  63. 'url' => 'admin/posts/index',
  64. 'params' => [
  65. 'prefix' => 'admin',
  66. 'controller' => 'Posts',
  67. 'action' => 'index',
  68. ]
  69. ]);
  70. $result = $this->factory->create($request, $this->response);
  71. $this->assertInstanceOf(
  72. 'TestApp\Controller\Admin\PostsController',
  73. $result
  74. );
  75. $this->assertSame($request, $result->request);
  76. $this->assertSame($this->response, $result->response);
  77. }
  78. /**
  79. * Test building a nested prefix app controller
  80. *
  81. * @return void
  82. */
  83. public function testNestedPrefixedAppController()
  84. {
  85. $request = new ServerRequest([
  86. 'url' => 'admin/sub/posts/index',
  87. 'params' => [
  88. 'prefix' => 'admin/sub',
  89. 'controller' => 'Posts',
  90. 'action' => 'index',
  91. ]
  92. ]);
  93. $result = $this->factory->create($request, $this->response);
  94. $this->assertInstanceOf(
  95. 'TestApp\Controller\Admin\Sub\PostsController',
  96. $result
  97. );
  98. $this->assertSame($request, $result->request);
  99. $this->assertSame($this->response, $result->response);
  100. }
  101. /**
  102. * Test building a plugin controller
  103. *
  104. * @return void
  105. */
  106. public function testPluginController()
  107. {
  108. $request = new ServerRequest([
  109. 'url' => 'test_plugin/test_plugin/index',
  110. 'params' => [
  111. 'plugin' => 'TestPlugin',
  112. 'controller' => 'TestPlugin',
  113. 'action' => 'index',
  114. ]
  115. ]);
  116. $result = $this->factory->create($request, $this->response);
  117. $this->assertInstanceOf(
  118. 'TestPlugin\Controller\TestPluginController',
  119. $result
  120. );
  121. $this->assertSame($request, $result->request);
  122. $this->assertSame($this->response, $result->response);
  123. }
  124. /**
  125. * Test building a vendored plugin controller.
  126. *
  127. * @return void
  128. */
  129. public function testVendorPluginController()
  130. {
  131. $request = new ServerRequest([
  132. 'url' => 'test_plugin_three/ovens/index',
  133. 'params' => [
  134. 'plugin' => 'Company/TestPluginThree',
  135. 'controller' => 'Ovens',
  136. 'action' => 'index',
  137. ]
  138. ]);
  139. $result = $this->factory->create($request, $this->response);
  140. $this->assertInstanceOf(
  141. 'Company\TestPluginThree\Controller\OvensController',
  142. $result
  143. );
  144. $this->assertSame($request, $result->request);
  145. $this->assertSame($this->response, $result->response);
  146. }
  147. /**
  148. * Test building a prefixed plugin controller
  149. *
  150. * @return void
  151. */
  152. public function testPrefixedPluginController()
  153. {
  154. $request = new ServerRequest([
  155. 'url' => 'test_plugin/admin/comments',
  156. 'params' => [
  157. 'prefix' => 'admin',
  158. 'plugin' => 'TestPlugin',
  159. 'controller' => 'Comments',
  160. 'action' => 'index',
  161. ]
  162. ]);
  163. $result = $this->factory->create($request, $this->response);
  164. $this->assertInstanceOf(
  165. 'TestPlugin\Controller\Admin\CommentsController',
  166. $result
  167. );
  168. $this->assertSame($request, $result->request);
  169. $this->assertSame($this->response, $result->response);
  170. }
  171. /**
  172. * @return void
  173. */
  174. public function testAbstractClassFailure()
  175. {
  176. $this->expectException(\Cake\Routing\Exception\MissingControllerException::class);
  177. $this->expectExceptionMessage('Controller class Abstract could not be found.');
  178. $request = new ServerRequest([
  179. 'url' => 'abstract/index',
  180. 'params' => [
  181. 'controller' => 'Abstract',
  182. 'action' => 'index',
  183. ]
  184. ]);
  185. $this->factory->create($request, $this->response);
  186. }
  187. /**
  188. * @return void
  189. */
  190. public function testInterfaceFailure()
  191. {
  192. $this->expectException(\Cake\Routing\Exception\MissingControllerException::class);
  193. $this->expectExceptionMessage('Controller class Interface could not be found.');
  194. $request = new ServerRequest([
  195. 'url' => 'interface/index',
  196. 'params' => [
  197. 'controller' => 'Interface',
  198. 'action' => 'index',
  199. ]
  200. ]);
  201. $this->factory->create($request, $this->response);
  202. }
  203. /**
  204. * @return void
  205. */
  206. public function testMissingClassFailure()
  207. {
  208. $this->expectException(\Cake\Routing\Exception\MissingControllerException::class);
  209. $this->expectExceptionMessage('Controller class Invisible could not be found.');
  210. $request = new ServerRequest([
  211. 'url' => 'interface/index',
  212. 'params' => [
  213. 'controller' => 'Invisible',
  214. 'action' => 'index',
  215. ]
  216. ]);
  217. $this->factory->create($request, $this->response);
  218. }
  219. /**
  220. * @return void
  221. */
  222. public function testSlashedControllerFailure()
  223. {
  224. $this->expectException(\Cake\Routing\Exception\MissingControllerException::class);
  225. $this->expectExceptionMessage('Controller class Admin/Posts could not be found.');
  226. $request = new ServerRequest([
  227. 'url' => 'admin/posts/index',
  228. 'params' => [
  229. 'controller' => 'Admin/Posts',
  230. 'action' => 'index',
  231. ]
  232. ]);
  233. $this->factory->create($request, $this->response);
  234. }
  235. /**
  236. * @return void
  237. */
  238. public function testAbsoluteReferenceFailure()
  239. {
  240. $this->expectException(\Cake\Routing\Exception\MissingControllerException::class);
  241. $this->expectExceptionMessage('Controller class TestApp\Controller\CakesController could not be found.');
  242. $request = new ServerRequest([
  243. 'url' => 'interface/index',
  244. 'params' => [
  245. 'controller' => 'TestApp\Controller\CakesController',
  246. 'action' => 'index',
  247. ]
  248. ]);
  249. $this->factory->create($request, $this->response);
  250. }
  251. /**
  252. * Test building controller name when passing no controller name
  253. *
  254. * @return void
  255. */
  256. public function testGetControllerClassNoControllerName()
  257. {
  258. $request = new ServerRequest([
  259. 'url' => 'test_plugin_three/ovens/index',
  260. 'params' => [
  261. 'plugin' => 'Company/TestPluginThree',
  262. 'controller' => 'Ovens',
  263. 'action' => 'index',
  264. ]
  265. ]);
  266. $result = $this->factory->getControllerClass($request);
  267. $this->assertSame('Company\TestPluginThree\Controller\OvensController', $result);
  268. }
  269. }