1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace TheCodingMachine\GraphQL\Controllers; |
5
|
|
|
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestObject; |
8
|
|
|
use Youshido\GraphQL\Type\InputObject\InputObjectType; |
9
|
|
|
use Youshido\GraphQL\Type\InputTypeInterface; |
10
|
|
|
use Youshido\GraphQL\Type\Object\ObjectType; |
11
|
|
|
use Youshido\GraphQL\Type\Scalar\StringType; |
12
|
|
|
use Youshido\GraphQL\Type\TypeInterface; |
13
|
|
|
|
14
|
|
|
abstract class AbstractQueryProviderTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
private $testObjectType; |
17
|
|
|
private $inputTestObjectType; |
18
|
|
|
private $typeMapper; |
19
|
|
|
private $hydrator; |
20
|
|
|
|
21
|
|
View Code Duplication |
protected function getTestObjectType() |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
if ($this->testObjectType === null) { |
24
|
|
|
$this->testObjectType = new ObjectType([ |
25
|
|
|
'name' => 'TestObject', |
26
|
|
|
'fields' => [ |
27
|
|
|
'test' => new StringType(), |
28
|
|
|
], |
29
|
|
|
]); |
30
|
|
|
} |
31
|
|
|
return $this->testObjectType; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
View Code Duplication |
protected function getInputTestObjectType() |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
if ($this->inputTestObjectType === null) { |
37
|
|
|
$this->inputTestObjectType = new InputObjectType([ |
38
|
|
|
'name' => 'TestObject', |
39
|
|
|
'fields' => [ |
40
|
|
|
'test' => new StringType(), |
41
|
|
|
], |
42
|
|
|
]); |
43
|
|
|
} |
44
|
|
|
return $this->inputTestObjectType; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
protected function getTypeMapper() |
48
|
|
|
{ |
49
|
|
|
if ($this->typeMapper === null) { |
50
|
|
|
$this->typeMapper = new class($this->getTestObjectType(), $this->getInputTestObjectType()) implements TypeMapperInterface { |
51
|
|
|
/** |
52
|
|
|
* @var ObjectType |
53
|
|
|
*/ |
54
|
|
|
private $testObjectType; |
55
|
|
|
/** |
56
|
|
|
* @var InputObjectType |
57
|
|
|
*/ |
58
|
|
|
private $inputTestObjectType; |
59
|
|
|
|
60
|
|
|
public function __construct(ObjectType $testObjectType, InputObjectType $inputTestObjectType) |
61
|
|
|
{ |
62
|
|
|
$this->testObjectType = $testObjectType; |
63
|
|
|
$this->inputTestObjectType = $inputTestObjectType; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function mapClassToType(string $className): TypeInterface |
67
|
|
|
{ |
68
|
|
|
if ($className === TestObject::class) { |
69
|
|
|
return $this->testObjectType; |
70
|
|
|
} else { |
71
|
|
|
throw new \RuntimeException('Unexpected type'); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function mapClassToInputType(string $className): InputTypeInterface |
76
|
|
|
{ |
77
|
|
|
if ($className === TestObject::class) { |
78
|
|
|
return $this->inputTestObjectType; |
79
|
|
|
} else { |
80
|
|
|
throw new \RuntimeException('Unexpected type'); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
}; |
84
|
|
|
} |
85
|
|
|
return $this->typeMapper; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
protected function getHydrator() |
89
|
|
|
{ |
90
|
|
|
if ($this->hydrator === null) { |
91
|
|
|
$this->hydrator = new class implements HydratorInterface { |
92
|
|
|
public function hydrate(array $data, TypeInterface $type) |
93
|
|
|
{ |
94
|
|
|
return new TestObject($data['test']); |
95
|
|
|
} |
96
|
|
|
}; |
97
|
|
|
} |
98
|
|
|
return $this->hydrator; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
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.