1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
|
3
|
|
|
namespace Portiny\GraphQL\Tests\Converter; |
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\Converter\MutationFieldConverter; |
10
|
|
|
|
11
|
|
|
final class MutationFieldConverterTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
public function testToArray(): void |
15
|
|
|
{ |
16
|
|
|
$mutationField = $this->getMutationField(); |
17
|
|
|
$output = MutationFieldConverter::toArray($mutationField); |
18
|
|
|
|
19
|
|
|
self::assertSame('Some name', key($output)); |
20
|
|
|
|
21
|
|
|
$mutationFieldAsArray = reset($output); |
22
|
|
|
self::assertInstanceOf(StringType::class, $mutationFieldAsArray['type']); |
23
|
|
|
self::assertSame('Some description', $mutationFieldAsArray['description']); |
24
|
|
|
self::assertArrayHasKey('someArg', $mutationFieldAsArray['args']); |
25
|
|
|
self::assertArrayHasKey('type', $mutationFieldAsArray['args']['someArg']); |
26
|
|
|
self::assertInstanceOf(StringType::class, $mutationFieldAsArray['args']['someArg']['type']); |
27
|
|
|
self::assertTrue(is_callable($mutationFieldAsArray['resolve'])); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
public function testToObject(): void |
32
|
|
|
{ |
33
|
|
|
$mutationField = $this->getMutationField(); |
34
|
|
|
$mutationFieldAsArray = MutationFieldConverter::toArray($mutationField); |
35
|
|
|
$output = MutationFieldConverter::toObject($mutationFieldAsArray); |
36
|
|
|
|
37
|
|
|
self::assertSame('Some name', $output->getName()); |
38
|
|
|
self::assertInstanceOf(StringType::class, $output->getType()); |
39
|
|
|
self::assertSame('Some description', $output->getDescription()); |
40
|
|
|
self::assertArrayHasKey('someArg', $output->getArgs()); |
41
|
|
|
self::assertArrayHasKey('type', $output->getArgs()['someArg']); |
42
|
|
|
self::assertInstanceOf(StringType::class, $output->getArgs()['someArg']['type']); |
43
|
|
|
self::assertSame('resolved', $output->resolve([], ['someArg' => ''])); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
private function getMutationField(): MutationFieldInterface |
48
|
|
|
{ |
49
|
|
|
return new class() implements MutationFieldInterface { |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
|
|
public function getName(): string |
55
|
|
|
{ |
56
|
|
|
return 'Some name'; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
public function getDescription(): string |
64
|
|
|
{ |
65
|
|
|
return 'Some description'; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
|
|
public function getArgs(): array |
73
|
|
|
{ |
74
|
|
|
return [ |
75
|
|
|
'someArg' => [ |
76
|
|
|
'type' => Type::string(), |
77
|
|
|
], |
78
|
|
|
]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
|
|
public function getType(): Type |
86
|
|
|
{ |
87
|
|
|
return Type::string(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
|
|
public function resolve(array $root, array $args, $context = null) |
95
|
|
|
{ |
96
|
|
|
return 'resolved'; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
}; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
} |
103
|
|
|
|