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