Completed
Pull Request — master (#60)
by David
02:00
created

RecursiveTypeMapperTest::getTypeMapper()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 16
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace TheCodingMachine\GraphQL\Controllers\Mappers;
4
5
use Doctrine\Common\Annotations\AnnotationReader;
6
use GraphQL\Type\Definition\InputObjectType;
7
use GraphQL\Type\Definition\InterfaceType;
8
use GraphQL\Type\Definition\ObjectType;
9
use Mouf\Picotainer\Picotainer;
10
use Symfony\Component\Cache\Simple\ArrayCache;
11
use Symfony\Component\Cache\Simple\NullCache;
12
use TheCodingMachine\GraphQL\Controllers\AbstractQueryProviderTest;
13
use TheCodingMachine\GraphQL\Controllers\Fixtures\Interfaces\ClassA;
14
use TheCodingMachine\GraphQL\Controllers\Fixtures\Interfaces\ClassB;
15
use TheCodingMachine\GraphQL\Controllers\Fixtures\Interfaces\ClassC;
16
use TheCodingMachine\GraphQL\Controllers\Fixtures\Interfaces\Types\ClassAType;
17
use TheCodingMachine\GraphQL\Controllers\Fixtures\Interfaces\Types\ClassBType;
18
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestObject;
19
use TheCodingMachine\GraphQL\Controllers\NamingStrategy;
20
use TheCodingMachine\GraphQL\Controllers\TypeGenerator;
21
22
class RecursiveTypeMapperTest extends AbstractQueryProviderTest
23
{
24
25
    public function testMapClassToType()
26
    {
27
        $objectType = new ObjectType([
28
            'name' => 'Foobar'
29
        ]);
30
31
        $typeMapper = new StaticTypeMapper();
32
        $typeMapper->setTypes([
33
            ClassB::class => $objectType
34
        ]);
35
36
        $recursiveTypeMapper = new RecursiveTypeMapper($typeMapper, new NamingStrategy(), new ArrayCache());
37
38
        $this->assertFalse($typeMapper->canMapClassToType(ClassC::class));
39
        $this->assertTrue($recursiveTypeMapper->canMapClassToType(ClassC::class));
40
        $this->assertSame($objectType, $recursiveTypeMapper->mapClassToType(ClassC::class));
41
42
        $this->assertFalse($recursiveTypeMapper->canMapClassToType(ClassA::class));
43
        $this->expectException(CannotMapTypeException::class);
44
        $recursiveTypeMapper->mapClassToType(ClassA::class);
45
    }
46
47
    public function testMapNameToType()
48
    {
49
        $objectType = new ObjectType([
50
            'name' => 'Foobar'
51
        ]);
52
53
        $typeMapper = new StaticTypeMapper();
54
        $typeMapper->setTypes([
55
            ClassB::class => $objectType
56
        ]);
57
58
        $recursiveTypeMapper = new RecursiveTypeMapper($typeMapper, new NamingStrategy(), new ArrayCache());
59
60
        $this->assertTrue($recursiveTypeMapper->canMapNameToType('Foobar'));
61
        $this->assertSame($objectType, $recursiveTypeMapper->mapNameToType('Foobar'));
62
    }
63
64
    public function testMapNameToType2()
65
    {
66
        $recursiveMapper = $this->getTypeMapper();
67
68
        $this->assertTrue($recursiveMapper->canMapNameToType('ClassA'));
69
        $this->assertTrue($recursiveMapper->canMapNameToType('ClassB'));
70
        $this->assertTrue($recursiveMapper->canMapNameToType('ClassAInterface'));
71
        $this->assertFalse($recursiveMapper->canMapNameToType('NotExists'));
72
        $this->assertSame('ClassA', $recursiveMapper->mapNameToType('ClassA')->name);
73
        $this->assertSame('ClassAInterface', $recursiveMapper->mapNameToType('ClassAInterface')->name);
74
75
        $this->expectException(CannotMapTypeException::class);
76
        $recursiveMapper->mapNameToType('NotExists');
77
    }
78
79
80
    public function testMapClassToInputType()
81
    {
82
        $inputObjectType = new InputObjectType([
83
            'name' => 'Foobar'
84
        ]);
85
86
        $typeMapper = new StaticTypeMapper();
87
        $typeMapper->setInputTypes([
88
            ClassB::class => $inputObjectType
89
        ]);
90
91
        $recursiveTypeMapper = new RecursiveTypeMapper($typeMapper, new NamingStrategy(), new ArrayCache());
92
93
        $this->assertFalse($recursiveTypeMapper->canMapClassToInputType(ClassC::class));
94
95
        $this->expectException(CannotMapTypeException::class);
96
        $recursiveTypeMapper->mapClassToInputType(ClassC::class);
97
    }
98
99
    protected function getTypeMapper()
100
    {
101
        $container = new Picotainer([
102
            ClassAType::class => function() {
103
                return new ClassAType();
104
            },
105
            ClassBType::class => function() {
106
                return new ClassBType();
107
            }
108
        ]);
109
110
        $typeGenerator = new TypeGenerator($this->getAnnotationReader(), $this->getControllerQueryProviderFactory());
111
112
        $mapper = new GlobTypeMapper('TheCodingMachine\GraphQL\Controllers\Fixtures\Interfaces\Types', $typeGenerator, $container, new \TheCodingMachine\GraphQL\Controllers\AnnotationReader(new AnnotationReader()), new NullCache());
113
114
        return new RecursiveTypeMapper($mapper, new NamingStrategy(), new ArrayCache());
115
    }
116
117
    public function testMapClassToInterfaceOrType()
118
    {
119
        $recursiveMapper = $this->getTypeMapper();
120
121
        $type = $recursiveMapper->mapClassToInterfaceOrType(ClassA::class);
122
        $this->assertInstanceOf(InterfaceType::class, $type);
123
        $this->assertSame('ClassAInterface', $type->name);
0 ignored issues
show
Bug introduced by
Accessing name on the interface GraphQL\Type\Definition\OutputType suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
124
125
        $classAObjectType = $recursiveMapper->mapClassToType(ClassA::class);
126
        $this->assertInstanceOf(ObjectType::class, $classAObjectType);
127
        $this->assertCount(1, $classAObjectType->getInterfaces());
128
129
        $type = $recursiveMapper->mapClassToInterfaceOrType(ClassC::class);
130
        $this->assertInstanceOf(ObjectType::class, $type);
131
        $this->assertSame('ClassB', $type->name);
132
133
        $interfaces = $recursiveMapper->findInterfaces(ClassC::class);
134
        $this->assertCount(1, $interfaces);
135
        $this->assertSame('ClassAInterface', $interfaces[0]->name);
136
137
        $classBObjectType = $recursiveMapper->mapClassToType(ClassC::class);
138
        $this->assertInstanceOf(ObjectType::class, $classBObjectType);
139
        $this->assertCount(1, $classBObjectType->getInterfaces());
140
141
        $this->expectException(CannotMapTypeException::class);
142
        $recursiveMapper->mapClassToInterfaceOrType('Not exists');
143
    }
144
145
    public function testGetOutputTypes()
146
    {
147
        $recursiveMapper = $this->getTypeMapper();
148
149
        $outputTypes = $recursiveMapper->getOutputTypes();
150
        $this->assertArrayHasKey('TheCodingMachine\\GraphQL\\Controllers\\Fixtures\\Interfaces\\ClassA', $outputTypes);
151
        $this->assertArrayHasKey('TheCodingMachine\\GraphQL\\Controllers\\Fixtures\\Interfaces\\ClassB', $outputTypes);
152
        $this->assertArrayNotHasKey('TheCodingMachine\\GraphQL\\Controllers\\Fixtures\\Interfaces\\ClassC', $outputTypes);
153
    }
154
155
156
}
157