class ArgumentsResolverTest

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Component/Utility/ArgumentsResolverTest.php \Drupal\Tests\Component\Utility\ArgumentsResolverTest
  2. 8.9.x core/tests/Drupal/Tests/Component/Utility/ArgumentsResolverTest.php \Drupal\Tests\Component\Utility\ArgumentsResolverTest
  3. 11.x core/tests/Drupal/Tests/Component/Utility/ArgumentsResolverTest.php \Drupal\Tests\Component\Utility\ArgumentsResolverTest

@coversDefaultClass \Drupal\Component\Utility\ArgumentsResolver @group Access

Hierarchy

Expanded class hierarchy of ArgumentsResolverTest

File

core/tests/Drupal/Tests/Component/Utility/ArgumentsResolverTest.php, line 14

Namespace

Drupal\Tests\Component\Utility
View source
class ArgumentsResolverTest extends TestCase {
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
    }
    
    /**
     * Tests the getArgument() method.
     *
     * @dataProvider providerTestGetArgument
     */
    public function testGetArgument($callable, $scalars, $objects, $wildcards, $expected) : void {
        $arguments = (new ArgumentsResolver($scalars, $objects, $wildcards))->getArguments($callable);
        $this->assertSame($expected, $arguments);
    }
    
    /**
     * Provides test data to testGetArgument().
     */
    public static 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 type hinted.
        $scalars = [
            'foo' => 'baz',
        ];
        $objects = [
            'foo' => new \stdClass(),
        ];
        $data[] = [
            function ($foo) {
            },
            $scalars,
            $objects,
            [],
            [
                'baz',
            ],
        ];
        // Test a static method string.
        $data[] = [
            TestStaticMethodClass::class . "::access",
            [],
            [
                'foo' => NULL,
            ],
            [],
            [
                NULL,
            ],
        ];
        return $data;
    }
    
    /**
     * Tests getArgument() with an object.
     */
    public function testGetArgumentObject() : void {
        $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 testGetWildcardArgument() : void {
        $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() : void {
        $a1 = $this->getMockBuilder('\\Drupal\\Tests\\Component\\Utility\\Test1Interface')
            ->getMock();
        $a2 = $this->getMockBuilder('\\Drupal\\Tests\\Component\\Utility\\TestClass')
            ->getMock();
        $a3 = $this->getMockBuilder('\\Drupal\\Tests\\Component\\Utility\\Test2Interface')
            ->getMock();
        $objects = [
            't1' => $a1,
            'tc' => $a2,
        ];
        $wildcards = [
            $a3,
        ];
        $resolver = new ArgumentsResolver([], $objects, $wildcards);
        $callable = function (Test1Interface $t1, TestClass $tc, Test2Interface $t2) {
        };
        $arguments = $resolver->getArguments($callable);
        $this->assertSame([
            $a1,
            $a2,
            $a3,
        ], $arguments);
        // Test again, but with the arguments in a different order.
        $callable = function (Test2Interface $t2, TestClass $tc, Test1Interface $t1) {
        };
        $arguments = $resolver->getArguments($callable);
        $this->assertSame([
            $a3,
            $a2,
            $a1,
        ], $arguments);
    }
    
    /**
     * Tests getArgument() with a wildcard parameter with no type hint.
     *
     * Without the type hint, the wildcard object will not be passed to the callable.
     */
    public function testGetWildcardArgumentNoTypeHint() : void {
        $a = $this->getMockBuilder('\\Drupal\\Tests\\Component\\Utility\\Test1Interface')
            ->getMock();
        $wildcards = [
            $a,
        ];
        $resolver = new ArgumentsResolver([], [], $wildcards);
        $callable = function ($route) {
        };
        $this->expectException(\RuntimeException::class);
        $this->expectExceptionMessage('requires a value for the "$route" argument.');
        $resolver->getArguments($callable);
    }
    
    /**
     * Tests getArgument() with a named parameter with no type hint and a value.
     *
     * Without the type hint, passing a value to a named parameter will still
     * receive the provided value.
     */
    public function testGetArgumentRouteNoTypeHintAndValue() : void {
        $scalars = [
            'route' => 'foo',
        ];
        $resolver = new ArgumentsResolver($scalars, [], []);
        $callable = function ($route) {
        };
        $arguments = $resolver->getArguments($callable);
        $this->assertSame([
            'foo',
        ], $arguments);
    }
    
    /**
     * Tests handleUnresolvedArgument() for a scalar argument.
     */
    public function testHandleNotUpcastedArgument() : void {
        $objects = [
            'foo' => 'bar',
        ];
        $scalars = [
            'foo' => 'baz',
        ];
        $resolver = new ArgumentsResolver($scalars, $objects, []);
        $callable = function (\stdClass $foo) {
        };
        $this->expectException(\RuntimeException::class);
        $this->expectExceptionMessage('requires a value for the "$foo" argument.');
        $resolver->getArguments($callable);
    }
    
    /**
     * Tests handleUnresolvedArgument() for missing arguments.
     *
     * @dataProvider providerTestHandleUnresolvedArgument
     */
    public function testHandleUnresolvedArgument($callable) : void {
        $resolver = new ArgumentsResolver([], [], []);
        $this->expectException(\RuntimeException::class);
        $this->expectExceptionMessage('requires a value for the "$foo" argument.');
        $resolver->getArguments($callable);
    }
    
    /**
     * Provides test data to testHandleUnresolvedArgument().
     */
    public static function providerTestHandleUnresolvedArgument() {
        $data = [];
        $data[] = [
            function ($foo) {
            },
        ];
        $data[] = [
            [
                new TestClass(),
                'access',
            ],
        ];
        $data[] = [
            'Drupal\\Tests\\Component\\Utility\\test_access_arguments_resolver_access',
        ];
        return $data;
    }

}

Members

Title Sort descending Modifiers Object type Summary
ArgumentsResolverTest::providerTestGetArgument public static function Provides test data to testGetArgument().
ArgumentsResolverTest::providerTestHandleUnresolvedArgument public static function Provides test data to testHandleUnresolvedArgument().
ArgumentsResolverTest::setUp protected function
ArgumentsResolverTest::testGetArgument public function Tests the getArgument() method.
ArgumentsResolverTest::testGetArgumentObject public function Tests getArgument() with an object.
ArgumentsResolverTest::testGetArgumentOrder public function Tests getArgument() with a Route, Request, and Account object.
ArgumentsResolverTest::testGetArgumentRouteNoTypeHintAndValue public function Tests getArgument() with a named parameter with no type hint and a value.
ArgumentsResolverTest::testGetWildcardArgument public function Tests getArgument() with a wildcard object for a parameter with a custom name.
ArgumentsResolverTest::testGetWildcardArgumentNoTypeHint public function Tests getArgument() with a wildcard parameter with no type hint.
ArgumentsResolverTest::testHandleNotUpcastedArgument public function Tests handleUnresolvedArgument() for a scalar argument.
ArgumentsResolverTest::testHandleUnresolvedArgument public function Tests handleUnresolvedArgument() for missing arguments.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.