|
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
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function testException1(): void |
|
55
|
|
|
{ |
|
56
|
|
|
$this->expectException(CannotMapTypeException::class); |
|
57
|
|
|
$this->typeMapper->mapClassToType(\Exception::class, $this->getTypeMapper()); |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function testException2(): void |
|
61
|
|
|
{ |
|
62
|
|
|
$this->expectException(CannotMapTypeException::class); |
|
63
|
|
|
$this->typeMapper->mapClassToInputType(\Exception::class); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function testException3(): void |
|
67
|
|
|
{ |
|
68
|
|
|
$this->expectException(CannotMapTypeException::class); |
|
69
|
|
|
$this->typeMapper->mapNameToType('notExists', $this->getTypeMapper()); |
|
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|