We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
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 |
||
| 9 | class AccessTest extends TestCase |
||
| 10 | { |
||
| 11 | /** @var \Closure */ |
||
| 12 | private $loader; |
||
| 13 | |||
| 14 | private $userNameQuery = 'query { user { name } }'; |
||
| 15 | |||
| 16 | private $userRolesQuery = 'query { user { roles } }'; |
||
| 17 | |||
| 18 | private $userIsEnabledQuery = 'query { user { isEnabled } }'; |
||
|
|
|||
| 19 | |||
| 20 | private $userFriendsQuery = <<<'EOF' |
||
| 21 | query { |
||
| 22 | user { |
||
| 23 | friends(first: 2) { |
||
| 24 | edges { |
||
| 25 | node { |
||
| 26 | name |
||
| 27 | } |
||
| 28 | } |
||
| 29 | } |
||
| 30 | } |
||
| 31 | } |
||
| 32 | EOF; |
||
| 33 | |||
| 34 | private $simpleMutationWithThunkQuery = <<<'EOF' |
||
| 35 | mutation M { |
||
| 36 | simpleMutationWithThunkFields(input: {inputData: %d, clientMutationId: "bac"}) { |
||
| 37 | result |
||
| 38 | clientMutationId |
||
| 39 | } |
||
| 40 | } |
||
| 41 | EOF; |
||
| 42 | |||
| 43 | public function setUp() |
||
| 44 | { |
||
| 45 | parent::setUp(); |
||
| 46 | // load types |
||
| 47 | $this->loader = function ($class) { |
||
| 48 | if (preg_match('@^'.preg_quote('Overblog\GraphQLBundle\Access\__DEFINITIONS__\\').'(.*)$@', $class, $matches)) { |
||
| 49 | $file = '/tmp/OverblogGraphQLBundle/'.Kernel::VERSION.'/access/cache/testaccess/overblog/graphql-bundle/__definitions__/'.$matches[1].'.php'; |
||
| 50 | if (file_exists($file)) { |
||
| 51 | require $file; |
||
| 52 | } |
||
| 53 | } |
||
| 54 | }; |
||
| 55 | spl_autoload_register($this->loader); |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @expectedException \RuntimeException |
||
| 60 | * @expectedExceptionMessage Type class for alias "RootQuery" could not be load. If you are using your own classLoader verify the path and the namespace please. |
||
| 61 | * @requires PHP 7 |
||
| 62 | */ |
||
| 63 | public function testCustomClassLoaderNotRegister() |
||
| 64 | { |
||
| 65 | spl_autoload_unregister($this->loader); |
||
| 66 | $this->assertResponse($this->userNameQuery, [], static::ANONYMOUS_USER, 'access'); |
||
| 67 | } |
||
| 68 | |||
| 69 | View Code Duplication | public function testNotAuthenticatedUserAccessToUserName() |
|
| 70 | { |
||
| 71 | $expected = [ |
||
| 72 | 'data' => [ |
||
| 73 | 'user' => [ |
||
| 74 | 'name' => null, |
||
| 75 | ], |
||
| 76 | ], |
||
| 77 | 'extensions' => [ |
||
| 78 | 'warnings' => [ |
||
| 79 | [ |
||
| 80 | 'message' => 'Access denied to this field.', |
||
| 81 | 'locations' => [['line' => 1, 'column' => 16]], |
||
| 82 | 'path' => ['user', 'name'], |
||
| 83 | 'category' => 'user', |
||
| 84 | ], |
||
| 85 | ], |
||
| 86 | ], |
||
| 87 | ]; |
||
| 88 | |||
| 89 | $this->assertResponse($this->userNameQuery, $expected, static::ANONYMOUS_USER, 'access'); |
||
| 90 | } |
||
| 91 | |||
| 92 | public function testFullyAuthenticatedUserAccessToUserName() |
||
| 93 | { |
||
| 94 | $expected = [ |
||
| 95 | 'data' => [ |
||
| 96 | 'user' => [ |
||
| 97 | 'name' => 'Dan', |
||
| 98 | ], |
||
| 99 | ], |
||
| 100 | ]; |
||
| 101 | |||
| 102 | $this->assertResponse($this->userNameQuery, $expected, static::USER_RYAN, 'access'); |
||
| 103 | } |
||
| 104 | |||
| 105 | public function testNotAuthenticatedUserAccessToUserRoles() |
||
| 106 | { |
||
| 107 | $this->assertResponse($this->userRolesQuery, $this->expectedFailedUserRoles(), static::ANONYMOUS_USER, 'access'); |
||
| 108 | } |
||
| 109 | |||
| 110 | public function testAuthenticatedUserAccessToUserRolesWithoutEnoughRights() |
||
| 111 | { |
||
| 112 | $this->assertResponse($this->userRolesQuery, $this->expectedFailedUserRoles(), static::USER_RYAN, 'access'); |
||
| 113 | } |
||
| 114 | |||
| 115 | public function testUserWithCorrectRightsAccessToUserRoles() |
||
| 116 | { |
||
| 117 | $expected = [ |
||
| 118 | 'data' => [ |
||
| 119 | 'user' => [ |
||
| 120 | 'roles' => ['ROLE_USER'], |
||
| 121 | ], |
||
| 122 | ], |
||
| 123 | ]; |
||
| 124 | |||
| 125 | $this->assertResponse($this->userRolesQuery, $expected, static::USER_ADMIN, 'access'); |
||
| 126 | } |
||
| 127 | |||
| 128 | View Code Duplication | public function testUserForbiddenField() |
|
| 129 | { |
||
| 130 | $expected = [ |
||
| 131 | 'data' => [ |
||
| 132 | 'user' => null, |
||
| 133 | ], |
||
| 134 | 'extensions' => [ |
||
| 135 | 'warnings' => [ |
||
| 136 | [ |
||
| 137 | 'message' => 'Access denied to this field.', |
||
| 138 | 'locations' => [ |
||
| 139 | [ |
||
| 140 | 'line' => 3, |
||
| 141 | 'column' => 5, |
||
| 142 | ], |
||
| 143 | ], |
||
| 144 | 'path' => ['user', 'forbidden'], |
||
| 145 | 'category' => 'user', |
||
| 146 | ], |
||
| 147 | ], |
||
| 148 | ], |
||
| 149 | ]; |
||
| 150 | |||
| 151 | $query = <<<'EOF' |
||
| 152 | query MyQuery { |
||
| 153 | user { |
||
| 154 | forbidden |
||
| 155 | } |
||
| 156 | } |
||
| 157 | EOF; |
||
| 158 | |||
| 159 | $this->assertResponse($query, $expected, static::USER_ADMIN, 'access'); |
||
| 160 | } |
||
| 161 | |||
| 162 | public function testUserAccessToUserFriends() |
||
| 179 | |||
| 180 | public function testMutationAllowedUser() |
||
| 196 | |||
| 197 | public function testMutationAllowedButNoRightsToDisplayPayload() |
||
| 198 | { |
||
| 199 | $expected = [ |
||
| 200 | 'data' => [ |
||
| 201 | 'simpleMutationWithThunkFields' => [ |
||
| 202 | 'result' => null, |
||
| 203 | 'clientMutationId' => 'bac', |
||
| 204 | ], |
||
| 226 | |||
| 227 | public function testMutationNotAllowedUser() |
||
| 251 | |||
| 252 | private function expectedFailedUserRoles() |
||
| 262 | } |
||
| 263 |
This check marks private properties in classes that are never used. Those properties can be removed.