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