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 (#68)
by Jérémiah
07:32
created

testBatchEndpointWithInvalidJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
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\Functional\Controller;
13
14
use Overblog\GraphQLBundle\Tests\Functional\TestCase;
15
16
class GraphControllerTest extends TestCase
17
{
18
    private $friendsQuery = <<<'EOF'
19
query FriendsQuery {
20
  user {
21
    friends(first: 2) {
22
      totalCount
23
      edges {
24
        friendshipTime
25
        node {
26
          name
27
        }
28
      }
29
    }
30
  }
31
}
32
EOF;
33
34
    private $friendsTotalCountQuery = <<<'EOF'
35
query FriendsTotalCountQuery {
36
  user {
37
    friends {
38
      totalCount
39
    }
40
  }
41
}
42
EOF;
43
44
    private $expectedData = [
45
        'user' => [
46
            'friends' => [
47
                'totalCount' => 4,
48
                'edges' => [
49
                    [
50
                        'friendshipTime' => 'Yesterday',
51
                        'node' => [
52
                            'name' => 'Nick',
53
                        ],
54
                    ],
55
                    [
56
                        'friendshipTime' => 'Yesterday',
57
                        'node' => [
58
                            'name' => 'Lee',
59
                        ],
60
                    ],
61
                ],
62
            ],
63
        ],
64
    ];
65
66
    /**
67
     * @param $uri
68
     * @dataProvider graphQLEndpointUriProvider
69
     */
70
    public function testEndpointAction($uri)
71
    {
72
        $client = static::createClient(['test_case' => 'connection']);
73
74
        $client->request('GET', $uri, ['query' => $this->friendsQuery], [], ['CONTENT_TYPE' => 'application/graphql']);
75
        $result = $client->getResponse()->getContent();
76
        $this->assertEquals(['data' => $this->expectedData], json_decode($result, true), $result);
77
    }
78
79
    public function graphQLEndpointUriProvider()
80
    {
81
        return [
82
            ['/'],
83
            ['/graphql/default'],
84
        ];
85
    }
86
87
    /**
88
     * @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
89
     * @expectedExceptionMessage Must provide query parameter
90
     */
91
    public function testEndpointWithEmptyQuery()
92
    {
93
        $client = static::createClient();
94
        $client->request('GET', '/', []);
95
        $client->getResponse()->getContent();
96
    }
97
98
    /**
99
     * @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
100
     * @expectedExceptionMessage The request content body must not be empty when using json content type request.
101
     */
102
    public function testEndpointWithEmptyJsonBodyQuery()
103
    {
104
        $client = static::createClient();
105
        $client->request('GET', '/', [], [], ['CONTENT_TYPE' => 'application/json']);
106
        $client->getResponse()->getContent();
107
    }
108
109
110
    /**
111
     * @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
112
     * @expectedExceptionMessage POST body sent invalid JSON
113
     */
114
    public function testEndpointWithInvalidBodyQuery()
115
    {
116
        $client = static::createClient();
117
        $client->request('GET', '/', [], [], ['CONTENT_TYPE' => 'application/json'], '{');
118
        $client->getResponse()->getContent();
119
    }
120
121
    public function testEndpointActionWithVariables()
122
    {
123
        $client = static::createClient(['test_case' => 'connection']);
124
125
        $query = <<<'EOF'
126
query FriendsQuery($firstFriends: Int) {
127
  user {
128
    friends(first: $firstFriends) {
129
      totalCount
130
      edges {
131
        friendshipTime
132
        node {
133
          name
134
        }
135
      }
136
    }
137
  }
138
}
139
EOF;
140
141
        $client->request('GET', '/', [], [], ['CONTENT_TYPE' => 'application/json'], json_encode(['query' => $query, 'variables' => '{"firstFriends": 2}']));
142
    }
143
144
    /**
145
     * @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
146
     * @expectedExceptionMessage Variables are invalid JSON
147
     */
148 View Code Duplication
    public function testEndpointActionWithInvalidVariables()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
149
    {
150
        $client = static::createClient(['test_case' => 'connection']);
151
152
        $query = <<<'EOF'
153
query {
154
  user
155
}
156
EOF;
157
158
        $client->request('GET', '/', ['query' => $query, 'variables' => '"firstFriends": 2}']);
159
    }
160
161
    /**
162
     * @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
163
     * @expectedExceptionMessage Could not found "fake" schema.
164
     */
165 View Code Duplication
    public function testMultipleEndpointActionWithUnknownSchemaName()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
166
    {
167
        $client = static::createClient(['test_case' => 'connection']);
168
169
        $query = <<<'EOF'
170
query {
171
  user
172
}
173
EOF;
174
175
        $client->request('GET', '/graphql/fake', ['query' => $query]);
176
    }
177
178
    public function testEndpointActionWithOperationName()
179
    {
180
        $client = static::createClient(['test_case' => 'connection']);
181
182
        $query = $this->friendsQuery."\n".$this->friendsTotalCountQuery;
183
184
        $client->request('POST', '/', ['query' => $query, 'operationName' => 'FriendsQuery'], [], ['CONTENT_TYPE' => 'application/x-www-form-urlencoded']);
185
        $result = $client->getResponse()->getContent();
186
        $this->assertEquals(['data' => $this->expectedData], json_decode($result, true), $result);
187
    }
188
189
    /**
190
     * @param $uri
191
     * @dataProvider graphQLBatchEndpointUriProvider
192
     */
193
    public function testBatchEndpointAction($uri)
194
    {
195
        $client = static::createClient(['test_case' => 'connection']);
196
197
        $data = [
198
            [
199
                'id' => 'friends',
200
                'query' => $this->friendsQuery,
201
            ],
202
            [
203
                'id' => 'friendsTotalCount',
204
                'query' => $this->friendsTotalCountQuery,
205
            ],
206
        ];
207
208
        $client->request('POST', $uri, [], [], ['CONTENT_TYPE' => 'application/json'], json_encode($data));
209
        $result = $client->getResponse()->getContent();
210
211
        $expected  = [
212
            ['id' => 'friends', 'payload' => ['data' => $this->expectedData]],
213
            ['id' => 'friendsTotalCount', 'payload' => ['data' => ['user' => ['friends' => ['totalCount' => 4]]]]],
214
        ];
215
        $this->assertEquals($expected, json_decode($result, true), $result);
216
    }
217
218
    public function graphQLBatchEndpointUriProvider()
219
    {
220
        return [
221
            ['/batch'],
222
            ['/graphql/default/batch'],
223
        ];
224
    }
225
226
    /**
227
     * @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
228
     * @expectedExceptionMessage Must provide at least one valid query.
229
     */
230
    public function testBatchEndpointWithEmptyQuery()
231
    {
232
        $client = static::createClient();
233
        $client->request('GET', '/batch', [], [], ['CONTENT_TYPE' => 'application/json'], '{}');
234
        $client->getResponse()->getContent();
235
    }
236
237
    /**
238
     * @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
239
     * @expectedExceptionMessage Only request with content type "application/json" is accepted.
240
     */
241
    public function testBatchEndpointWrongContentType()
242
    {
243
        $client = static::createClient();
244
        $client->request('GET', '/batch');
245
        $client->getResponse()->getContent();
246
    }
247
248
    /**
249
     * @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
250
     * @expectedExceptionMessage POST body sent invalid JSON
251
     */
252
    public function testBatchEndpointWithInvalidJson()
253
    {
254
        $client = static::createClient();
255
        $client->request('GET', '/batch', [], [], ['CONTENT_TYPE' => 'application/json'], '{');
256
        $client->getResponse()->getContent();
257
    }
258
259
    /**
260
     * @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
261
     * @expectedExceptionMessage 1 is not a valid query
262
     */
263
    public function testBatchEndpointWithInvalidQuery()
264
    {
265
        $client = static::createClient();
266
        $client->request('GET', '/batch', [], [], ['CONTENT_TYPE' => 'application/json'], '{"test" : {"query": 1}}');
267
        $client->getResponse()->getContent();
268
    }
269
}
270