ControllerFactoryTest.php 7.4 KB

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