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\Config; |
13
|
|
|
|
14
|
|
|
use Overblog\GraphQLBundle\Relay\Connection\Output\ConnectionBuilder; |
15
|
|
|
use Overblog\GraphQLBundle\Resolver\Config\FieldsConfigSolution; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @property FieldsConfigSolution $configSolution |
19
|
|
|
*/ |
20
|
|
|
class FieldsConfigSolutionTest extends AbstractConfigSolutionTest |
21
|
|
|
{ |
22
|
|
|
protected function createConfigSolution() |
23
|
|
|
{ |
24
|
|
|
$typeConfigSolution = $this->getMockBuilder('Overblog\GraphQLBundle\Resolver\Config\TypeConfigSolution')->getMock(); |
25
|
|
|
$resolveCallbackConfigSolution = $this->getMockBuilder('Overblog\GraphQLBundle\Resolver\Config\ResolveCallbackConfigSolution')->getMock(); |
26
|
|
|
|
27
|
|
|
return new FieldsConfigSolution($typeConfigSolution, $resolveCallbackConfigSolution); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @expectedException \Overblog\GraphQLBundle\Error\UserError |
32
|
|
|
* @expectedExceptionMessage Access denied to this field |
33
|
|
|
*/ |
34
|
|
|
public function testResolveAccessAndWrapResolveCallbackWithScalarValueAndAccessDenied() |
35
|
|
|
{ |
36
|
|
|
$callback = $this->invokeResolveAccessAndWrapResolveCallback(false); |
37
|
|
|
$callback('toto'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @expectedException \Overblog\GraphQLBundle\Error\UserError |
42
|
|
|
* @expectedExceptionMessage Access denied to this field |
43
|
|
|
*/ |
44
|
|
|
public function testResolveAccessAndWrapResolveCallbackWithScalarValueAndExpressionEvalThrowingException() |
45
|
|
|
{ |
46
|
|
|
$callback = $this->invokeResolveAccessAndWrapResolveCallback('@=oups'); |
47
|
|
|
$callback('titi'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testResolveAccessAndWrapResolveCallbackWithScalarValueAndAccessDeniedGranted() |
51
|
|
|
{ |
52
|
|
|
$callback = $this->invokeResolveAccessAndWrapResolveCallback(true); |
53
|
|
|
$this->assertEquals('toto', $callback('toto')); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testResolveAccessAndWrapResolveCallbackWithArrayAndAccessDeniedToEveryItemStartingByTo() |
57
|
|
|
{ |
58
|
|
|
$callback = $this->invokeResolveAccessAndWrapResolveCallback('@=not(object matches "/^to.*/i")'); |
59
|
|
|
$this->assertEquals( |
60
|
|
|
[ |
61
|
|
|
'tata', |
62
|
|
|
'titi', |
63
|
|
|
'tata', |
64
|
|
|
], |
65
|
|
|
$callback( |
66
|
|
|
[ |
67
|
|
|
'tata', |
68
|
|
|
'titi', |
69
|
|
|
'tata', |
70
|
|
|
'toto', |
71
|
|
|
'tota', |
72
|
|
|
] |
73
|
|
|
) |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testResolveAccessAndWrapResolveCallbackWithRelayConnectionAndAccessGrantedToEveryNodeStartingByTo() |
78
|
|
|
{ |
79
|
|
|
$callback = $this->invokeResolveAccessAndWrapResolveCallback('@=object matches "/^to.*/i"'); |
80
|
|
|
$this->assertEquals( |
81
|
|
|
ConnectionBuilder::connectionFromArray(['toto', 'toti', null, null, null]), |
82
|
|
|
$callback( |
83
|
|
|
ConnectionBuilder::connectionFromArray(['toto', 'toti', 'coco', 'foo', 'bar']) |
84
|
|
|
) |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param bool|string $hasAccess |
90
|
|
|
* @param callable|null $callback |
91
|
|
|
* |
92
|
|
|
* @return callback |
93
|
|
|
*/ |
94
|
|
|
private function invokeResolveAccessAndWrapResolveCallback($hasAccess, callable $callback = null) |
95
|
|
|
{ |
96
|
|
|
if (null === $callback) { |
97
|
|
|
$callback = function ($value) { |
98
|
|
|
return $value; |
99
|
|
|
}; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $this->invokeMethod( |
103
|
|
|
$this->configSolution, |
104
|
|
|
'resolveAccessAndWrapResolveCallback', |
105
|
|
|
[ |
106
|
|
|
$hasAccess, |
107
|
|
|
$callback, |
108
|
|
|
] |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Call protected/private method of a class. |
114
|
|
|
* |
115
|
|
|
* @see https://jtreminio.com/2013/03/unit-testing-tutorial-part-3-testing-protected-private-methods-coverage-reports-and-crap/ |
116
|
|
|
* |
117
|
|
|
* @param object $object Instantiated object that we will run method on. |
118
|
|
|
* @param string $methodName Method name to call |
119
|
|
|
* @param array $parameters Array of parameters to pass into method. |
120
|
|
|
* |
121
|
|
|
* @return mixed Method return. |
122
|
|
|
*/ |
123
|
|
|
private function invokeMethod($object, $methodName, array $parameters = []) |
124
|
|
|
{ |
125
|
|
|
$reflection = new \ReflectionClass(get_class($object)); |
126
|
|
|
$method = $reflection->getMethod($methodName); |
127
|
|
|
$method->setAccessible(true); |
128
|
|
|
|
129
|
|
|
return $method->invokeArgs($object, $parameters); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|