ArgumentResolverTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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\Session\Session;
  13. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  14. use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
  15. use Symfony\Component\HttpKernel\Controller\ArgumentResolver;
  16. use Symfony\Component\HttpKernel\Controller\ArgumentResolver\RequestAttributeValueResolver;
  17. use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface;
  18. use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadataFactory;
  19. use Symfony\Component\HttpKernel\Tests\Fixtures\Controller\ExtendingRequest;
  20. use Symfony\Component\HttpKernel\Tests\Fixtures\Controller\ExtendingSession;
  21. use Symfony\Component\HttpKernel\Tests\Fixtures\Controller\NullableController;
  22. use Symfony\Component\HttpKernel\Tests\Fixtures\Controller\VariadicController;
  23. use Symfony\Component\HttpFoundation\Request;
  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, array(new RequestAttributeValueResolver())));
  37. }
  38. public function testGetArguments()
  39. {
  40. $request = Request::create('/');
  41. $request->attributes->set('foo', 'foo');
  42. $controller = array(new self(), 'controllerWithFoo');
  43. $this->assertEquals(array('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 = array(new self(), 'controllerWithoutArguments');
  49. $this->assertEquals(array(), 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 = array(new self(), 'controllerWithFooAndDefaultBar');
  56. $this->assertEquals(array('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 = array(new self(), 'controllerWithFooAndDefaultBar');
  64. $this->assertEquals(array('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(array('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(array('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(array('foo', null), self::$resolver->getArguments($request, $controller));
  86. // Test default bar overridden by request attribute
  87. $request->attributes->set('bar', 'bar');
  88. $this->assertEquals(array('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(array('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 = array(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 = array(new self(), 'controllerWithRequest');
  115. $this->assertEquals(array($request), self::$resolver->getArguments($request, $controller), '->getArguments() injects the request');
  116. }
  117. public function testGetArgumentsInjectsExtendingRequest()
  118. {
  119. $request = ExtendingRequest::create('/');
  120. $controller = array(new self(), 'controllerWithExtendingRequest');
  121. $this->assertEquals(array($request), self::$resolver->getArguments($request, $controller), '->getArguments() injects the request when extended');
  122. }
  123. /**
  124. * @requires PHP 5.6
  125. */
  126. public function testGetVariadicArguments()
  127. {
  128. $request = Request::create('/');
  129. $request->attributes->set('foo', 'foo');
  130. $request->attributes->set('bar', array('foo', 'bar'));
  131. $controller = array(new VariadicController(), 'action');
  132. $this->assertEquals(array('foo', 'foo', 'bar'), self::$resolver->getArguments($request, $controller));
  133. }
  134. /**
  135. * @requires PHP 5.6
  136. * @expectedException \InvalidArgumentException
  137. */
  138. public function testGetVariadicArgumentsWithoutArrayInRequest()
  139. {
  140. $request = Request::create('/');
  141. $request->attributes->set('foo', 'foo');
  142. $request->attributes->set('bar', 'foo');
  143. $controller = array(new VariadicController(), 'action');
  144. self::$resolver->getArguments($request, $controller);
  145. }
  146. /**
  147. * @requires PHP 5.6
  148. * @expectedException \InvalidArgumentException
  149. */
  150. public function testGetArgumentWithoutArray()
  151. {
  152. $factory = new ArgumentMetadataFactory();
  153. $valueResolver = $this->getMockBuilder(ArgumentValueResolverInterface::class)->getMock();
  154. $resolver = new ArgumentResolver($factory, array($valueResolver));
  155. $valueResolver->expects($this->any())->method('supports')->willReturn(true);
  156. $valueResolver->expects($this->any())->method('resolve')->willReturn('foo');
  157. $request = Request::create('/');
  158. $request->attributes->set('foo', 'foo');
  159. $request->attributes->set('bar', 'foo');
  160. $controller = array($this, 'controllerWithFooAndDefaultBar');
  161. $resolver->getArguments($request, $controller);
  162. }
  163. /**
  164. * @expectedException \RuntimeException
  165. */
  166. public function testIfExceptionIsThrownWhenMissingAnArgument()
  167. {
  168. $request = Request::create('/');
  169. $controller = array($this, 'controllerWithFoo');
  170. self::$resolver->getArguments($request, $controller);
  171. }
  172. /**
  173. * @requires PHP 7.1
  174. */
  175. public function testGetNullableArguments()
  176. {
  177. $request = Request::create('/');
  178. $request->attributes->set('foo', 'foo');
  179. $request->attributes->set('bar', new \stdClass());
  180. $request->attributes->set('mandatory', 'mandatory');
  181. $controller = array(new NullableController(), 'action');
  182. $this->assertEquals(array('foo', new \stdClass(), 'value', 'mandatory'), self::$resolver->getArguments($request, $controller));
  183. }
  184. /**
  185. * @requires PHP 7.1
  186. */
  187. public function testGetNullableArgumentsWithDefaults()
  188. {
  189. $request = Request::create('/');
  190. $request->attributes->set('mandatory', 'mandatory');
  191. $controller = array(new NullableController(), 'action');
  192. $this->assertEquals(array(null, null, 'value', 'mandatory'), self::$resolver->getArguments($request, $controller));
  193. }
  194. public function testGetSessionArguments()
  195. {
  196. $session = new Session(new MockArraySessionStorage());
  197. $request = Request::create('/');
  198. $request->setSession($session);
  199. $controller = array($this, 'controllerWithSession');
  200. $this->assertEquals(array($session), self::$resolver->getArguments($request, $controller));
  201. }
  202. public function testGetSessionArgumentsWithExtendedSession()
  203. {
  204. $session = new ExtendingSession(new MockArraySessionStorage());
  205. $request = Request::create('/');
  206. $request->setSession($session);
  207. $controller = array($this, 'controllerWithExtendingSession');
  208. $this->assertEquals(array($session), self::$resolver->getArguments($request, $controller));
  209. }
  210. public function testGetSessionArgumentsWithInterface()
  211. {
  212. $session = $this->getMockBuilder(SessionInterface::class)->getMock();
  213. $request = Request::create('/');
  214. $request->setSession($session);
  215. $controller = array($this, 'controllerWithSessionInterface');
  216. $this->assertEquals(array($session), self::$resolver->getArguments($request, $controller));
  217. }
  218. /**
  219. * @expectedException \RuntimeException
  220. */
  221. public function testGetSessionMissMatchWithInterface()
  222. {
  223. $session = $this->getMockBuilder(SessionInterface::class)->getMock();
  224. $request = Request::create('/');
  225. $request->setSession($session);
  226. $controller = array($this, 'controllerWithExtendingSession');
  227. self::$resolver->getArguments($request, $controller);
  228. }
  229. /**
  230. * @expectedException \RuntimeException
  231. */
  232. public function testGetSessionMissMatchWithImplementation()
  233. {
  234. $session = new Session(new MockArraySessionStorage());
  235. $request = Request::create('/');
  236. $request->setSession($session);
  237. $controller = array($this, 'controllerWithExtendingSession');
  238. self::$resolver->getArguments($request, $controller);
  239. }
  240. /**
  241. * @expectedException \RuntimeException
  242. */
  243. public function testGetSessionMissMatchOnNull()
  244. {
  245. $request = Request::create('/');
  246. $controller = array($this, 'controllerWithExtendingSession');
  247. self::$resolver->getArguments($request, $controller);
  248. }
  249. public function __invoke($foo, $bar = null)
  250. {
  251. }
  252. public function controllerWithFoo($foo)
  253. {
  254. }
  255. public function controllerWithoutArguments()
  256. {
  257. }
  258. protected function controllerWithFooAndDefaultBar($foo, $bar = null)
  259. {
  260. }
  261. protected function controllerWithFooBarFoobar($foo, $bar, $foobar)
  262. {
  263. }
  264. protected function controllerWithRequest(Request $request)
  265. {
  266. }
  267. protected function controllerWithExtendingRequest(ExtendingRequest $request)
  268. {
  269. }
  270. protected function controllerWithSession(Session $session)
  271. {
  272. }
  273. protected function controllerWithSessionInterface(SessionInterface $session)
  274. {
  275. }
  276. protected function controllerWithExtendingSession(ExtendingSession $session)
  277. {
  278. }
  279. }
  280. function controller_function($foo, $foobar)
  281. {
  282. }