Completed
Pull Request — master (#60)
by David
01:49
created

StaticTypeMapperTest::testException3()   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\Type;
6
use PHPUnit\Framework\TestCase;
7
use TheCodingMachine\GraphQL\Controllers\AbstractQueryProviderTest;
8
use TheCodingMachine\GraphQL\Controllers\Mappers\CannotMapTypeException;
9
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestObject;
10
use GraphQL\Type\Definition\InputObjectType;
11
use GraphQL\Type\Definition\ObjectType;
12
13
class StaticTypeMapperTest extends AbstractQueryProviderTest
14
{
15
    /**
16
     * @var StaticTypeMapper
17
     */
18
    private $typeMapper;
19
20
    public function setUp(): void
21
    {
22
        $this->typeMapper = new StaticTypeMapper();
23
        $this->typeMapper->setTypes([
24
            TestObject::class => new ObjectType([
25
                'name'    => 'TestObject',
26
                'fields'  => [
27
                    'test'   => Type::string(),
28
                ],
29
            ])
30
        ]);
31
        $this->typeMapper->setInputTypes([
32
            TestObject::class => new InputObjectType([
33
                'name'    => 'TestInputObject',
34
                'fields'  => [
35
                    'test'   => Type::string(),
36
                ],
37
            ])
38
        ]);
39
    }
40
41
    public function testStaticTypeMapper(): void
42
    {
43
        $this->assertTrue($this->typeMapper->canMapClassToType(TestObject::class));
44
        $this->assertFalse($this->typeMapper->canMapClassToType(\Exception::class));
45
        $this->assertTrue($this->typeMapper->canMapClassToInputType(TestObject::class));
46
        $this->assertFalse($this->typeMapper->canMapClassToInputType(\Exception::class));
47
        $this->assertInstanceOf(ObjectType::class, $this->typeMapper->mapClassToType(TestObject::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...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

47
        $this->assertInstanceOf(ObjectType::class, $this->typeMapper->mapClassToType(TestObject::class, /** @scrutinizer ignore-type */ $this->getTypeMapper()));
Loading history...
48
        $this->assertInstanceOf(InputObjectType::class, $this->typeMapper->mapClassToInputType(TestObject::class));
49
        $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

49
        $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...
50
        $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

50
        $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...
51
        $this->assertSame('TestInputObject', $this->typeMapper->mapNameToType('TestInputObject', $this->getTypeMapper())->name);
52
        $this->assertTrue($this->typeMapper->canMapNameToType('TestObject'));
53
        $this->assertTrue($this->typeMapper->canMapNameToType('TestInputObject'));
54
        $this->assertFalse($this->typeMapper->canMapNameToType('NotExists'));
55
    }
56
57
    public function testException1(): void
58
    {
59
        $this->expectException(CannotMapTypeException::class);
60
        $this->typeMapper->mapClassToType(\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...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

60
        $this->typeMapper->mapClassToType(\Exception::class, /** @scrutinizer ignore-type */ $this->getTypeMapper());
Loading history...
61
    }
62
63
    public function testException2(): void
64
    {
65
        $this->expectException(CannotMapTypeException::class);
66
        $this->typeMapper->mapClassToInputType(\Exception::class);
67
    }
68
69
    public function testException3(): void
70
    {
71
        $this->expectException(CannotMapTypeException::class);
72
        $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

72
        $this->typeMapper->mapNameToType('notExists', /** @scrutinizer ignore-type */ $this->getTypeMapper());
Loading history...
73
    }
74
}
75