RecursiveTypeMapperTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 71
dl 0
loc 133
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testMapClassToType() 0 20 1
A testGetOutputTypes() 0 8 1
A testMapNameToType() 0 15 1
A testMapClassToInputType() 0 17 1
A getTypeMapper() 0 18 1
A testMapNameToType2() 0 13 1
A testMapClassToInterfaceOrType() 0 26 1
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
use TheCodingMachine\GraphQL\Controllers\Types\MutableObjectType;
22
23
class RecursiveTypeMapperTest extends AbstractQueryProviderTest
24
{
25
26
    public function testMapClassToType()
27
    {
28
        $objectType = new MutableObjectType([
29
            'name' => 'Foobar'
30
        ]);
31
32
        $typeMapper = new StaticTypeMapper();
33
        $typeMapper->setTypes([
34
            ClassB::class => $objectType
35
        ]);
36
37
        $recursiveTypeMapper = new RecursiveTypeMapper($typeMapper, new NamingStrategy(), new ArrayCache(), $this->getTypeRegistry());
38
39
        $this->assertFalse($typeMapper->canMapClassToType(ClassC::class));
40
        $this->assertTrue($recursiveTypeMapper->canMapClassToType(ClassC::class));
41
        $this->assertSame($objectType, $recursiveTypeMapper->mapClassToType(ClassC::class, null));
42
43
        $this->assertFalse($recursiveTypeMapper->canMapClassToType(ClassA::class));
44
        $this->expectException(CannotMapTypeException::class);
45
        $recursiveTypeMapper->mapClassToType(ClassA::class, null);
46
    }
47
48
    public function testMapNameToType()
49
    {
50
        $objectType = new MutableObjectType([
51
            'name' => 'Foobar'
52
        ]);
53
54
        $typeMapper = new StaticTypeMapper();
55
        $typeMapper->setTypes([
56
            ClassB::class => $objectType
57
        ]);
58
59
        $recursiveTypeMapper = new RecursiveTypeMapper($typeMapper, new NamingStrategy(), new ArrayCache(), $this->getTypeRegistry());
60
61
        $this->assertTrue($recursiveTypeMapper->canMapNameToType('Foobar'));
62
        $this->assertSame($objectType, $recursiveTypeMapper->mapNameToType('Foobar'));
63
    }
64
65
    public function testMapNameToType2()
66
    {
67
        $recursiveMapper = $this->getTypeMapper();
68
69
        $this->assertTrue($recursiveMapper->canMapNameToType('ClassA'));
70
        $this->assertTrue($recursiveMapper->canMapNameToType('ClassB'));
71
        $this->assertTrue($recursiveMapper->canMapNameToType('ClassAInterface'));
72
        $this->assertFalse($recursiveMapper->canMapNameToType('NotExists'));
73
        $this->assertSame('ClassA', $recursiveMapper->mapNameToType('ClassA')->name);
74
        $this->assertSame('ClassAInterface', $recursiveMapper->mapNameToType('ClassAInterface')->name);
75
76
        $this->expectException(CannotMapTypeException::class);
77
        $recursiveMapper->mapNameToType('NotExists');
78
    }
79
80
81
    public function testMapClassToInputType()
82
    {
83
        $inputObjectType = new InputObjectType([
84
            'name' => 'Foobar'
85
        ]);
86
87
        $typeMapper = new StaticTypeMapper();
88
        $typeMapper->setInputTypes([
89
            ClassB::class => $inputObjectType
90
        ]);
91
92
        $recursiveTypeMapper = new RecursiveTypeMapper($typeMapper, new NamingStrategy(), new ArrayCache(), $this->getTypeRegistry());
93
94
        $this->assertFalse($recursiveTypeMapper->canMapClassToInputType(ClassC::class));
95
96
        $this->expectException(CannotMapTypeException::class);
97
        $recursiveTypeMapper->mapClassToInputType(ClassC::class);
98
    }
99
100
    protected function getTypeMapper()
101
    {
102
        $container = new Picotainer([
103
            ClassAType::class => function() {
104
                return new ClassAType();
105
            },
106
            ClassBType::class => function() {
107
                return new ClassBType();
108
            }
109
        ]);
110
111
        $namingStrategy = new NamingStrategy();
112
113
        $typeGenerator = new TypeGenerator($this->getAnnotationReader(), $this->getControllerQueryProviderFactory(), $namingStrategy, $this->getTypeRegistry(), $this->getRegistry());
114
115
        $mapper = new GlobTypeMapper('TheCodingMachine\GraphQL\Controllers\Fixtures\Interfaces\Types', $typeGenerator, $this->getInputTypeGenerator(), $this->getInputTypeUtils(), $container, new \TheCodingMachine\GraphQL\Controllers\AnnotationReader(new AnnotationReader()), $namingStrategy, new NullCache());
116
117
        return new RecursiveTypeMapper($mapper, new NamingStrategy(), new ArrayCache(), $this->getTypeRegistry());
118
    }
119
120
    public function testMapClassToInterfaceOrType()
121
    {
122
        $recursiveMapper = $this->getTypeMapper();
123
124
        $type = $recursiveMapper->mapClassToInterfaceOrType(ClassA::class, null);
125
        $this->assertInstanceOf(InterfaceType::class, $type);
126
        $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...
127
128
        $classAObjectType = $recursiveMapper->mapClassToType(ClassA::class, null);
129
        $this->assertInstanceOf(ObjectType::class, $classAObjectType);
130
        $this->assertCount(1, $classAObjectType->getInterfaces());
131
132
        $type = $recursiveMapper->mapClassToInterfaceOrType(ClassC::class, null);
133
        $this->assertInstanceOf(ObjectType::class, $type);
134
        $this->assertSame('ClassB', $type->name);
135
136
        $interfaces = $recursiveMapper->findInterfaces(ClassC::class);
137
        $this->assertCount(1, $interfaces);
138
        $this->assertSame('ClassAInterface', $interfaces[0]->name);
139
140
        $classBObjectType = $recursiveMapper->mapClassToType(ClassC::class, null);
141
        $this->assertInstanceOf(ObjectType::class, $classBObjectType);
142
        $this->assertCount(1, $classBObjectType->getInterfaces());
143
144
        $this->expectException(CannotMapTypeException::class);
145
        $recursiveMapper->mapClassToInterfaceOrType('Not exists', null);
146
    }
147
148
    public function testGetOutputTypes()
149
    {
150
        $recursiveMapper = $this->getTypeMapper();
151
152
        $outputTypes = $recursiveMapper->getOutputTypes();
153
        $this->assertArrayHasKey('TheCodingMachine\\GraphQL\\Controllers\\Fixtures\\Interfaces\\ClassA', $outputTypes);
154
        $this->assertArrayHasKey('TheCodingMachine\\GraphQL\\Controllers\\Fixtures\\Interfaces\\ClassB', $outputTypes);
155
        $this->assertArrayNotHasKey('TheCodingMachine\\GraphQL\\Controllers\\Fixtures\\Interfaces\\ClassC', $outputTypes);
156
    }
157
158
159
}
160