| @@ 13-126 (lines=114) @@ | ||
| 10 | use Portiny\GraphQL\Provider\MutationFieldsProvider; |
|
| 11 | use Portiny\GraphQL\Tests\Source\Provider\SomeMutationField; |
|
| 12 | ||
| 13 | class MutationFieldsProviderTest extends TestCase |
|
| 14 | { |
|
| 15 | /** |
|
| 16 | * @var MutationFieldsProviderInterface |
|
| 17 | */ |
|
| 18 | private $mutationFieldsProvider; |
|
| 19 | ||
| 20 | protected function setUp(): void |
|
| 21 | { |
|
| 22 | $this->mutationFieldsProvider = new MutationFieldsProvider(); |
|
| 23 | } |
|
| 24 | ||
| 25 | public function testAddField(): void |
|
| 26 | { |
|
| 27 | $mutationField = $this->getMutationField(); |
|
| 28 | ||
| 29 | $this->assertEmpty($this->mutationFieldsProvider->getFields()); |
|
| 30 | ||
| 31 | $this->mutationFieldsProvider->addField($mutationField); |
|
| 32 | ||
| 33 | $this->assertCount(1, $this->mutationFieldsProvider->getFields()); |
|
| 34 | } |
|
| 35 | ||
| 36 | /** |
|
| 37 | * @expectedException \Portiny\GraphQL\Exception\Provider\ExistingMutationFieldException |
|
| 38 | * @expectedExceptionMessage Mutation field with name "Some name" is already registered. |
|
| 39 | */ |
|
| 40 | public function testAddFieldAlreadyExists(): void |
|
| 41 | { |
|
| 42 | $mutationField = $this->getMutationField(); |
|
| 43 | ||
| 44 | $this->assertEmpty($this->mutationFieldsProvider->getFields()); |
|
| 45 | ||
| 46 | $this->mutationFieldsProvider->addField($mutationField); |
|
| 47 | $this->mutationFieldsProvider->addField($mutationField); |
|
| 48 | } |
|
| 49 | ||
| 50 | public function testGetFields(): void |
|
| 51 | { |
|
| 52 | $mutationField = $this->getMutationField(); |
|
| 53 | $this->mutationFieldsProvider->addField($mutationField); |
|
| 54 | ||
| 55 | $fields = $this->mutationFieldsProvider->getFields(); |
|
| 56 | $this->assertCount(1, $fields); |
|
| 57 | $this->assertSame($mutationField, reset($fields)); |
|
| 58 | } |
|
| 59 | ||
| 60 | public function testConvertFieldsToArray(): void |
|
| 61 | { |
|
| 62 | $mutationField = $this->getMutationField(); |
|
| 63 | $this->mutationFieldsProvider->addField($mutationField); |
|
| 64 | ||
| 65 | $output = $this->mutationFieldsProvider->convertFieldsToArray(); |
|
| 66 | $this->assertSame('Some name', key($output)); |
|
| 67 | ||
| 68 | $mutationFieldAsArray = reset($output); |
|
| 69 | $this->assertInstanceOf(StringType::class, $mutationFieldAsArray['type']); |
|
| 70 | $this->assertSame('Some description', $mutationFieldAsArray['description']); |
|
| 71 | $this->assertArrayHasKey('someArg', $mutationFieldAsArray['args']); |
|
| 72 | $this->assertArrayHasKey('type', $mutationFieldAsArray['args']['someArg']); |
|
| 73 | $this->assertInstanceOf(StringType::class, $mutationFieldAsArray['args']['someArg']['type']); |
|
| 74 | $this->assertTrue(is_callable($mutationFieldAsArray['resolve'])); |
|
| 75 | ||
| 76 | $output = $this->mutationFieldsProvider->convertFieldsToArray([SomeMutationField::class]); |
|
| 77 | $this->assertEmpty($output); |
|
| 78 | } |
|
| 79 | ||
| 80 | private function getMutationField(): MutationFieldInterface |
|
| 81 | { |
|
| 82 | return new class() implements MutationFieldInterface { |
|
| 83 | /** |
|
| 84 | * {@inheritdoc} |
|
| 85 | */ |
|
| 86 | public function getName(): string |
|
| 87 | { |
|
| 88 | return 'Some name'; |
|
| 89 | } |
|
| 90 | ||
| 91 | /** |
|
| 92 | * {@inheritdoc} |
|
| 93 | */ |
|
| 94 | public function getType(): Type |
|
| 95 | { |
|
| 96 | return Type::string(); |
|
| 97 | } |
|
| 98 | ||
| 99 | /** |
|
| 100 | * {@inheritdoc} |
|
| 101 | */ |
|
| 102 | public function getDescription(): string |
|
| 103 | { |
|
| 104 | return 'Some description'; |
|
| 105 | } |
|
| 106 | ||
| 107 | /** |
|
| 108 | * {@inheritdoc} |
|
| 109 | */ |
|
| 110 | public function getArgs(): array |
|
| 111 | { |
|
| 112 | return [ |
|
| 113 | 'someArg' => ['type' => Type::string()], |
|
| 114 | ]; |
|
| 115 | } |
|
| 116 | ||
| 117 | /** |
|
| 118 | * {@inheritdoc} |
|
| 119 | */ |
|
| 120 | public function resolve(array $root, array $args, $context = NULL) |
|
| 121 | { |
|
| 122 | return 'resolved'; |
|
| 123 | } |
|
| 124 | }; |
|
| 125 | } |
|
| 126 | } |
|
| 127 | ||
| @@ 13-126 (lines=114) @@ | ||
| 10 | use Portiny\GraphQL\Provider\QueryFieldsProvider; |
|
| 11 | use Portiny\GraphQL\Tests\Source\Provider\SomeQueryField; |
|
| 12 | ||
| 13 | class QueryFieldsProviderTest extends TestCase |
|
| 14 | { |
|
| 15 | /** |
|
| 16 | * @var QueryFieldsProviderInterface |
|
| 17 | */ |
|
| 18 | private $queryFieldsProvider; |
|
| 19 | ||
| 20 | protected function setUp(): void |
|
| 21 | { |
|
| 22 | $this->queryFieldsProvider = new QueryFieldsProvider(); |
|
| 23 | } |
|
| 24 | ||
| 25 | public function testAddField(): void |
|
| 26 | { |
|
| 27 | $queryField = $this->getQueryField(); |
|
| 28 | ||
| 29 | $this->assertEmpty($this->queryFieldsProvider->getFields()); |
|
| 30 | ||
| 31 | $this->queryFieldsProvider->addField($queryField); |
|
| 32 | ||
| 33 | $this->assertCount(1, $this->queryFieldsProvider->getFields()); |
|
| 34 | } |
|
| 35 | ||
| 36 | /** |
|
| 37 | * @expectedException \Portiny\GraphQL\Exception\Provider\ExistingQueryFieldException |
|
| 38 | * @expectedExceptionMessage Query field with name "Some name" is already registered. |
|
| 39 | */ |
|
| 40 | public function testAddFieldAlreadyExists(): void |
|
| 41 | { |
|
| 42 | $queryField = $this->getQueryField(); |
|
| 43 | ||
| 44 | $this->assertEmpty($this->queryFieldsProvider->getFields()); |
|
| 45 | ||
| 46 | $this->queryFieldsProvider->addField($queryField); |
|
| 47 | $this->queryFieldsProvider->addField($queryField); |
|
| 48 | } |
|
| 49 | ||
| 50 | public function testGetFields(): void |
|
| 51 | { |
|
| 52 | $queryField = $this->getQueryField(); |
|
| 53 | $this->queryFieldsProvider->addField($queryField); |
|
| 54 | ||
| 55 | $fields = $this->queryFieldsProvider->getFields(); |
|
| 56 | $this->assertCount(1, $fields); |
|
| 57 | $this->assertSame($queryField, reset($fields)); |
|
| 58 | } |
|
| 59 | ||
| 60 | public function testConvertFieldsToArray(): void |
|
| 61 | { |
|
| 62 | $queryField = $this->getQueryField(); |
|
| 63 | $this->queryFieldsProvider->addField($queryField); |
|
| 64 | ||
| 65 | $output = $this->queryFieldsProvider->convertFieldsToArray(); |
|
| 66 | $this->assertSame('Some name', key($output)); |
|
| 67 | ||
| 68 | $queryFieldAsArray = reset($output); |
|
| 69 | $this->assertInstanceOf(StringType::class, $queryFieldAsArray['type']); |
|
| 70 | $this->assertSame('Some description', $queryFieldAsArray['description']); |
|
| 71 | $this->assertArrayHasKey('someArg', $queryFieldAsArray['args']); |
|
| 72 | $this->assertArrayHasKey('type', $queryFieldAsArray['args']['someArg']); |
|
| 73 | $this->assertInstanceOf(StringType::class, $queryFieldAsArray['args']['someArg']['type']); |
|
| 74 | $this->assertTrue(is_callable($queryFieldAsArray['resolve'])); |
|
| 75 | ||
| 76 | $output = $this->queryFieldsProvider->convertFieldsToArray([SomeQueryField::class]); |
|
| 77 | $this->assertEmpty($output); |
|
| 78 | } |
|
| 79 | ||
| 80 | private function getQueryField(): QueryFieldInterface |
|
| 81 | { |
|
| 82 | return new class() implements QueryFieldInterface { |
|
| 83 | /** |
|
| 84 | * {@inheritdoc} |
|
| 85 | */ |
|
| 86 | public function getName(): string |
|
| 87 | { |
|
| 88 | return 'Some name'; |
|
| 89 | } |
|
| 90 | ||
| 91 | /** |
|
| 92 | * {@inheritdoc} |
|
| 93 | */ |
|
| 94 | public function getType(): Type |
|
| 95 | { |
|
| 96 | return Type::string(); |
|
| 97 | } |
|
| 98 | ||
| 99 | /** |
|
| 100 | * {@inheritdoc} |
|
| 101 | */ |
|
| 102 | public function getDescription(): string |
|
| 103 | { |
|
| 104 | return 'Some description'; |
|
| 105 | } |
|
| 106 | ||
| 107 | /** |
|
| 108 | * {@inheritdoc} |
|
| 109 | */ |
|
| 110 | public function getArgs(): array |
|
| 111 | { |
|
| 112 | return [ |
|
| 113 | 'someArg' => ['type' => Type::string()], |
|
| 114 | ]; |
|
| 115 | } |
|
| 116 | ||
| 117 | /** |
|
| 118 | * {@inheritdoc} |
|
| 119 | */ |
|
| 120 | public function resolve(array $root, array $args, $context = NULL) |
|
| 121 | { |
|
| 122 | return 'resolved'; |
|
| 123 | } |
|
| 124 | }; |
|
| 125 | } |
|
| 126 | } |
|
| 127 | ||