Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Completed
Pull Request — master (#21)
by Jérémiah
09:05
created

MaxQueryDepthTest::testFragmentQueries()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 3
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
    /**
62
     * @expectedException \InvalidArgumentException
63
     * @expectedExceptionMessage $maxQueryDepth argument must be greater or equal to 0.
64
     */
65
    public function testMaxQueryDepthMustBeGreaterOrEqualTo0()
66
    {
67
        new MaxQueryDepth(-1);
68
    }
69
70
    public function queryDataProvider()
71
    {
72
        return [
73
            [1], // Valid because depth under default limit (7)
74
            [2],
75
            [3],
76
            [4],
77
            [5],
78
            [6],
79
            [7],
80
            [8, 9], // Valid because depth under new limit (9)
81
            [10, 0], // Valid because 0 depth disable limit
82
            [
83
                10,
84
                8,
85
                [$this->createFormattedError(8, 10)],
86
            ], // failed because depth over limit (8)
87
            [
88
                60,
89
                8,
90
                [$this->createFormattedError(8, 58)],
91
            ], // failed because depth over limit (8) and stop count at 58
92
        ];
93
    }
94
95
    private function createFormattedError($max, $count)
96
    {
97
        return FormattedError::create(MaxQueryDepth::maxQueryDepthErrorMessage($max, $count), [new SourceLocation(1, 17)]);
98
    }
99
100
    private function buildRecursiveQuery($depth)
101
    {
102
        $query = sprintf('query MyQuery { human%s }', $this->buildRecursiveQueryPart($depth));
103
104
        return $query;
105
    }
106
107
    private function buildRecursiveUsingFragmentQuery($depth)
108
    {
109
        $query = sprintf(
110
            'query MyQuery { human { ...F1 } } fragment F1 on Human %s',
111
            $this->buildRecursiveQueryPart($depth)
112
        );
113
114
        return $query;
115
    }
116
    private function buildRecursiveUsingInlineFragmentQuery($depth)
117
    {
118
        $query = sprintf(
119
            'query MyQuery { human { ...on Human %s } }',
120
            $this->buildRecursiveQueryPart($depth)
121
        );
122
123
        return $query;
124
    }
125
126
    private function buildRecursiveQueryPart($depth)
127
    {
128
        $templates = [
129
            'human' => ' { firstName%s } ',
130
            'dog' => ' dog { name%s } ',
131
        ];
132
133
        $part = $templates['human'];
134
135
        for ($i = 1; $i <= $depth; ++$i) {
136
            $key = ($i % 2 == 1) ? 'human' : 'dog';
137
            $template = $templates[$key];
138
139
            $part = sprintf($part, ('human' == $key ? ' owner ' : '').$template);
140
        }
141
        $part = str_replace('%s', '', $part);
142
143
        return $part;
144
    }
145
146
    private function assertDocumentValidator($queryString, $depth, array $expectedErrors = [])
147
    {
148
        $errors = DocumentValidator::validate(
149
            Schema::buildSchema(),
150
            Parser::parse($queryString),
151
            [new MaxQueryDepth($depth)]
152
        );
153
154
        $this->assertEquals($expectedErrors, array_map(['GraphQL\Error', 'formatError'], $errors), $queryString);
155
156
        return $errors;
157
    }
158
}
159