Completed
Push — master ( 78ac87...d80d23 )
by David
17s queued 10s
created

GlobTypeMapperTest::testEmptyGlobTypeMapper()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 12
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 Mouf\Picotainer\Picotainer;
7
use Symfony\Component\Cache\Simple\ArrayCache;
8
use Symfony\Component\Cache\Simple\NullCache;
9
use Test;
10
use TheCodingMachine\GraphQL\Controllers\AbstractQueryProviderTest;
11
use TheCodingMachine\GraphQL\Controllers\Annotations\Exceptions\ClassNotFoundException;
12
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestObject;
13
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestType;
14
use TheCodingMachine\GraphQL\Controllers\Fixtures\Types\FooExtendType;
15
use TheCodingMachine\GraphQL\Controllers\Fixtures\Types\FooType;
16
use TheCodingMachine\GraphQL\Controllers\Fixtures\Types\TestFactory;
17
use TheCodingMachine\GraphQL\Controllers\NamingStrategy;
18
use TheCodingMachine\GraphQL\Controllers\TypeGenerator;
19
use GraphQL\Type\Definition\ObjectType;
20
21
class GlobTypeMapperTest extends AbstractQueryProviderTest
22
{
23
    public function testGlobTypeMapper()
24
    {
25
        $container = new Picotainer([
26
            FooType::class => function () {
27
                return new FooType();
28
            }
29
        ]);
30
31
        $typeGenerator = $this->getTypeGenerator();
32
        $inputTypeGenerator = $this->getInputTypeGenerator();
33
34
        $cache = new ArrayCache();
35
36
        $mapper = new GlobTypeMapper('TheCodingMachine\GraphQL\Controllers\Fixtures\Types', $typeGenerator, $inputTypeGenerator, $this->getInputTypeUtils(), $container, new \TheCodingMachine\GraphQL\Controllers\AnnotationReader(new AnnotationReader()), new NamingStrategy(), $cache);
37
38
        $this->assertSame([TestObject::class], $mapper->getSupportedClasses());
39
        $this->assertTrue($mapper->canMapClassToType(TestObject::class));
40
        $this->assertInstanceOf(ObjectType::class, $mapper->mapClassToType(TestObject::class, null, $this->getTypeMapper()));
41
        $this->assertInstanceOf(ObjectType::class, $mapper->mapNameToType('Foo', $this->getTypeMapper()));
42
        $this->assertTrue($mapper->canMapNameToType('Foo'));
43
        $this->assertFalse($mapper->canMapNameToType('NotExists'));
44
45
        // Again to test cache
46
        $anotherMapperSameCache = new GlobTypeMapper('TheCodingMachine\GraphQL\Controllers\Fixtures\Types', $typeGenerator, $this->getInputTypeGenerator(), $this->getInputTypeUtils(), $container, new \TheCodingMachine\GraphQL\Controllers\AnnotationReader(new AnnotationReader()), new NamingStrategy(), $cache);
47
        $this->assertTrue($anotherMapperSameCache->canMapClassToType(TestObject::class));
48
        $this->assertTrue($anotherMapperSameCache->canMapNameToType('Foo'));
49
50
        $this->expectException(CannotMapTypeException::class);
51
        $mapper->mapClassToType(\stdClass::class, null, $this->getTypeMapper());
52
    }
53
54
    public function testGlobTypeMapperDuplicateTypesException()
55
    {
56
        $container = new Picotainer([
57
            TestType::class => function () {
58
                return new TestType();
59
            }
60
        ]);
61
62
        $typeGenerator = $this->getTypeGenerator();
63
64
        $mapper = new GlobTypeMapper('TheCodingMachine\GraphQL\Controllers\Fixtures\DuplicateTypes', $typeGenerator, $this->getInputTypeGenerator(), $this->getInputTypeUtils(), $container, new \TheCodingMachine\GraphQL\Controllers\AnnotationReader(new AnnotationReader()), new NamingStrategy(), new NullCache());
65
66
        $this->expectException(DuplicateMappingException::class);
67
        $mapper->canMapClassToType(TestType::class);
68
    }
69
70
    public function testGlobTypeMapperDuplicateInputTypesException()
71
    {
72
        $container = new Picotainer([
73
            /*TestType::class => function() {
74
                return new TestType();
75
            }*/
76
        ]);
77
78
        $typeGenerator = $this->getTypeGenerator();
79
80
        $mapper = new GlobTypeMapper('TheCodingMachine\GraphQL\Controllers\Fixtures\DuplicateInputTypes', $typeGenerator, $this->getInputTypeGenerator(), $this->getInputTypeUtils(), $container, new \TheCodingMachine\GraphQL\Controllers\AnnotationReader(new AnnotationReader()), new NamingStrategy(), new NullCache());
81
82
        $this->expectException(DuplicateMappingException::class);
83
        $this->expectExceptionMessage('The class \'TheCodingMachine\GraphQL\Controllers\Fixtures\TestObject\' should be mapped to only one GraphQL Input type. Two methods are pointing via the @Factory annotation to this class: \'TheCodingMachine\GraphQL\Controllers\Fixtures\DuplicateInputTypes\TestFactory::myFactory\' and \'TheCodingMachine\GraphQL\Controllers\Fixtures\DuplicateInputTypes\TestFactory2::myFactory\'');
84
        $mapper->canMapClassToInputType(TestObject::class);
85
    }
86
87
    public function testGlobTypeMapperClassNotFoundException()
88
    {
89
        $container = new Picotainer([
90
            TestType::class => function () {
91
                return new TestType();
92
            }
93
        ]);
94
95
        $typeGenerator = $this->getTypeGenerator();
96
97
        $mapper = new GlobTypeMapper('TheCodingMachine\GraphQL\Controllers\Fixtures\BadClassType', $typeGenerator, $this->getInputTypeGenerator(), $this->getInputTypeUtils(), $container, new \TheCodingMachine\GraphQL\Controllers\AnnotationReader(new AnnotationReader()), new NamingStrategy(), new NullCache());
98
99
        $this->expectException(ClassNotFoundException::class);
100
        $this->expectExceptionMessage("Could not autoload class 'Foobar' defined in @Type annotation of class 'TheCodingMachine\\GraphQL\\Controllers\\Fixtures\\BadClassType\\TestType'");
101
        $mapper->canMapClassToType(TestType::class);
102
    }
103
104
    public function testGlobTypeMapperNameNotFoundException()
105
    {
106
        $container = new Picotainer([
107
            FooType::class => function () {
108
                return new FooType();
109
            }
110
        ]);
111
112
        $typeGenerator = $this->getTypeGenerator();
113
114
        $mapper = new GlobTypeMapper('TheCodingMachine\GraphQL\Controllers\Fixtures\Types', $typeGenerator, $this->getInputTypeGenerator(), $this->getInputTypeUtils(), $container, new \TheCodingMachine\GraphQL\Controllers\AnnotationReader(new AnnotationReader()), new NamingStrategy(), new NullCache());
115
116
        $this->expectException(CannotMapTypeException::class);
117
        $mapper->mapNameToType('NotExists', $this->getTypeMapper());
118
    }
119
120
    public function testGlobTypeMapperInputType()
121
    {
122
        $container = new Picotainer([
123
            FooType::class => function () {
124
                return new FooType();
125
            },
126
            TestFactory::class => function () {
127
                return new TestFactory();
128
            }
129
        ]);
130
131
        $typeGenerator = $this->getTypeGenerator();
132
133
        $cache = new ArrayCache();
134
135
        $mapper = new GlobTypeMapper('TheCodingMachine\GraphQL\Controllers\Fixtures\Types', $typeGenerator, $this->getInputTypeGenerator(), $this->getInputTypeUtils(), $container, new \TheCodingMachine\GraphQL\Controllers\AnnotationReader(new AnnotationReader()), new NamingStrategy(), $cache);
136
137
        $this->assertTrue($mapper->canMapClassToInputType(TestObject::class));
138
139
        $inputType = $mapper->mapClassToInputType(TestObject::class, $this->getTypeMapper());
140
141
        $this->assertSame('TestObjectInput', $inputType->name);
142
143
        // Again to test cache
144
        $anotherMapperSameCache = new GlobTypeMapper('TheCodingMachine\GraphQL\Controllers\Fixtures\Types', $typeGenerator, $this->getInputTypeGenerator(), $this->getInputTypeUtils(), $container, new \TheCodingMachine\GraphQL\Controllers\AnnotationReader(new AnnotationReader()), new NamingStrategy(), $cache);
145
146
        $this->assertTrue($anotherMapperSameCache->canMapClassToInputType(TestObject::class));
147
        $this->assertSame('TestObjectInput', $anotherMapperSameCache->mapClassToInputType(TestObject::class, $this->getTypeMapper())->name);
148
149
150
        $this->expectException(CannotMapTypeException::class);
151
        $mapper->mapClassToInputType(TestType::class, $this->getTypeMapper());
152
    }
153
154
    public function testGlobTypeMapperExtend()
155
    {
156
        $container = new Picotainer([
157
            FooType::class => function () {
158
                return new FooType();
159
            },
160
            FooExtendType::class => function () {
161
                return new FooExtendType();
162
            }
163
        ]);
164
165
        $typeGenerator = $this->getTypeGenerator();
166
        $inputTypeGenerator = $this->getInputTypeGenerator();
167
168
        $cache = new ArrayCache();
169
170
        $mapper = new GlobTypeMapper('TheCodingMachine\GraphQL\Controllers\Fixtures\Types', $typeGenerator, $inputTypeGenerator, $this->getInputTypeUtils(), $container, new \TheCodingMachine\GraphQL\Controllers\AnnotationReader(new AnnotationReader()), new NamingStrategy(), $cache);
171
172
        $type = $mapper->mapClassToType(TestObject::class, null, $this->getTypeMapper());
173
174
        $this->assertTrue($mapper->canExtendTypeForClass(TestObject::class, $type, $this->getTypeMapper()));
175
        $mapper->extendTypeForClass(TestObject::class, $type, $this->getTypeMapper());
176
        $mapper->extendTypeForName('TestObject', $type, $this->getTypeMapper());
177
        $this->assertTrue($mapper->canExtendTypeForName('TestObject', $type, $this->getTypeMapper()));
178
        $this->assertFalse($mapper->canExtendTypeForName('NotExists', $type, $this->getTypeMapper()));
179
180
        // Again to test cache
181
        $anotherMapperSameCache = new GlobTypeMapper('TheCodingMachine\GraphQL\Controllers\Fixtures\Types', $typeGenerator, $this->getInputTypeGenerator(), $this->getInputTypeUtils(), $container, new \TheCodingMachine\GraphQL\Controllers\AnnotationReader(new AnnotationReader()), new NamingStrategy(), $cache);
182
        $this->assertTrue($anotherMapperSameCache->canExtendTypeForClass(TestObject::class, $type, $this->getTypeMapper()));
183
        $this->assertTrue($anotherMapperSameCache->canExtendTypeForName('TestObject', $type, $this->getTypeMapper()));
184
185
        $this->expectException(CannotMapTypeException::class);
186
        $mapper->extendTypeForClass(\stdClass::class, $type, $this->getTypeMapper());
187
    }
188
189
    public function testEmptyGlobTypeMapper()
190
    {
191
        $container = new Picotainer([]);
192
193
        $typeGenerator = $this->getTypeGenerator();
194
        $inputTypeGenerator = $this->getInputTypeGenerator();
195
196
        $cache = new ArrayCache();
197
198
        $mapper = new GlobTypeMapper('TheCodingMachine\GraphQL\Controllers\Fixtures\Integration\Controllers', $typeGenerator, $inputTypeGenerator, $this->getInputTypeUtils(), $container, new \TheCodingMachine\GraphQL\Controllers\AnnotationReader(new AnnotationReader()), new NamingStrategy(), $cache);
199
200
        $this->assertSame([], $mapper->getSupportedClasses());
201
    }
202
}