ControllerFactoryTest.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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\Response;
  18. use Cake\Http\ServerRequest;
  19. use Cake\TestSuite\TestCase;
  20. /**
  21. * Test case for ControllerFactory.
  22. */
  23. class ControllerFactoryTest extends TestCase
  24. {
  25. /**
  26. * Setup
  27. *
  28. * @return void
  29. */
  30. public function setUp()
  31. {
  32. parent::setUp();
  33. static::setAppNamespace();
  34. $this->factory = new ControllerFactory();
  35. $this->response = $this->getMockBuilder('Cake\Http\Response')->getMock();
  36. }
  37. /**
  38. * Test building an application controller
  39. *
  40. * @return void
  41. */
  42. public function testApplicationController()
  43. {
  44. $request = new ServerRequest([
  45. 'url' => 'cakes/index',
  46. 'params' => [
  47. 'controller' => 'Cakes',
  48. 'action' => 'index',
  49. ]
  50. ]);
  51. $result = $this->factory->create($request, $this->response);
  52. $this->assertInstanceOf('TestApp\Controller\CakesController', $result);
  53. $this->assertSame($request, $result->request);
  54. $this->assertSame($this->response, $result->response);
  55. }
  56. /**
  57. * Test building a prefixed app controller.
  58. *
  59. * @return void
  60. */
  61. public function testPrefixedAppController()
  62. {
  63. $request = new ServerRequest([
  64. 'url' => 'admin/posts/index',
  65. 'params' => [
  66. 'prefix' => 'admin',
  67. 'controller' => 'Posts',
  68. 'action' => 'index',
  69. ]
  70. ]);
  71. $result = $this->factory->create($request, $this->response);
  72. $this->assertInstanceOf(
  73. 'TestApp\Controller\Admin\PostsController',
  74. $result
  75. );
  76. $this->assertSame($request, $result->request);
  77. $this->assertSame($this->response, $result->response);
  78. }
  79. /**
  80. * Test building a nested prefix app controller
  81. *
  82. * @return void
  83. */
  84. public function testNestedPrefixedAppController()
  85. {
  86. $request = new ServerRequest([
  87. 'url' => 'admin/sub/posts/index',
  88. 'params' => [
  89. 'prefix' => 'admin/sub',
  90. 'controller' => 'Posts',
  91. 'action' => 'index',
  92. ]
  93. ]);
  94. $result = $this->factory->create($request, $this->response);
  95. $this->assertInstanceOf(
  96. 'TestApp\Controller\Admin\Sub\PostsController',
  97. $result
  98. );
  99. $this->assertSame($request, $result->request);
  100. $this->assertSame($this->response, $result->response);
  101. }
  102. /**
  103. * Test building a plugin controller
  104. *
  105. * @return void
  106. */
  107. public function testPluginController()
  108. {
  109. $request = new ServerRequest([
  110. 'url' => 'test_plugin/test_plugin/index',
  111. 'params' => [
  112. 'plugin' => 'TestPlugin',
  113. 'controller' => 'TestPlugin',
  114. 'action' => 'index',
  115. ]
  116. ]);
  117. $result = $this->factory->create($request, $this->response);
  118. $this->assertInstanceOf(
  119. 'TestPlugin\Controller\TestPluginController',
  120. $result
  121. );
  122. $this->assertSame($request, $result->request);
  123. $this->assertSame($this->response, $result->response);
  124. }
  125. /**
  126. * Test building a vendored plugin controller.
  127. *
  128. * @return void
  129. */
  130. public function testVendorPluginController()
  131. {
  132. $request = new ServerRequest([
  133. 'url' => 'test_plugin_three/ovens/index',
  134. 'params' => [
  135. 'plugin' => 'Company/TestPluginThree',
  136. 'controller' => 'Ovens',
  137. 'action' => 'index',
  138. ]
  139. ]);
  140. $result = $this->factory->create($request, $this->response);
  141. $this->assertInstanceOf(
  142. 'Company\TestPluginThree\Controller\OvensController',
  143. $result
  144. );
  145. $this->assertSame($request, $result->request);
  146. $this->assertSame($this->response, $result->response);
  147. }
  148. /**
  149. * Test building a prefixed plugin controller
  150. *
  151. * @return void
  152. */
  153. public function testPrefixedPluginController()
  154. {
  155. $request = new ServerRequest([
  156. 'url' => 'test_plugin/admin/comments',
  157. 'params' => [
  158. 'prefix' => 'admin',
  159. 'plugin' => 'TestPlugin',
  160. 'controller' => 'Comments',
  161. 'action' => 'index',
  162. ]
  163. ]);
  164. $result = $this->factory->create($request, $this->response);
  165. $this->assertInstanceOf(
  166. 'TestPlugin\Controller\Admin\CommentsController',
  167. $result
  168. );
  169. $this->assertSame($request, $result->request);
  170. $this->assertSame($this->response, $result->response);
  171. }
  172. /**
  173. * @expectedException \Cake\Routing\Exception\MissingControllerException
  174. * @expectedExceptionMessage Controller class Abstract could not be found.
  175. * @return void
  176. */
  177. public function testAbstractClassFailure()
  178. {
  179. $request = new ServerRequest([
  180. 'url' => 'abstract/index',
  181. 'params' => [
  182. 'controller' => 'Abstract',
  183. 'action' => 'index',
  184. ]
  185. ]);
  186. $this->factory->create($request, $this->response);
  187. }
  188. /**
  189. * @expectedException \Cake\Routing\Exception\MissingControllerException
  190. * @expectedExceptionMessage Controller class Interface could not be found.
  191. * @return void
  192. */
  193. public function testInterfaceFailure()
  194. {
  195. $request = new ServerRequest([
  196. 'url' => 'interface/index',
  197. 'params' => [
  198. 'controller' => 'Interface',
  199. 'action' => 'index',
  200. ]
  201. ]);
  202. $this->factory->create($request, $this->response);
  203. }
  204. /**
  205. * @expectedException \Cake\Routing\Exception\MissingControllerException
  206. * @expectedExceptionMessage Controller class Invisible could not be found.
  207. * @return void
  208. */
  209. public function testMissingClassFailure()
  210. {
  211. $request = new ServerRequest([
  212. 'url' => 'interface/index',
  213. 'params' => [
  214. 'controller' => 'Invisible',
  215. 'action' => 'index',
  216. ]
  217. ]);
  218. $this->factory->create($request, $this->response);
  219. }
  220. /**
  221. * @expectedException \Cake\Routing\Exception\MissingControllerException
  222. * @expectedExceptionMessage Controller class Admin/Posts could not be found.
  223. * @return void
  224. */
  225. public function testSlashedControllerFailure()
  226. {
  227. $request = new ServerRequest([
  228. 'url' => 'admin/posts/index',
  229. 'params' => [
  230. 'controller' => 'Admin/Posts',
  231. 'action' => 'index',
  232. ]
  233. ]);
  234. $this->factory->create($request, $this->response);
  235. }
  236. /**
  237. * @expectedException \Cake\Routing\Exception\MissingControllerException
  238. * @expectedExceptionMessage Controller class TestApp\Controller\CakesController could not be found.
  239. * @return void
  240. */
  241. public function testAbsoluteReferenceFailure()
  242. {
  243. $request = new ServerRequest([
  244. 'url' => 'interface/index',
  245. 'params' => [
  246. 'controller' => 'TestApp\Controller\CakesController',
  247. 'action' => 'index',
  248. ]
  249. ]);
  250. $this->factory->create($request, $this->response);
  251. }
  252. }