Completed
Push — master ( 1e6eb5...15e5c5 )
by David
18s queued 11s
created

RecursiveTypeMapperTest::testMapClassToType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 20
rs 9.8666
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 GraphQL\Type\Definition\InputObjectType;
7
use GraphQL\Type\Definition\InterfaceType;
8
use GraphQL\Type\Definition\ObjectType;
9
use Mouf\Picotainer\Picotainer;
10
use Symfony\Component\Cache\Simple\NullCache;
11
use TheCodingMachine\GraphQL\Controllers\AbstractQueryProviderTest;
12
use TheCodingMachine\GraphQL\Controllers\Fixtures\Interfaces\ClassA;
13
use TheCodingMachine\GraphQL\Controllers\Fixtures\Interfaces\ClassB;
14
use TheCodingMachine\GraphQL\Controllers\Fixtures\Interfaces\ClassC;
15
use TheCodingMachine\GraphQL\Controllers\Fixtures\Interfaces\Types\ClassAType;
16
use TheCodingMachine\GraphQL\Controllers\Fixtures\Interfaces\Types\ClassBType;
17
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestObject;
18
use TheCodingMachine\GraphQL\Controllers\TypeGenerator;
19
20
class RecursiveTypeMapperTest extends AbstractQueryProviderTest
21
{
22
23
    public function testMapClassToType()
24
    {
25
        $objectType = new ObjectType([
26
            'name' => 'Foobar'
27
        ]);
28
29
        $typeMapper = new StaticTypeMapper();
30
        $typeMapper->setTypes([
31
            ClassB::class => $objectType
32
        ]);
33
34
        $recursiveTypeMapper = new RecursiveTypeMapper($typeMapper);
35
36
        $this->assertFalse($typeMapper->canMapClassToType(ClassC::class));
37
        $this->assertTrue($recursiveTypeMapper->canMapClassToType(ClassC::class));
38
        $this->assertSame($objectType, $recursiveTypeMapper->mapClassToType(ClassC::class));
39
40
        $this->assertFalse($recursiveTypeMapper->canMapClassToType(ClassA::class));
41
        $this->expectException(CannotMapTypeException::class);
42
        $recursiveTypeMapper->mapClassToType(ClassA::class);
43
    }
44
45
    public function testMapClassToInputType()
46
    {
47
        $inputObjectType = new InputObjectType([
48
            'name' => 'Foobar'
49
        ]);
50
51
        $typeMapper = new StaticTypeMapper();
52
        $typeMapper->setInputTypes([
53
            ClassB::class => $inputObjectType
54
        ]);
55
56
        $recursiveTypeMapper = new RecursiveTypeMapper($typeMapper);
57
58
        $this->assertFalse($recursiveTypeMapper->canMapClassToInputType(ClassC::class));
59
60
        $this->expectException(CannotMapTypeException::class);
61
        $recursiveTypeMapper->mapClassToInputType(ClassC::class);
62
    }
63
64
    public function testMapClassToInterfaceOrType()
65
    {
66
        $container = new Picotainer([
67
            ClassAType::class => function() {
68
                return new ClassAType();
69
            },
70
            ClassBType::class => function() {
71
                return new ClassBType();
72
            }
73
        ]);
74
75
        $typeGenerator = new TypeGenerator($this->getRegistry());
76
77
        $mapper = new GlobTypeMapper('TheCodingMachine\GraphQL\Controllers\Fixtures\Interfaces\Types', $typeGenerator, $container, new \TheCodingMachine\GraphQL\Controllers\AnnotationReader(new AnnotationReader()), new NullCache());
78
79
        $recursiveMapper = new RecursiveTypeMapper($mapper);
80
81
        $type = $recursiveMapper->mapClassToInterfaceOrType(ClassA::class);
82
        $this->assertInstanceOf(InterfaceType::class, $type);
83
        $this->assertSame('ClassAInterface', $type->name);
0 ignored issues
show
Bug introduced by
Accessing name on the interface GraphQL\Type\Definition\OutputType suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
84
85
        $type = $recursiveMapper->mapClassToInterfaceOrType(ClassC::class);
86
        $this->assertInstanceOf(ObjectType::class, $type);
87
        $this->assertSame('ClassB', $type->name);
88
89
        $this->expectException(CannotMapTypeException::class);
90
        $recursiveMapper->mapClassToInterfaceOrType('Not exists');
91
    }
92
}
93