|
| 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\UniqueDirectiveNames; |
| 8 | + |
| 9 | +final class UniqueDirectiveNamesTest 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 UniqueDirectiveNames(), $sdlString, $schema, $errors); |
| 17 | + } |
| 18 | + |
| 19 | + /** |
| 20 | + * @see describe('Validate: Unique directive names') |
| 21 | + * @see it('no directive') |
| 22 | + */ |
| 23 | + public function testNoDirective(): void |
| 24 | + { |
| 25 | + $this->expectValidSDL( |
| 26 | + new UniqueDirectiveNames(), |
| 27 | + ' |
| 28 | + type Foo |
| 29 | + ' |
| 30 | + ); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * @see it('one directive') |
| 35 | + */ |
| 36 | + public function testOneDirective(): void |
| 37 | + { |
| 38 | + $this->expectValidSDL( |
| 39 | + new UniqueDirectiveNames(), |
| 40 | + ' |
| 41 | + directive @foo on SCHEMA |
| 42 | + ' |
| 43 | + ); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @see it('many directives') |
| 48 | + */ |
| 49 | + public function testManyDirectives(): void |
| 50 | + { |
| 51 | + $this->expectValidSDL( |
| 52 | + new UniqueDirectiveNames(), |
| 53 | + ' |
| 54 | + directive @foo on SCHEMA |
| 55 | + directive @bar on SCHEMA |
| 56 | + directive @baz on SCHEMA |
| 57 | + ' |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @see it('directive and non-directive definitions named the same') |
| 63 | + */ |
| 64 | + public function testDirectiveAndNonDirectiveDefinitionsNamedTheSame(): void |
| 65 | + { |
| 66 | + $this->expectValidSDL( |
| 67 | + new UniqueDirectiveNames(), |
| 68 | + ' |
| 69 | + query foo { __typename } |
| 70 | + fragment foo on foo { __typename } |
| 71 | + type foo |
| 72 | +
|
| 73 | + directive @foo on SCHEMA |
| 74 | + ' |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @see it('directives named the same') |
| 80 | + */ |
| 81 | + public function testDirectivesNamedTheSame(): void |
| 82 | + { |
| 83 | + $this->expectSDLErrors( |
| 84 | + ' |
| 85 | + directive @foo on SCHEMA |
| 86 | +
|
| 87 | + directive @foo on SCHEMA |
| 88 | + ', |
| 89 | + null, |
| 90 | + [ |
| 91 | + [ |
| 92 | + 'message' => 'There can be only one directive named "@foo".', |
| 93 | + 'locations' => [ |
| 94 | + ['line' => 2, 'column' => 18], |
| 95 | + ['line' => 4, 'column' => 18], |
| 96 | + ], |
| 97 | + ], |
| 98 | + ], |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * @see it('adding new directive to existing schema') |
| 104 | + */ |
| 105 | + public function testAddingNewDirectiveToExistingSchema(): void |
| 106 | + { |
| 107 | + $schema = BuildSchema::build('directive @foo on SCHEMA'); |
| 108 | + |
| 109 | + $this->expectValidSDL(new UniqueDirectiveNames(), 'directive @bar on SCHEMA', $schema); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * @see it('adding new directive with standard name to existing schema') |
| 114 | + */ |
| 115 | + public function testAddingNewDirectiveWithStandardNameToExistingSchema(): void |
| 116 | + { |
| 117 | + $schema = BuildSchema::build('type foo'); |
| 118 | + |
| 119 | + $this->expectSDLErrors( |
| 120 | + 'directive @skip on SCHEMA', |
| 121 | + $schema, |
| 122 | + [ |
| 123 | + [ |
| 124 | + 'message' => 'Directive "@skip" already exists in the schema. It cannot be redefined.', |
| 125 | + 'locations' => [ |
| 126 | + ['line' => 1, 'column' => 12], |
| 127 | + ], |
| 128 | + ], |
| 129 | + ], |
| 130 | + ); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * @see it('adding new directive to existing schema with same-named type') |
| 135 | + */ |
| 136 | + public function testAddingNewDirectiveToExistingSchemaWithSameNamedType(): void |
| 137 | + { |
| 138 | + $schema = BuildSchema::build('type foo'); |
| 139 | + |
| 140 | + $this->expectValidSDL(new UniqueDirectiveNames(), 'directive @foo on SCHEMA', $schema); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * @see it('adding conflicting directives to existing schema') |
| 145 | + */ |
| 146 | + public function testAddingConflictingDirectivesToExistingSchema(): void |
| 147 | + { |
| 148 | + $schema = BuildSchema::build('directive @foo on SCHEMA'); |
| 149 | + |
| 150 | + $this->expectSDLErrors( |
| 151 | + 'directive @foo on SCHEMA', |
| 152 | + $schema, |
| 153 | + [ |
| 154 | + [ |
| 155 | + 'message' => 'Directive "@foo" already exists in the schema. It cannot be redefined.', |
| 156 | + 'locations' => [['line' => 1, 'column' => 12]], |
| 157 | + ], |
| 158 | + ], |
| 159 | + ); |
| 160 | + } |
| 161 | +} |
0 commit comments