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