Skip to content

Commit 5d78276

Browse files
committed
Getter with argument with default values can be called without any values
1 parent ed77fd4 commit 5d78276

File tree

2 files changed

+22
-27
lines changed

2 files changed

+22
-27
lines changed

src/DefaultFieldResolver.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ private function getGetter($source, string $name): ?ReflectionMethod
105105
private function orderArguments(ReflectionMethod $method, ?array $args): array
106106
{
107107
$result = [];
108+
if (!$args) {
109+
return $result;
110+
}
111+
108112
foreach ($method->getParameters() as $param) {
109113
if (array_key_exists($param->getName(), $args)) {
110114
$arg = $args[$param->getName()];

tests/DefaultFieldResolverTest.php

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use GraphQL\Doctrine\DefaultFieldResolver;
88
use GraphQL\Doctrine\Definition\EntityID;
99
use GraphQL\Type\Definition\ResolveInfo;
10+
use GraphQLTests\Doctrine\Blog\Model\Special\DefaultValue;
1011
use GraphQLTests\Doctrine\Blog\Model\Special\IgnoredGetter;
1112

1213
class DefaultFieldResolverTest extends \PHPUnit\Framework\TestCase
@@ -25,46 +26,36 @@ public function getEntity()
2526
};
2627

2728
return [
28-
[null, 'privateProperty'],
29-
[null, 'protectedProperty'],
30-
['publicProperty', 'publicProperty'],
31-
[null, 'private'],
32-
[null, 'protected'],
33-
['getPublic', 'public'],
34-
[['real entity', 2, ['foo']], 'publicWithArgs', ['arg2' => 2, 'arg1' => $entityID]],
35-
[null, 'nonExisting'],
36-
[null, '__call'],
37-
[true, 'isValid'],
38-
[true, 'hasMoney'],
29+
[null, new IgnoredGetter(), 'privateProperty'],
30+
[null, new IgnoredGetter(), 'protectedProperty'],
31+
['publicProperty', new IgnoredGetter(), 'publicProperty'],
32+
[null, new IgnoredGetter(), 'private'],
33+
[null, new IgnoredGetter(), 'protected'],
34+
['getPublic', new IgnoredGetter(), 'public'],
35+
[['real entity', 2, ['foo']], new IgnoredGetter(), 'publicWithArgs', ['arg2' => 2, 'arg1' => $entityID]],
36+
[null, new IgnoredGetter(), 'nonExisting'],
37+
[null, new IgnoredGetter(), '__call'],
38+
[true, new IgnoredGetter(), 'isValid'],
39+
[true, new IgnoredGetter(), 'hasMoney'],
40+
['john', new DefaultValue(), 'nameWithDefaultValueOnArgument'],
41+
['jane', new DefaultValue(), 'nameWithDefaultValueOnArgument', ['name' => 'jane']],
42+
['bar', ['foo' => 'bar'], 'foo'],
3943
];
4044
}
4145

4246
/**
4347
* @dataProvider providerDefaultFieldResolver
4448
*
4549
* @param mixed $expected
50+
* @param array|object $source
4651
* @param string $fieldName
4752
* @param null|array $args
4853
*/
49-
public function testDefaultFieldResolver($expected, string $fieldName, ?array $args = null): void
54+
public function testDefaultFieldResolver($expected, $source, string $fieldName, ?array $args = null): void
5055
{
51-
$object = new IgnoredGetter();
52-
5356
$resolver = new DefaultFieldResolver();
5457
$info = new ResolveInfo(['fieldName' => $fieldName]);
55-
$actual = $resolver($object, $args, null, $info);
58+
$actual = $resolver($source, $args, null, $info);
5659
self::assertSame($expected, $actual);
5760
}
58-
59-
public function testDefaultFieldResolverOnArray(): void
60-
{
61-
$array = [
62-
'foo' => 'bar',
63-
];
64-
65-
$resolver = new DefaultFieldResolver();
66-
$info = new ResolveInfo(['fieldName' => 'foo']);
67-
$actual = $resolver($array, null, null, $info);
68-
self::assertSame('bar', $actual);
69-
}
7061
}

0 commit comments

Comments
 (0)