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
|
|
|
|
14
|
|
|
class StaticTypeMapperTest extends AbstractQueryProviderTest |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var StaticTypeMapper |
18
|
|
|
*/ |
19
|
|
|
private $typeMapper; |
20
|
|
|
|
21
|
|
|
public function setUp(): void |
22
|
|
|
{ |
23
|
|
|
$this->typeMapper = new StaticTypeMapper(); |
24
|
|
|
$this->typeMapper->setTypes([ |
25
|
|
|
TestObject::class => new ObjectType([ |
26
|
|
|
'name' => 'TestObject', |
27
|
|
|
'fields' => [ |
28
|
|
|
'test' => Type::string(), |
29
|
|
|
], |
30
|
|
|
]) |
31
|
|
|
]); |
32
|
|
|
$this->typeMapper->setInputTypes([ |
33
|
|
|
TestObject::class => new InputObjectType([ |
34
|
|
|
'name' => 'TestInputObject', |
35
|
|
|
'fields' => [ |
36
|
|
|
'test' => Type::string(), |
37
|
|
|
], |
38
|
|
|
]) |
39
|
|
|
]); |
40
|
|
|
$this->typeMapper->setNotMappedTypes([ |
41
|
|
|
new ObjectType([ |
42
|
|
|
'name' => 'TestNotMappedObject', |
43
|
|
|
'fields' => [ |
44
|
|
|
'test' => Type::string(), |
45
|
|
|
] |
46
|
|
|
]) |
47
|
|
|
]); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testStaticTypeMapper(): void |
51
|
|
|
{ |
52
|
|
|
$this->assertTrue($this->typeMapper->canMapClassToType(TestObject::class)); |
53
|
|
|
$this->assertFalse($this->typeMapper->canMapClassToType(\Exception::class)); |
54
|
|
|
$this->assertTrue($this->typeMapper->canMapClassToInputType(TestObject::class)); |
55
|
|
|
$this->assertFalse($this->typeMapper->canMapClassToInputType(\Exception::class)); |
56
|
|
|
$this->assertInstanceOf(ObjectType::class, $this->typeMapper->mapClassToType(TestObject::class, null, $this->getTypeMapper())); |
|
|
|
|
57
|
|
|
$this->assertInstanceOf(InputObjectType::class, $this->typeMapper->mapClassToInputType(TestObject::class, $this->getTypeMapper())); |
|
|
|
|
58
|
|
|
$this->assertSame([TestObject::class], $this->typeMapper->getSupportedClasses()); |
|
|
|
|
59
|
|
|
$this->assertSame('TestObject', $this->typeMapper->mapNameToType('TestObject', $this->getTypeMapper())->name); |
|
|
|
|
60
|
|
|
$this->assertSame('TestInputObject', $this->typeMapper->mapNameToType('TestInputObject', $this->getTypeMapper())->name); |
61
|
|
|
$this->assertSame('TestNotMappedObject', $this->typeMapper->mapNameToType('TestNotMappedObject', $this->getTypeMapper())->name); |
62
|
|
|
$this->assertTrue($this->typeMapper->canMapNameToType('TestObject')); |
63
|
|
|
$this->assertTrue($this->typeMapper->canMapNameToType('TestInputObject')); |
64
|
|
|
$this->assertTrue($this->typeMapper->canMapNameToType('TestNotMappedObject')); |
65
|
|
|
$this->assertFalse($this->typeMapper->canMapNameToType('NotExists')); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testException1(): void |
69
|
|
|
{ |
70
|
|
|
$this->expectException(CannotMapTypeException::class); |
71
|
|
|
$this->typeMapper->mapClassToType(\Exception::class, null, $this->getTypeMapper()); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testException2(): void |
75
|
|
|
{ |
76
|
|
|
$this->expectException(CannotMapTypeException::class); |
77
|
|
|
$this->typeMapper->mapClassToInputType(\Exception::class, $this->getTypeMapper()); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testException3(): void |
81
|
|
|
{ |
82
|
|
|
$this->expectException(CannotMapTypeException::class); |
83
|
|
|
$this->typeMapper->mapNameToType('notExists', $this->getTypeMapper()); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function testUnsupportedSubtypes(): void |
87
|
|
|
{ |
88
|
|
|
$this->expectException(CannotMapTypeException::class); |
89
|
|
|
$this->typeMapper->mapClassToType(TestObject::class, new StringType(), $this->getTypeMapper()); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|