|
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\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
|
|
|
self::assertEmpty($this->mutationFieldsProvider->getFields()); |
|
30
|
|
|
|
|
31
|
|
|
$this->mutationFieldsProvider->addField($mutationField); |
|
32
|
|
|
|
|
33
|
|
|
self::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
|
|
|
self::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
|
|
|
self::assertCount(1, $fields); |
|
57
|
|
|
self::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
|
|
|
self::assertSame('Some name', key($output)); |
|
67
|
|
|
|
|
68
|
|
|
$mutationFieldAsArray = reset($output); |
|
69
|
|
|
self::assertInstanceOf(StringType::class, $mutationFieldAsArray['type']); |
|
70
|
|
|
self::assertSame('Some description', $mutationFieldAsArray['description']); |
|
71
|
|
|
self::assertArrayHasKey('someArg', $mutationFieldAsArray['args']); |
|
72
|
|
|
self::assertArrayHasKey('type', $mutationFieldAsArray['args']['someArg']); |
|
73
|
|
|
self::assertInstanceOf(StringType::class, $mutationFieldAsArray['args']['someArg']['type']); |
|
74
|
|
|
self::assertTrue(is_callable($mutationFieldAsArray['resolve'])); |
|
75
|
|
|
|
|
76
|
|
|
$output = $this->mutationFieldsProvider->convertFieldsToArray([SomeMutationField::class]); |
|
77
|
|
|
self::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 getDescription(): string |
|
95
|
|
|
{ |
|
96
|
|
|
return 'Some description'; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* {@inheritdoc} |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getArgs(): array |
|
103
|
|
|
{ |
|
104
|
|
|
return [ |
|
105
|
|
|
'someArg' => ['type' => Type::string()], |
|
106
|
|
|
]; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* {@inheritdoc} |
|
111
|
|
|
*/ |
|
112
|
|
|
public function getType(): Type |
|
113
|
|
|
{ |
|
114
|
|
|
return Type::string(); |
|
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
|
|
|
|