Completed
Push — master ( 7ec8b3...6c2ef1 )
by David
28s
created

StaticTypeMapperTest::testUnsupportedSubtypes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
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
14
class StaticTypeMapperTest extends AbstractQueryProviderTest
15
{
16
    /**
17
     * @var StaticTypeMapper
18
     */
19
    private $typeMapper;
20
21
    public function setUp(): void
22
    {
23
        $this->typeMapper = new StaticTypeMapper();
24
        $this->typeMapper->setTypes([
25
            TestObject::class => new ObjectType([
26
                'name'    => 'TestObject',
27
                'fields'  => [
28
                    'test'   => Type::string(),
29
                ],
30
            ])
31
        ]);
32
        $this->typeMapper->setInputTypes([
33
            TestObject::class => new InputObjectType([
34
                'name'    => 'TestInputObject',
35
                'fields'  => [
36
                    'test'   => Type::string(),
37
                ],
38
            ])
39
        ]);
40
        $this->typeMapper->setNotMappedTypes([
41
            new ObjectType([
42
                'name' => 'TestNotMappedObject',
43
                'fields'  => [
44
                    'test'   => Type::string(),
45
                ]
46
            ])
47
        ]);
48
    }
49
50
    public function testStaticTypeMapper(): void
51
    {
52
        $this->assertTrue($this->typeMapper->canMapClassToType(TestObject::class));
53
        $this->assertFalse($this->typeMapper->canMapClassToType(\Exception::class));
54
        $this->assertTrue($this->typeMapper->canMapClassToInputType(TestObject::class));
55
        $this->assertFalse($this->typeMapper->canMapClassToInputType(\Exception::class));
56
        $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

56
        $this->assertInstanceOf(ObjectType::class, $this->typeMapper->mapClassToType(TestObject::class, null, /** @scrutinizer ignore-type */ $this->getTypeMapper()));
Loading history...
57
        $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

57
        $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...
58
        $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

58
        $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...
59
        $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

59
        $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...
60
        $this->assertSame('TestInputObject', $this->typeMapper->mapNameToType('TestInputObject', $this->getTypeMapper())->name);
61
        $this->assertSame('TestNotMappedObject', $this->typeMapper->mapNameToType('TestNotMappedObject', $this->getTypeMapper())->name);
62
        $this->assertTrue($this->typeMapper->canMapNameToType('TestObject'));
63
        $this->assertTrue($this->typeMapper->canMapNameToType('TestInputObject'));
64
        $this->assertTrue($this->typeMapper->canMapNameToType('TestNotMappedObject'));
65
        $this->assertFalse($this->typeMapper->canMapNameToType('NotExists'));
66
    }
67
68
    public function testException1(): void
69
    {
70
        $this->expectException(CannotMapTypeException::class);
71
        $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

71
        $this->typeMapper->mapClassToType(\Exception::class, null, /** @scrutinizer ignore-type */ $this->getTypeMapper());
Loading history...
72
    }
73
74
    public function testException2(): void
75
    {
76
        $this->expectException(CannotMapTypeException::class);
77
        $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

77
        $this->typeMapper->mapClassToInputType(\Exception::class, /** @scrutinizer ignore-type */ $this->getTypeMapper());
Loading history...
78
    }
79
80
    public function testException3(): void
81
    {
82
        $this->expectException(CannotMapTypeException::class);
83
        $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

83
        $this->typeMapper->mapNameToType('notExists', /** @scrutinizer ignore-type */ $this->getTypeMapper());
Loading history...
84
    }
85
86
    public function testUnsupportedSubtypes(): void
87
    {
88
        $this->expectException(CannotMapTypeException::class);
89
        $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

89
        $this->typeMapper->mapClassToType(TestObject::class, new StringType(), /** @scrutinizer ignore-type */ $this->getTypeMapper());
Loading history...
90
    }
91
}
92