ArgumentResolverTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Session\Session;
  14. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  15. use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
  16. use Symfony\Component\HttpKernel\Controller\ArgumentResolver;
  17. use Symfony\Component\HttpKernel\Controller\ArgumentResolver\RequestAttributeValueResolver;
  18. use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface;
  19. use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadataFactory;
  20. use Symfony\Component\HttpKernel\Tests\Fixtures\Controller\ExtendingRequest;
  21. use Symfony\Component\HttpKernel\Tests\Fixtures\Controller\ExtendingSession;
  22. use Symfony\Component\HttpKernel\Tests\Fixtures\Controller\NullableController;
  23. use Symfony\Component\HttpKernel\Tests\Fixtures\Controller\VariadicController;
  24. class ArgumentResolverTest extends TestCase
  25. {
  26. /** @var ArgumentResolver */
  27. private static $resolver;
  28. public static function setUpBeforeClass()
  29. {
  30. $factory = new ArgumentMetadataFactory();
  31. self::$resolver = new ArgumentResolver($factory);
  32. }
  33. public function testDefaultState()
  34. {
  35. $this->assertEquals(self::$resolver, new ArgumentResolver());
  36. $this->assertNotEquals(self::$resolver, new ArgumentResolver(null, [new RequestAttributeValueResolver()]));
  37. }
  38. public function testGetArguments()
  39. {
  40. $request = Request::create('/');
  41. $request->attributes->set('foo', 'foo');
  42. $controller = [new self(), 'controllerWithFoo'];
  43. $this->assertEquals(['foo'], self::$resolver->getArguments($request, $controller), '->getArguments() returns an array of arguments for the controller method');
  44. }
  45. public function testGetArgumentsReturnsEmptyArrayWhenNoArguments()
  46. {
  47. $request = Request::create('/');
  48. $controller = [new self(), 'controllerWithoutArguments'];
  49. $this->assertEquals([], self::$resolver->getArguments($request, $controller), '->getArguments() returns an empty array if the method takes no arguments');
  50. }
  51. public function testGetArgumentsUsesDefaultValue()
  52. {
  53. $request = Request::create('/');
  54. $request->attributes->set('foo', 'foo');
  55. $controller = [new self(), 'controllerWithFooAndDefaultBar'];
  56. $this->assertEquals(['foo', null], self::$resolver->getArguments($request, $controller), '->getArguments() uses default values if present');
  57. }
  58. public function testGetArgumentsOverrideDefaultValueByRequestAttribute()
  59. {
  60. $request = Request::create('/');
  61. $request->attributes->set('foo', 'foo');
  62. $request->attributes->set('bar', 'bar');
  63. $controller = [new self(), 'controllerWithFooAndDefaultBar'];
  64. $this->assertEquals(['foo', 'bar'], self::$resolver->getArguments($request, $controller), '->getArguments() overrides default values if provided in the request attributes');
  65. }
  66. public function testGetArgumentsFromClosure()
  67. {
  68. $request = Request::create('/');
  69. $request->attributes->set('foo', 'foo');
  70. $controller = function ($foo) {};
  71. $this->assertEquals(['foo'], self::$resolver->getArguments($request, $controller));
  72. }
  73. public function testGetArgumentsUsesDefaultValueFromClosure()
  74. {
  75. $request = Request::create('/');
  76. $request->attributes->set('foo', 'foo');
  77. $controller = function ($foo, $bar = 'bar') {};
  78. $this->assertEquals(['foo', 'bar'], self::$resolver->getArguments($request, $controller));
  79. }
  80. public function testGetArgumentsFromInvokableObject()
  81. {
  82. $request = Request::create('/');
  83. $request->attributes->set('foo', 'foo');
  84. $controller = new self();
  85. $this->assertEquals(['foo', null], self::$resolver->getArguments($request, $controller));
  86. // Test default bar overridden by request attribute
  87. $request->attributes->set('bar', 'bar');
  88. $this->assertEquals(['foo', 'bar'], self::$resolver->getArguments($request, $controller));
  89. }
  90. public function testGetArgumentsFromFunctionName()
  91. {
  92. $request = Request::create('/');
  93. $request->attributes->set('foo', 'foo');
  94. $request->attributes->set('foobar', 'foobar');
  95. $controller = __NAMESPACE__.'\controller_function';
  96. $this->assertEquals(['foo', 'foobar'], self::$resolver->getArguments($request, $controller));
  97. }
  98. public function testGetArgumentsFailsOnUnresolvedValue()
  99. {
  100. $request = Request::create('/');
  101. $request->attributes->set('foo', 'foo');
  102. $request->attributes->set('foobar', 'foobar');
  103. $controller = [new self(), 'controllerWithFooBarFoobar'];
  104. try {
  105. self::$resolver->getArguments($request, $controller);
  106. $this->fail('->getArguments() throws a \RuntimeException exception if it cannot determine the argument value');
  107. } catch (\Exception $e) {
  108. $this->assertInstanceOf('\RuntimeException', $e, '->getArguments() throws a \RuntimeException exception if it cannot determine the argument value');
  109. }
  110. }
  111. public function testGetArgumentsInjectsRequest()
  112. {
  113. $request = Request::create('/');
  114. $controller = [new self(), 'controllerWithRequest'];
  115. $this->assertEquals([$request], self::$resolver->getArguments($request, $controller), '->getArguments() injects the request');
  116. }
  117. public function testGetArgumentsInjectsExtendingRequest()
  118. {
  119. $request = ExtendingRequest::create('/');
  120. $controller = [new self(), 'controllerWithExtendingRequest'];
  121. $this->assertEquals([$request], self::$resolver->getArguments($request, $controller), '->getArguments() injects the request when extended');
  122. }
  123. public function testGetVariadicArguments()
  124. {
  125. $request = Request::create('/');
  126. $request->attributes->set('foo', 'foo');
  127. $request->attributes->set('bar', ['foo', 'bar']);
  128. $controller = [new VariadicController(), 'action'];
  129. $this->assertEquals(['foo', 'foo', 'bar'], self::$resolver->getArguments($request, $controller));
  130. }
  131. /**
  132. * @expectedException \InvalidArgumentException
  133. */
  134. public function testGetVariadicArgumentsWithoutArrayInRequest()
  135. {
  136. $request = Request::create('/');
  137. $request->attributes->set('foo', 'foo');
  138. $request->attributes->set('bar', 'foo');
  139. $controller = [new VariadicController(), 'action'];
  140. self::$resolver->getArguments($request, $controller);
  141. }
  142. /**
  143. * @expectedException \InvalidArgumentException
  144. */
  145. public function testGetArgumentWithoutArray()
  146. {
  147. $factory = new ArgumentMetadataFactory();
  148. $valueResolver = $this->getMockBuilder(ArgumentValueResolverInterface::class)->getMock();
  149. $resolver = new ArgumentResolver($factory, [$valueResolver]);
  150. $valueResolver->expects($this->any())->method('supports')->willReturn(true);
  151. $valueResolver->expects($this->any())->method('resolve')->willReturn('foo');
  152. $request = Request::create('/');
  153. $request->attributes->set('foo', 'foo');
  154. $request->attributes->set('bar', 'foo');
  155. $controller = [$this, 'controllerWithFooAndDefaultBar'];
  156. $resolver->getArguments($request, $controller);
  157. }
  158. /**
  159. * @expectedException \RuntimeException
  160. */
  161. public function testIfExceptionIsThrownWhenMissingAnArgument()
  162. {
  163. $request = Request::create('/');
  164. $controller = [$this, 'controllerWithFoo'];
  165. self::$resolver->getArguments($request, $controller);
  166. }
  167. public function testGetNullableArguments()
  168. {
  169. $request = Request::create('/');
  170. $request->attributes->set('foo', 'foo');
  171. $request->attributes->set('bar', new \stdClass());
  172. $request->attributes->set('mandatory', 'mandatory');
  173. $controller = [new NullableController(), 'action'];
  174. $this->assertEquals(['foo', new \stdClass(), 'value', 'mandatory'], self::$resolver->getArguments($request, $controller));
  175. }
  176. public function testGetNullableArgumentsWithDefaults()
  177. {
  178. $request = Request::create('/');
  179. $request->attributes->set('mandatory', 'mandatory');
  180. $controller = [new NullableController(), 'action'];
  181. $this->assertEquals([null, null, 'value', 'mandatory'], self::$resolver->getArguments($request, $controller));
  182. }
  183. public function testGetSessionArguments()
  184. {
  185. $session = new Session(new MockArraySessionStorage());
  186. $request = Request::create('/');
  187. $request->setSession($session);
  188. $controller = [$this, 'controllerWithSession'];
  189. $this->assertEquals([$session], self::$resolver->getArguments($request, $controller));
  190. }
  191. public function testGetSessionArgumentsWithExtendedSession()
  192. {
  193. $session = new ExtendingSession(new MockArraySessionStorage());
  194. $request = Request::create('/');
  195. $request->setSession($session);
  196. $controller = [$this, 'controllerWithExtendingSession'];
  197. $this->assertEquals([$session], self::$resolver->getArguments($request, $controller));
  198. }
  199. public function testGetSessionArgumentsWithInterface()
  200. {
  201. $session = $this->getMockBuilder(SessionInterface::class)->getMock();
  202. $request = Request::create('/');
  203. $request->setSession($session);
  204. $controller = [$this, 'controllerWithSessionInterface'];
  205. $this->assertEquals([$session], self::$resolver->getArguments($request, $controller));
  206. }
  207. /**
  208. * @expectedException \RuntimeException
  209. */
  210. public function testGetSessionMissMatchWithInterface()
  211. {
  212. $session = $this->getMockBuilder(SessionInterface::class)->getMock();
  213. $request = Request::create('/');
  214. $request->setSession($session);
  215. $controller = [$this, 'controllerWithExtendingSession'];
  216. self::$resolver->getArguments($request, $controller);
  217. }
  218. /**
  219. * @expectedException \RuntimeException
  220. */
  221. public function testGetSessionMissMatchWithImplementation()
  222. {
  223. $session = new Session(new MockArraySessionStorage());
  224. $request = Request::create('/');
  225. $request->setSession($session);
  226. $controller = [$this, 'controllerWithExtendingSession'];
  227. self::$resolver->getArguments($request, $controller);
  228. }
  229. /**
  230. * @expectedException \RuntimeException
  231. */
  232. public function testGetSessionMissMatchOnNull()
  233. {
  234. $request = Request::create('/');
  235. $controller = [$this, 'controllerWithExtendingSession'];
  236. self::$resolver->getArguments($request, $controller);
  237. }
  238. public function __invoke($foo, $bar = null)
  239. {
  240. }
  241. public function controllerWithFoo($foo)
  242. {
  243. }
  244. public function controllerWithoutArguments()
  245. {
  246. }
  247. protected function controllerWithFooAndDefaultBar($foo, $bar = null)
  248. {
  249. }
  250. protected function controllerWithFooBarFoobar($foo, $bar, $foobar)
  251. {
  252. }
  253. protected function controllerWithRequest(Request $request)
  254. {
  255. }
  256. protected function controllerWithExtendingRequest(ExtendingRequest $request)
  257. {
  258. }
  259. protected function controllerWithSession(Session $session)
  260. {
  261. }
  262. protected function controllerWithSessionInterface(SessionInterface $session)
  263. {
  264. }
  265. protected function controllerWithExtendingSession(ExtendingSession $session)
  266. {
  267. }
  268. }
  269. function controller_function($foo, $foobar)
  270. {
  271. }