Completed
Pull Request — master (#60)
by David
03:41
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
Bug introduced by
The method mapNameToType() 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

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

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...
51
        $this->assertSame('TestInputObject', $this->typeMapper->mapNameToType('TestInputObject', $this->getTypeMapper())->name);
52
    }
53
54
    public function testException1(): void
55
    {
56
        $this->expectException(CannotMapTypeException::class);
57
        $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

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

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