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

CompositeTypeMapperTest.php$0 ➔ mapNameToType()   A

Complexity

Conditions 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 2
1
<?php
2
3
namespace TheCodingMachine\GraphQL\Controllers\Mappers;
4
5
use GraphQL\Type\Definition\InputType;
6
use GraphQL\Type\Definition\OutputType;
7
use GraphQL\Type\Definition\Type;
8
use PHPUnit\Framework\TestCase;
9
use TheCodingMachine\GraphQL\Controllers\AbstractQueryProviderTest;
10
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestObject;
11
use TheCodingMachine\GraphQL\Controllers\TypeMappingException;
12
use GraphQL\Type\Definition\InputObjectType;
13
use GraphQL\Type\Definition\ObjectType;
14
15
class CompositeTypeMapperTest extends AbstractQueryProviderTest
16
{
17
    /**
18
     * @var CompositeTypeMapper
19
     */
20
    protected $composite;
21
22
    public function setUp()
23
    {
24
        $typeMapper1 = new class() implements TypeMapperInterface {
25
            public function mapClassToType(string $className, RecursiveTypeMapperInterface $recursiveTypeMapper): ObjectType
26
            {
27
                if ($className === TestObject::class) {
28
                    return new ObjectType([
29
                        'name'    => 'TestObject',
30
                        'fields'  => [
31
                            'test'   => Type::string(),
32
                        ],
33
                    ]);
34
                } else {
35
                    throw TypeMappingException::createFromType(TestObject::class);
0 ignored issues
show
Bug introduced by
TheCodingMachine\GraphQL...tures\TestObject::class of type string is incompatible with the type phpDocumentor\Reflection\Type expected by parameter $type of TheCodingMachine\GraphQL...ption::createFromType(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

35
                    throw TypeMappingException::createFromType(/** @scrutinizer ignore-type */ TestObject::class);
Loading history...
36
                }
37
            }
38
39
            public function mapClassToInputType(string $className): InputType
40
            {
41
                if ($className === TestObject::class) {
42
                    return new InputObjectType([
43
                        'name'    => 'TestObject',
44
                        'fields'  => [
45
                            'test'   => Type::string(),
46
                        ],
47
                    ]);
48
                } else {
49
                    throw TypeMappingException::createFromType(TestObject::class);
0 ignored issues
show
Bug introduced by
TheCodingMachine\GraphQL...tures\TestObject::class of type string is incompatible with the type phpDocumentor\Reflection\Type expected by parameter $type of TheCodingMachine\GraphQL...ption::createFromType(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

49
                    throw TypeMappingException::createFromType(/** @scrutinizer ignore-type */ TestObject::class);
Loading history...
50
                }
51
            }
52
53
            public function canMapClassToType(string $className): bool
54
            {
55
                return $className === TestObject::class;
56
            }
57
58
            public function canMapClassToInputType(string $className): bool
59
            {
60
                return $className === TestObject::class;
61
            }
62
63
            /**
64
             * Returns the list of classes that have matching input GraphQL types.
65
             *
66
             * @return string[]
67
             */
68
            public function getSupportedClasses(): array
69
            {
70
                return [TestObject::class];
71
            }
72
73
            /**
74
             * Returns a GraphQL type by name (can be either an input or output type)
75
             *
76
             * @param string $typeName The name of the GraphQL type
77
             * @param RecursiveTypeMapperInterface $recursiveTypeMapper
78
             * @return Type&(InputType|OutputType)
79
             */
80
            public function mapNameToType(string $typeName, RecursiveTypeMapperInterface $recursiveTypeMapper): Type
81
            {
82
                switch ($typeName) {
83
                    case 'TestObject':
84
                        return new ObjectType([
85
                            'name'    => 'TestObject',
86
                            'fields'  => [
87
                                'test'   => Type::string(),
88
                            ],
89
                        ]);
90
                    default:
91
                        throw CannotMapTypeException::createForName($typeName);
92
                }
93
            }
94
95
            /**
96
             * Returns true if this type mapper can map the $typeName GraphQL name to a GraphQL type.
97
             *
98
             * @param string $typeName The name of the GraphQL type
99
             * @return bool
100
             */
101
            public function canMapNameToType(string $typeName): bool
102
            {
103
                return $typeName === 'TestObject';
104
            }
105
        };
106
107
        $this->composite = new CompositeTypeMapper([$typeMapper1]);
108
    }
109
110
111
    public function testComposite(): void
112
    {
113
        $this->assertTrue($this->composite->canMapClassToType(TestObject::class));
114
        $this->assertFalse($this->composite->canMapClassToType(\Exception::class));
115
        $this->assertTrue($this->composite->canMapClassToInputType(TestObject::class));
116
        $this->assertFalse($this->composite->canMapClassToInputType(\Exception::class));
117
        $this->assertInstanceOf(ObjectType::class, $this->composite->mapClassToType(TestObject::class, $this->getTypeMapper()));
118
        $this->assertInstanceOf(InputObjectType::class, $this->composite->mapClassToInputType(TestObject::class));
119
        $this->assertSame([TestObject::class], $this->composite->getSupportedClasses());
120
        $this->assertInstanceOf(ObjectType::class, $this->composite->mapNameToType('TestObject', $this->getTypeMapper()));
121
        $this->assertTrue($this->composite->canMapNameToType('TestObject'));
122
        $this->assertFalse($this->composite->canMapNameToType('NotExists'));
123
    }
124
125
    public function testException1(): void
126
    {
127
        $this->expectException(CannotMapTypeException::class);
128
        $this->composite->mapClassToType(\Exception::class, $this->getTypeMapper());
129
    }
130
131
    public function testException2(): void
132
    {
133
        $this->expectException(CannotMapTypeException::class);
134
        $this->composite->mapClassToInputType(\Exception::class);
135
    }
136
137
    public function testException3(): void
138
    {
139
        $this->expectException(CannotMapTypeException::class);
140
        $this->composite->mapNameToType('NotExists', $this->getTypeMapper());
141
    }
142
}
143