Completed
Push — master ( 64f0fa...cc6449 )
by David
13s queued 11s
created

testGlobTypeMapperNameNotFoundException()   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 TheCodingMachine\GraphQL\Controllers\AbstractQueryProviderTest;
10
use TheCodingMachine\GraphQL\Controllers\Annotations\Exceptions\ClassNotFoundException;
11
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestObject;
12
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestType;
13
use TheCodingMachine\GraphQL\Controllers\Fixtures\Types\FooType;
14
use TheCodingMachine\GraphQL\Controllers\TypeGenerator;
15
use GraphQL\Type\Definition\ObjectType;
16
17
class GlobTypeMapperTest extends AbstractQueryProviderTest
18
{
19
    public function testGlobTypeMapper()
20
    {
21
        $container = new Picotainer([
22
            FooType::class => function() {
23
                return new FooType();
24
            }
25
        ]);
26
27
        $typeGenerator = $this->getTypeGenerator();
28
29
        $cache = new ArrayCache();
30
31
        $mapper = new GlobTypeMapper('TheCodingMachine\GraphQL\Controllers\Fixtures\Types', $typeGenerator, $container, new \TheCodingMachine\GraphQL\Controllers\AnnotationReader(new AnnotationReader()), $cache);
32
33
        $this->assertSame([TestObject::class], $mapper->getSupportedClasses());
34
        $this->assertTrue($mapper->canMapClassToType(TestObject::class));
35
        $this->assertInstanceOf(ObjectType::class, $mapper->mapClassToType(TestObject::class, $this->getTypeMapper()));
36
        $this->assertInstanceOf(ObjectType::class, $mapper->mapNameToType('Foo', $this->getTypeMapper()));
37
        $this->assertTrue($mapper->canMapNameToType('Foo'));
38
        $this->assertFalse($mapper->canMapNameToType('NotExists'));
39
40
        $anotherMapperSameCache = new GlobTypeMapper('TheCodingMachine\GraphQL\Controllers\Fixtures\Types', $typeGenerator, $container, new \TheCodingMachine\GraphQL\Controllers\AnnotationReader(new AnnotationReader()), $cache);
41
        $this->assertTrue($anotherMapperSameCache->canMapClassToType(TestObject::class));
42
        $this->assertTrue($anotherMapperSameCache->canMapNameToType('Foo'));
43
44
        $this->expectException(CannotMapTypeException::class);
45
        $mapper->mapClassToType(\stdClass::class, $this->getTypeMapper());
46
    }
47
48
    public function testGlobTypeMapperException()
49
    {
50
        $container = new Picotainer([
51
            TestType::class => function() {
52
                return new TestType();
53
            }
54
        ]);
55
56
        $typeGenerator = $this->getTypeGenerator();
57
58
        $mapper = new GlobTypeMapper('TheCodingMachine\GraphQL\Controllers\Fixtures\DuplicateTypes', $typeGenerator, $container, new \TheCodingMachine\GraphQL\Controllers\AnnotationReader(new AnnotationReader()), new NullCache());
59
60
        $this->expectException(DuplicateMappingException::class);
61
        $mapper->canMapClassToType(TestType::class);
62
    }
63
64
    public function testGlobTypeMapperClassNotFoundException()
65
    {
66
        $container = new Picotainer([
67
            TestType::class => function() {
68
                return new TestType();
69
            }
70
        ]);
71
72
        $typeGenerator = $this->getTypeGenerator();
73
74
        $mapper = new GlobTypeMapper('TheCodingMachine\GraphQL\Controllers\Fixtures\BadClassType', $typeGenerator, $container, new \TheCodingMachine\GraphQL\Controllers\AnnotationReader(new AnnotationReader()), new NullCache());
75
76
        $this->expectException(ClassNotFoundException::class);
77
        $this->expectExceptionMessage("Could not autoload class 'Foobar' defined in @Type annotation of class 'TheCodingMachine\\GraphQL\\Controllers\\Fixtures\\BadClassType\\TestType'");
78
        $mapper->canMapClassToType(TestType::class);
79
    }
80
81
    public function testGlobTypeMapperNameNotFoundException()
82
    {
83
        $container = new Picotainer([
84
            FooType::class => function() {
85
                return new FooType();
86
            }
87
        ]);
88
89
        $typeGenerator = $this->getTypeGenerator();
90
91
        $mapper = new GlobTypeMapper('TheCodingMachine\GraphQL\Controllers\Fixtures\Types', $typeGenerator, $container, new \TheCodingMachine\GraphQL\Controllers\AnnotationReader(new AnnotationReader()), new NullCache());
92
93
        $this->expectException(CannotMapTypeException::class);
94
        $mapper->mapNameToType('NotExists', $this->getTypeMapper());
95
    }
96
97
    public function testGlobTypeMapperInputType()
98
    {
99
        $container = new Picotainer([
100
            FooType::class => function() {
101
                return new FooType();
102
            }
103
        ]);
104
105
        $typeGenerator = $this->getTypeGenerator();
106
107
        $mapper = new GlobTypeMapper('TheCodingMachine\GraphQL\Controllers\Fixtures\Types', $typeGenerator, $container, new \TheCodingMachine\GraphQL\Controllers\AnnotationReader(new AnnotationReader()), new NullCache());
108
109
        $this->assertFalse($mapper->canMapClassToInputType(TestObject::class));
110
111
        $this->expectException(CannotMapTypeException::class);
112
        $mapper->mapClassToInputType(TestType::class);
113
    }
114
}
115