1
|
|
|
<?php |
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\Provider\MutationFieldsProvider; |
11
|
|
|
|
12
|
|
|
|
13
|
|
View Code Duplication |
class MutationFieldsProviderTest extends TestCase |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var MutationFieldsProviderInterface |
18
|
|
|
*/ |
19
|
|
|
private $mutationFieldProvider; |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
protected function setUp() |
23
|
|
|
{ |
24
|
|
|
$this->mutationFieldProvider = new MutationFieldsProvider; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
public function testAddField() |
29
|
|
|
{ |
30
|
|
|
$mutationField = $this->getMutationField(); |
31
|
|
|
|
32
|
|
|
$this->assertEmpty($this->mutationFieldProvider->getFields()); |
33
|
|
|
|
34
|
|
|
$this->mutationFieldProvider->addField($mutationField); |
35
|
|
|
|
36
|
|
|
$this->assertCount(1, $this->mutationFieldProvider->getFields()); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @expectedException \Portiny\GraphQL\Exception\Provider\ExistingMutationFieldException |
42
|
|
|
* @expectedExceptionMessage Mutation field with name "Some name" is already registered. |
43
|
|
|
*/ |
44
|
|
|
public function testAddFieldAlreadyExists() |
45
|
|
|
{ |
46
|
|
|
$mutationField = $this->getMutationField(); |
47
|
|
|
|
48
|
|
|
$this->assertEmpty($this->mutationFieldProvider->getFields()); |
49
|
|
|
|
50
|
|
|
$this->mutationFieldProvider->addField($mutationField); |
51
|
|
|
$this->mutationFieldProvider->addField($mutationField); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
public function testGetFields() |
56
|
|
|
{ |
57
|
|
|
$mutationField = $this->getMutationField(); |
58
|
|
|
$this->mutationFieldProvider->addField($mutationField); |
59
|
|
|
|
60
|
|
|
$fields = $this->mutationFieldProvider->getFields(); |
61
|
|
|
$this->assertCount(1, $fields); |
62
|
|
|
$this->assertSame($mutationField, reset($fields)); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
public function testConvertFieldsToArray() |
67
|
|
|
{ |
68
|
|
|
$mutationField = $this->getMutationField(); |
69
|
|
|
$this->mutationFieldProvider->addField($mutationField); |
70
|
|
|
|
71
|
|
|
$output = $this->mutationFieldProvider->convertFieldsToArray(); |
72
|
|
|
$this->assertSame('Some name', key($output)); |
73
|
|
|
|
74
|
|
|
$mutationFieldAsArray = reset($output); |
75
|
|
|
$this->assertInstanceOf(StringType::class, $mutationFieldAsArray['type']); |
76
|
|
|
$this->assertSame('Some description', $mutationFieldAsArray['description']); |
77
|
|
|
$this->assertArrayHasKey('someArg', $mutationFieldAsArray['args']); |
78
|
|
|
$this->assertArrayHasKey('type', $mutationFieldAsArray['args']['someArg']); |
79
|
|
|
$this->assertInstanceOf(StringType::class, $mutationFieldAsArray['args']['someArg']['type']); |
80
|
|
|
$this->assertTrue(is_callable($mutationFieldAsArray['resolve'])); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
private function getMutationField(): MutationFieldInterface |
85
|
|
|
{ |
86
|
|
|
return (new class () implements MutationFieldInterface |
87
|
|
|
{ |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
|
|
public function getName(): string |
93
|
|
|
{ |
94
|
|
|
return 'Some name'; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
*/ |
101
|
|
|
public function getType(): Type |
102
|
|
|
{ |
103
|
|
|
return Type::string(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* {@inheritdoc} |
109
|
|
|
*/ |
110
|
|
|
public function getDescription(): string |
111
|
|
|
{ |
112
|
|
|
return 'Some description'; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritdoc} |
118
|
|
|
*/ |
119
|
|
|
public function getArgs(): array |
120
|
|
|
{ |
121
|
|
|
return [ |
122
|
|
|
'someArg' => ['type' => Type::string()] |
123
|
|
|
]; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* {@inheritdoc} |
129
|
|
|
*/ |
130
|
|
|
public function resolve(array $root, array $args, $context = NULL) |
131
|
|
|
{ |
132
|
|
|
return 'resolved'; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
}); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
} |
139
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.