ControllerResolverTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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->createMock(LoggerInterface::class);
  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. public function testGetControllerOnObjectWithoutInvokeMethod()
  78. {
  79. $this->expectException(\InvalidArgumentException::class);
  80. $resolver = $this->createControllerResolver();
  81. $request = Request::create('/');
  82. $request->attributes->set('_controller', new \stdClass());
  83. $resolver->getController($request);
  84. }
  85. public function testGetControllerWithFunction()
  86. {
  87. $resolver = $this->createControllerResolver();
  88. $request = Request::create('/');
  89. $request->attributes->set('_controller', 'Symfony\Component\HttpKernel\Tests\Controller\some_controller_function');
  90. $controller = $resolver->getController($request);
  91. $this->assertSame('Symfony\Component\HttpKernel\Tests\Controller\some_controller_function', $controller);
  92. }
  93. public function testGetControllerWithClosure()
  94. {
  95. $resolver = $this->createControllerResolver();
  96. $closure = function () {
  97. return 'test';
  98. };
  99. $request = Request::create('/');
  100. $request->attributes->set('_controller', $closure);
  101. $controller = $resolver->getController($request);
  102. $this->assertInstanceOf(\Closure::class, $controller);
  103. $this->assertSame('test', $controller());
  104. }
  105. /**
  106. * @dataProvider getStaticControllers
  107. */
  108. public function testGetControllerWithStaticController($staticController, $returnValue)
  109. {
  110. $resolver = $this->createControllerResolver();
  111. $request = Request::create('/');
  112. $request->attributes->set('_controller', $staticController);
  113. $controller = $resolver->getController($request);
  114. $this->assertSame($staticController, $controller);
  115. $this->assertSame($returnValue, $controller());
  116. }
  117. public static function getStaticControllers()
  118. {
  119. return [
  120. [TestAbstractController::class.'::staticAction', 'foo'],
  121. [[TestAbstractController::class, 'staticAction'], 'foo'],
  122. [PrivateConstructorController::class.'::staticAction', 'bar'],
  123. [[PrivateConstructorController::class, 'staticAction'], 'bar'],
  124. ];
  125. }
  126. /**
  127. * @dataProvider getUndefinedControllers
  128. */
  129. public function testGetControllerWithUndefinedController($controller, $exceptionName = null, $exceptionMessage = null)
  130. {
  131. $resolver = $this->createControllerResolver();
  132. $this->expectException($exceptionName);
  133. $this->expectExceptionMessage($exceptionMessage);
  134. $request = Request::create('/');
  135. $request->attributes->set('_controller', $controller);
  136. $resolver->getController($request);
  137. }
  138. public static function getUndefinedControllers()
  139. {
  140. $controller = new ControllerTest();
  141. return [
  142. ['foo', \Error::class, \PHP_VERSION_ID < 80000 ? 'Class \'foo\' not found' : 'Class "foo" not found'],
  143. ['oof::bar', \Error::class, \PHP_VERSION_ID < 80000 ? 'Class \'oof\' not found' : 'Class "oof" not found'],
  144. [['oof', 'bar'], \Error::class, \PHP_VERSION_ID < 80000 ? 'Class \'oof\' not found' : 'Class "oof" not found'],
  145. ['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"?'],
  146. ['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'],
  147. ['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'],
  148. ['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"'],
  149. ['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".'],
  150. [[$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"?'],
  151. [[$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'],
  152. [[$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'],
  153. [[$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"'],
  154. [$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".'],
  155. [['a' => 'foo', 'b' => 'bar'], \InvalidArgumentException::class, 'The controller for URI "/" is not callable: Invalid array callable, expected [controller, method].'],
  156. ];
  157. }
  158. protected function createControllerResolver(LoggerInterface $logger = null)
  159. {
  160. return new ControllerResolver($logger);
  161. }
  162. }
  163. function some_controller_function($foo, $foobar)
  164. {
  165. }
  166. class ControllerTest
  167. {
  168. public function __construct()
  169. {
  170. }
  171. public function __toString(): string
  172. {
  173. return '';
  174. }
  175. public function publicAction()
  176. {
  177. }
  178. private function privateAction()
  179. {
  180. }
  181. protected function protectedAction()
  182. {
  183. }
  184. public static function staticAction()
  185. {
  186. }
  187. }
  188. class InvokableController
  189. {
  190. public function __invoke($foo, $bar = null)
  191. {
  192. }
  193. }
  194. abstract class TestAbstractController
  195. {
  196. public static function staticAction()
  197. {
  198. return 'foo';
  199. }
  200. }
  201. class PrivateConstructorController
  202. {
  203. private function __construct()
  204. {
  205. }
  206. public static function staticAction()
  207. {
  208. return 'bar';
  209. }
  210. }