Passed
Pull Request — master (#44)
by David
02:04
created

testGlobTypeMapperClassNotFoundException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 15
c 0
b 0
f 0
rs 10
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\NullCache;
8
use TheCodingMachine\GraphQL\Controllers\AbstractQueryProviderTest;
9
use TheCodingMachine\GraphQL\Controllers\Annotations\Exceptions\ClassNotFoundException;
10
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestObject;
11
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestType;
12
use TheCodingMachine\GraphQL\Controllers\Fixtures\Types\FooType;
13
use TheCodingMachine\GraphQL\Controllers\TypeGenerator;
14
use GraphQL\Type\Definition\ObjectType;
15
16
class GlobTypeMapperTest extends AbstractQueryProviderTest
17
{
18
    public function testGlobTypeMapper()
19
    {
20
        $container = new Picotainer([
21
            FooType::class => function() {
22
                return new FooType();
23
            }
24
        ]);
25
26
        $typeGenerator = new TypeGenerator($this->getRegistry());
27
28
        $mapper = new GlobTypeMapper('TheCodingMachine\GraphQL\Controllers\Fixtures\Types', $typeGenerator, $container, new \TheCodingMachine\GraphQL\Controllers\AnnotationReader(new AnnotationReader()), new NullCache());
29
30
        $this->assertTrue($mapper->canMapClassToType(TestObject::class));
31
        $this->assertInstanceOf(ObjectType::class, $mapper->mapClassToType(TestObject::class));
32
        $this->assertSame([TestObject::class], $mapper->getSupportedClasses());
33
34
        $this->expectException(CannotMapTypeException::class);
35
        $mapper->mapClassToType(\stdClass::class);
36
    }
37
38
    public function testGlobTypeMapperException()
39
    {
40
        $container = new Picotainer([
41
            TestType::class => function() {
42
                return new TestType();
43
            }
44
        ]);
45
46
        $typeGenerator = new TypeGenerator($this->getRegistry());
47
48
        $mapper = new GlobTypeMapper('TheCodingMachine\GraphQL\Controllers\Fixtures\DuplicateTypes', $typeGenerator, $container, new \TheCodingMachine\GraphQL\Controllers\AnnotationReader(new AnnotationReader()), new NullCache());
49
50
        $this->expectException(DuplicateMappingException::class);
51
        $mapper->canMapClassToType(TestType::class);
52
    }
53
54
    public function testGlobTypeMapperClassNotFoundException()
55
    {
56
        $container = new Picotainer([
57
            TestType::class => function() {
58
                return new TestType();
59
            }
60
        ]);
61
62
        $typeGenerator = new TypeGenerator($this->getRegistry());
63
64
        $mapper = new GlobTypeMapper('TheCodingMachine\GraphQL\Controllers\Fixtures\BadClassType', $typeGenerator, $container, new \TheCodingMachine\GraphQL\Controllers\AnnotationReader(new AnnotationReader()), new NullCache());
65
66
        $this->expectException(ClassNotFoundException::class);
67
        $this->expectExceptionMessage("Could not autoload class 'Foobar' defined in @Type annotation of class 'TheCodingMachine\\GraphQL\\Controllers\\Fixtures\\BadClassType\\TestType'");
68
        $mapper->canMapClassToType(TestType::class);
69
    }
70
71
    public function testGlobTypeMapperInputType()
72
    {
73
        $container = new Picotainer([
74
            FooType::class => function() {
75
                return new FooType();
76
            }
77
        ]);
78
79
        $typeGenerator = new TypeGenerator($this->getRegistry());
80
81
        $mapper = new GlobTypeMapper('TheCodingMachine\GraphQL\Controllers\Fixtures\Types', $typeGenerator, $container, new \TheCodingMachine\GraphQL\Controllers\AnnotationReader(new AnnotationReader()), new NullCache());
82
83
        $this->assertFalse($mapper->canMapClassToInputType(TestObject::class));
84
85
        $this->expectException(CannotMapTypeException::class);
86
        $mapper->mapClassToInputType(TestType::class);
87
    }
88
}
89