View source
<?php
namespace Drupal\Tests\Component\Utility;
use Drupal\Component\Utility\ArgumentsResolver;
use PHPUnit\Framework\TestCase;
class ArgumentsResolverTest extends TestCase {
protected function setUp() : void {
parent::setUp();
}
public function testGetArgument($callable, $scalars, $objects, $wildcards, $expected) {
$arguments = (new ArgumentsResolver($scalars, $objects, $wildcards))
->getArguments($callable);
$this
->assertSame($expected, $arguments);
}
public function providerTestGetArgument() {
$data = [];
$data[] = [
function ($foo = 'foo') {
},
[],
[],
[],
[
'foo',
],
];
$data[] = [
function ($foo = 'foo') {
},
[
'foo' => 'bar',
],
[],
[],
[
'bar',
],
];
$data[] = [
function ($foo) {
},
[
'foo' => 'bar',
],
[],
[],
[
'bar',
],
];
$data[] = [
function ($foo) {
},
[],
[
'foo' => NULL,
],
[],
[
NULL,
],
];
$scalars = [
'foo' => 'baz',
];
$objects = [
'foo' => new \stdClass(),
];
$data[] = [
function ($foo) {
},
$scalars,
$objects,
[],
[
'baz',
],
];
return $data;
}
public function testGetArgumentObject() {
$callable = function (\stdClass $object) {
};
$object = new \stdClass();
$arguments = (new ArgumentsResolver([], [
'object' => $object,
], []))
->getArguments($callable);
$this
->assertSame([
$object,
], $arguments);
}
public function testGetWildcardArgument() {
$callable = function (\stdClass $custom_name) {
};
$object = new \stdClass();
$arguments = (new ArgumentsResolver([], [], [
$object,
]))
->getArguments($callable);
$this
->assertSame([
$object,
], $arguments);
}
public function testGetArgumentOrder() {
$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);
$callable = function (Test2Interface $t2, TestClass $tc, Test1Interface $t1) {
};
$arguments = $resolver
->getArguments($callable);
$this
->assertSame([
$a3,
$a2,
$a1,
], $arguments);
}
public function testGetWildcardArgumentNoTypehint() {
$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);
}
public function testGetArgumentRouteNoTypehintAndValue() {
$scalars = [
'route' => 'foo',
];
$resolver = new ArgumentsResolver($scalars, [], []);
$callable = function ($route) {
};
$arguments = $resolver
->getArguments($callable);
$this
->assertSame([
'foo',
], $arguments);
}
public function testHandleNotUpcastedArgument() {
$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);
}
public function testHandleUnresolvedArgument($callable) {
$resolver = new ArgumentsResolver([], [], []);
$this
->expectException(\RuntimeException::class);
$this
->expectExceptionMessage('requires a value for the "$foo" argument.');
$resolver
->getArguments($callable);
}
public function providerTestHandleUnresolvedArgument() {
$data = [];
$data[] = [
function ($foo) {
},
];
$data[] = [
[
new TestClass(),
'access',
],
];
$data[] = [
'Drupal\\Tests\\Component\\Utility\\test_access_arguments_resolver_access',
];
return $data;
}
}
class TestClass {
public function access($foo) {
}
}
interface Test1Interface {
}
interface Test2Interface {
}
function test_access_arguments_resolver_access($foo) {
}