5 * Contains \Drupal\Tests\Component\Utility\ArgumentsResolverTest.
8 namespace Drupal\Tests\Component\Utility;
10 use Drupal\Component\Utility\ArgumentsResolver;
11 use PHPUnit\Framework\TestCase;
14 * @coversDefaultClass \Drupal\Component\Utility\ArgumentsResolver
17 class ArgumentsResolverTest extends TestCase {
22 protected function setUp() {
27 * Tests the getArgument() method.
29 * @dataProvider providerTestGetArgument
31 public function testGetArgument($callable, $scalars, $objects, $wildcards, $expected) {
32 $arguments = (new ArgumentsResolver($scalars, $objects, $wildcards))->getArguments($callable);
33 $this->assertSame($expected, $arguments);
37 * Provides test data to testGetArgument().
39 public function providerTestGetArgument() {
42 // Test an optional parameter with no provided value.
44 function ($foo = 'foo') {}, [], [], [] , ['foo'],
47 // Test an optional parameter with a provided value.
49 function ($foo = 'foo') {}, ['foo' => 'bar'], [], [], ['bar'],
52 // Test with a provided value.
54 function ($foo) {}, ['foo' => 'bar'], [], [], ['bar'],
57 // Test with an explicitly NULL value.
59 function ($foo) {}, [], ['foo' => NULL], [], [NULL],
62 // Test with a raw value that overrides the provided upcast value, since
63 // it is not typehinted.
64 $scalars = ['foo' => 'baz'];
65 $objects = ['foo' => new \stdClass()];
67 function ($foo) {}, $scalars, $objects, [], ['baz'],
74 * Tests getArgument() with an object.
76 public function testGetArgumentObject() {
77 $callable = function (\stdClass $object) {};
79 $object = new \stdClass();
80 $arguments = (new ArgumentsResolver([], ['object' => $object], []))->getArguments($callable);
81 $this->assertSame([$object], $arguments);
85 * Tests getArgument() with a wildcard object for a parameter with a custom name.
87 public function testGetWildcardArgument() {
88 $callable = function (\stdClass $custom_name) {};
90 $object = new \stdClass();
91 $arguments = (new ArgumentsResolver([], [], [$object]))->getArguments($callable);
92 $this->assertSame([$object], $arguments);
96 * Tests getArgument() with a Route, Request, and Account object.
98 public function testGetArgumentOrder() {
99 $a1 = $this->getMockBuilder('\Drupal\Tests\Component\Utility\Test1Interface')->getMock();
100 $a2 = $this->getMockBuilder('\Drupal\Tests\Component\Utility\TestClass')->getMock();
101 $a3 = $this->getMockBuilder('\Drupal\Tests\Component\Utility\Test2Interface')->getMock();
108 $resolver = new ArgumentsResolver([], $objects, $wildcards);
110 $callable = function (Test1Interface $t1, TestClass $tc, Test2Interface $t2) {};
111 $arguments = $resolver->getArguments($callable);
112 $this->assertSame([$a1, $a2, $a3], $arguments);
114 // Test again, but with the arguments in a different order.
115 $callable = function (Test2Interface $t2, TestClass $tc, Test1Interface $t1) {};
116 $arguments = $resolver->getArguments($callable);
117 $this->assertSame([$a3, $a2, $a1], $arguments);
121 * Tests getArgument() with a wildcard parameter with no typehint.
123 * Without the typehint, the wildcard object will not be passed to the callable.
125 public function testGetWildcardArgumentNoTypehint() {
126 $a = $this->getMockBuilder('\Drupal\Tests\Component\Utility\Test1Interface')->getMock();
128 $resolver = new ArgumentsResolver([], [], $wildcards);
130 $callable = function ($route) {};
131 if (method_exists($this, 'expectException')) {
132 $this->expectException(\RuntimeException::class);
133 $this->expectExceptionMessage('requires a value for the "$route" argument.');
136 $this->setExpectedException(\RuntimeException::class, 'requires a value for the "$route" argument.');
138 $resolver->getArguments($callable);
142 * Tests getArgument() with a named parameter with no typehint and a value.
144 * Without the typehint, passing a value to a named parameter will still
145 * receive the provided value.
147 public function testGetArgumentRouteNoTypehintAndValue() {
148 $scalars = ['route' => 'foo'];
149 $resolver = new ArgumentsResolver($scalars, [], []);
151 $callable = function ($route) {};
152 $arguments = $resolver->getArguments($callable);
153 $this->assertSame(['foo'], $arguments);
157 * Tests handleUnresolvedArgument() for a scalar argument.
159 public function testHandleNotUpcastedArgument() {
160 $objects = ['foo' => 'bar'];
161 $scalars = ['foo' => 'baz'];
162 $resolver = new ArgumentsResolver($scalars, $objects, []);
164 $callable = function (\stdClass $foo) {};
165 if (method_exists($this, 'expectException')) {
166 $this->expectException(\RuntimeException::class);
167 $this->expectExceptionMessage('requires a value for the "$foo" argument.');
170 $this->setExpectedException(\RuntimeException::class, 'requires a value for the "$foo" argument.');
172 $resolver->getArguments($callable);
176 * Tests handleUnresolvedArgument() for missing arguments.
178 * @dataProvider providerTestHandleUnresolvedArgument
180 public function testHandleUnresolvedArgument($callable) {
181 $resolver = new ArgumentsResolver([], [], []);
182 if (method_exists($this, 'expectException')) {
183 $this->expectException(\RuntimeException::class);
184 $this->expectExceptionMessage('requires a value for the "$foo" argument.');
187 $this->setExpectedException(\RuntimeException::class, 'requires a value for the "$foo" argument.');
189 $resolver->getArguments($callable);
193 * Provides test data to testHandleUnresolvedArgument().
195 public function providerTestHandleUnresolvedArgument() {
197 $data[] = [function ($foo) {}];
198 $data[] = [[new TestClass(), 'access']];
199 $data[] = ['Drupal\Tests\Component\Utility\test_access_arguments_resolver_access'];
206 * Provides a test class.
210 public function access($foo) {
216 * Provides a test interface.
218 interface Test1Interface {
222 * Provides a different test interface.
224 interface Test2Interface {
227 function test_access_arguments_resolver_access($foo) {