ControllerResolverTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpKernel\Tests\Controller;
  11. use PHPUnit\Framework\TestCase;
  12. use Psr\Log\LoggerInterface;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpKernel\Controller\ControllerResolver;
  15. class ControllerResolverTest extends TestCase
  16. {
  17. public function testGetControllerWithoutControllerParameter()
  18. {
  19. $logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
  20. $logger->expects($this->once())->method('warning')->with('Unable to look for the controller as the "_controller" parameter is missing.');
  21. $resolver = $this->createControllerResolver($logger);
  22. $request = Request::create('/');
  23. $this->assertFalse($resolver->getController($request), '->getController() returns false when the request has no _controller attribute');
  24. }
  25. public function testGetControllerWithLambda()
  26. {
  27. $resolver = $this->createControllerResolver();
  28. $request = Request::create('/');
  29. $request->attributes->set('_controller', $lambda = function () {});
  30. $controller = $resolver->getController($request);
  31. $this->assertSame($lambda, $controller);
  32. }
  33. public function testGetControllerWithObjectAndInvokeMethod()
  34. {
  35. $resolver = $this->createControllerResolver();
  36. $object = new InvokableController();
  37. $request = Request::create('/');
  38. $request->attributes->set('_controller', $object);
  39. $controller = $resolver->getController($request);
  40. $this->assertSame($object, $controller);
  41. }
  42. public function testGetControllerWithObjectAndMethod()
  43. {
  44. $resolver = $this->createControllerResolver();
  45. $object = new ControllerTest();
  46. $request = Request::create('/');
  47. $request->attributes->set('_controller', [$object, 'publicAction']);
  48. $controller = $resolver->getController($request);
  49. $this->assertSame([$object, 'publicAction'], $controller);
  50. }
  51. public function testGetControllerWithClassAndMethodAsArray()
  52. {
  53. $resolver = $this->createControllerResolver();
  54. $request = Request::create('/');
  55. $request->attributes->set('_controller', [ControllerTest::class, 'publicAction']);
  56. $controller = $resolver->getController($request);
  57. $this->assertInstanceOf(ControllerTest::class, $controller[0]);
  58. $this->assertSame('publicAction', $controller[1]);
  59. }
  60. public function testGetControllerWithClassAndMethodAsString()
  61. {
  62. $resolver = $this->createControllerResolver();
  63. $request = Request::create('/');
  64. $request->attributes->set('_controller', ControllerTest::class.'::publicAction');
  65. $controller = $resolver->getController($request);
  66. $this->assertInstanceOf(ControllerTest::class, $controller[0]);
  67. $this->assertSame('publicAction', $controller[1]);
  68. }
  69. public function testGetControllerWithInvokableClass()
  70. {
  71. $resolver = $this->createControllerResolver();
  72. $request = Request::create('/');
  73. $request->attributes->set('_controller', InvokableController::class);
  74. $controller = $resolver->getController($request);
  75. $this->assertInstanceOf(InvokableController::class, $controller);
  76. }
  77. /**
  78. * @expectedException \InvalidArgumentException
  79. */
  80. public function testGetControllerOnObjectWithoutInvokeMethod()
  81. {
  82. $resolver = $this->createControllerResolver();
  83. $request = Request::create('/');
  84. $request->attributes->set('_controller', new \stdClass());
  85. $resolver->getController($request);
  86. }
  87. public function testGetControllerWithFunction()
  88. {
  89. $resolver = $this->createControllerResolver();
  90. $request = Request::create('/');
  91. $request->attributes->set('_controller', 'Symfony\Component\HttpKernel\Tests\Controller\some_controller_function');
  92. $controller = $resolver->getController($request);
  93. $this->assertSame('Symfony\Component\HttpKernel\Tests\Controller\some_controller_function', $controller);
  94. }
  95. public function testGetControllerWithClosure()
  96. {
  97. $resolver = $this->createControllerResolver();
  98. $closure = function () {
  99. return 'test';
  100. };
  101. $request = Request::create('/');
  102. $request->attributes->set('_controller', $closure);
  103. $controller = $resolver->getController($request);
  104. $this->assertInstanceOf(\Closure::class, $controller);
  105. $this->assertSame('test', $controller());
  106. }
  107. /**
  108. * @dataProvider getStaticControllers
  109. */
  110. public function testGetControllerWithStaticController($staticController, $returnValue)
  111. {
  112. $resolver = $this->createControllerResolver();
  113. $request = Request::create('/');
  114. $request->attributes->set('_controller', $staticController);
  115. $controller = $resolver->getController($request);
  116. $this->assertSame($staticController, $controller);
  117. $this->assertSame($returnValue, $controller());
  118. }
  119. public function getStaticControllers()
  120. {
  121. return [
  122. [TestAbstractController::class.'::staticAction', 'foo'],
  123. [[TestAbstractController::class, 'staticAction'], 'foo'],
  124. [PrivateConstructorController::class.'::staticAction', 'bar'],
  125. [[PrivateConstructorController::class, 'staticAction'], 'bar'],
  126. ];
  127. }
  128. /**
  129. * @dataProvider getUndefinedControllers
  130. */
  131. public function testGetControllerWithUndefinedController($controller, $exceptionName = null, $exceptionMessage = null)
  132. {
  133. $resolver = $this->createControllerResolver();
  134. if (method_exists($this, 'expectException')) {
  135. $this->expectException($exceptionName);
  136. $this->expectExceptionMessage($exceptionMessage);
  137. } else {
  138. $this->setExpectedException($exceptionName, $exceptionMessage);
  139. }
  140. $request = Request::create('/');
  141. $request->attributes->set('_controller', $controller);
  142. $resolver->getController($request);
  143. }
  144. public function getUndefinedControllers()
  145. {
  146. $controller = new ControllerTest();
  147. return [
  148. ['foo', \Error::class, 'Class \'foo\' not found'],
  149. ['oof::bar', \Error::class, 'Class \'oof\' not found'],
  150. [['oof', 'bar'], \Error::class, 'Class \'oof\' not found'],
  151. ['Symfony\Component\HttpKernel\Tests\Controller\ControllerTest::staticsAction', \InvalidArgumentException::class, 'The controller for URI "/" is not callable. Expected method "staticsAction" on class "Symfony\Component\HttpKernel\Tests\Controller\ControllerTest", did you mean "staticAction"?'],
  152. ['Symfony\Component\HttpKernel\Tests\Controller\ControllerTest::privateAction', \InvalidArgumentException::class, 'The controller for URI "/" is not callable. Method "privateAction" on class "Symfony\Component\HttpKernel\Tests\Controller\ControllerTest" should be public and non-abstract'],
  153. ['Symfony\Component\HttpKernel\Tests\Controller\ControllerTest::protectedAction', \InvalidArgumentException::class, 'The controller for URI "/" is not callable. Method "protectedAction" on class "Symfony\Component\HttpKernel\Tests\Controller\ControllerTest" should be public and non-abstract'],
  154. ['Symfony\Component\HttpKernel\Tests\Controller\ControllerTest::undefinedAction', \InvalidArgumentException::class, 'The controller for URI "/" is not callable. Expected method "undefinedAction" on class "Symfony\Component\HttpKernel\Tests\Controller\ControllerTest". Available methods: "publicAction", "staticAction"'],
  155. ['Symfony\Component\HttpKernel\Tests\Controller\ControllerTest', \InvalidArgumentException::class, 'The controller for URI "/" is not callable. Controller class "Symfony\Component\HttpKernel\Tests\Controller\ControllerTest" cannot be called without a method name. You need to implement "__invoke" or use one of the available methods: "publicAction", "staticAction".'],
  156. [[$controller, 'staticsAction'], \InvalidArgumentException::class, 'The controller for URI "/" is not callable. Expected method "staticsAction" on class "Symfony\Component\HttpKernel\Tests\Controller\ControllerTest", did you mean "staticAction"?'],
  157. [[$controller, 'privateAction'], \InvalidArgumentException::class, 'The controller for URI "/" is not callable. Method "privateAction" on class "Symfony\Component\HttpKernel\Tests\Controller\ControllerTest" should be public and non-abstract'],
  158. [[$controller, 'protectedAction'], \InvalidArgumentException::class, 'The controller for URI "/" is not callable. Method "protectedAction" on class "Symfony\Component\HttpKernel\Tests\Controller\ControllerTest" should be public and non-abstract'],
  159. [[$controller, 'undefinedAction'], \InvalidArgumentException::class, 'The controller for URI "/" is not callable. Expected method "undefinedAction" on class "Symfony\Component\HttpKernel\Tests\Controller\ControllerTest". Available methods: "publicAction", "staticAction"'],
  160. [$controller, \InvalidArgumentException::class, 'The controller for URI "/" is not callable. Controller class "Symfony\Component\HttpKernel\Tests\Controller\ControllerTest" cannot be called without a method name. You need to implement "__invoke" or use one of the available methods: "publicAction", "staticAction".'],
  161. [['a' => 'foo', 'b' => 'bar'], \InvalidArgumentException::class, 'The controller for URI "/" is not callable. Invalid array callable, expected [controller, method].'],
  162. ];
  163. }
  164. protected function createControllerResolver(LoggerInterface $logger = null)
  165. {
  166. return new ControllerResolver($logger);
  167. }
  168. }
  169. function some_controller_function($foo, $foobar)
  170. {
  171. }
  172. class ControllerTest
  173. {
  174. public function __construct()
  175. {
  176. }
  177. public function __toString()
  178. {
  179. return '';
  180. }
  181. public function publicAction()
  182. {
  183. }
  184. private function privateAction()
  185. {
  186. }
  187. protected function protectedAction()
  188. {
  189. }
  190. public static function staticAction()
  191. {
  192. }
  193. }
  194. class InvokableController
  195. {
  196. public function __invoke($foo, $bar = null)
  197. {
  198. }
  199. }
  200. abstract class TestAbstractController
  201. {
  202. public static function staticAction()
  203. {
  204. return 'foo';
  205. }
  206. }
  207. class PrivateConstructorController
  208. {
  209. private function __construct()
  210. {
  211. }
  212. public static function staticAction()
  213. {
  214. return 'bar';
  215. }
  216. }