diff --git a/core/tests/Drupal/Tests/Component/Utility/ArgumentsResolverTest.php b/core/tests/Drupal/Tests/Component/Utility/ArgumentsResolverTest.php new file mode 100644 index 0000000..c786308 --- /dev/null +++ b/core/tests/Drupal/Tests/Component/Utility/ArgumentsResolverTest.php @@ -0,0 +1,221 @@ +getArguments($callable); + $this->assertSame($expected, $arguments); + } + + /** + * Provides test data to testGetArgument(). + */ + public function providerTestGetArgument() { + $data = []; + + // Test an optional parameter with no provided value. + $data[] = [ + function($foo = 'foo') {}, [], [], [] , ['foo'], + ]; + + // Test an optional parameter with a provided value. + $data[] = [ + function($foo = 'foo') {}, ['foo' => 'bar'], [], [], ['bar'], + ]; + + // Test with a provided value. + $data[] = [ + function($foo) {}, ['foo' => 'bar'], [], [], ['bar'], + ]; + + // Test with an explicitly NULL value. + $data[] = [ + function($foo) {}, [], ['foo' => NULL], [], [NULL], + ]; + + // Test with a raw value that overrides the provided upcast value, since + // it is not typehinted. + $scalars = ['foo' => 'baz']; + $objects = ['foo' => new \stdClass()]; + $data[] = [ + function($foo) {}, $scalars, $objects, [], ['baz'], + ]; + + return $data; + } + + /** + * Tests getArgument() with an object. + */ + public function testGetArgumentObject() { + $callable = function(\stdClass $object) {}; + + $object = new \stdClass(); + $arguments = (new ArgumentsResolver([], ['object' => $object], []))->getArguments($callable); + $this->assertSame([$object], $arguments); + } + + /** + * Tests getArgument() with a wildcard object for a parameter with a custom name. + */ + public function testGetWilcardArgument() { + $callable = function(\stdClass $custom_name) {}; + + $object = new \stdClass(); + $arguments = (new ArgumentsResolver([], [], [$object]))->getArguments($callable); + $this->assertSame([$object], $arguments); + } + + /** + * Tests getArgument() with a Route, Request, and Account object. + */ + public function testGetArgumentOrder() { + $a1 = $this->getMock('\Drupal\Tests\Component\Utility\TestInterface1'); + $a2 = $this->getMock('\Drupal\Tests\Component\Utility\TestClass'); + $a3 = $this->getMock('\Drupal\Tests\Component\Utility\TestInterface2'); + + $objects = [ + 't1' => $a1, + 'tc' => $a2, + ]; + $wildcards = [$a3]; + $resolver = new ArgumentsResolver([], $objects, $wildcards); + + $callable = function(TestInterface1 $t1, TestClass $tc, TestInterface2 $t2) {}; + $arguments = $resolver->getArguments($callable); + $this->assertSame([$a1, $a2, $a3], $arguments); + + // Test again, but with the arguments in a different order. + $callable = function(TestInterface2 $t2, TestClass $tc, TestInterface1 $t1) {}; + $arguments = $resolver->getArguments($callable); + $this->assertSame([$a3, $a2, $a1], $arguments); + } + + /** + * Tests getArgument() with a wildcard parameter with no typehint. + * + * Without the typehint, the wildcard object will not be passed to the callable. + * + * @expectedException \RuntimeException + * @expectedExceptionMessage requires a value for the "$route" argument. + */ + public function testGetWildcardArgumentNoTypehint() { + $a = $this->getMock('\Drupal\Tests\Component\Utility\TestInterface1'); + $wildcards = [$a]; + $resolver = new ArgumentsResolver([], [], $wildcards); + + $callable = function($route) {}; + $arguments = $resolver->getArguments($callable); + $this->assertNull($arguments); + } + + /** + * Tests getArgument() with a named parameter with no typehint and a value. + * + * Without the typehint, passing a value to a named parameter will still + * receive the provided value. + */ + public function testGetArgumentRouteNoTypehintAndValue() { + $scalars = ['route' => 'foo']; + $resolver = new ArgumentsResolver($scalars, [], []); + + $callable = function($route) {}; + $arguments = $resolver->getArguments($callable); + $this->assertSame(['foo'], $arguments); + } + + /** + * Tests handleUnresolvedArgument() for a scalar argument. + * + * @expectedException \RuntimeException + * @expectedExceptionMessage requires a value for the "$foo" argument. + */ + public function testHandleNotUpcastedArgument() { + $objects = ['foo' => 'bar']; + $scalars = ['foo' => 'baz']; + $resolver = new ArgumentsResolver($scalars, $objects, []); + + $callable = function(\stdClass $foo) {}; + $arguments = $resolver->getArguments($callable); + $this->assertNull($arguments); + } + + /** + * Tests handleUnresolvedArgument() for missing arguments. + * + * @expectedException \RuntimeException + * @expectedExceptionMessage requires a value for the "$foo" argument. + * + * @dataProvider providerTestHandleUnresolvedArgument + */ + public function testHandleUnresolvedArgument($callable) { + $resolver = new ArgumentsResolver([], [], []); + $arguments = $resolver->getArguments($callable); + $this->assertNull($arguments); + } + + /** + * Provides test data to testHandleUnresolvedArgument(). + */ + public function providerTestHandleUnresolvedArgument() { + $data = []; + $data[] = [function($foo) {}]; + $data[] = [[new TestClass(), 'access']]; + $data[] = ['test_access_arguments_resolver_access']; + return $data; + } + +} + +/** + * Provides a test class. + */ +class TestClass { + public function access($foo) { + } +} + +/** + * Provides a test interface. + */ +interface TestInterface1 { +} + +/** + * Provides a different test interface. + */ +interface TestInterface2 { +} + +} + +namespace { + function test_access_arguments_resolver_access($foo) { + } +} diff --git a/core/tests/Drupal/Tests/Core/Access/AccessArgumentsResolverTest.php b/core/tests/Drupal/Tests/Core/Access/AccessArgumentsResolverTest.php deleted file mode 100644 index 2a46556..0000000 --- a/core/tests/Drupal/Tests/Core/Access/AccessArgumentsResolverTest.php +++ /dev/null @@ -1,242 +0,0 @@ -account = $this->getMock('Drupal\Core\Session\AccountInterface'); - $this->route = new Route('/test'); - } - - /** - * Tests the getArgument() method. - * - * @dataProvider providerTestGetArgument - */ - public function testGetArgument($callable, $request, $expected) { - $arguments = (new AccessArgumentsResolver())->getArguments($callable, $this->route, $request, $this->account); - $this->assertSame($expected, $arguments); - } - - /** - * Provides test data to testGetArgument(). - */ - public function providerTestGetArgument() { - $data = array(); - - // Test an optional parameter with no provided value. - $data[] = array( - function($foo = 'foo') {}, new Request(), array('foo'), - ); - - // Test an optional parameter with a provided value. - $request = new Request(); - $request->attributes->set('foo', 'bar'); - $data[] = array( - function($foo = 'foo') {}, $request, array('bar'), - ); - - // Test with a provided value. - $request = new Request(); - $request->attributes->set('foo', 'bar'); - $data[] = array( - function($foo) {}, $request, array('bar'), - ); - - // Test with an explicitly NULL value. - $request = new Request(); - $request->attributes->set('foo', NULL); - $data[] = array( - function($foo) {}, $request, array(NULL), - ); - - // Test with a raw value that overrides the provided upcast value, since - // it is not typehinted. - $request = new Request(); - $request->attributes->set('foo', 'bar'); - $request->attributes->set('_raw_variables', new ParameterBag(array('foo' => 'baz'))); - $data[] = array( - function($foo) {}, $request, array('baz'), - ); - - return $data; - } - - /** - * Tests getArgument() with a Route object. - */ - public function testGetArgumentRoute() { - $callable = function(Route $route) {}; - $request = new Request(); - - $arguments = (new AccessArgumentsResolver())->getArguments($callable, $this->route, $request, $this->account); - $this->assertSame(array($this->route), $arguments); - } - - /** - * Tests getArgument() with a Route object for a parameter with a custom name. - */ - public function testGetArgumentRouteCustomName() { - $callable = function(Route $custom_name) {}; - $request = new Request(); - - $arguments = (new AccessArgumentsResolver())->getArguments($callable, $this->route, $request, $this->account); - $this->assertSame(array($this->route), $arguments); - } - - /** - * Tests getArgument() with a Route, Request, and Account object. - */ - public function testGetArgumentRouteRequestAccount() { - $callable = function(Route $route, Request $request, AccountInterface $account) {}; - $request = new Request(); - - $arguments = (new AccessArgumentsResolver())->getArguments($callable, $this->route, $request, $this->account); - $this->assertSame(array($this->route, $request, $this->account), $arguments); - - // Test again, but with the arguments in a different order. - $callable = function(AccountInterface $account, Request $request, Route $route) {}; - $request = new Request(); - - $arguments = (new AccessArgumentsResolver())->getArguments($callable, $this->route, $request, $this->account); - $this->assertSame(array($this->account, $request, $this->route), $arguments); - } - - /** - * Tests getArgument() with a '$route' parameter with no typehint. - * - * Without the typehint, the Route object will not be passed to the callable. - * - * @expectedException \RuntimeException - * @expectedExceptionMessage requires a value for the "$route" argument. - */ - public function testGetArgumentRouteNoTypehint() { - $callable = function($route) {}; - $request = new Request(); - - $arguments = (new AccessArgumentsResolver())->getArguments($callable, $this->route, $request, $this->account); - $this->assertNull($arguments); - } - - /** - * Tests getArgument() with a '$route' parameter with no typehint and a value. - * - * Without the typehint, passing a value to a parameter named '$route' will - * still receive the provided value. - */ - public function testGetArgumentRouteNoTypehintAndValue() { - $callable = function($route) {}; - $request = new Request(); - $request->attributes->set('route', 'foo'); - - $arguments = (new AccessArgumentsResolver())->getArguments($callable, $this->route, $request, $this->account); - $this->assertSame(array('foo'), $arguments); - } - - /** - * Tests getArgument() when upcasting is bypassed. - */ - public function testGetArgumentBypassUpcasting() { - $callable = function(Route $route = NULL) {}; - - $request = new Request(); - $request->attributes->set('route', NULL); - - $arguments = (new AccessArgumentsResolver())->getArguments($callable, $this->route, $request, $this->account); - $this->assertSame(array(NULL), $arguments); - } - - /** - * Tests handleUnresolvedArgument() for a non-upcast argument. - * - * @expectedException \RuntimeException - * @expectedExceptionMessage requires a value for the "$foo" argument. - */ - public function testHandleNotUpcastedArgument() { - $callable = function(\stdClass $foo) {}; - - $request = new Request(); - $request->attributes->set('foo', 'bar'); - $request->attributes->set('_raw_variables', new ParameterBag(array('foo' => 'baz'))); - - $arguments = (new AccessArgumentsResolver())->getArguments($callable, $this->route, $request, $this->account); - $this->assertNull($arguments); - } - - /** - * Tests handleUnresolvedArgument() for missing arguments. - * - * @expectedException \RuntimeException - * @expectedExceptionMessage requires a value for the "$foo" argument. - * - * @dataProvider providerTestHandleUnresolvedArgument - */ - public function testHandleUnresolvedArgument($callable) { - $request = new Request(); - - $arguments = (new AccessArgumentsResolver())->getArguments($callable, $this->route, $request, $this->account); - $this->assertNull($arguments); - } - - /** - * Provides test data to testHandleUnresolvedArgument(). - */ - public function providerTestHandleUnresolvedArgument() { - $data = array(); - $data[] = array(function($foo) {}); - $data[] = array(array(new TestClass(), 'access')); - $data[] = array('test_access_arguments_resolver_access'); - return $data; - } - -} - -/** - * Provides a test class. - */ -class TestClass { - public function access($foo) { - } -} - -} - -namespace { - function test_access_arguments_resolver_access($foo) { - } -}