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 Overblog\GraphQLBundle\Request\Validator\Rule\QueryComplexity; |
15
|
|
|
|
16
|
|
|
class QueryComplexityTest extends AbstractQuerySecurityTest |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @param $max |
20
|
|
|
* @param $count |
21
|
|
|
* |
22
|
|
|
* @return string |
23
|
|
|
*/ |
24
|
|
|
protected function getErrorMessage($max, $count) |
25
|
|
|
{ |
26
|
|
|
return QueryComplexity::maxQueryComplexityErrorMessage($max, $count); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param $maxDepth |
31
|
|
|
* |
32
|
|
|
* @return QueryComplexity |
33
|
|
|
*/ |
34
|
|
|
protected function createRule($maxDepth) |
35
|
|
|
{ |
36
|
|
|
return new QueryComplexity($maxDepth); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testSimpleQueries() |
40
|
|
|
{ |
41
|
|
|
$query = 'query MyQuery { human { firstName } }'; |
42
|
|
|
|
43
|
|
|
$this->assertDocumentValidators($query, 2, 3); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testInlineFragmentQueries() |
47
|
|
|
{ |
48
|
|
|
$query = 'query MyQuery { human { ... on Human { firstName } } }'; |
49
|
|
|
|
50
|
|
|
$this->assertDocumentValidators($query, 2, 3); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function testFragmentQueries() |
54
|
|
|
{ |
55
|
|
|
$query = 'query MyQuery { human { ...F1 } } fragment F1 on Human { firstName}'; |
56
|
|
|
|
57
|
|
|
$this->assertDocumentValidators($query, 2, 3); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function testAliasesQueries() |
61
|
|
|
{ |
62
|
|
|
$query = 'query MyQuery { thomas: human(name: "Thomas") { firstName } jeremy: human(name: "Jeremy") { firstName } }'; |
63
|
|
|
|
64
|
|
|
$this->assertDocumentValidators($query, 4, 5); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function testCustomComplexityQueries() |
68
|
|
|
{ |
69
|
|
|
$query = 'query MyQuery { human { dogs { name } } }'; |
70
|
|
|
|
71
|
|
|
$this->assertDocumentValidators($query, 12, 13); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testCustomComplexityWithArgsQueries() |
75
|
|
|
{ |
76
|
|
|
$query = 'query MyQuery { human { dogs(name: "Root") { name } } }'; |
77
|
|
|
|
78
|
|
|
$this->assertDocumentValidators($query, 3, 4); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function testCustomComplexityWithVariablesQueries() |
82
|
|
|
{ |
83
|
|
|
$query = 'query MyQuery($dog: String!) { human { dogs(name: $dog) { name } } }'; |
84
|
|
|
|
85
|
|
|
QueryComplexity::setRawVariableValues(['dog' => 'Roots']); |
86
|
|
|
|
87
|
|
|
$this->assertDocumentValidators($query, 3, 4); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function testComplexityIntrospectionQuery() |
91
|
|
|
{ |
92
|
|
|
$this->assertIntrospectionQuery(109); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function testIntrospectionTypeMetaFieldQuery() |
96
|
|
|
{ |
97
|
|
|
$this->assertIntrospectionTypeMetaFieldQuery(2); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function testTypeNameMetaFieldQuery() |
101
|
|
|
{ |
102
|
|
|
$this->assertTypeNameMetaFieldQuery(3); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
private function assertDocumentValidators($query, $queryComplexity, $startComplexity) |
106
|
|
|
{ |
107
|
|
|
for ($maxComplexity = $startComplexity; $maxComplexity >= 0; --$maxComplexity) { |
108
|
|
|
$positions = []; |
109
|
|
|
|
110
|
|
|
if ($maxComplexity < $queryComplexity && $maxComplexity !== QueryComplexity::DISABLED) { |
111
|
|
|
$positions = [$this->createFormattedError($maxComplexity, $queryComplexity)]; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$this->assertDocumentValidator($query, $maxComplexity, $positions); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|