|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace TheCodingMachine\GraphQLite\Mappers\Proxys; |
| 4 | + |
| 5 | +use GraphQL\Type\Definition\InterfaceType; |
| 6 | +use GraphQL\Type\Definition\ObjectType; |
| 7 | +use GraphQL\Type\Definition\Type; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | +use RuntimeException; |
| 10 | +use TheCodingMachine\GraphQLite\Fixtures\StaticTypeMapper\Types\TestLegacyObject; |
| 11 | +use TheCodingMachine\GraphQLite\Types\MutableObjectType; |
| 12 | +use TheCodingMachine\GraphQLite\Types\NoFieldsException; |
| 13 | + |
| 14 | +class MutableObjectTypeAdapterTest extends TestCase |
| 15 | +{ |
| 16 | + |
| 17 | + private function getAdapter(): MutableObjectTypeAdapter |
| 18 | + { |
| 19 | + $type = new ObjectType([ |
| 20 | + 'name' => 'TestLegacyObject', |
| 21 | + 'fields' => [ |
| 22 | + 'foo' => [ |
| 23 | + 'type' =>Type::int(), |
| 24 | + 'resolve' => function(TestLegacyObject $source) { |
| 25 | + return $source->getFoo(); |
| 26 | + } |
| 27 | + ] |
| 28 | + ] |
| 29 | + ]); |
| 30 | + |
| 31 | + $adapter = new MutableObjectTypeAdapter($type); |
| 32 | + |
| 33 | + return $adapter; |
| 34 | + } |
| 35 | + public function testAdapter() |
| 36 | + { |
| 37 | + $type = new ObjectType([ |
| 38 | + 'name' => 'TestLegacyObject', |
| 39 | + 'fields' => [ |
| 40 | + 'foo' => [ |
| 41 | + 'type' =>Type::int(), |
| 42 | + 'resolve' => function(TestLegacyObject $source) { |
| 43 | + return $source->getFoo(); |
| 44 | + } |
| 45 | + ] |
| 46 | + ] |
| 47 | + ]); |
| 48 | + |
| 49 | + $adapter = new MutableObjectTypeAdapter($type); |
| 50 | + |
| 51 | + $adapter->assertValid(); |
| 52 | + |
| 53 | + $adapter->freeze(); |
| 54 | + $this->assertSame($type->getInterfaces(), $adapter->getInterfaces()); |
| 55 | + $this->assertSame($type->jsonSerialize(), $adapter->jsonSerialize()); |
| 56 | + $this->assertSame($type->getField('foo'), $adapter->getField('foo')); |
| 57 | + $this->assertSame($type->hasField('foo'), $adapter->hasField('foo')); |
| 58 | + $interfaceType = new InterfaceType(['name' => 'Foo']); |
| 59 | + $this->assertSame($type->implementsInterface($interfaceType), $adapter->implementsInterface($interfaceType)); |
| 60 | + |
| 61 | + } |
| 62 | + |
| 63 | + public function testGetStatus(): void |
| 64 | + { |
| 65 | + $type = $this->getAdapter(); |
| 66 | + |
| 67 | + $this->assertSame(MutableObjectType::STATUS_PENDING, $type->getStatus()); |
| 68 | + $type->freeze(); |
| 69 | + $this->assertSame(MutableObjectType::STATUS_FROZEN, $type->getStatus()); |
| 70 | + } |
| 71 | + |
| 72 | + public function testAddFields(): void |
| 73 | + { |
| 74 | + $type = $this->getAdapter(); |
| 75 | + |
| 76 | + $type->addFields(function() { |
| 77 | + return [ |
| 78 | + 'test' => Type::int(), |
| 79 | + 'test2' => Type::string(), |
| 80 | + ]; |
| 81 | + }); |
| 82 | + $type->addFields(function() { |
| 83 | + return [ |
| 84 | + 'test3' => Type::int(), |
| 85 | + ]; |
| 86 | + }); |
| 87 | + $type->freeze(); |
| 88 | + $fields = $type->getFields(); |
| 89 | + $this->assertCount(4, $fields); |
| 90 | + $this->assertArrayHasKey('test', $fields); |
| 91 | + $this->assertSame(Type::int(), $fields['test']->getType()); |
| 92 | + } |
| 93 | + |
| 94 | + public function testHasFieldError(): void |
| 95 | + { |
| 96 | + $type = $this->getAdapter(); |
| 97 | + |
| 98 | + $this->expectException(RuntimeException::class); |
| 99 | + $type->hasField('test'); |
| 100 | + } |
| 101 | + |
| 102 | + public function testGetFieldError(): void |
| 103 | + { |
| 104 | + $type = $this->getAdapter(); |
| 105 | + |
| 106 | + $this->expectException(RuntimeException::class); |
| 107 | + $type->getField('test'); |
| 108 | + } |
| 109 | + |
| 110 | + public function testGetFieldsError(): void |
| 111 | + { |
| 112 | + $type = $this->getAdapter(); |
| 113 | + |
| 114 | + $this->expectException(RuntimeException::class); |
| 115 | + $type->getFields(); |
| 116 | + } |
| 117 | + |
| 118 | + public function testAddFieldsError(): void |
| 119 | + { |
| 120 | + $type = $this->getAdapter(); |
| 121 | + |
| 122 | + $type->freeze(); |
| 123 | + $this->expectException(RuntimeException::class); |
| 124 | + $type->addFields(function() {}); |
| 125 | + } |
| 126 | + |
| 127 | + public function testNoFieldsType(): void |
| 128 | + { |
| 129 | + $type = new ObjectType([ |
| 130 | + 'name' => 'TestLegacyObject', |
| 131 | + 'fields' => [ |
| 132 | + |
| 133 | + ] |
| 134 | + ]); |
| 135 | + |
| 136 | + $adapter = new MutableObjectTypeAdapter($type); |
| 137 | + |
| 138 | + $adapter->freeze(); |
| 139 | + $this->expectException(NoFieldsException::class); |
| 140 | + $this->expectExceptionMessage('The GraphQL object type "TestLegacyObject" has no fields defined. Please check that some fields are defined (using the @Field annotation). If some fields are defined, please check that at least one is visible to the current user.'); |
| 141 | + $adapter->getFields(); |
| 142 | + } |
| 143 | + |
| 144 | +} |
0 commit comments