|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the OverblogGraphQLBundle package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Overblog <http://github.com/overblog/> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Overblog\GraphQLBundle\Tests\Resolver; |
|
13
|
|
|
|
|
14
|
|
|
use Overblog\GraphQLBundle\ExpressionLanguage\ExpressionLanguage; |
|
15
|
|
|
use Overblog\GraphQLBundle\Relay\Connection\Output\ConnectionBuilder; |
|
16
|
|
|
use Overblog\GraphQLBundle\Resolver\ConfigResolver; |
|
17
|
|
|
use Overblog\GraphQLBundle\Tests\DIContainerMockTrait; |
|
18
|
|
|
use Symfony\Component\ExpressionLanguage\Expression; |
|
19
|
|
|
|
|
20
|
|
|
class ConfigResolverTest extends \PHPUnit_Framework_TestCase |
|
21
|
|
|
{ |
|
22
|
|
|
use DIContainerMockTrait; |
|
23
|
|
|
|
|
24
|
|
|
/** @var ConfigResolver */ |
|
25
|
|
|
private $configResolver; |
|
26
|
|
|
|
|
27
|
|
|
public function setUp() |
|
28
|
|
|
{ |
|
29
|
|
|
$container = $this->getDIContainerMock(); |
|
30
|
|
|
$container |
|
31
|
|
|
->method('get') |
|
32
|
|
|
->will($this->returnValue(new \stdClass())); |
|
33
|
|
|
|
|
34
|
|
|
$expressionLanguage = new ExpressionLanguage(); |
|
35
|
|
|
$expressionLanguage->setContainer($container); |
|
36
|
|
|
|
|
37
|
|
|
$typeResolver = $this->getMockBuilder('Overblog\GraphQLBundle\Resolver\TypeResolver') |
|
38
|
|
|
->disableOriginalConstructor() |
|
39
|
|
|
->getMock(); |
|
40
|
|
|
$typeResolver |
|
41
|
|
|
->method('resolve') |
|
42
|
|
|
->will($this->returnValue(new \stdClass())); |
|
43
|
|
|
|
|
44
|
|
|
$fieldResolver = $this->getMockBuilder('Overblog\GraphQLBundle\Resolver\FieldResolver') |
|
45
|
|
|
->disableOriginalConstructor() |
|
46
|
|
|
->getMock(); |
|
47
|
|
|
$fieldResolver |
|
48
|
|
|
->method('resolve') |
|
49
|
|
|
->will($this->returnValue(new \stdClass())); |
|
50
|
|
|
|
|
51
|
|
|
$argResolver = $this->getMockBuilder('Overblog\GraphQLBundle\Resolver\ArgResolver') |
|
52
|
|
|
->disableOriginalConstructor() |
|
53
|
|
|
->getMock(); |
|
54
|
|
|
$argResolver |
|
55
|
|
|
->method('resolve') |
|
56
|
|
|
->will($this->returnValue(new \stdClass())); |
|
57
|
|
|
|
|
58
|
|
|
$this->configResolver = new ConfigResolver( |
|
59
|
|
|
$typeResolver, |
|
60
|
|
|
$fieldResolver, |
|
61
|
|
|
$argResolver, |
|
62
|
|
|
$expressionLanguage, |
|
63
|
|
|
true |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @expectedException \RuntimeException |
|
69
|
|
|
* @expectedExceptionMessage Config must be an array or implement \ArrayAccess interface |
|
70
|
|
|
*/ |
|
71
|
|
|
public function testConfigNotArrayOrImplementArrayAccess() |
|
72
|
|
|
{ |
|
73
|
|
|
$this->configResolver->resolve('Not Array'); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function testResolveValues() |
|
77
|
|
|
{ |
|
78
|
|
|
$config = $this->configResolver->resolve( |
|
79
|
|
|
[ |
|
80
|
|
|
'values' => [ |
|
81
|
|
|
'test' => ['value' => 'my test value'], |
|
82
|
|
|
'toto' => ['value' => 'my toto value'], |
|
83
|
|
|
'expression-language-test' => ['value' => '@=["my", "test"]'], |
|
84
|
|
|
], |
|
85
|
|
|
] |
|
86
|
|
|
); |
|
87
|
|
|
|
|
88
|
|
|
$expected = [ |
|
89
|
|
|
'values' => [ |
|
90
|
|
|
'test' => ['value' => 'my test value'], |
|
91
|
|
|
'toto' => ['value' => 'my toto value'], |
|
92
|
|
|
'expression-language-test' => ['value' => ['my', 'test']], |
|
93
|
|
|
], |
|
94
|
|
|
]; |
|
95
|
|
|
|
|
96
|
|
|
$this->assertEquals($expected, $config); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @expectedException \Overblog\GraphQLBundle\Error\UserError |
|
101
|
|
|
* @expectedExceptionMessage Access denied to this field |
|
102
|
|
|
*/ |
|
103
|
|
|
public function testResolveAccessAndWrapResolveCallbackWithScalarValueAndAccessDenied() |
|
104
|
|
|
{ |
|
105
|
|
|
$callback = $this->invokeResolveAccessAndWrapResolveCallback(false); |
|
106
|
|
|
$callback('toto'); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @expectedException \Overblog\GraphQLBundle\Error\UserError |
|
111
|
|
|
* @expectedExceptionMessage Access denied to this field |
|
112
|
|
|
*/ |
|
113
|
|
|
public function testResolveAccessAndWrapResolveCallbackWithScalarValueAndExpressionEvalThrowingException() |
|
114
|
|
|
{ |
|
115
|
|
|
$callback = $this->invokeResolveAccessAndWrapResolveCallback('@=oups'); |
|
116
|
|
|
$callback('titi'); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function testResolveAccessAndWrapResolveCallbackWithScalarValueAndAccessDeniedGranted() |
|
120
|
|
|
{ |
|
121
|
|
|
$callback = $this->invokeResolveAccessAndWrapResolveCallback(true); |
|
122
|
|
|
$this->assertEquals('toto', $callback('toto')); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function testResolveAccessAndWrapResolveCallbackWithArrayAndAccessDeniedToEveryItemStartingByTo() |
|
126
|
|
|
{ |
|
127
|
|
|
$callback = $this->invokeResolveAccessAndWrapResolveCallback('@=not(object matches "/^to.*/i")'); |
|
128
|
|
|
$this->assertEquals( |
|
129
|
|
|
[ |
|
130
|
|
|
'tata', |
|
131
|
|
|
'titi', |
|
132
|
|
|
'tata', |
|
133
|
|
|
], |
|
134
|
|
|
$callback( |
|
135
|
|
|
[ |
|
136
|
|
|
'tata', |
|
137
|
|
|
'titi', |
|
138
|
|
|
'tata', |
|
139
|
|
|
'toto', |
|
140
|
|
|
'tota', |
|
141
|
|
|
] |
|
142
|
|
|
) |
|
143
|
|
|
); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
public function testResolveAccessAndWrapResolveCallbackWithRelayConnectionAndAccessGrantedToEveryNodeStartingByTo() |
|
147
|
|
|
{ |
|
148
|
|
|
$callback = $this->invokeResolveAccessAndWrapResolveCallback('@=object matches "/^to.*/i"'); |
|
149
|
|
|
$this->assertEquals( |
|
150
|
|
|
ConnectionBuilder::connectionFromArray(['toto', 'toti', null, null, null]), |
|
151
|
|
|
$callback( |
|
152
|
|
|
ConnectionBuilder::connectionFromArray(['toto', 'toti', 'coco', 'foo', 'bar']) |
|
153
|
|
|
) |
|
154
|
|
|
); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @param bool|string $hasAccess |
|
159
|
|
|
* @param callable|null $callback |
|
160
|
|
|
* |
|
161
|
|
|
* @return callback |
|
162
|
|
|
*/ |
|
163
|
|
|
private function invokeResolveAccessAndWrapResolveCallback($hasAccess, callable $callback = null) |
|
164
|
|
|
{ |
|
165
|
|
|
if (null === $callback) { |
|
166
|
|
|
$callback = function ($value) { |
|
167
|
|
|
return $value; |
|
168
|
|
|
}; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
return $this->invokeMethod( |
|
172
|
|
|
$this->configResolver, |
|
173
|
|
|
'resolveAccessAndWrapResolveCallback', |
|
174
|
|
|
[ |
|
175
|
|
|
$hasAccess, |
|
176
|
|
|
$callback, |
|
177
|
|
|
] |
|
178
|
|
|
); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Call protected/private method of a class. |
|
183
|
|
|
* |
|
184
|
|
|
* @see https://jtreminio.com/2013/03/unit-testing-tutorial-part-3-testing-protected-private-methods-coverage-reports-and-crap/ |
|
185
|
|
|
* |
|
186
|
|
|
* @param object $object Instantiated object that we will run method on. |
|
187
|
|
|
* @param string $methodName Method name to call |
|
188
|
|
|
* @param array $parameters Array of parameters to pass into method. |
|
189
|
|
|
* |
|
190
|
|
|
* @return mixed Method return. |
|
191
|
|
|
*/ |
|
192
|
|
|
private function invokeMethod($object, $methodName, array $parameters = []) |
|
193
|
|
|
{ |
|
194
|
|
|
$reflection = new \ReflectionClass(get_class($object)); |
|
195
|
|
|
$method = $reflection->getMethod($methodName); |
|
196
|
|
|
$method->setAccessible(true); |
|
197
|
|
|
|
|
198
|
|
|
return $method->invokeArgs($object, $parameters); |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|