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