1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TheCodingMachine\GraphQL\Controllers\Mappers; |
4
|
|
|
|
5
|
|
|
use GraphQL\Type\Definition\Type; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use TheCodingMachine\GraphQL\Controllers\AbstractQueryProviderTest; |
8
|
|
|
use TheCodingMachine\GraphQL\Controllers\Mappers\CannotMapTypeException; |
9
|
|
|
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestObject; |
10
|
|
|
use GraphQL\Type\Definition\InputObjectType; |
11
|
|
|
use GraphQL\Type\Definition\ObjectType; |
12
|
|
|
|
13
|
|
|
class StaticTypeMapperTest extends AbstractQueryProviderTest |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var StaticTypeMapper |
17
|
|
|
*/ |
18
|
|
|
private $typeMapper; |
19
|
|
|
|
20
|
|
|
public function setUp(): void |
21
|
|
|
{ |
22
|
|
|
$this->typeMapper = new StaticTypeMapper(); |
23
|
|
|
$this->typeMapper->setTypes([ |
24
|
|
|
TestObject::class => new ObjectType([ |
25
|
|
|
'name' => 'TestObject', |
26
|
|
|
'fields' => [ |
27
|
|
|
'test' => Type::string(), |
28
|
|
|
], |
29
|
|
|
]) |
30
|
|
|
]); |
31
|
|
|
$this->typeMapper->setInputTypes([ |
32
|
|
|
TestObject::class => new InputObjectType([ |
33
|
|
|
'name' => 'TestInputObject', |
34
|
|
|
'fields' => [ |
35
|
|
|
'test' => Type::string(), |
36
|
|
|
], |
37
|
|
|
]) |
38
|
|
|
]); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testStaticTypeMapper(): void |
42
|
|
|
{ |
43
|
|
|
$this->assertTrue($this->typeMapper->canMapClassToType(TestObject::class)); |
44
|
|
|
$this->assertFalse($this->typeMapper->canMapClassToType(\Exception::class)); |
45
|
|
|
$this->assertTrue($this->typeMapper->canMapClassToInputType(TestObject::class)); |
46
|
|
|
$this->assertFalse($this->typeMapper->canMapClassToInputType(\Exception::class)); |
47
|
|
|
$this->assertInstanceOf(ObjectType::class, $this->typeMapper->mapClassToType(TestObject::class, $this->getTypeMapper())); |
|
|
|
|
48
|
|
|
$this->assertInstanceOf(InputObjectType::class, $this->typeMapper->mapClassToInputType(TestObject::class)); |
49
|
|
|
$this->assertSame([TestObject::class], $this->typeMapper->getSupportedClasses()); |
|
|
|
|
50
|
|
|
$this->assertSame('TestObject', $this->typeMapper->mapNameToType('TestObject', $this->getTypeMapper())->name); |
|
|
|
|
51
|
|
|
$this->assertSame('TestInputObject', $this->typeMapper->mapNameToType('TestInputObject', $this->getTypeMapper())->name); |
52
|
|
|
$this->assertTrue($this->typeMapper->canMapNameToType('TestObject')); |
53
|
|
|
$this->assertTrue($this->typeMapper->canMapNameToType('TestInputObject')); |
54
|
|
|
$this->assertFalse($this->typeMapper->canMapNameToType('NotExists')); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testException1(): void |
58
|
|
|
{ |
59
|
|
|
$this->expectException(CannotMapTypeException::class); |
60
|
|
|
$this->typeMapper->mapClassToType(\Exception::class, $this->getTypeMapper()); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testException2(): void |
64
|
|
|
{ |
65
|
|
|
$this->expectException(CannotMapTypeException::class); |
66
|
|
|
$this->typeMapper->mapClassToInputType(\Exception::class); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testException3(): void |
70
|
|
|
{ |
71
|
|
|
$this->expectException(CannotMapTypeException::class); |
72
|
|
|
$this->typeMapper->mapNameToType('notExists', $this->getTypeMapper()); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|