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

Passed
Pull Request — master (#195)
by Renato
07:27 queued 01:12
created

GraphControllerTest   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 310
Duplicated Lines 13.55 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 21
c 3
b 0
f 0
lcom 1
cbo 5
dl 42
loc 310
rs 10

21 Methods

Rating   Name   Duplication   Size   Complexity  
A testEndpointAction() 9 9 1
A graphQLEndpointUriProvider() 0 7 1
A testEndpointWithEmptyQuery() 0 6 1
A testEndpointWithEmptyJsonBodyQuery() 0 6 1
A testEndpointWithInvalidBodyQuery() 0 6 1
A testEndpointActionWithInvalidVariables() 12 12 1
A testMultipleEndpointActionWithUnknownSchemaName() 12 12 1
A testEndpointActionWithOperationName() 0 10 1
B testBatchEndpointAction() 0 24 1
A graphQLBatchEndpointUriProvider() 0 7 1
A testBatchEndpointWithEmptyQuery() 0 6 1
A testBatchEndpointWrongContentType() 0 6 1
A testBatchEndpointWithInvalidJson() 0 6 1
A testBatchEndpointWithInvalidQuery() 0 6 1
A testPreflightedRequestWhenDisabled() 0 8 1
A testUnAuthorizedMethod() 0 6 1
A testPreflightedRequestWhenEnabled() 0 6 1
A testNoCORSHeadersIfOriginHeaderNotExists() 9 9 1
A assertCORSHeadersNotExists() 0 9 1
A assertCORSHeadersExists() 0 10 1
B testEndpointActionWithVariables() 0 24 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
        $this->assertSame(200, $client->getResponse()->getStatusCode());
145
    }
146
147
    /**
148
     * @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
149
     * @expectedExceptionMessage Variables are invalid JSON
150
     */
151 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...
152
    {
153
        $client = static::createClient(['test_case' => 'connection']);
154
155
        $query = <<<'EOF'
156
query {
157
  user
158
}
159
EOF;
160
161
        $client->request('GET', '/', ['query' => $query, 'variables' => '"firstFriends": 2}']);
162
    }
163
164
    /**
165
     * @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
166
     * @expectedExceptionMessage Could not found "fake" schema.
167
     */
168 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...
169
    {
170
        $client = static::createClient(['test_case' => 'connection']);
171
172
        $query = <<<'EOF'
173
query {
174
  user
175
}
176
EOF;
177
178
        $client->request('GET', '/graphql/fake', ['query' => $query]);
179
    }
180
181
    public function testEndpointActionWithOperationName()
182
    {
183
        $client = static::createClient(['test_case' => 'connection']);
184
185
        $query = $this->friendsQuery."\n".$this->friendsTotalCountQuery;
186
187
        $client->request('POST', '/', ['query' => $query, 'operationName' => 'FriendsQuery'], [], ['CONTENT_TYPE' => 'application/x-www-form-urlencoded']);
188
        $result = $client->getResponse()->getContent();
189
        $this->assertEquals(['data' => $this->expectedData], json_decode($result, true), $result);
190
    }
191
192
    /**
193
     * @param $uri
194
     * @dataProvider graphQLBatchEndpointUriProvider
195
     */
196
    public function testBatchEndpointAction($uri)
197
    {
198
        $client = static::createClient(['test_case' => 'connection']);
199
200
        $data = [
201
            [
202
                'id' => 'friends',
203
                'query' => $this->friendsQuery,
204
            ],
205
            [
206
                'id' => 'friendsTotalCount',
207
                'query' => $this->friendsTotalCountQuery,
208
            ],
209
        ];
210
211
        $client->request('POST', $uri, [], [], ['CONTENT_TYPE' => 'application/json'], json_encode($data));
212
        $result = $client->getResponse()->getContent();
213
214
        $expected = [
215
            ['id' => 'friends', 'payload' => ['data' => $this->expectedData]],
216
            ['id' => 'friendsTotalCount', 'payload' => ['data' => ['user' => ['friends' => ['totalCount' => 4]]]]],
217
        ];
218
        $this->assertEquals($expected, json_decode($result, true), $result);
219
    }
220
221
    public function graphQLBatchEndpointUriProvider()
222
    {
223
        return [
224
            ['/batch'],
225
            ['/graphql/default/batch'],
226
        ];
227
    }
