Completed
Push — master ( 6c2ef1...d70f56 )
by David
15s queued 10s
created

StaticTypeMapperTest::testException5()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
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 GraphQL\Type\Definition\StringType;
6
use GraphQL\Type\Definition\Type;
7
use PHPUnit\Framework\TestCase;
8
use TheCodingMachine\GraphQL\Controllers\AbstractQueryProviderTest;
9
use TheCodingMachine\GraphQL\Controllers\Mappers\CannotMapTypeException;
10
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestObject;
11
use GraphQL\Type\Definition\InputObjectType;
12
use GraphQL\Type\Definition\ObjectType;
13
use TheCodingMachine\GraphQL\Controllers\Types\MutableObjectType;
14
15
class StaticTypeMapperTest extends AbstractQueryProviderTest
16
{
17
    /**
18
     * @var StaticTypeMapper
19
     */
20
    private $typeMapper;
21
22
    public function setUp(): void
23
    {
24
        $this->typeMapper = new StaticTypeMapper();
25
        $this->typeMapper->setTypes([
26
            TestObject::class => new MutableObjectType([
27
                'name'    => 'TestObject',
28
                'fields'  => [
29
                    'test'   => Type::string(),
30
                ],
31
            ])
32
        ]);
33
        $this->typeMapper->setInputTypes([
34
            TestObject::class => new InputObjectType([
35
                'name'    => 'TestInputObject',
36
                'fields'  => [
37
                    'test'   => Type::string(),
38
                ],
39
            ])
40
        ]);
41
        $this->typeMapper->setNotMappedTypes([
42
            new ObjectType([
43
                'name' => 'TestNotMappedObject',
44
                'fields'  => [
45
                    'test'   => Type::string(),
46
                ]
47
            ])
48
        ]);
49
    }
50
51
    public function testStaticTypeMapper(): void
52
    {
53
        $this->assertTrue($this->typeMapper->canMapClassToType(TestObject::class));
54
        $this->assertFalse($this->typeMapper->canMapClassToType(\Exception::class));
55
        $this->assertTrue($this->typeMapper->canMapClassToInputType(TestObject::class));
56
        $this->assertFalse($this->typeMapper->canMapClassToInputType(\Exception::class));
57
        $this->assertInstanceOf(ObjectType::class, $this->typeMapper->mapClassToType(TestObject::class, null, $this->getTypeMapper()));
0 ignored issues
show
Bug introduced by
It seems like $this->getTypeMapper() can also be of type TheCodingMachine\GraphQL...appers\StaticTypeMapper; however, parameter $recursiveTypeMapper of TheCodingMachine\GraphQL...apper::mapClassToType() does only seem to accept TheCodingMachine\GraphQL...siveTypeMapperInterface, maybe add an additional type check? ( Ignorable by Annotation )

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

57
        $this->assertInstanceOf(ObjectType::class, $this->typeMapper->mapClassToType(TestObject::class, null, /** @scrutinizer ignore-type */ $this->getTypeMapper()));
Loading history...
58
        $this->assertInstanceOf(InputObjectType::class, $this->typeMapper->mapClassToInputType(TestObject::class, $this->getTypeMapper()));
0 ignored issues
show
Unused Code introduced by
The call to TheCodingMachine\GraphQL...::mapClassToInputType() has too many arguments starting with $this->getTypeMapper(). ( Ignorable by Annotation )

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

58
        $this->assertInstanceOf(InputObjectType::class, $this->typeMapper->/** @scrutinizer ignore-call */ mapClassToInputType(TestObject::class, $this->getTypeMapper()));

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
59
        $this->assertSame([TestObject::class], $this->typeMapper->getSupportedClasses());
0 ignored issues
show
Bug introduced by
The method getSupportedClasses() does not exist on TheCodingMachine\GraphQL...ers\RecursiveTypeMapper. ( Ignorable by Annotation )

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

59
        $this->assertSame([TestObject::class], $this->typeMapper->/** @scrutinizer ignore-call */ getSupportedClasses());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
60
        $this->assertSame('TestObject', $this->typeMapper->mapNameToType('TestObject', $this->getTypeMapper())->name);
0 ignored issues
show
Unused Code introduced by
The call to TheCodingMachine\GraphQL...Mapper::mapNameToType() has too many arguments starting with $this->getTypeMapper(). ( Ignorable by Annotation )

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

60
        $this->assertSame('TestObject', $this->typeMapper->/** @scrutinizer ignore-call */ mapNameToType('TestObject', $this->getTypeMapper())->name);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
61
        $this->assertSame('TestInputObject', $this->typeMapper->mapNameToType('TestInputObject', $this->getTypeMapper())->name);
62
        $this->assertSame('TestNotMappedObject', $this->typeMapper->mapNameToType('TestNotMappedObject', $this->getTypeMapper())->name);
63
        $this->assertTrue($this->typeMapper->canMapNameToType('TestObject'));
64
        $this->assertTrue($this->typeMapper->canMapNameToType('TestInputObject'));
65
        $this->assertTrue($this->typeMapper->canMapNameToType('TestNotMappedObject'));
66
        $this->assertFalse($this->typeMapper->canMapNameToType('NotExists'));
67
    }
68
69
    public function testException1(): void
70
    {
71
        $this->expectException(CannotMapTypeException::class);
72
        $this->typeMapper->mapClassToType(\Exception::class, null, $this->getTypeMapper());
0 ignored issues
show
Bug introduced by
It seems like $this->getTypeMapper() can also be of type TheCodingMachine\GraphQL...appers\StaticTypeMapper; however, parameter $recursiveTypeMapper of TheCodingMachine\GraphQL...apper::mapClassToType() does only seem to accept TheCodingMachine\GraphQL...siveTypeMapperInterface, maybe add an additional type check? ( Ignorable by Annotation )

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

72
        $this->typeMapper->mapClassToType(\Exception::class, null, /** @scrutinizer ignore-type */ $this->getTypeMapper());
Loading history...
73
    }
74
75
    public function testException2(): void
76
    {
77
        $this->expectException(CannotMapTypeException::class);
78
        $this->typeMapper->mapClassToInputType(\Exception::class, $this->getTypeMapper());
0 ignored issues
show
Bug introduced by
It seems like $this->getTypeMapper() can also be of type TheCodingMachine\GraphQL...appers\StaticTypeMapper; however, parameter $recursiveTypeMapper of TheCodingMachine\GraphQL...::mapClassToInputType() does only seem to accept TheCodingMachine\GraphQL...siveTypeMapperInterface, maybe add an additional type check? ( Ignorable by Annotation )

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

78
        $this->typeMapper->mapClassToInputType(\Exception::class, /** @scrutinizer ignore-type */ $this->getTypeMapper());
Loading history...
79
    }
80
81
    public function testException3(): void
82
    {
83
        $this->expectException(CannotMapTypeException::class);
84
        $this->typeMapper->mapNameToType('notExists', $this->getTypeMapper());
0 ignored issues
show
Bug introduced by
It seems like $this->getTypeMapper() can also be of type TheCodingMachine\GraphQL...appers\StaticTypeMapper; however, parameter $recursiveTypeMapper of TheCodingMachine\GraphQL...Mapper::mapNameToType() does only seem to accept TheCodingMachine\GraphQL...siveTypeMapperInterface, maybe add an additional type check? ( Ignorable by Annotation )

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

84
        $this->typeMapper->mapNameToType('notExists', /** @scrutinizer ignore-type */ $this->getTypeMapper());
Loading history...
85
    }
86
87
    public function testException4()
88
    {
89
        $type = new MutableObjectType(['name'=>'foo']);
90
91
        $this->expectException(CannotMapTypeExceptionInterface::class);
92
        $this->typeMapper->extendTypeForClass('foo', $type, $this->getTypeMapper());
0 ignored issues
show
Bug introduced by
It seems like $this->getTypeMapper() can also be of type TheCodingMachine\GraphQL...appers\StaticTypeMapper; however, parameter $recursiveTypeMapper of TheCodingMachine\GraphQL...r::extendTypeForClass() does only seem to accept TheCodingMachine\GraphQL...siveTypeMapperInterface, maybe add an additional type check? ( Ignorable by Annotation )

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

92
        $this->typeMapper->extendTypeForClass('foo', $type, /** @scrutinizer ignore-type */ $this->getTypeMapper());
Loading history...
93
    }
94
95
    public function testException5()
96
    {
97
        $type = new MutableObjectType(['name'=>'foo']);
98
99
        $this->expectException(CannotMapTypeExceptionInterface::class);
100
        $this->typeMapper->extendTypeForName('foo', $type, $this->getTypeMapper());
0 ignored issues
show
Bug introduced by
It seems like $this->getTypeMapper() can also be of type TheCodingMachine\GraphQL...appers\StaticTypeMapper; however, parameter $recursiveTypeMapper of TheCodingMachine\GraphQL...er::extendTypeForName() does only seem to accept TheCodingMachine\GraphQL...siveTypeMapperInterface, maybe add an additional type check? ( Ignorable by Annotation )

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

100
        $this->typeMapper->extendTypeForName('foo', $type, /** @scrutinizer ignore-type */ $this->getTypeMapper());
Loading history...
101
    }
102
103
    public function testUnsupportedSubtypes(): void
104
    {
105
        $this->expectException(CannotMapTypeException::class);
106
        $this->typeMapper->mapClassToType(TestObject::class, new StringType(), $this->getTypeMapper());
0 ignored issues
show
Bug introduced by
It seems like $this->getTypeMapper() can also be of type TheCodingMachine\GraphQL...appers\StaticTypeMapper; however, parameter $recursiveTypeMapper of TheCodingMachine\GraphQL...apper::mapClassToType() does only seem to accept TheCodingMachine\GraphQL...siveTypeMapperInterface, maybe add an additional type check? ( Ignorable by Annotation )

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

106
        $this->typeMapper->mapClassToType(TestObject::class, new StringType(), /** @scrutinizer ignore-type */ $this->getTypeMapper());
Loading history...
107
    }
108
}
109