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
Push — master ( a91966...23285a )
by Jérémiah
08:45
created

GraphControllerTest::testUnAuthorizedMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
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
use Symfony\Component\HttpKernel\Client;
16
17
class GraphControllerTest extends TestCase
18
{
19
    private $friendsQuery = <<<'EOF'
20
query FriendsQuery {
21
  user {
22
    friends(first: 2) {
23
      totalCount
24
      edges {
25
        friendshipTime
26
        node {
27
          name
28
        }
29
      }
30
    }
31
  }
32
}
33
EOF;
34
35
    private $friendsTotalCountQuery = <<<'EOF'
36
query FriendsTotalCountQuery {
37
  user {
38
    friends {
39
      totalCount
40
    }
41
  }
42
}
43
EOF;
44
45
    private $expectedData = [
46
        'user' => [
47
            'friends' => [
48
                'totalCount' => 4,
49
                'edges' => [
50
                    [
51
                        'friendshipTime' => 'Yesterday',
52
                        'node' => [
53
                            'name' => 'Nick',
54
                        ],
55
                    ],
56
                    [
57
                        'friendshipTime' => 'Yesterday',
58
                        'node' => [
59
                            'name' => 'Lee',
60
                        ],
61
                    ],
62
                ],
63
            ],
64
        ],
65
    ];
66
67
    /**
68
     * @param $uri
69
     * @dataProvider graphQLEndpointUriProvider
70
     */
71 View Code Duplication
    public function testEndpointAction($uri)
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...
72
    {
73
        $client = static::createClient(['test_case' => 'connectionWithCORS']);
74
75
        $client->request('GET', $uri, ['query' => $this->friendsQuery], [], ['CONTENT_TYPE' => 'application/graphql', 'HTTP_Origin' => 'http://example.com']);
76
        $result = $client->getResponse()->getContent();
77
        $this->assertEquals(['data' => $this->expectedData], json_decode($result, true), $result);
78
        $this->assertCORSHeadersExists($client);
79
    }
80
81
    public function graphQLEndpointUriProvider()
82
    {
83
        return [
84
            ['/'],
85
            ['/graphql/default'],
86
        ];
87
    }
88
89
    /**
90
     * @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
91
     * @expectedExceptionMessage Must provide query parameter
92
     */
93
    public function testEndpointWithEmptyQuery()
94
    {
95
        $client = static::createClient();
96
        $client->request('GET', '/', []);
97
        $client->getResponse()->getContent();
98
    }
99
100
    /**
101
     * @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
102
     * @expectedExceptionMessage The request content body must not be empty when using json content type request.
103
     */
104
    public function testEndpointWithEmptyJsonBodyQuery()
105
    {
106
        $client = static::createClient();
107
        $client->request('GET', '/', [], [], ['CONTENT_TYPE' => 'application/json']);
108
        $client->getResponse()->getContent();
109
    }
110
111
    /**
112
     * @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
113
     * @expectedExceptionMessage POST body sent invalid JSON
114
     */
115
    public function testEndpointWithInvalidBodyQuery()
116
    {
117
        $client = static::createClient();
118
        $client->request('GET', '/', [], [], ['CONTENT_TYPE' => 'application/json'], '{');
119
        $client->getResponse()->getContent();
120
    }
121
122
    public function testEndpointActionWithVariables()
123
    {
124
        $client = static::createClient(['test_case' => 'connection']);
125
126
        $query = <<<'EOF'
127
query FriendsQuery($firstFriends: Int) {
128
  user {
129
    friends(first: $firstFriends) {
130
      totalCount
131
      edges {
132
        friendshipTime
133
        node {
134
          name
135
        }
136
      }
137
    }
138
  }
139
}
140
EOF;
141
142
        $client->request('GET', '/', [], [], ['CONTENT_TYPE' => 'application/json'], json_encode(['query' => $query, 'variables' => '{"firstFriends": 2}']));
143
    }
144
145
    /**
146
     * @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
147
     * @expectedExceptionMessage Variables are invalid JSON
148
     */
149 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...
150
    {
151
        $client = static::createClient(['test_case' => 'connection']);
152
153
        $query = <<<'EOF'
154
query {
155
  user
156
}
157
EOF;
158
159
        $client->request('GET', '/', ['query' => $query, 'variables' => '"firstFriends": 2}']);
160
    }
161
162
    /**
163
     * @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
164
     * @expectedExceptionMessage Could not found "fake" schema.
165
     */
166 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...
167
    {
168
        $client = static::createClient(['test_case' => 'connection']);
169
170
        $query = <<<'EOF'
171
query {
172
  user
173
}
174
EOF;
175
176
        $client->request('GET', '/graphql/fake', ['query' => $query]);
177
    }
178
179
    public function testEndpointActionWithOperationName()
180
    {
181
        $client = static::createClient(['test_case' => 'connection']);
182
183
        $query = $this->friendsQuery."\n".$this->friendsTotalCountQuery;
184
185
        $client->request('POST', '/', ['query' => $query, 'operationName' => 'FriendsQuery'], [], ['CONTENT_TYPE' => 'application/x-www-form-urlencoded']);
186
        $result = $client->getResponse()->getContent();
187
        $this->assertEquals(['data' => $this->expectedData], json_decode($result, true), $result);
188
    }
189
190
    /**
191
     * @param $uri
192
     * @dataProvider graphQLBatchEndpointUriProvider
193
     */
194
    public function testBatchEndpointAction($uri)
195
    {
196
        $client = static::createClient(['test_case' => 'connection']);
197
198
        $data = [
199
            [
200
                'id' => 'friends',
201
                'query' => $this->friendsQuery,
202
            ],
203
            [
204
                'id' => 'friendsTotalCount',
205
                'query' => $this->friendsTotalCountQuery,
206
            ],
207
        ];
208
209
        $client->request('POST', $uri, [], [], ['CONTENT_TYPE' => 'application/json'], json_encode($data));
210
        $result = $client->getResponse()->getContent();
211
212
        $expected  = [
213
            ['id' => 'friends', 'payload' => ['data' => $this->expectedData]],
214
            ['id' => 'friendsTotalCount', 'payload' => ['data' => ['user' => ['friends' => ['totalCount' => 4]]]]],
215
        ];
216
        $this->assertEquals($expected, json_decode($result, true), $result);
217
    }
218
219
    public function graphQLBatchEndpointUriProvider()
220
    {
221
        return [
222
            ['/batch'],
223
            ['/graphql/default/batch'],
224
        ];
225
    }
226
227
    /**
228
     * @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
229
     * @expectedExceptionMessage Must provide at least one valid query.
230
     */
231
    public function testBatchEndpointWithEmptyQuery()
232
    {
233
        $client = static::createClient();
234
        $client->request('GET', '/batch', [], [], ['CONTENT_TYPE' => 'application/json'], '{}');
235
        $client->getResponse()->getContent();
236
    }
237
238
    /**
239
     * @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
240
     * @expectedExceptionMessage Only request with content type "application/json" is accepted.
241
     */
242
    public function testBatchEndpointWrongContentType()
243
    {
244
        $client = static::createClient();
245
        $client->request('GET', '/batch');
246
        $client->getResponse()->getContent();
247
    }
248
249
    /**
250
     * @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
251
     * @expectedExceptionMessage POST body sent invalid JSON
252
     */
253
    public function testBatchEndpointWithInvalidJson()
254
    {
255
        $client = static::createClient();
256
        $client->request('GET', '/batch', [], [], ['CONTENT_TYPE' => 'application/json'], '{');
257
        $client->getResponse()->getContent();
258
    }
259
260
    /**
261
     * @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
262
     * @expectedExceptionMessage 1 is not a valid query
263
     */
264
    public function testBatchEndpointWithInvalidQuery()
265
    {
266
        $client = static::createClient();
267
        $client->request('GET', '/batch', [], [], ['CONTENT_TYPE' => 'application/json'], '{"test" : {"query": 1}}');
268
        $client->getResponse()->getContent();
269
    }
270
271
    public function testPreflightedRequestWhenDisabled()
272
    {
273
        $client = static::createClient(['test_case' => 'connection']);
274
        $client->request('OPTIONS', '/', [], [], ['HTTP_Origin' => 'http://example.com']);
275
        $response = $client->getResponse();
276
        $this->assertEquals(200, $response->getStatusCode());
277
        $this->assertCORSHeadersNotExists($client);
278
    }
279
280
    public function testUnAuthorizedMethod()
281
    {
282
        $client = static::createClient(['test_case' => 'connection']);
283
        $client->request('PUT', '/', [], [], ['HTTP_Origin' => 'http://example.com']);
284
        $this->assertEquals(405, $client->getResponse()->getStatusCode());
285
    }
286
287
    public function testPreflightedRequestWhenEnabled()
288
    {
289
        $client = static::createClient(['test_case' => 'connectionWithCORS']);
290
        $client->request('OPTIONS', '/batch', [], [], ['HTTP_Origin' => 'http://example.com']);
291
        $this->assertCORSHeadersExists($client);
292
    }
293
294 View Code Duplication
    public function testNoCORSHeadersIfOriginHeaderNotExists()
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...
295
    {
296
        $client = static::createClient(['test_case' => 'connectionWithCORS']);
297
298
        $client->request('GET', '/', ['query' => $this->friendsQuery], [], ['CONTENT_TYPE' => 'application/graphql']);
299
        $result = $client->getResponse()->getContent();
300
        $this->assertEquals(['data' => $this->expectedData], json_decode($result, true), $result);
301
        $this->assertCORSHeadersNotExists($client);
302
    }
303
304
    private function assertCORSHeadersNotExists(Client $client)
305
    {
306
        $headers = $client->getResponse()->headers->all();
307
        $this->assertArrayNotHasKey('access-control-allow-origin', $headers);
308
        $this->assertArrayNotHasKey('access-control-allow-methods', $headers);
309
        $this->assertArrayNotHasKey('access-control-allow-credentials', $headers);
310
        $this->assertArrayNotHasKey('access-control-allow-headers', $headers);
311
        $this->assertArrayNotHasKey('access-control-max-age', $headers);
312
    }
313
314
    private function assertCORSHeadersExists(Client $client)
315
    {
316
        $response = $client->getResponse();
317
        $this->assertEquals(200, $response->getStatusCode());
318
        $this->assertEquals('http://example.com', $response->headers->get('Access-Control-Allow-Origin'));
319
        $this->assertEquals('OPTIONS, GET, POST', $response->headers->get('Access-Control-Allow-Methods'));
320
        $this->assertEquals('true', $response->headers->get('Access-Control-Allow-Credentials'));
321
        $this->assertEquals('Content-Type, Authorization', $response->headers->get('Access-Control-Allow-Headers'));
322
        $this->assertEquals(3600, $response->headers->get('Access-Control-Max-Age'));
323
    }
324
}
325