Passed
Push — master ( 18ff69...81bea8 )
by Adrien
02:23
created

testCanGetIdWhenReadingVariable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GraphQLTests\Doctrine\Definition;
6
7
use Doctrine\Common\Annotations\AnnotationRegistry;
8
use GraphQL\Doctrine\Definition\EntityIDType;
9
use GraphQL\Error\Error;
10
use GraphQL\Language\AST\BooleanValueNode;
11
use GraphQL\Language\AST\StringValueNode;
12
use GraphQLTests\Doctrine\Blog\Model\User;
13
use GraphQLTests\Doctrine\EntityManagerTrait;
14
15
final class EntityIDTypeTest extends \PHPUnit\Framework\TestCase
16
{
17
    use EntityManagerTrait;
18
19
    /**
20
     * @var EntityIDType
21
     */
22
    private $type;
23
24
    protected function setUp(): void
25
    {
26
        AnnotationRegistry::registerLoader('class_exists');
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\Common\Annotati...istry::registerLoader() has been deprecated: This method is deprecated and will be removed in doctrine/annotations 2.0. Annotations will be autoloaded in 2.0. ( Ignorable by Annotation )

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

26
        /** @scrutinizer ignore-deprecated */ AnnotationRegistry::registerLoader('class_exists');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
27
        $this->setUpEntityManager();
28
        $this->type = new EntityIDType($this->entityManager, User::class, 'UserID');
29
    }
30
31
    public function testMetadata(): void
32
    {
33
        self::assertSame('UserID', $this->type->name);
34
        self::assertSame('Automatically generated type to be used as input where an object of type `User` is needed', $this->type->description);
35
    }
36
37
    public function testCanGetIdWhenReadingVariable(): void
38
    {
39
        $actual = $this->type->parseValue('123')->getId();
40
        self::assertSame('123', $actual);
41
    }
42
43
    public function testWillThrowIfParsingInvalidValue(): void
44
    {
45
        $this->expectErrorMessage('EntityID cannot represent value: false');
46
        $this->type->parseValue(false);
47
    }
48
49
    public function testCanGetEntityFromRepositoryWhenReadingVariable(): void
50
    {
51
        $actual = $this->type->parseValue('123')->getEntity();
52
        self::assertInstanceOf(User::class, $actual);
53
        self::assertSame(123, $actual->getId());
54
    }
55
56
    public function testNonExistingEntityThrowErrorWhenReadingVariable(): void
57
    {
58
        $this->expectExceptionMessage('Entity not found for class `GraphQLTests\Doctrine\Blog\Model\User` and ID `non-existing-id`');
59
        $this->type->parseValue('non-existing-id')->getEntity();
60
    }
61
62
    public function testCanGetIdWhenReadingLiteral(): void
63
    {
64
        $ast = new StringValueNode(['value' => '123']);
65
        $actual = $this->type->parseLiteral($ast)->getId();
66
        self::assertSame('123', $actual);
67
    }
68
69
    public function testCanGetEntityFromRepositoryWhenReadingLiteral(): void
70
    {
71
        $ast = new StringValueNode(['value' => '123']);
72
        $actual = $this->type->parseLiteral($ast)->getEntity();
73
        self::assertInstanceOf(User::class, $actual);
74
        self::assertSame(123, $actual->getId());
75
    }
76
77
    public function testNonExistingEntityThrowErrorWhenReadingLiteral(): void
78
    {
79
        $this->expectExceptionMessage('Entity not found for class `GraphQLTests\Doctrine\Blog\Model\User` and ID `non-existing-id`');
80
        $ast = new StringValueNode(['value' => 'non-existing-id']);
81
        $this->type->parseLiteral($ast)->getEntity();
82
    }
83
84
    public function testWillThrowIfParsingInvalidLiteralValue(): void
85
    {
86
        $this->expectException(Error::class);
87
        $ast = new BooleanValueNode(['value' => false]);
88
        $this->type->parseLiteral($ast);
89
    }
90
91
    public function testCanGetIdFromEntity(): void
92
    {
93
        $user = new User(456);
94
95
        $actual = $this->type->serialize($user);
96
        self::assertSame('456', $actual);
97
    }
98
}
99