|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace GraphQL\Tests\Validator; |
| 4 | + |
| 5 | +use GraphQL\Type\Schema; |
| 6 | +use GraphQL\Utils\BuildSchema; |
| 7 | +use GraphQL\Validator\Rules\PossibleTypeExtensions; |
| 8 | + |
| 9 | +final class PossibleTypeExtensionsTest extends ValidatorTestCase |
| 10 | +{ |
| 11 | + /** |
| 12 | + * @param array<int, array<string, mixed>> $errors |
| 13 | + */ |
| 14 | + private function expectSDLErrors(string $sdlString, ?Schema $schema, array $errors): void |
| 15 | + { |
| 16 | + $this->expectSDLErrorsFromRule(new PossibleTypeExtensions(), $sdlString, $schema, $errors); |
| 17 | + } |
| 18 | + |
| 19 | + /** |
| 20 | + * @see describe('Validate: Possible type extensions') |
| 21 | + * @see it('no extensions') |
| 22 | + */ |
| 23 | + public function testNoExtensions(): void |
| 24 | + { |
| 25 | + $this->expectValidSDL( |
| 26 | + new PossibleTypeExtensions(), |
| 27 | + ' |
| 28 | + scalar FooScalar |
| 29 | + type FooObject |
| 30 | + interface FooInterface |
| 31 | + union FooUnion |
| 32 | + enum FooEnum |
| 33 | + input FooInputObject |
| 34 | + ' |
| 35 | + ); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @see it('one extension per type') |
| 40 | + */ |
| 41 | + public function testOneExtensionPerType(): void |
| 42 | + { |
| 43 | + $this->expectValidSDL( |
| 44 | + new PossibleTypeExtensions(), |
| 45 | + ' |
| 46 | + scalar FooScalar |
| 47 | + type FooObject |
| 48 | + interface FooInterface |
| 49 | + union FooUnion |
| 50 | + enum FooEnum |
| 51 | + input FooInputObject |
| 52 | +
|
| 53 | + extend scalar FooScalar @dummy |
| 54 | + extend type FooObject @dummy |
| 55 | + extend interface FooInterface @dummy |
| 56 | + extend union FooUnion @dummy |
| 57 | + extend enum FooEnum @dummy |
| 58 | + extend input FooInputObject @dummy |
| 59 | + ' |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @see it('many extensions per type') |
| 65 | + */ |
| 66 | + public function testManyExtensionsPerType(): void |
| 67 | + { |
| 68 | + $this->expectValidSDL( |
| 69 | + new PossibleTypeExtensions(), |
| 70 | + ' |
| 71 | + scalar FooScalar |
| 72 | + type FooObject |
| 73 | + interface FooInterface |
| 74 | + union FooUnion |
| 75 | + enum FooEnum |
| 76 | + input FooInputObject |
| 77 | +
|
| 78 | + extend scalar FooScalar @dummy |
| 79 | + extend type FooObject @dummy |
| 80 | + extend interface FooInterface @dummy |
| 81 | + extend union FooUnion @dummy |
| 82 | + extend enum FooEnum @dummy |
| 83 | + extend input FooInputObject @dummy |
| 84 | +
|
| 85 | + extend scalar FooScalar @dummy |
| 86 | + extend type FooObject @dummy |
| 87 | + extend interface FooInterface @dummy |
| 88 | + extend union FooUnion @dummy |
| 89 | + extend enum FooEnum @dummy |
| 90 | + extend input FooInputObject @dummy |
| 91 | + ' |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * @see it('extending unknown type') |
| 97 | + */ |
| 98 | + public function testExtendingUnknownType(): void |
| 99 | + { |
| 100 | + $message = 'Cannot extend type "Unknown" because it is not defined. Did you mean "Known"?'; |
| 101 | + $this->expectSDLErrors( |
| 102 | + ' |
| 103 | + type Known |
| 104 | +
|
| 105 | + extend scalar Unknown @dummy |
| 106 | + extend type Unknown @dummy |
| 107 | + extend interface Unknown @dummy |
| 108 | + extend union Unknown @dummy |
| 109 | + extend enum Unknown @dummy |
| 110 | + extend input Unknown @dummy |
| 111 | + ', |
| 112 | + null, |
| 113 | + [ |
| 114 | + [ |
| 115 | + 'message' => $message, |
| 116 | + 'locations' => [ |
| 117 | + ['line' => 4, 'column' => 21], |
| 118 | + ], |
| 119 | + ], |
| 120 | + [ |
| 121 | + 'message' => $message, |
| 122 | + 'locations' => [ |
| 123 | + ['line' => 5, 'column' => 19], |
| 124 | + ], |
| 125 | + ], |
| 126 | + [ |
| 127 | + 'message' => $message, |
| 128 | + 'locations' => [ |
| 129 | + ['line' => 6, 'column' => 24], |
| 130 | + ], |
| 131 | + ], |
| 132 | + [ |
| 133 | + 'message' => $message, |
| 134 | + 'locations' => [ |
| 135 | + ['line' => 7, 'column' => 20], |
| 136 | + ], |
| 137 | + ], |
| 138 | + [ |
| 139 | + 'message' => $message, |
| 140 | + 'locations' => [ |
| 141 | + ['line' => 8, 'column' => 19], |
| 142 | + ], |
| 143 | + ], |
| 144 | + [ |
| 145 | + 'message' => $message, |
| 146 | + 'locations' => [ |
| 147 | + ['line' => 9, 'column' => 20], |
| 148 | + ], |
| 149 | + ], |
| 150 | + ], |
| 151 | + ); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * @see it('does not consider non-type definitions') |
| 156 | + */ |
| 157 | + public function testDoesNotConsiderNonTypeDefinitions(): void |
| 158 | + { |
| 159 | + $message = 'Cannot extend type "Foo" because it is not defined.'; |
| 160 | + $this->expectSDLErrors( |
| 161 | + ' |
| 162 | + query Foo { __typename } |
| 163 | + fragment Foo on Query { __typename } |
| 164 | + directive @Foo on SCHEMA |
| 165 | +
|
| 166 | + extend scalar Foo @dummy |
| 167 | + extend type Foo @dummy |
| 168 | + extend interface Foo @dummy |
| 169 | + extend union Foo @dummy |
| 170 | + extend enum Foo @dummy |
| 171 | + extend input Foo @dummy |
| 172 | + ', |
| 173 | + null, |
| 174 | + [ |
| 175 | + [ |
| 176 | + 'message' => $message, |
| 177 | + 'locations' => [ |
| 178 | + ['line' => 6, 'column' => 21], |
| 179 | + ], |
| 180 | + ], |
| 181 | + [ |
| 182 | + 'message' => $message, |
| 183 | + 'locations' => [ |
| 184 | + ['line' => 7, 'column' => 19], |
| 185 | + ], |
| 186 | + ], |
| 187 | + [ |
| 188 | + 'message' => $message, |
| 189 | + 'locations' => [ |
| 190 | + ['line' => 8, 'column' => 24], |
| 191 | + ], |
| 192 | + ], |
| 193 | + [ |
| 194 | + 'message' => $message, |
| 195 | + 'locations' => [ |
| 196 | + ['line' => 9, 'column' => 20], |
| 197 | + ], |
| 198 | + ], |
| 199 | + [ |
| 200 | + 'message' => $message, |
| 201 | + 'locations' => [ |
| 202 | + ['line' => 10, 'column' => 19], |
| 203 | + ], |
| 204 | + ], |
| 205 | + [ |
| 206 | + 'message' => $message, |
| 207 | + 'locations' => [ |
| 208 | + ['line' => 11, 'column' => 20], |
| 209 | + ], |
| 210 | + ], |
| 211 | + ], |
| 212 | + ); |
| 213 | + } |
| 214 | + |
| 215 | + /** |
| 216 | + * @see it('extending with different kinds') |
| 217 | + */ |
| 218 | + public function testExtendingWithDifferentKinds(): void |
| 219 | + { |
| 220 | + $this->expectSDLErrors( |
| 221 | + ' |
| 222 | + scalar FooScalar |
| 223 | + type FooObject |
| 224 | + interface FooInterface |
| 225 | + union FooUnion |
| 226 | + enum FooEnum |
| 227 | + input FooInputObject |
| 228 | +
|
| 229 | + extend type FooScalar @dummy |
| 230 | + extend interface FooObject @dummy |
| 231 | + extend union FooInterface @dummy |
| 232 | + extend enum FooUnion @dummy |
| 233 | + extend input FooEnum @dummy |
| 234 | + extend scalar FooInputObject @dummy |
| 235 | + ', |
| 236 | + null, |
| 237 | + [ |
| 238 | + [ |
| 239 | + 'message' => 'Cannot extend non-object type "FooScalar".', |
| 240 | + 'locations' => [ |
| 241 | + ['line' => 2, 'column' => 7], |
| 242 | + ['line' => 9, 'column' => 7], |
| 243 | + ], |
| 244 | + ], |
| 245 | + [ |
| 246 | + 'message' => 'Cannot extend non-interface type "FooObject".', |
| 247 | + 'locations' => [ |
| 248 | + ['line' => 3, 'column' => 7], |
| 249 | + ['line' => 10, 'column' => 7], |
| 250 | + ], |
| 251 | + ], |
| 252 | + [ |
| 253 | + 'message' => 'Cannot extend non-union type "FooInterface".', |
| 254 | + 'locations' => [ |
| 255 | + ['line' => 4, 'column' => 7], |
| 256 | + ['line' => 11, 'column' => 7], |
| 257 | + ], |
| 258 | + ], |
| 259 | + [ |
| 260 | + 'message' => 'Cannot extend non-enum type "FooUnion".', |
| 261 | + 'locations' => [ |
| 262 | + ['line' => 5, 'column' => 7], |
| 263 | + ['line' => 12, 'column' => 7], |
| 264 | + ], |
| 265 | + ], |
| 266 | + [ |
| 267 | + 'message' => 'Cannot extend non-input object type "FooEnum".', |
| 268 | + 'locations' => [ |
| 269 | + ['line' => 6, 'column' => 7], |
| 270 | + ['line' => 13, 'column' => 7], |
| 271 | + ], |
| 272 | + ], |
| 273 | + [ |
| 274 | + 'message' => 'Cannot extend non-scalar type "FooInputObject".', |
| 275 | + 'locations' => [ |
| 276 | + ['line' => 7, 'column' => 7], |
| 277 | + ['line' => 14, 'column' => 7], |
| 278 | + ], |
| 279 | + ], |
| 280 | + ], |
| 281 | + ); |
| 282 | + } |
| 283 | + |
| 284 | + /** |
| 285 | + * @see it('extending types within existing schema') |
| 286 | + */ |
| 287 | + public function testExtendingTypesWithinExistingSchema(): void |
| 288 | + { |
| 289 | + $schema = BuildSchema::build(' |
| 290 | + scalar FooScalar |
| 291 | + type FooObject |
| 292 | + interface FooInterface |
| 293 | + union FooUnion |
| 294 | + enum FooEnum |
| 295 | + input FooInputObject |
| 296 | + '); |
| 297 | + $sdl = ' |
| 298 | + extend scalar FooScalar @dummy |
| 299 | + extend type FooObject @dummy |
| 300 | + extend interface FooInterface @dummy |
| 301 | + extend union FooUnion @dummy |
| 302 | + extend enum FooEnum @dummy |
| 303 | + extend input FooInputObject @dummy |
| 304 | + '; |
| 305 | + $this->expectValidSDL( |
| 306 | + new PossibleTypeExtensions(), |
| 307 | + $sdl, |
| 308 | + $schema, |
| 309 | + ); |
| 310 | + } |
| 311 | + |
| 312 | + /** |
| 313 | + * @see it('extending unknown types within existing schema') |
| 314 | + */ |
| 315 | + public function testExtendingUnknownTypesWithinExistingSchema(): void |
| 316 | + { |
| 317 | + $schema = BuildSchema::build('type Known'); |
| 318 | + $sdl = ' |
| 319 | + extend scalar Unknown @dummy |
| 320 | + extend type Unknown @dummy |
| 321 | + extend interface Unknown @dummy |
| 322 | + extend union Unknown @dummy |
| 323 | + extend enum Unknown @dummy |
| 324 | + extend input Unknown @dummy |
| 325 | + '; |
| 326 | + $message = 'Cannot extend type "Unknown" because it is not defined. Did you mean "Known"?'; |
| 327 | + $this->expectSDLErrors( |
| 328 | + $sdl, |
| 329 | + $schema, |
| 330 | + [ |
| 331 | + [ |
| 332 | + 'message' => $message, |
| 333 | + 'locations' => [ |
| 334 | + ['line' => 2, 'column' => 21], |
| 335 | + ], |
| 336 | + ], |
| 337 | + [ |
| 338 | + 'message' => $message, |
| 339 | + 'locations' => [ |
| 340 | + ['line' => 3, 'column' => 19], |
| 341 | + ], |
| 342 | + ], |
| 343 | + [ |
| 344 | + 'message' => $message, |
| 345 | + 'locations' => [ |
| 346 | + ['line' => 4, 'column' => 24], |
| 347 | + ], |
| 348 | + ], |
| 349 | + [ |
| 350 | + 'message' => $message, |
| 351 | + 'locations' => [ |
| 352 | + ['line' => 5, 'column' => 20], |
| 353 | + ], |
| 354 | + ], |
| 355 | + [ |
| 356 | + 'message' => $message, |
| 357 | + 'locations' => [ |
| 358 | + ['line' => 6, 'column' => 19], |
| 359 | + ], |
| 360 | + ], |
| 361 | + [ |
| 362 | + 'message' => $message, |
| 363 | + 'locations' => [ |
| 364 | + ['line' => 7, 'column' => 20], |
| 365 | + ], |
| 366 | + ], |
| 367 | + ], |
| 368 | + ); |
| 369 | + } |
| 370 | + |
| 371 | + /** |
| 372 | + * @see it('extending types with different kinds within existing schema') |
| 373 | + */ |
| 374 | + public function testExtendingTypesWithDifferentKindsWithinExistingSchema(): void |
| 375 | + { |
| 376 | + $schema = BuildSchema::build(' |
| 377 | + scalar FooScalar |
| 378 | + type FooObject |
| 379 | + interface FooInterface |
| 380 | + union FooUnion |
| 381 | + enum FooEnum |
| 382 | + input FooInputObject |
| 383 | + '); |
| 384 | + $sdl = ' |
| 385 | + extend type FooScalar @dummy |
| 386 | + extend interface FooObject @dummy |
| 387 | + extend union FooInterface @dummy |
| 388 | + extend enum FooUnion @dummy |
| 389 | + extend input FooEnum @dummy |
| 390 | + extend scalar FooInputObject @dummy |
| 391 | + '; |
| 392 | + $this->expectSDLErrors( |
| 393 | + $sdl, |
| 394 | + $schema, |
| 395 | + [ |
| 396 | + [ |
| 397 | + 'message' => 'Cannot extend non-object type "FooScalar".', |
| 398 | + 'locations' => [ |
| 399 | + ['line' => 2, 'column' => 7], |
| 400 | + ], |
| 401 | + ], |
| 402 | + [ |
| 403 | + 'message' => 'Cannot extend non-interface type "FooObject".', |
| 404 | + 'locations' => [ |
| 405 | + ['line' => 3, 'column' => 7], |
| 406 | + ], |
| 407 | + ], |
| 408 | + [ |
| 409 | + 'message' => 'Cannot extend non-union type "FooInterface".', |
| 410 | + 'locations' => [ |
| 411 | + ['line' => 4, 'column' => 7], |
| 412 | + ], |
| 413 | + ], |
| 414 | + [ |
| 415 | + 'message' => 'Cannot extend non-enum type "FooUnion".', |
| 416 | + 'locations' => [ |
| 417 | + ['line' => 5, 'column' => 7], |
| 418 | + ], |
| 419 | + ], |
| 420 | + [ |
| 421 | + 'message' => 'Cannot extend non-input object type "FooEnum".', |
| 422 | + 'locations' => [ |
| 423 | + ['line' => 6, 'column' => 7], |
| 424 | + ], |
| 425 | + ], |
| 426 | + [ |
| 427 | + 'message' => 'Cannot extend non-scalar type "FooInputObject".', |
| 428 | + 'locations' => [ |
| 429 | + ['line' => 7, 'column' => 7], |
| 430 | + ], |
| 431 | + ], |
| 432 | + ], |
| 433 | + ); |
| 434 | + } |
| 435 | +} |
0 commit comments