228
229
    /**
230
     * @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
231
     * @expectedExceptionMessage Must provide at least one valid query.
232
     */
233
    public function testBatchEndpointWithEmptyQuery()
234
    {
235
        $client = static::createClient();
236
        $client->request('GET', '/batch', [], [], ['CONTENT_TYPE' => 'application/json'], '{}');
237
        $client->getResponse()->getContent();
238
    }
239
240
    /**
241
     * @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
242
     * @expectedExceptionMessage Only request with content type "application/json" is accepted.
243
     */
244
    public function testBatchEndpointWrongContentType()
245
    {
246
        $client = static::createClient();
247
        $client->request('GET', '/batch');
248
        $client->getResponse()->getContent();
249
    }
250
251
    /**
252
     * @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
253
     * @expectedExceptionMessage POST body sent invalid JSON
254
     */
255
    public function testBatchEndpointWithInvalidJson()
256
    {
257
        $client = static::createClient();
258
        $client->request('GET', '/batch', [], [], ['CONTENT_TYPE' => 'application/json'], '{');
259
        $client->getResponse()->getContent();
260
    }
261
262
    /**
263
     * @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
264
     * @expectedExceptionMessage 1 is not a valid query
265
     */
266
    public function testBatchEndpointWithInvalidQuery()
267
    {
268
        $client = static::createClient();
269
        $client->request('GET', '/batch', [], [], ['CONTENT_TYPE' => 'application/json'], '{"test" : {"query": 1}}');
270
        $client->getResponse()->getContent();
271
    }
272
273
    public function testPreflightedRequestWhenDisabled()
274
    {
275
        $client = static::createClient(['test_case' => 'connection']);
276
        $client->request('OPTIONS', '/', [], [], ['HTTP_Origin' => 'http://example.com']);
277
        $response = $client->getResponse();
278
        $this->assertEquals(200, $response->getStatusCode());
279
        $this->assertCORSHeadersNotExists($client);
280
    }
281
282
    public function testUnAuthorizedMethod()
283
    {
284
        $client = static::createClient(['test_case' => 'connection']);
285
        $client->request('PUT', '/', [], [], ['HTTP_Origin' => 'http://example.com']);
286
        $this->assertEquals(405, $client->getResponse()->getStatusCode());
287
    }
288
289
    public function testPreflightedRequestWhenEnabled()
290
    {
291
        $client = static::createClient(['test_case' => 'connectionWithCORS']);
292
        $client->request('OPTIONS', '/batch', [], [], ['HTTP_Origin' => 'http://example.com']);
293
        $this->assertCORSHeadersExists($client);
294
    }
295
296 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...
297
    {
298
        $client = static::createClient(['test_case' => 'connectionWithCORS']);
299
300
        $client->request('GET', '/', ['query' => $this->friendsQuery], [], ['CONTENT_TYPE' => 'application/graphql']);
301
        $result = $client->getResponse()->getContent();
302
        $this->assertEquals(['data' => $this->expectedData], json_decode($result, true), $result);
303
        $this->assertCORSHeadersNotExists($client);
304
    }
305
306
    private function assertCORSHeadersNotExists(Client $client)
307
    {
308
        $headers = $client->getResponse()->headers->all();
309
        $this->assertArrayNotHasKey('access-control-allow-origin', $headers);
310
        $this->assertArrayNotHasKey('access-control-allow-methods', $headers);
311
        $this->assertArrayNotHasKey('access-control-allow-credentials', $headers);
312
        $this->assertArrayNotHasKey('access-control-allow-headers', $headers);
313
        $this->assertArrayNotHasKey('access-control-max-age', $headers);
314
    }
315
316
    private function assertCORSHeadersExists(Client $client)
317
    {
318
        $response = $client->getResponse();
319
        $this->assertEquals(200, $response->getStatusCode());
320
        $this->assertEquals('http://example.com', $response->headers->get('Access-Control-Allow-Origin'));
321
        $this->assertEquals('OPTIONS, GET, POST', $response->headers->get('Access-Control-Allow-Methods'));
322
        $this->assertEquals('true', $response->headers->get('Access-Control-Allow-Credentials'));
323
        $this->assertEquals('Content-Type, Authorization', $response->headers->get('Access-Control-Allow-Headers'));
324
        $this->assertEquals(3600, $response->headers->get('Access-Control-Max-Age'));
325
    }
326
}
327