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\Request\Validator\Rule; |
13
|
|
|
|
14
|
|
|
use GraphQL\FormattedError; |
15
|
|
|
use GraphQL\Language\Parser; |
16
|
|
|
use GraphQL\Language\SourceLocation; |
17
|
|
|
use GraphQL\Type\Introspection; |
18
|
|
|
use GraphQL\Validator\DocumentValidator; |
19
|
|
|
use Overblog\GraphQLBundle\Request\Validator\Rule\MaxQueryDepth; |
20
|
|
|
|
21
|
|
|
class MaxQueryDepthTest extends \PHPUnit_Framework_TestCase |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @param $queryDepth |
25
|
|
|
* @param int $maxQueryDepth |
26
|
|
|
* @param array $expectedErrors |
27
|
|
|
* @dataProvider queryDataProvider |
28
|
|
|
*/ |
29
|
|
|
public function testSimpleQueries($queryDepth, $maxQueryDepth = 7, $expectedErrors = []) |
30
|
|
|
{ |
31
|
|
|
$this->assertDocumentValidator($this->buildRecursiveQuery($queryDepth), $maxQueryDepth, $expectedErrors); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param $queryDepth |
36
|
|
|
* @param int $maxQueryDepth |
37
|
|
|
* @param array $expectedErrors |
38
|
|
|
* @dataProvider queryDataProvider |
39
|
|
|
*/ |
40
|
|
|
public function testFragmentQueries($queryDepth, $maxQueryDepth = 7, $expectedErrors = []) |
41
|
|
|
{ |
42
|
|
|
$this->assertDocumentValidator($this->buildRecursiveUsingFragmentQuery($queryDepth), $maxQueryDepth, $expectedErrors); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param $queryDepth |
47
|
|
|
* @param int $maxQueryDepth |
48
|
|
|
* @param array $expectedErrors |
49
|
|
|
* @dataProvider queryDataProvider |
50
|
|
|
*/ |
51
|
|
|
public function testInlineFragmentQueries($queryDepth, $maxQueryDepth = 7, $expectedErrors = []) |
52
|
|
|
{ |
53
|
|
|
$this->assertDocumentValidator($this->buildRecursiveUsingInlineFragmentQuery($queryDepth), $maxQueryDepth, $expectedErrors); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testIgnoreIntrospectionQuery() |
57
|
|
|
{ |
58
|
|
|
$this->assertDocumentValidator(Introspection::getIntrospectionQuery(true), 1); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function queryDataProvider() |
62
|
|
|
{ |
63
|
|
|
return [ |
64
|
|
|
[1], // Valid because depth under default limit (7) |
65
|
|
|
[2], |
66
|
|
|
[3], |
67
|
|
|
[4], |
68
|
|
|
[5], |
69
|
|
|
[6], |
70
|
|
|
[7], |
71
|
|
|
[8, 9], // Valid because depth under new limit (9) |
72
|
|
|
[ |
73
|
|
|
10, |
74
|
|
|
8, |
75
|
|
|
[$this->createFormattedError(8, 10)], |
76
|
|
|
], // failed because depth over limit (8) |
77
|
|
|
[ |
78
|
|
|
60, |
79
|
|
|
8, |
80
|
|
|
[$this->createFormattedError(8, 58)], |
81
|
|
|
], // failed because depth over limit (8) and stop count at 58 |
82
|
|
|
]; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
private function createFormattedError($max, $count) |
86
|
|
|
{ |
87
|
|
|
return FormattedError::create(MaxQueryDepth::maxQueryDepthErrorMessage($max, $count), [new SourceLocation(1, 17)]); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
private function buildRecursiveQuery($depth) |
91
|
|
|
{ |
92
|
|
|
$query = sprintf('query MyQuery { human%s }', $this->buildRecursiveQueryPart($depth)); |
93
|
|
|
|
94
|
|
|
return $query; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
private function buildRecursiveUsingFragmentQuery($depth) |
98
|
|
|
{ |
99
|
|
|
$query = sprintf( |
100
|
|
|
'query MyQuery { human { ...F1 } } fragment F1 on Human %s', |
101
|
|
|
$this->buildRecursiveQueryPart($depth) |
102
|
|
|
); |
103
|
|
|
|
104
|
|
|
return $query; |
105
|
|
|
} |
106
|
|
|
private function buildRecursiveUsingInlineFragmentQuery($depth) |
107
|
|
|
{ |
108
|
|
|
$query = sprintf( |
109
|
|
|
'query MyQuery { human { ...on Human %s } }', |
110
|
|
|
$this->buildRecursiveQueryPart($depth) |
111
|
|
|
); |
112
|
|
|
|
113
|
|
|
return $query; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
private function buildRecursiveQueryPart($depth) |
117
|
|
|
{ |
118
|
|
|
$templates = [ |
119
|
|
|
'human' => ' { firstName%s } ', |
120
|
|
|
'dog' => ' dog { name%s } ', |
121
|
|
|
]; |
122
|
|
|
|
123
|
|
|
$part = $templates['human']; |
124
|
|
|
|
125
|
|
|
for ($i = 1; $i <= $depth; ++$i) { |
126
|
|
|
$key = ($i % 2 == 1) ? 'human' : 'dog'; |
127
|
|
|
$template = $templates[$key]; |
128
|
|
|
|
129
|
|
|
$part = sprintf($part, ('human' == $key ? ' owner ' : '').$template); |
130
|
|
|
} |
131
|
|
|
$part = str_replace('%s', '', $part); |
132
|
|
|
|
133
|
|
|
return $part; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
private function assertDocumentValidator($queryString, $depth, array $expectedErrors = []) |
137
|
|
|
{ |
138
|
|
|
$errors = DocumentValidator::validate( |
139
|
|
|
Schema::buildSchema(), |
140
|
|
|
Parser::parse($queryString), |
141
|
|
|
[new MaxQueryDepth($depth)] |
142
|
|
|
); |
143
|
|
|
|
144
|
|
|
$this->assertEquals($expectedErrors, array_map(['GraphQL\Error', 'formatError'], $errors), $queryString); |
145
|
|
|
|
146
|
|
|
return $errors; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|