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 ( 8a0c8b...6ebb14 )
by Jérémiah
06:05
created

AccessTest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 247
Duplicated Lines 25.1 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 14
c 6
b 0
f 0
lcom 1
cbo 3
dl 62
loc 247
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A testNotAuthenticatedUserAccessToUserName() 20 20 1
A testFullyAuthenticatedUserAccessToUserName() 0 12 1
A testNotAuthenticatedUserAccessToUserRoles() 0 4 1
A testAuthenticatedUserAccessToUserRolesWithoutEnoughRights() 0 4 1
A testUserWithCorrectRightsAccessToUserRoles() 0 12 1
A testUserAccessToUserFriends() 0 17 1
B testUserForbiddenField() 31 31 1
A testMutationAllowedUser() 0 16 1
B testMutationAllowedButNoRightsToDisplayPayload() 0 27 1
A testMutationNotAllowedUser() 0 22 1
A expectedFailedUserRoles() 0 10 1
A assertResponse() 11 11 1
A createClientAuthenticated() 0 13 2

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\Security;
13
14
use Overblog\GraphQLBundle\Tests\Functional\app\Mutation\SimpleMutationWithThunkFieldsMutation;
15
use Overblog\GraphQLBundle\Tests\Functional\TestCase;
16
17
class AccessTest extends TestCase
18
{
19
    const USER_RYAN = 'ryan';
20
    const USER_ADMIN = 'admin';
21
    const ANONYMOUS_USER = null;
22
23
    private $userNameQuery = 'query MyQuery { user { name } }';
24
25
    private $userRolesQuery = 'query MyQuery { user { roles } }';
26
27
    private $userIsEnabledQuery = 'query MyQuery { user { isEnabled } }';
0 ignored issues
show
Unused Code introduced by
The property $userIsEnabledQuery is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
28
29
    private $userFriendsQuery = <<<EOF
30
query MyQuery {
31
  user {
32
    friends(first: 2) {
33
      edges {
34
        node {
35
          name
36
        }
37
      }
38
    }
39
  }
40
}
41
EOF;
42
43
    private $simpleMutationWithThunkQuery = <<<EOF
44
mutation M {
45
  simpleMutationWithThunkFields(input: {inputData: %d, clientMutationId: "bac"}) {
46
    result
47
    clientMutationId
48
  }
49
}
50
EOF;
51
52 View Code Duplication
    public function testNotAuthenticatedUserAccessToUserName()
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...
53
    {
54
        $expected = [
55
            'data' => [
56
                'user' => [
57
                    'name' => null,
58
                ],
59
            ],
60
            'extensions' => [
61
                'warnings' => [
62
                    [
63
                        'message' => 'Access denied to this field.',
64
                        'locations' => [['line' => 1, 'column' => 24]],
65
                    ],
66
                ],
67
            ],
68
        ];
69
70
        $this->assertResponse($this->userNameQuery, $expected, static::ANONYMOUS_USER);
71
    }
72
73
    public function testFullyAuthenticatedUserAccessToUserName()
74
    {
75
        $expected = [
76
            'data' => [
77
                'user' => [
78
                    'name' => 'Dan',
79
                ],
80
            ],
81
        ];
82
83
        $this->assertResponse($this->userNameQuery, $expected, static::USER_RYAN);
84
    }
85
86
    public function testNotAuthenticatedUserAccessToUserRoles()
87
    {
88
        $this->assertResponse($this->userRolesQuery, $this->expectedFailedUserRoles(), static::ANONYMOUS_USER);
89
    }
90
91
    public function testAuthenticatedUserAccessToUserRolesWithoutEnoughRights()
92
    {
93
        $this->assertResponse($this->userRolesQuery, $this->expectedFailedUserRoles(), static::USER_RYAN);
94
    }
95
96
    public function testUserWithCorrectRightsAccessToUserRoles()
97
    {
98
        $expected = [
99
            'data' => [
100
                'user' => [
101
                    'roles' => ['ROLE_USER'],
102
                ],
103
            ],
104
        ];
105
106
        $this->assertResponse($this->userRolesQuery, $expected, static::USER_ADMIN);
107
    }
108
109 View Code Duplication
    public function testUserForbiddenField()
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...
110
    {
111
        $expected = [
112
            'data' => [
113
                'user' => null,
114
            ],
115
            'extensions' => [
116
                'warnings' => [
117
                    [
118
                        'message' => 'Access denied to this field.',
119
                        'locations' => [
120
                            [
121
                                'line' => 3,
122
                                'column' => 5,
123
                            ],
124
                        ],
125
                    ],
126
                ],
127
            ],
128
        ];
129
130
        $query = <<<EOF
131
query MyQuery {
132
  user {
133
    forbidden
134
  }
135
}
136
EOF;
137
138
        $this->assertResponse($query, $expected, static::USER_ADMIN);
139
    }
140
141
    public function testUserAccessToUserFriends()
142
    {
143
        $expected = [
144
            'data' => [
145
                'user' => [
146
                    'friends' => [
147
                        'edges' => [
148
                            ['node' => ['name' => 'Nick']],
149
                            ['node' => null],
150
                        ],
151
                    ],
152
                ],
153
            ],
154
        ];
155
156
        $this->assertResponse($this->userFriendsQuery, $expected, static::USER_ADMIN);
157
    }
158
159
    public function testMutationAllowedUser()
160
    {
161
        $result = 123;
162
163
        $expected = [
164
            'data' => [
165
                'simpleMutationWithThunkFields' => [
166
                    'result' => $result,
167
                    'clientMutationId' => 'bac',
168
                ],
169
            ],
170
        ];
171
172
        $this->assertResponse(sprintf($this->simpleMutationWithThunkQuery, $result), $expected, static::USER_ADMIN);
173
        $this->assertTrue(SimpleMutationWithThunkFieldsMutation::hasMutate(true));
174
    }
175
176
    public function testMutationAllowedButNoRightsToDisplayPayload()
177
    {
178
        $expected = [
179
            'data' => [
180
                'simpleMutationWithThunkFields' => [
181
                    'result' => null,
182
                    'clientMutationId' => 'bac',
183
                ],
184
            ],
185
            'extensions' => [
186
                'warnings' => [
187
                    [
188
                        'message' => 'Access denied to this field.',
189
                        'locations' => [
190
                            [
191
                                'line' => 3,
192
                                'column' => 5,
193
                            ],
194
                        ],
195
                    ],
196
                ],
197
            ],
198
        ];
199
200
        $this->assertResponse(sprintf($this->simpleMutationWithThunkQuery, 321), $expected, static::USER_ADMIN);
201
        $this->assertTrue(SimpleMutationWithThunkFieldsMutation::hasMutate(true));
202
    }
203
204
    public function testMutationNotAllowedUser()
205
    {
206
        $expected = [
207
            'data' => [
208
                'simpleMutationWithThunkFields' => null,
209
            ],
210
            'errors' => [
211
                [
212
                    'message' => 'Access denied to this field.',
213
                    'locations' => [
214
                        [
215
                            'line' => 2,
216
                            'column' => 3,
217
                        ],
218
                    ],
219
                ],
220
            ],
221
        ];
222
223
        $this->assertResponse(sprintf($this->simpleMutationWithThunkQuery, 123), $expected, static::USER_RYAN);
224
        $this->assertFalse(SimpleMutationWithThunkFieldsMutation::hasMutate(true));
225
    }
226
227
    private function expectedFailedUserRoles()
228
    {
229
        return [
230
            'data' => [
231
                'user' => [
232
                    'roles' => [0 => null],
233
                ],
234
            ],
235
        ];
236
    }
237
238 View Code Duplication
    private static function assertResponse($query, array $expected, $username)
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...
239
    {
240
        $client = self::createClientAuthenticated($username);
241
        $client->request('GET', '/', ['query' => $query]);
242
243
        $result = $client->getResponse()->getContent();
244
245
        static::assertEquals($expected, json_decode($result, true), $result);
246
247
        return $client;
248
    }
249
250
    private static function createClientAuthenticated($username)
251
    {
252
        $client = static::createClient(['test_case' => 'access']);
253
254
        if ($username) {
255
            $client->setServerParameters([
256
                'PHP_AUTH_USER' => $username,
257
                'PHP_AUTH_PW' => '123',
258
            ]);
259
        }
260
261
        return $client;
262
    }
263
}
264