Nav apraksta

ControllerResolverTest.php 13KB

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