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 |
||
| 19 | use Youshido\GraphQL\Type\Object\ObjectType; |
||
| 20 | use Youshido\GraphQL\Type\Scalar\IntType; |
||
| 21 | use Youshido\GraphQL\Type\TypeMap; |
||
| 22 | use Youshido\GraphQL\Type\Union\UnionType; |
||
| 23 | use Youshido\GraphQL\Validator\ConfigValidator\ConfigValidator; |
||
| 24 | use Youshido\Tests\DataProvider\TestObjectType; |
||
| 25 | use Youshido\Tests\DataProvider\TestUnionType; |
||
| 26 | |||
| 27 | class UnionTypeTest extends \PHPUnit_Framework_TestCase |
||
| 28 | { |
||
| 29 | public function testInlineCreation(): void |
||
| 30 | { |
||
| 31 | $object = new ObjectType([ |
||
| 32 | 'name' => 'TestObject', |
||
| 33 | 'fields' => ['id' => ['type' => new IntType()]], |
||
| 34 | ]); |
||
| 35 | |||
| 36 | $type = new UnionType([ |
||
| 37 | 'name' => 'Car', |
||
| 38 | 'description' => 'Union collect cars types', |
||
| 39 | 'types' => [ |
||
| 40 | new TestObjectType(), |
||
| 41 | $object, |
||
| 42 | ], |
||
| 43 | 'resolveType' => static function ($type) { |
||
| 44 | return $type; |
||
| 45 | }, |
||
| 46 | ]); |
||
| 47 | |||
| 48 | $this->assertEquals('Car', $type->getName()); |
||
| 49 | $this->assertEquals('Union collect cars types', $type->getDescription()); |
||
| 50 | $this->assertEquals([new TestObjectType(), $object], $type->getTypes()); |
||
| 51 | $this->assertEquals('test', $type->resolveType('test')); |
||
| 52 | $this->assertEquals(TypeMap::KIND_UNION, $type->getKind()); |
||
| 53 | $this->assertEquals($type, $type->getNamedType()); |
||
| 54 | $this->assertTrue($type->isValidValue(true)); |
||
| 55 | } |
||
| 56 | |||
| 57 | public function testObjectCreation(): void |
||
| 58 | { |
||
| 59 | $type = new TestUnionType(); |
||
| 60 | |||
| 61 | $this->assertEquals('TestUnion', $type->getName()); |
||
| 62 | $this->assertEquals('Union collect cars types', $type->getDescription()); |
||
| 63 | $this->assertEquals([new TestObjectType()], $type->getTypes()); |
||
| 64 | $this->assertEquals('test', $type->resolveType('test')); |
||
| 65 | } |
||
| 66 | |||
| 67 | |||
| 68 | public function testInvalidTypesWithScalar(): void |
||
| 69 | { |
||
| 70 | $this->expectException(\Youshido\GraphQL\Exception\ConfigurationException::class); |
||
| 71 | |||
| 72 | $type = new UnionType([ |
||
| 73 | 'name' => 'Car', |
||
| 74 | 'description' => 'Union collect cars types', |
||
| 75 | 'types' => [ |
||
| 76 | 'test', new IntType(), |
||
| 77 | ], |
||
| 78 | 'resolveType' => static function ($type) { |
||
| 79 | return $type; |
||
| 80 | }, |
||
| 81 | ]); |
||
| 82 | ConfigValidator::getInstance()->assertValidConfig($type->getConfig()); |
||
| 83 | } |
||
| 84 | |||
| 85 | |||
| 86 | public function testInvalidTypes(): void |
||
| 87 | { |
||
| 88 | $this->expectException(\Youshido\GraphQL\Exception\ConfigurationException::class); |
||
| 89 | |||
| 90 | $type = new UnionType([ |
||
| 91 | 'name' => 'Car', |
||
| 92 | 'description' => 'Union collect cars types', |
||
| 93 | 'types' => [ |
||
| 94 | new IntType(), |
||
| 95 | ], |
||
| 96 | 'resolveType' => static function ($type) { |
||
| 103 |