Passed
Pull Request — master (#63)
by David
02:05
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
        $anotherMapperSameCache = new GlobTypeMapper('TheCodingMachine\GraphQL\Controllers\Fixtures\Types', $typeGenerator, $this->getInputTypeGenerator(), $container, new \TheCodingMachine\GraphQL\Controllers\AnnotationReader(new AnnotationReader()), new NamingStrategy(), $cache);
45
        $this->assertTrue($anotherMapperSameCache->canMapClassToType(TestObject::class));
46
        $this->assertTrue($anotherMapperSameCache->canMapNameToType('Foo'));
47
48
        $this->expectException(CannotMapTypeException::class);
49
        $mapper->mapClassToType(\stdClass::class, $this->getTypeMapper());
50
    }
51
52
    public function testGlobTypeMapperDuplicateTypesException()
53
    {
54
        $container = new Picotainer([
55
            TestType::class => function() {
56
                return new TestType();
57
            }
58
        ]);
59
60
        $typeGenerator = $this->getTypeGenerator();
61
62
        $mapper = new GlobTypeMapper('TheCodingMachine\GraphQL\Controllers\Fixtures\DuplicateTypes', $typeGenerator, $this->getInputTypeGenerator(), $container, new \TheCodingMachine\GraphQL\Controllers\AnnotationReader(new AnnotationReader()), new NamingStrategy(), new NullCache());
63
64
        $this->expectException(DuplicateMappingException::class);
65
        $mapper->canMapClassToType(TestType::class);
66
    }
67
68
    public function testGlobTypeMapperDuplicateInputTypesException()
69
    {
70
        $container = new Picotainer([
71
            /*TestType::class => function() {
72
                return new TestType();
73
            }*/
74
        ]);
75
76
        $typeGenerator = $this->getTypeGenerator();
77
78
        $mapper = new GlobTypeMapper('TheCodingMachine\GraphQL\Controllers\Fixtures\DuplicateInputTypes', $typeGenerator, $this->getInputTypeGenerator(), $container, new \TheCodingMachine\GraphQL\Controllers\AnnotationReader(new AnnotationReader()), new NamingStrategy(), new NullCache());
79
80
        $this->expectException(DuplicateMappingException::class);
81
        $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\'');
82
        $mapper->canMapClassToInputType(TestObject::class);
83
    }
84
85
    public function testGlobTypeMapperClassNotFoundException()
86
    {
87
        $container = new Picotainer([
88
            TestType::class => function() {
89
                return new TestType();
90
            }
91
        ]);
92
93
        $typeGenerator = $this->getTypeGenerator();
94
95
        $mapper = new GlobTypeMapper('TheCodingMachine\GraphQL\Controllers\Fixtures\BadClassType', $typeGenerator, $this->getInputTypeGenerator(), $container, new \TheCodingMachine\GraphQL\Controllers\AnnotationReader(new AnnotationReader()), new NamingStrategy(), new NullCache());
96
97
        $this->expectException(ClassNotFoundException::class);
98
        $this->expectExceptionMessage("Could not autoload class 'Foobar' defined in @Type annotation of class 'TheCodingMachine\\GraphQL\\Controllers\\Fixtures\\BadClassType\\TestType'");
99
        $mapper->canMapClassToType(TestType::class);
100
    }
101
102
    public function testGlobTypeMapperNameNotFoundException()
103
    {
104
        $container = new Picotainer([
105
            FooType::class => function() {
106
                return new FooType();
107
            }
108
        ]);
109
110
        $typeGenerator = $this->getTypeGenerator();
111
112
        $mapper = new GlobTypeMapper('TheCodingMachine\GraphQL\Controllers\Fixtures\Types', $typeGenerator, $this->getInputTypeGenerator(), $container, new \TheCodingMachine\GraphQL\Controllers\AnnotationReader(new AnnotationReader()), new NamingStrategy(), new NullCache());
113
114
        $this->expectException(CannotMapTypeException::class);
115
        $mapper->mapNameToType('NotExists', $this->getTypeMapper());
116
    }
117
118
    public function testGlobTypeMapperInputType()
119
    {
120
        $container = new Picotainer([
121
            FooType::class => function() {
122
                return new FooType();
123
            },
124
            TestFactory::class => function() {
125
                return new TestFactory();
126
            }
127
        ]);
128
129
        $typeGenerator = $this->getTypeGenerator();
130
131
        $mapper = new GlobTypeMapper('TheCodingMachine\GraphQL\Controllers\Fixtures\Types', $typeGenerator, $this->getInputTypeGenerator(), $container, new \TheCodingMachine\GraphQL\Controllers\AnnotationReader(new AnnotationReader()), new NamingStrategy(), new NullCache());
132
133
        $this->assertTrue($mapper->canMapClassToInputType(TestObject::class));
134
135
        $inputType = $mapper->mapClassToInputType(TestObject::class, $this->getTypeMapper());
136
137
        $this->assertSame('TestObjectInput', $inputType->name);
138
139
        $this->expectException(CannotMapTypeException::class);
140
        $mapper->mapClassToInputType(TestType::class, $this->getTypeMapper());
141
    }
142
}
143