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 |
||
| 21 | |||
| 22 | use Youshido\GraphQL\Schema\Schema; |
||
| 23 | use Youshido\GraphQL\Type\NonNullType; |
||
| 24 | use Youshido\GraphQL\Type\Object\ObjectType; |
||
| 25 | use Youshido\GraphQL\Type\Scalar\IntType; |
||
| 26 | use Youshido\GraphQL\Type\Scalar\StringType; |
||
| 27 | use Youshido\GraphQL\Validator\SchemaValidator\SchemaValidator; |
||
| 28 | use Youshido\Tests\DataProvider\TestEmptySchema; |
||
| 29 | use Youshido\Tests\DataProvider\TestInterfaceType; |
||
| 30 | |||
| 31 | class SchemaValidatorTest extends \PHPUnit_Framework_TestCase |
||
| 32 | { |
||
| 33 | public function testInvalidSchema(): void |
||
| 34 | { |
||
| 35 | $this->expectException(\Youshido\GraphQL\Exception\ConfigurationException::class); |
||
| 36 | |||
| 37 | $validator = new SchemaValidator(); |
||
| 38 | $validator->validate(new TestEmptySchema()); |
||
| 39 | } |
||
| 40 | |||
| 41 | |||
| 42 | public function testInvalidInterfacesSimpleType(): void |
||
| 43 | { |
||
| 44 | $this->expectException(\Youshido\GraphQL\Exception\ConfigurationException::class); |
||
| 45 | $this->expectExceptionMessage('Implementation of TestInterface is invalid for the field name'); |
||
| 46 | |||
| 47 | $schema = new Schema([ |
||
| 48 | 'query' => new ObjectType([ |
||
| 49 | 'name' => 'RootQuery', |
||
| 50 | 'fields' => [ |
||
| 51 | 'user' => new ObjectType([ |
||
| 52 | 'name' => 'User', |
||
| 53 | 'fields' => [ |
||
| 54 | 'name' => new IntType(), |
||
| 55 | ], |
||
| 56 | 'interfaces' => [new TestInterfaceType()], |
||
| 57 | ]), |
||
| 58 | ], |
||
| 59 | ]), |
||
| 60 | ]); |
||
| 61 | |||
| 62 | $validator = new SchemaValidator(); |
||
| 63 | $validator->validate($schema); |
||
| 64 | } |
||
| 65 | |||
| 66 | |||
| 67 | public function testInvalidInterfacesCompositeType(): void |
||
| 68 | { |
||
| 69 | $this->expectException(\Youshido\GraphQL\Exception\ConfigurationException::class); |
||
| 70 | $this->expectExceptionMessage('Implementation of TestInterface is invalid for the field name'); |
||
| 71 | |||
| 72 | $schema = new Schema([ |
||
| 73 | 'query' => new ObjectType([ |
||
| 74 | 'name' => 'RootQuery', |
||
| 75 | 'fields' => [ |
||
| 76 | 'user' => new ObjectType([ |
||
| 77 | 'name' => 'User', |
||
| 78 | 'fields' => [ |
||
| 79 | 'name' => new NonNullType(new StringType()), |
||
| 80 | ], |
||
| 81 | 'interfaces' => [new TestInterfaceType()], |
||
| 82 | ]), |
||
| 83 | ], |
||
| 84 | ]), |
||
| 85 | ]); |
||
| 86 | |||
| 87 | $validator = new SchemaValidator(); |
||
| 88 | $validator->validate($schema); |
||
| 89 | } |
||
| 90 | |||
| 91 | |||
| 92 | public function testInvalidInterfaces(): void |
||
| 93 | { |
||
| 94 | $this->expectException(\Youshido\GraphQL\Exception\ConfigurationException::class); |
||
| 95 | $this->expectExceptionMessage('Implementation of TestInterface is invalid for the field name'); |
||
| 96 | |||
| 97 | $schema = new Schema([ |
||
| 98 | 'query' => new ObjectType([ |
||
| 99 | 'name' => 'RootQuery', |
||
| 100 | 'fields' => [ |
||
| 101 | 'user' => new ObjectType([ |
||
| 102 | 'name' => 'User', |
||
| 103 | 'fields' => [ |
||
| 104 | 'name' => new IntType(), |
||
| 105 | ], |
||
| 106 | 'interfaces' => [new TestInterfaceType()], |
||
| 107 | ]), |
||
| 108 | ], |
||
| 109 | ]), |
||
| 110 | ]); |
||
| 111 | |||
| 112 | $validator = new SchemaValidator(); |
||
| 113 | $validator->validate($schema); |
||
| 114 | } |
||
| 115 | |||
| 116 | public function testValidSchema(): void |
||
| 117 | { |
||
| 118 | $schema = new Schema([ |
||
| 119 | 'query' => new ObjectType([ |
||
| 120 | 'name' => 'RootQuery', |
||
| 121 | 'fields' => [ |
||
| 122 | 'user' => new ObjectType([ |
||
| 123 | 'name' => 'User', |
||
| 124 | 'fields' => [ |
||
| 125 | 'name' => new StringType(), |
||
| 126 | ], |
||
| 127 | 'interfaces' => [new TestInterfaceType()], |
||
| 128 | ]), |
||
| 129 | ], |
||
| 130 | ]), |
||
| 131 | ]); |
||
| 132 | |||
| 133 | $validator = new SchemaValidator(); |
||
| 134 | |||
| 143 |