ControllerResolverTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. use Symfony\Component\HttpKernel\Tests\Fixtures\Controller\NullableController;
  16. use Symfony\Component\HttpKernel\Tests\Fixtures\Controller\VariadicController;
  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. * @expectedException \InvalidArgumentException
  96. */
  97. public function testGetControllerOnNonUndefinedFunction($controller)
  98. {
  99. $resolver = $this->createControllerResolver();
  100. $request = Request::create('/');
  101. $request->attributes->set('_controller', $controller);
  102. $resolver->getController($request);
  103. }
  104. public function getUndefinedControllers()
  105. {
  106. return array(
  107. array('foo'),
  108. array('oof::bar'),
  109. array('stdClass'),
  110. array('Symfony\Component\HttpKernel\Tests\Controller\ControllerResolverTest::bar'),
  111. );
  112. }
  113. public function testGetArguments()
  114. {
  115. $resolver = $this->createControllerResolver();
  116. $request = Request::create('/');
  117. $controller = array(new self(), 'testGetArguments');
  118. $this->assertEquals(array(), $resolver->getArguments($request, $controller), '->getArguments() returns an empty array if the method takes no arguments');
  119. $request = Request::create('/');
  120. $request->attributes->set('foo', 'foo');
  121. $controller = array(new self(), 'controllerMethod1');
  122. $this->assertEquals(array('foo'), $resolver->getArguments($request, $controller), '->getArguments() returns an array of arguments for the controller method');
  123. $request = Request::create('/');
  124. $request->attributes->set('foo', 'foo');
  125. $controller = array(new self(), 'controllerMethod2');
  126. $this->assertEquals(array('foo', null), $resolver->getArguments($request, $controller), '->getArguments() uses default values if present');
  127. $request->attributes->set('bar', 'bar');
  128. $this->assertEquals(array('foo', 'bar'), $resolver->getArguments($request, $controller), '->getArguments() overrides default values if provided in the request attributes');
  129. $request = Request::create('/');
  130. $request->attributes->set('foo', 'foo');
  131. $controller = function ($foo) {};
  132. $this->assertEquals(array('foo'), $resolver->getArguments($request, $controller));
  133. $request = Request::create('/');
  134. $request->attributes->set('foo', 'foo');
  135. $controller = function ($foo, $bar = 'bar') {};
  136. $this->assertEquals(array('foo', 'bar'), $resolver->getArguments($request, $controller));
  137. $request = Request::create('/');
  138. $request->attributes->set('foo', 'foo');
  139. $controller = new self();
  140. $this->assertEquals(array('foo', null), $resolver->getArguments($request, $controller));
  141. $request->attributes->set('bar', 'bar');
  142. $this->assertEquals(array('foo', 'bar'), $resolver->getArguments($request, $controller));
  143. $request = Request::create('/');
  144. $request->attributes->set('foo', 'foo');
  145. $request->attributes->set('foobar', 'foobar');
  146. $controller = 'Symfony\Component\HttpKernel\Tests\Controller\some_controller_function';
  147. $this->assertEquals(array('foo', 'foobar'), $resolver->getArguments($request, $controller));
  148. $request = Request::create('/');
  149. $request->attributes->set('foo', 'foo');
  150. $request->attributes->set('foobar', 'foobar');
  151. $controller = array(new self(), 'controllerMethod3');
  152. if (\PHP_VERSION_ID === 50316) {
  153. $this->markTestSkipped('PHP 5.3.16 has a major bug in the Reflection sub-system');
  154. } else {
  155. try {
  156. $resolver->getArguments($request, $controller);
  157. $this->fail('->getArguments() throws a \RuntimeException exception if it cannot determine the argument value');
  158. } catch (\Exception $e) {
  159. $this->assertInstanceOf('\RuntimeException', $e, '->getArguments() throws a \RuntimeException exception if it cannot determine the argument value');
  160. }
  161. }
  162. $request = Request::create('/');
  163. $controller = array(new self(), 'controllerMethod5');
  164. $this->assertEquals(array($request), $resolver->getArguments($request, $controller), '->getArguments() injects the request');
  165. }
  166. /**
  167. * @requires PHP 5.6
  168. */
  169. public function testGetVariadicArguments()
  170. {
  171. $resolver = new ControllerResolver();
  172. $request = Request::create('/');
  173. $request->attributes->set('foo', 'foo');
  174. $request->attributes->set('bar', array('foo', 'bar'));
  175. $controller = array(new VariadicController(), 'action');
  176. $this->assertEquals(array('foo', 'foo', 'bar'), $resolver->getArguments($request, $controller));
  177. }
  178. public function testCreateControllerCanReturnAnyCallable()
  179. {
  180. $mock = $this->getMockBuilder('Symfony\Component\HttpKernel\Controller\ControllerResolver')->setMethods(array('createController'))->getMock();
  181. $mock->expects($this->once())->method('createController')->will($this->returnValue('Symfony\Component\HttpKernel\Tests\Controller\some_controller_function'));
  182. $request = Request::create('/');
  183. $request->attributes->set('_controller', 'foobar');
  184. $mock->getController($request);
  185. }
  186. /**
  187. * @expectedException \RuntimeException
  188. */
  189. public function testIfExceptionIsThrownWhenMissingAnArgument()
  190. {
  191. $resolver = new ControllerResolver();
  192. $request = Request::create('/');
  193. $controller = array($this, 'controllerMethod1');
  194. $resolver->getArguments($request, $controller);
  195. }
  196. /**
  197. * @requires PHP 7.1
  198. */
  199. public function testGetNullableArguments()
  200. {
  201. $resolver = new ControllerResolver();
  202. $request = Request::create('/');
  203. $request->attributes->set('foo', 'foo');
  204. $request->attributes->set('bar', new \stdClass());
  205. $request->attributes->set('mandatory', 'mandatory');
  206. $controller = array(new NullableController(), 'action');
  207. $this->assertEquals(array('foo', new \stdClass(), 'value', 'mandatory'), $resolver->getArguments($request, $controller));
  208. }
  209. /**
  210. * @requires PHP 7.1
  211. */
  212. public function testGetNullableArgumentsWithDefaults()
  213. {
  214. $resolver = new ControllerResolver();
  215. $request = Request::create('/');
  216. $request->attributes->set('mandatory', 'mandatory');
  217. $controller = array(new NullableController(), 'action');
  218. $this->assertEquals(array(null, null, 'value', 'mandatory'), $resolver->getArguments($request, $controller));
  219. }
  220. protected function createControllerResolver(LoggerInterface $logger = null)
  221. {
  222. return new ControllerResolver($logger);
  223. }
  224. public function __invoke($foo, $bar = null)
  225. {
  226. }
  227. public function controllerMethod1($foo)
  228. {
  229. }
  230. protected function controllerMethod2($foo, $bar = null)
  231. {
  232. }
  233. protected function controllerMethod3($foo, $bar, $foobar)
  234. {
  235. }
  236. protected static function controllerMethod4()
  237. {
  238. }
  239. protected function controllerMethod5(Request $request)
  240. {
  241. }
  242. }
  243. function some_controller_function($foo, $foobar)
  244. {
  245. }