1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TheCodingMachine\GraphQL\Controllers\Mappers; |
4
|
|
|
|
5
|
|
|
use GraphQL\Type\Definition\InputType; |
6
|
|
|
use GraphQL\Type\Definition\OutputType; |
7
|
|
|
use GraphQL\Type\Definition\Type; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
use TheCodingMachine\GraphQL\Controllers\AbstractQueryProviderTest; |
10
|
|
|
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestObject; |
11
|
|
|
use TheCodingMachine\GraphQL\Controllers\TypeMappingException; |
12
|
|
|
use GraphQL\Type\Definition\InputObjectType; |
13
|
|
|
use GraphQL\Type\Definition\ObjectType; |
14
|
|
|
|
15
|
|
|
class CompositeTypeMapperTest extends AbstractQueryProviderTest |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var CompositeTypeMapper |
19
|
|
|
*/ |
20
|
|
|
protected $composite; |
21
|
|
|
|
22
|
|
|
public function setUp() |
23
|
|
|
{ |
24
|
|
|
$typeMapper1 = new class() implements TypeMapperInterface { |
25
|
|
|
public function mapClassToType(string $className, RecursiveTypeMapperInterface $recursiveTypeMapper): ObjectType |
26
|
|
|
{ |
27
|
|
|
if ($className === TestObject::class) { |
28
|
|
|
return new ObjectType([ |
29
|
|
|
'name' => 'TestObject', |
30
|
|
|
'fields' => [ |
31
|
|
|
'test' => Type::string(), |
32
|
|
|
], |
33
|
|
|
]); |
34
|
|
|
} else { |
35
|
|
|
throw TypeMappingException::createFromType(TestObject::class); |
|
|
|
|
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function mapClassToInputType(string $className): InputType |
40
|
|
|
{ |
41
|
|
|
if ($className === TestObject::class) { |
42
|
|
|
return new InputObjectType([ |
43
|
|
|
'name' => 'TestObject', |
44
|
|
|
'fields' => [ |
45
|
|
|
'test' => Type::string(), |
46
|
|
|
], |
47
|
|
|
]); |
48
|
|
|
} else { |
49
|
|
|
throw TypeMappingException::createFromType(TestObject::class); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function canMapClassToType(string $className): bool |
54
|
|
|
{ |
55
|
|
|
return $className === TestObject::class; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function canMapClassToInputType(string $className): bool |
59
|
|
|
{ |
60
|
|
|
return $className === TestObject::class; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Returns the list of classes that have matching input GraphQL types. |
65
|
|
|
* |
66
|
|
|
* @return string[] |
67
|
|
|
*/ |
68
|
|
|
public function getSupportedClasses(): array |
69
|
|
|
{ |
70
|
|
|
return [TestObject::class]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Returns a GraphQL type by name (can be either an input or output type) |
75
|
|
|
* |
76
|
|
|
* @param string $typeName The name of the GraphQL type |
77
|
|
|
* @param RecursiveTypeMapperInterface $recursiveTypeMapper |
78
|
|
|
* @return Type&(InputType|OutputType) |
79
|
|
|
*/ |
80
|
|
|
public function mapNameToType(string $typeName, RecursiveTypeMapperInterface $recursiveTypeMapper): Type |
81
|
|
|
{ |
82
|
|
|
switch ($typeName) { |
83
|
|
|
case 'TestObject': |
84
|
|
|
return new ObjectType([ |
85
|
|
|
'name' => 'TestObject', |
86
|
|
|
'fields' => [ |
87
|
|
|
'test' => Type::string(), |
88
|
|
|
], |
89
|
|
|
]); |
90
|
|
|
default: |
91
|
|
|
throw CannotMapTypeException::createForName($typeName); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
}; |
95
|
|
|
|
96
|
|
|
$this->composite = new CompositeTypeMapper([$typeMapper1]); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
public function testComposite(): void |
101
|
|
|
{ |
102
|
|
|
$this->assertTrue($this->composite->canMapClassToType(TestObject::class)); |
103
|
|
|
$this->assertFalse($this->composite->canMapClassToType(\Exception::class)); |
104
|
|
|
$this->assertTrue($this->composite->canMapClassToInputType(TestObject::class)); |
105
|
|
|
$this->assertFalse($this->composite->canMapClassToInputType(\Exception::class)); |
106
|
|
|
$this->assertInstanceOf(ObjectType::class, $this->composite->mapClassToType(TestObject::class, $this->getTypeMapper())); |
107
|
|
|
$this->assertInstanceOf(InputObjectType::class, $this->composite->mapClassToInputType(TestObject::class)); |
108
|
|
|
$this->assertSame([TestObject::class], $this->composite->getSupportedClasses()); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function testException1(): void |
112
|
|
|
{ |
113
|
|
|
$this->expectException(CannotMapTypeException::class); |
114
|
|
|
$this->composite->mapClassToType(\Exception::class, $this->getTypeMapper()); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function testException2(): void |
118
|
|
|
{ |
119
|
|
|
$this->expectException(CannotMapTypeException::class); |
120
|
|
|
$this->composite->mapClassToInputType(\Exception::class); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|