ControllerResolverTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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\HttpKernel\Controller\ControllerResolver;
  14. use Symfony\Component\HttpKernel\Tests\Fixtures\Controller\NullableController;
  15. use Symfony\Component\HttpKernel\Tests\Fixtures\Controller\VariadicController;
  16. use Symfony\Component\HttpFoundation\Request;
  17. class ControllerResolverTest extends TestCase
  18. {
  19. public function testGetControllerWithoutControllerParameter()
  20. {
  21. $logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
  22. $logger->expects($this->once())->method('warning')->with('Unable to look for the controller as the "_controller" parameter is missing.');
  23. $resolver = $this->createControllerResolver($logger);
  24. $request = Request::create('/');
  25. $this->assertFalse($resolver->getController($request), '->getController() returns false when the request has no _controller attribute');
  26. }
  27. public function testGetControllerWithLambda()
  28. {
  29. $resolver = $this->createControllerResolver();
  30. $request = Request::create('/');
  31. $request->attributes->set('_controller', $lambda = function () {});
  32. $controller = $resolver->getController($request);
  33. $this->assertSame($lambda, $controller);
  34. }
  35. public function testGetControllerWithObjectAndInvokeMethod()
  36. {
  37. $resolver = $this->createControllerResolver();
  38. $request = Request::create('/');
  39. $request->attributes->set('_controller', $this);
  40. $controller = $resolver->getController($request);
  41. $this->assertSame($this, $controller);
  42. }
  43. public function testGetControllerWithObjectAndMethod()
  44. {
  45. $resolver = $this->createControllerResolver();
  46. $request = Request::create('/');
  47. $request->attributes->set('_controller', array($this, 'controllerMethod1'));
  48. $controller = $resolver->getController($request);
  49. $this->assertSame(array($this, 'controllerMethod1'), $controller);
  50. }
  51. public function testGetControllerWithClassAndMethod()
  52. {
  53. $resolver = $this->createControllerResolver();
  54. $request = Request::create('/');
  55. $request->attributes->set('_controller', array('Symfony\Component\HttpKernel\Tests\Controller\ControllerResolverTest', 'controllerMethod4'));
  56. $controller = $resolver->getController($request);
  57. $this->assertSame(array('Symfony\Component\HttpKernel\Tests\Controller\ControllerResolverTest', 'controllerMethod4'), $controller);
  58. }
  59. public function testGetControllerWithObjectAndMethodAsString()
  60. {
  61. $resolver = $this->createControllerResolver();
  62. $request = Request::create('/');
  63. $request->attributes->set('_controller', 'Symfony\Component\HttpKernel\Tests\Controller\ControllerResolverTest::controllerMethod1');
  64. $controller = $resolver->getController($request);
  65. $this->assertInstanceOf('Symfony\Component\HttpKernel\Tests\Controller\ControllerResolverTest', $controller[0], '->getController() returns a PHP callable');
  66. }
  67. public function testGetControllerWithClassAndInvokeMethod()
  68. {
  69. $resolver = $this->createControllerResolver();
  70. $request = Request::create('/');
  71. $request->attributes->set('_controller', 'Symfony\Component\HttpKernel\Tests\Controller\ControllerResolverTest');
  72. $controller = $resolver->getController($request);
  73. $this->assertInstanceOf('Symfony\Component\HttpKernel\Tests\Controller\ControllerResolverTest', $controller);
  74. }
  75. /**
  76. * @expectedException \InvalidArgumentException
  77. */
  78. public function testGetControllerOnObjectWithoutInvokeMethod()
  79. {
  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. /**
  94. * @dataProvider getUndefinedControllers
  95. */
  96. public function testGetControllerOnNonUndefinedFunction($controller, $exceptionName = null, $exceptionMessage = null)
  97. {
  98. $resolver = $this->createControllerResolver();
  99. if (method_exists($this, 'expectException')) {
  100. $this->expectException($exceptionName);
  101. $this->expectExceptionMessage($exceptionMessage);
  102. } else {
  103. $this->setExpectedException($exceptionName, $exceptionMessage);
  104. }
  105. $request = Request::create('/');
  106. $request->attributes->set('_controller', $controller);
  107. $resolver->getController($request);
  108. }
  109. public function getUndefinedControllers()
  110. {
  111. return array(
  112. array(1, 'InvalidArgumentException', 'Unable to find controller "1".'),
  113. array('foo', 'InvalidArgumentException', 'Unable to find controller "foo".'),
  114. array('oof::bar', 'InvalidArgumentException', 'Class "oof" does not exist.'),
  115. array('stdClass', 'InvalidArgumentException', 'Unable to find controller "stdClass".'),
  116. array('Symfony\Component\HttpKernel\Tests\Controller\ControllerTest::staticsAction', 'InvalidArgumentException', 'The controller for URI "/" is not callable. Expected method "staticsAction" on class "Symfony\Component\HttpKernel\Tests\Controller\ControllerTest", did you mean "staticAction"?'),
  117. array('Symfony\Component\HttpKernel\Tests\Controller\ControllerTest::privateAction', 'InvalidArgumentException', 'The controller for URI "/" is not callable. Method "privateAction" on class "Symfony\Component\HttpKernel\Tests\Controller\ControllerTest" should be public and non-abstract'),
  118. array('Symfony\Component\HttpKernel\Tests\Controller\ControllerTest::protectedAction', 'InvalidArgumentException', 'The controller for URI "/" is not callable. Method "protectedAction" on class "Symfony\Component\HttpKernel\Tests\Controller\ControllerTest" should be public and non-abstract'),
  119. array('Symfony\Component\HttpKernel\Tests\Controller\ControllerTest::undefinedAction', 'InvalidArgumentException', 'The controller for URI "/" is not callable. Expected method "undefinedAction" on class "Symfony\Component\HttpKernel\Tests\Controller\ControllerTest". Available methods: "publicAction", "staticAction"'),
  120. );
  121. }
  122. /**
  123. * @group legacy
  124. */
  125. public function testGetArguments()
  126. {
  127. $resolver = $this->createControllerResolver();
  128. $request = Request::create('/');
  129. $controller = array(new self(), 'testGetArguments');
  130. $this->assertEquals(array(), $resolver->getArguments($request, $controller), '->getArguments() returns an empty array if the method takes no arguments');
  131. $request = Request::create('/');
  132. $request->attributes->set('foo', 'foo');
  133. $controller = array(new self(), 'controllerMethod1');
  134. $this->assertEquals(array('foo'), $resolver->getArguments($request, $controller), '->getArguments() returns an array of arguments for the controller method');
  135. $request = Request::create('/');
  136. $request->attributes->set('foo', 'foo');
  137. $controller = array(new self(), 'controllerMethod2');
  138. $this->assertEquals(array('foo', null), $resolver->getArguments($request, $controller), '->getArguments() uses default values if present');
  139. $request->attributes->set('bar', 'bar');
  140. $this->assertEquals(array('foo', 'bar'), $resolver->getArguments($request, $controller), '->getArguments() overrides default values if provided in the request attributes');
  141. $request = Request::create('/');
  142. $request->attributes->set('foo', 'foo');
  143. $controller = function ($foo) {};
  144. $this->assertEquals(array('foo'), $resolver->getArguments($request, $controller));
  145. $request = Request::create('/');
  146. $request->attributes->set('foo', 'foo');
  147. $controller = function ($foo, $bar = 'bar') {};
  148. $this->assertEquals(array('foo', 'bar'), $resolver->getArguments($request, $controller));
  149. $request = Request::create('/');
  150. $request->attributes->set('foo', 'foo');
  151. $controller = new self();
  152. $this->assertEquals(array('foo', null), $resolver->getArguments($request, $controller));
  153. $request->attributes->set('bar', 'bar');
  154. $this->assertEquals(array('foo', 'bar'), $resolver->getArguments($request, $controller));
  155. $request = Request::create('/');
  156. $request->attributes->set('foo', 'foo');
  157. $request->attributes->set('foobar', 'foobar');
  158. $controller = 'Symfony\Component\HttpKernel\Tests\Controller\some_controller_function';
  159. $this->assertEquals(array('foo', 'foobar'), $resolver->getArguments($request, $controller));
  160. $request = Request::create('/');
  161. $request->attributes->set('foo', 'foo');
  162. $request->attributes->set('foobar', 'foobar');
  163. $controller = array(new self(), 'controllerMethod3');
  164. try {
  165. $resolver->getArguments($request, $controller);
  166. $this->fail('->getArguments() throws a \RuntimeException exception if it cannot determine the argument value');
  167. } catch (\Exception $e) {
  168. $this->assertInstanceOf('\RuntimeException', $e, '->getArguments() throws a \RuntimeException exception if it cannot determine the argument value');
  169. }
  170. $request = Request::create('/');
  171. $controller = array(new self(), 'controllerMethod5');
  172. $this->assertEquals(array($request), $resolver->getArguments($request, $controller), '->getArguments() injects the request');
  173. }
  174. /**
  175. * @requires PHP 5.6
  176. * @group legacy
  177. */
  178. public function testGetVariadicArguments()
  179. {
  180. $resolver = new ControllerResolver();
  181. $request = Request::create('/');
  182. $request->attributes->set('foo', 'foo');
  183. $request->attributes->set('bar', array('foo', 'bar'));
  184. $controller = array(new VariadicController(), 'action');
  185. $this->assertEquals(array('foo', 'foo', 'bar'), $resolver->getArguments($request, $controller));
  186. }
  187. public function testCreateControllerCanReturnAnyCallable()
  188. {
  189. $mock = $this->getMockBuilder('Symfony\Component\HttpKernel\Controller\ControllerResolver')->setMethods(array('createController'))->getMock();
  190. $mock->expects($this->once())->method('createController')->will($this->returnValue('Symfony\Component\HttpKernel\Tests\Controller\some_controller_function'));
  191. $request = Request::create('/');
  192. $request->attributes->set('_controller', 'foobar');
  193. $mock->getController($request);
  194. }
  195. /**
  196. * @expectedException \RuntimeException
  197. * @group legacy
  198. */
  199. public function testIfExceptionIsThrownWhenMissingAnArgument()
  200. {
  201. $resolver = new ControllerResolver();
  202. $request = Request::create('/');
  203. $controller = array($this, 'controllerMethod1');
  204. $resolver->getArguments($request, $controller);
  205. }
  206. /**
  207. * @requires PHP 7.1
  208. * @group legacy
  209. */
  210. public function testGetNullableArguments()
  211. {
  212. $resolver = new ControllerResolver();
  213. $request = Request::create('/');
  214. $request->attributes->set('foo', 'foo');
  215. $request->attributes->set('bar', new \stdClass());
  216. $request->attributes->set('mandatory', 'mandatory');
  217. $controller = array(new NullableController(), 'action');
  218. $this->assertEquals(array('foo', new \stdClass(), 'value', 'mandatory'), $resolver->getArguments($request, $controller));
  219. }
  220. /**
  221. * @requires PHP 7.1
  222. * @group legacy
  223. */
  224. public function testGetNullableArgumentsWithDefaults()
  225. {
  226. $resolver = new ControllerResolver();
  227. $request = Request::create('/');
  228. $request->attributes->set('mandatory', 'mandatory');
  229. $controller = array(new NullableController(), 'action');
  230. $this->assertEquals(array(null, null, 'value', 'mandatory'), $resolver->getArguments($request, $controller));
  231. }
  232. protected function createControllerResolver(LoggerInterface $logger = null)
  233. {
  234. return new ControllerResolver($logger);
  235. }
  236. public function __invoke($foo, $bar = null)
  237. {
  238. }
  239. public function controllerMethod1($foo)
  240. {
  241. }
  242. protected function controllerMethod2($foo, $bar = null)
  243. {
  244. }
  245. protected function controllerMethod3($foo, $bar, $foobar)
  246. {
  247. }
  248. protected static function controllerMethod4()
  249. {
  250. }
  251. protected function controllerMethod5(Request $request)
  252. {
  253. }
  254. }
  255. function some_controller_function($foo, $foobar)
  256. {
  257. }
  258. class ControllerTest
  259. {
  260. public function publicAction()
  261. {
  262. }
  263. private function privateAction()
  264. {
  265. }
  266. protected function protectedAction()
  267. {
  268. }
  269. public static function staticAction()
  270. {
  271. }
  272. }