1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TheCodingMachine\GraphQL\Controllers\Mappers; |
4
|
|
|
|
5
|
|
|
use GraphQL\Type\Definition\StringType; |
6
|
|
|
use GraphQL\Type\Definition\Type; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use TheCodingMachine\GraphQL\Controllers\AbstractQueryProviderTest; |
9
|
|
|
use TheCodingMachine\GraphQL\Controllers\Mappers\CannotMapTypeException; |
10
|
|
|
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestObject; |
11
|
|
|
use GraphQL\Type\Definition\InputObjectType; |
12
|
|
|
use GraphQL\Type\Definition\ObjectType; |
13
|
|
|
use TheCodingMachine\GraphQL\Controllers\Types\MutableObjectType; |
14
|
|
|
|
15
|
|
|
class StaticTypeMapperTest extends AbstractQueryProviderTest |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var StaticTypeMapper |
19
|
|
|
*/ |
20
|
|
|
private $typeMapper; |
21
|
|
|
|
22
|
|
|
public function setUp(): void |
23
|
|
|
{ |
24
|
|
|
$this->typeMapper = new StaticTypeMapper(); |
25
|
|
|
$this->typeMapper->setTypes([ |
26
|
|
|
TestObject::class => new MutableObjectType([ |
27
|
|
|
'name' => 'TestObject', |
28
|
|
|
'fields' => [ |
29
|
|
|
'test' => Type::string(), |
30
|
|
|
], |
31
|
|
|
]) |
32
|
|
|
]); |
33
|
|
|
$this->typeMapper->setInputTypes([ |
34
|
|
|
TestObject::class => new InputObjectType([ |
35
|
|
|
'name' => 'TestInputObject', |
36
|
|
|
'fields' => [ |
37
|
|
|
'test' => Type::string(), |
38
|
|
|
], |
39
|
|
|
]) |
40
|
|
|
]); |
41
|
|
|
$this->typeMapper->setNotMappedTypes([ |
42
|
|
|
new ObjectType([ |
43
|
|
|
'name' => 'TestNotMappedObject', |
44
|
|
|
'fields' => [ |
45
|
|
|
'test' => Type::string(), |
46
|
|
|
] |
47
|
|
|
]) |
48
|
|
|
]); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testStaticTypeMapper(): void |
52
|
|
|
{ |
53
|
|
|
$this->assertTrue($this->typeMapper->canMapClassToType(TestObject::class)); |
54
|
|
|
$this->assertFalse($this->typeMapper->canMapClassToType(\Exception::class)); |
55
|
|
|
$this->assertTrue($this->typeMapper->canMapClassToInputType(TestObject::class)); |
56
|
|
|
$this->assertFalse($this->typeMapper->canMapClassToInputType(\Exception::class)); |
57
|
|
|
$this->assertInstanceOf(ObjectType::class, $this->typeMapper->mapClassToType(TestObject::class, null, $this->getTypeMapper())); |
|
|
|
|
58
|
|
|
$this->assertInstanceOf(InputObjectType::class, $this->typeMapper->mapClassToInputType(TestObject::class, $this->getTypeMapper())); |
|
|
|
|
59
|
|
|
$this->assertSame([TestObject::class], $this->typeMapper->getSupportedClasses()); |
|
|
|
|
60
|
|
|
$this->assertSame('TestObject', $this->typeMapper->mapNameToType('TestObject', $this->getTypeMapper())->name); |
|
|
|
|
61
|
|
|
$this->assertSame('TestInputObject', $this->typeMapper->mapNameToType('TestInputObject', $this->getTypeMapper())->name); |
62
|
|
|
$this->assertSame('TestNotMappedObject', $this->typeMapper->mapNameToType('TestNotMappedObject', $this->getTypeMapper())->name); |
63
|
|
|
$this->assertTrue($this->typeMapper->canMapNameToType('TestObject')); |
64
|
|
|
$this->assertTrue($this->typeMapper->canMapNameToType('TestInputObject')); |
65
|
|
|
$this->assertTrue($this->typeMapper->canMapNameToType('TestNotMappedObject')); |
66
|
|
|
$this->assertFalse($this->typeMapper->canMapNameToType('NotExists')); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testException1(): void |
70
|
|
|
{ |
71
|
|
|
$this->expectException(CannotMapTypeException::class); |
72
|
|
|
$this->typeMapper->mapClassToType(\Exception::class, null, $this->getTypeMapper()); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function testException2(): void |
76
|
|
|
{ |
77
|
|
|
$this->expectException(CannotMapTypeException::class); |
78
|
|
|
$this->typeMapper->mapClassToInputType(\Exception::class, $this->getTypeMapper()); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function testException3(): void |
82
|
|
|
{ |
83
|
|
|
$this->expectException(CannotMapTypeException::class); |
84
|
|
|
$this->typeMapper->mapNameToType('notExists', $this->getTypeMapper()); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function testException4() |
88
|
|
|
{ |
89
|
|
|
$type = new MutableObjectType(['name'=>'foo']); |
90
|
|
|
|
91
|
|
|
$this->expectException(CannotMapTypeExceptionInterface::class); |
92
|
|
|
$this->typeMapper->extendTypeForClass('foo', $type, $this->getTypeMapper()); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function testException5() |
96
|
|
|
{ |
97
|
|
|
$type = new MutableObjectType(['name'=>'foo']); |
98
|
|
|
|
99
|
|
|
$this->expectException(CannotMapTypeExceptionInterface::class); |
100
|
|
|
$this->typeMapper->extendTypeForName('foo', $type, $this->getTypeMapper()); |
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function testUnsupportedSubtypes(): void |
104
|
|
|
{ |
105
|
|
|
$this->expectException(CannotMapTypeException::class); |
106
|
|
|
$this->typeMapper->mapClassToType(TestObject::class, new StringType(), $this->getTypeMapper()); |
|
|
|
|
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|