1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TheCodingMachine\GraphQL\Controllers\Mappers; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use TheCodingMachine\GraphQL\Controllers\Mappers\CannotMapTypeException; |
7
|
|
|
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestObject; |
8
|
|
|
use TheCodingMachine\GraphQL\Controllers\Mappers\TypeMapperInterface; |
9
|
|
|
use TheCodingMachine\GraphQL\Controllers\TypeMappingException; |
10
|
|
|
use Youshido\GraphQL\Type\InputObject\InputObjectType; |
11
|
|
|
use Youshido\GraphQL\Type\InputTypeInterface; |
12
|
|
|
use Youshido\GraphQL\Type\Object\ObjectType; |
13
|
|
|
use Youshido\GraphQL\Type\Scalar\StringType; |
14
|
|
|
use Youshido\GraphQL\Type\TypeInterface; |
15
|
|
|
|
16
|
|
|
class CompositeTypeMapperTest extends TestCase |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var CompositeTypeMapper |
20
|
|
|
*/ |
21
|
|
|
protected $composite; |
22
|
|
|
|
23
|
|
|
public function setUp() |
24
|
|
|
{ |
25
|
|
|
$typeMapper1 = new class() implements TypeMapperInterface { |
26
|
|
|
public function mapClassToType(string $className): TypeInterface |
27
|
|
|
{ |
28
|
|
|
if ($className === TestObject::class) { |
29
|
|
|
return new ObjectType([ |
30
|
|
|
'name' => 'TestObject', |
31
|
|
|
'fields' => [ |
32
|
|
|
'test' => new StringType(), |
33
|
|
|
], |
34
|
|
|
]); |
35
|
|
|
} else { |
36
|
|
|
throw TypeMappingException::createFromType(TestObject::class); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function mapClassToInputType(string $className): InputTypeInterface |
41
|
|
|
{ |
42
|
|
|
if ($className === TestObject::class) { |
43
|
|
|
return new InputObjectType([ |
44
|
|
|
'name' => 'TestObject', |
45
|
|
|
'fields' => [ |
46
|
|
|
'test' => new StringType(), |
47
|
|
|
], |
48
|
|
|
]); |
49
|
|
|
} else { |
50
|
|
|
throw TypeMappingException::createFromType(TestObject::class); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function canMapClassToType(string $className): bool |
55
|
|
|
{ |
56
|
|
|
return $className === TestObject::class; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function canMapClassToInputType(string $className): bool |
60
|
|
|
{ |
61
|
|
|
return $className === TestObject::class; |
62
|
|
|
} |
63
|
|
|
}; |
64
|
|
|
|
65
|
|
|
$this->composite = new CompositeTypeMapper([$typeMapper1]); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
public function testComposite(): void |
70
|
|
|
{ |
71
|
|
|
$this->assertTrue($this->composite->canMapClassToType(TestObject::class)); |
72
|
|
|
$this->assertFalse($this->composite->canMapClassToType(\Exception::class)); |
73
|
|
|
$this->assertTrue($this->composite->canMapClassToInputType(TestObject::class)); |
74
|
|
|
$this->assertFalse($this->composite->canMapClassToInputType(\Exception::class)); |
75
|
|
|
$this->assertInstanceOf(ObjectType::class, $this->composite->mapClassToType(TestObject::class)); |
76
|
|
|
$this->assertInstanceOf(InputObjectType::class, $this->composite->mapClassToInputType(TestObject::class)); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testException1(): void |
80
|
|
|
{ |
81
|
|
|
$this->expectException(CannotMapTypeException::class); |
82
|
|
|
$this->composite->mapClassToType(\Exception::class); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testException2(): void |
86
|
|
|
{ |
87
|
|
|
$this->expectException(CannotMapTypeException::class); |
88
|
|
|
$this->composite->mapClassToInputType(\Exception::class); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|