|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Overblog\GraphQLBundle\Tests\Resolver; |
|
4
|
|
|
|
|
5
|
|
|
use Overblog\GraphQLBundle\Resolver\ResolverMap; |
|
6
|
|
|
use Overblog\GraphQLBundle\Resolver\UnresolvableException; |
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
|
|
9
|
|
|
class ResolverMapTest extends TestCase |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @param array|\ArrayAccess $map |
|
13
|
|
|
* @param string $typeName |
|
14
|
|
|
* @param string $fieldName |
|
15
|
|
|
* @param \Closure|null $expectedResolver |
|
16
|
|
|
* |
|
17
|
|
|
* @dataProvider validMapDataProvider |
|
18
|
|
|
*/ |
|
19
|
|
|
public function testResolve($map, $typeName, $fieldName, $expectedResolver) |
|
20
|
|
|
{ |
|
21
|
|
|
$resolverMap = $this->createResolverMapMock($map); |
|
22
|
|
|
$resolver = $resolverMap->resolve($typeName, $fieldName); |
|
|
|
|
|
|
23
|
|
|
$this->assertSame($expectedResolver, $resolver); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function testMapMustBeOverride() |
|
27
|
|
|
{ |
|
28
|
|
|
/** @var ResolverMap|\PHPUnit_Framework_MockObject_MockObject $resolverMap */ |
|
29
|
|
|
$resolverMap = $this->getMockBuilder(ResolverMap::class)->setMethods(null)->getMock(); |
|
30
|
|
|
$this->expectException(\LogicException::class); |
|
31
|
|
|
$this->expectExceptionMessage(sprintf( |
|
32
|
|
|
'You must override the %s::map() method.', |
|
33
|
|
|
get_class($resolverMap) |
|
34
|
|
|
)); |
|
35
|
|
|
|
|
36
|
|
|
$resolverMap->resolve('Foo', 'bar'); |
|
|
|
|
|
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @dataProvider invalidMapDataProvider |
|
41
|
|
|
* |
|
42
|
|
|
* @param mixed $invalidMap |
|
43
|
|
|
* @param string $invalidType |
|
44
|
|
|
*/ |
|
45
|
|
|
public function testInvalidMap($invalidMap, $invalidType) |
|
46
|
|
|
{ |
|
47
|
|
|
$resolverMap = $this->createResolverMapMock($invalidMap); |
|
48
|
|
|
$this->expectException(\RuntimeException::class); |
|
49
|
|
|
$this->expectExceptionMessage(sprintf( |
|
50
|
|
|
'%s::map() should return an array or an instance of \ArrayAccess and \Traversable but got "%s".', |
|
51
|
|
|
get_class($resolverMap), |
|
52
|
|
|
$invalidType |
|
53
|
|
|
)); |
|
54
|
|
|
$resolverMap->resolve('Foo', 'bar'); |
|
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function testUnresolvable() |
|
58
|
|
|
{ |
|
59
|
|
|
$resolverMap = $this->createResolverMapMock([ |
|
60
|
|
|
'Query' => [ |
|
61
|
|
|
ResolverMap::RESOLVE_FIELD => function () { |
|
62
|
|
|
}, |
|
63
|
|
|
], |
|
64
|
|
|
]); |
|
65
|
|
|
$this->expectException(UnresolvableException::class); |
|
66
|
|
|
$this->expectExceptionMessage('Field "Foo.bar" could not be resolved.'); |
|
67
|
|
|
$resolverMap->resolve('Foo', 'bar'); |
|
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function invalidMapDataProvider() |
|
71
|
|
|
{ |
|
72
|
|
|
return [ |
|
73
|
|
|
[null, 'NULL'], |
|
74
|
|
|
[false, 'boolean'], |
|
75
|
|
|
[true, 'boolean'], |
|
76
|
|
|
['baz', 'string'], |
|
77
|
|
|
[new \stdClass(), 'stdClass'], |
|
78
|
|
|
]; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function validMapDataProvider() |
|
82
|
|
|
{ |
|
83
|
|
|
$arrayMap = $this->map(); |
|
84
|
|
|
$objectMap = new \ArrayObject($arrayMap); |
|
85
|
|
|
|
|
86
|
|
|
$validMap = []; |
|
87
|
|
|
|
|
88
|
|
|
foreach ([$arrayMap, $objectMap] as $map) { |
|
89
|
|
|
$validMap = array_merge($validMap, [ |
|
90
|
|
|
[$map, 'Query', ResolverMap::RESOLVE_FIELD, $map['Query'][ResolverMap::RESOLVE_FIELD]], |
|
91
|
|
|
[$map, 'Query', 'foo', $map['Query']['foo']], |
|
92
|
|
|
[$map, 'Query', 'bar', $map['Query']['bar']], |
|
93
|
|
|
[$map, 'Query', 'baz', null], |
|
94
|
|
|
[$map, 'FooInterface', ResolverMap::RESOLVE_TYPE, $map['FooInterface'][ResolverMap::RESOLVE_TYPE]], |
|
95
|
|
|
]); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return $validMap; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param mixed $map |
|
103
|
|
|
* |
|
104
|
|
|
* @return ResolverMap|\PHPUnit_Framework_MockObject_MockObject |
|
105
|
|
|
*/ |
|
106
|
|
View Code Duplication |
private function createResolverMapMock($map) |
|
|
|
|
|
|
107
|
|
|
{ |
|
108
|
|
|
/** @var ResolverMap|\PHPUnit_Framework_MockObject_MockObject $resolverMap */ |
|
109
|
|
|
$resolverMap = $this->getMockBuilder(ResolverMap::class)->setMethods(['map'])->getMock(); |
|
110
|
|
|
$resolverMap->method('map')->willReturn($map); |
|
111
|
|
|
|
|
112
|
|
|
return $resolverMap; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
private function map() |
|
116
|
|
|
{ |
|
117
|
|
|
return [ |
|
118
|
|
|
'Query' => [ |
|
119
|
|
|
ResolverMap::RESOLVE_FIELD => function () { |
|
120
|
|
|
}, |
|
121
|
|
|
'foo' => function () { |
|
122
|
|
|
}, |
|
123
|
|
|
'bar' => function () { |
|
124
|
|
|
}, |
|
125
|
|
|
'baz' => null, |
|
126
|
|
|
], |
|
127
|
|
|
'FooInterface' => [ |
|
128
|
|
|
ResolverMap::RESOLVE_TYPE => function () { |
|
129
|
|
|
}, |
|
130
|
|
|
], |
|
131
|
|
|
]; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: