Passed
Pull Request — master (#63)
by David
01:46
created

GlobTypeMapperTest::testGlobTypeMapperException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 14
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\FooType;
15
use TheCodingMachine\GraphQL\Controllers\Fixtures\Types\TestFactory;
16
use TheCodingMachine\GraphQL\Controllers\NamingStrategy;
17
use TheCodingMachine\GraphQL\Controllers\TypeGenerator;
18
use GraphQL\Type\Definition\ObjectType;
19
20
class GlobTypeMapperTest extends AbstractQueryProviderTest
21
{
22
    public function testGlobTypeMapper()
23
    {
24
        $container = new Picotainer([
25
            FooType::class => function() {
26
                return new FooType();
27
            }
28
        ]);
29
30
        $typeGenerator = $this->getTypeGenerator();
31
        $inputTypeGenerator = $this->getInputTypeGenerator();
32
33
        $cache = new ArrayCache();
34
35
        $mapper = new GlobTypeMapper('TheCodingMachine\GraphQL\Controllers\Fixtures\Types', $typeGenerator, $inputTypeGenerator, $container, new \TheCodingMachine\GraphQL\Controllers\AnnotationReader(new AnnotationReader()), new NamingStrategy(), $cache);
36
37
        $this->assertSame([TestObject::class], $mapper->getSupportedClasses());
38
        $this->assertTrue($mapper->canMapClassToType(TestObject::class));
39
        $this->assertInstanceOf(ObjectType::class, $mapper->mapClassToType(TestObject::class, $this->getTypeMapper()));
40
        $this->assertInstanceOf(ObjectType::class, $mapper->mapNameToType('Foo', $this->getTypeMapper()));
41
        $this->assertTrue($mapper->canMapNameToType('Foo'));
42
        $this->assertFalse($mapper->canMapNameToType('NotExists'));
43
44
        // Again to test cache
45
        $anotherMapperSameCache = new GlobTypeMapper('TheCodingMachine\GraphQL\Controllers\Fixtures\Types', $typeGenerator, $this->getInputTypeGenerator(), $container, new \TheCodingMachine\GraphQL\Controllers\AnnotationReader(new AnnotationReader()), new NamingStrategy(), $cache);
46
        $this->assertTrue($anotherMapperSameCache->canMapClassToType(TestObject::class));
47
        $this->assertTrue($anotherMapperSameCache->canMapNameToType('Foo'));
48
49
        $this->expectException(CannotMapTypeException::class);
50
        $mapper->mapClassToType(\stdClass::class, $this->getTypeMapper());
51
    }
52
53
    public function testGlobTypeMapperDuplicateTypesException()
54
    {
55
        $container = new Picotainer([
56
            TestType::class => function() {
57
                return new TestType();
58
            }
59
        ]);
60
61
        $typeGenerator = $this->getTypeGenerator();
62
63
        $mapper = new GlobTypeMapper('TheCodingMachine\GraphQL\Controllers\Fixtures\DuplicateTypes', $typeGenerator, $this->getInputTypeGenerator(), $container, new \TheCodingMachine\GraphQL\Controllers\AnnotationReader(new AnnotationReader()), new NamingStrategy(), new NullCache());
64
65
        $this->expectException(DuplicateMappingException::class);
66
        $mapper->canMapClassToType(TestType::class);
67
    }
68
69
    public function testGlobTypeMapperDuplicateInputTypesException()
70
    {
71
        $container = new Picotainer([
72
            /*TestType::class => function() {
73
                return new TestType();
74
            }*/
75
        ]);
76
77
        $typeGenerator = $this->getTypeGenerator();
78
79
        $mapper = new GlobTypeMapper('TheCodingMachine\GraphQL\Controllers\Fixtures\DuplicateInputTypes', $typeGenerator, $this->getInputTypeGenerator(), $container, new \TheCodingMachine\GraphQL\Controllers\AnnotationReader(new AnnotationReader()), new NamingStrategy(), new NullCache());
80
81
        $this->expectException(DuplicateMappingException::class);
82
        $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\'');
83
        $mapper->canMapClassToInputType(TestObject::class);
84
    }
85
86
    public function testGlobTypeMapperClassNotFoundException()
87
    {
88
        $container = new Picotainer([
89
            TestType::class => function() {
90
                return new TestType();
91
            }
92
        ]);
93
94
        $typeGenerator = $this->getTypeGenerator();
95
96
        $mapper = new GlobTypeMapper('TheCodingMachine\GraphQL\Controllers\Fixtures\BadClassType', $typeGenerator, $this->getInputTypeGenerator(), $container, new \TheCodingMachine\GraphQL\Controllers\AnnotationReader(new AnnotationReader()), new NamingStrategy(), new NullCache());
97
98
        $this->expectException(ClassNotFoundException::class);
99
        $this->expectExceptionMessage("Could not autoload class 'Foobar' defined in @Type annotation of class 'TheCodingMachine\\GraphQL\\Controllers\\Fixtures\\BadClassType\\TestType'");
100
        $mapper->canMapClassToType(TestType::class);
101
    }
102
103
    public function testGlobTypeMapperNameNotFoundException()
104
    {
105
        $container = new Picotainer([
106
            FooType::class => function() {
107
                return new FooType();
108
            }
109
        ]);
110
111
        $typeGenerator = $this->getTypeGenerator();
112
113
        $mapper = new GlobTypeMapper('TheCodingMachine\GraphQL\Controllers\Fixtures\Types', $typeGenerator, $this->getInputTypeGenerator(), $container, new \TheCodingMachine\GraphQL\Controllers\AnnotationReader(new AnnotationReader()), new NamingStrategy(), new NullCache());
114
115
        $this->expectException(CannotMapTypeException::class);
116
        $mapper->mapNameToType('NotExists', $this->getTypeMapper());
117
    }
118
119
    public function testGlobTypeMapperInputType()
120
    {
121
        $container = new Picotainer([
122
            FooType::class => function() {
123
                return new FooType();
124
            },
125
            TestFactory::class => function() {
126
                return new TestFactory();
127
            }
128
        ]);
129
130
        $typeGenerator = $this->getTypeGenerator();
131
132
        $cache = new ArrayCache();
133
134
        $mapper = new GlobTypeMapper('TheCodingMachine\GraphQL\Controllers\Fixtures\Types', $typeGenerator, $this->getInputTypeGenerator(), $container, new \TheCodingMachine\GraphQL\Controllers\AnnotationReader(new AnnotationReader()), new NamingStrategy(), $cache);
135
136
        $this->assertTrue($mapper->canMapClassToInputType(TestObject::class));
137
138
        $inputType = $mapper->mapClassToInputType(TestObject::class, $this->getTypeMapper());
139
140
        $this->assertSame('TestObjectInput', $inputType->name);
141
142
        // Again to test cache
143
        $anotherMapperSameCache = new GlobTypeMapper('TheCodingMachine\GraphQL\Controllers\Fixtures\Types', $typeGenerator, $this->getInputTypeGenerator(), $container, new \TheCodingMachine\GraphQL\Controllers\AnnotationReader(new AnnotationReader()), new NamingStrategy(), $cache);
144
145
        $this->assertTrue($anotherMapperSameCache->canMapClassToInputType(TestObject::class));
146
        $this->assertSame('TestObjectInput', $anotherMapperSameCache->mapClassToInputType(TestObject::class, $this->getTypeMapper())->name);
147
148
149
        $this->expectException(CannotMapTypeException::class);
150
        $mapper->mapClassToInputType(TestType::class, $this->getTypeMapper());
151
    }
152
}
153