Completed
Push — master ( 5c6cf0...c4384e )
by Adrien
03:09
created

EntityIDTypeTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
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\Language\AST\StringValueNode;
10
use GraphQLTests\Doctrine\Blog\Model\User;
11
use GraphQLTests\Doctrine\EntityManagerTrait;
12
13
final class EntityIDTypeTest extends \PHPUnit\Framework\TestCase
14
{
15
    use EntityManagerTrait;
16
17
    /**
18
     * @var EntityIDType
19
     */
20
    private $type;
21
22
    public function setUp(): void
23
    {
24
        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 autoloading should be deferred to the globally registered autoloader by then. For now, use @example AnnotationRegistry::registerLoader('class_exists') ( Ignorable by Annotation )

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

24
        /** @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...
25
        $this->setUpEntityManager();
26
        $this->type = new EntityIDType($this->entityManager, User::class, 'UserID');
27
    }
28
29
    public function testMetadata(): void
30
    {
31
        self::assertSame('UserID', $this->type->name);
32
        self::assertSame('Automatically generated type to be used as input where an object of type `User` is needed', $this->type->description);
33
    }
34
35
    public function testCanGetIdWhenReadingVariable(): void
36
    {
37
        $actual = $this->type->parseValue('123')->getId();
38
        self::assertSame('123', $actual);
39
    }
40
41
    public function testCanGetEntityFromRepositoryWhenReadingVariable(): void
42
    {
43
        $actual = $this->type->parseValue('123')->getEntity();
44
        self::assertInstanceOf(User::class, $actual);
45
        self::assertSame(123, $actual->getId());
46
    }
47
48
    public function testNonExistingEntityThrowErrorWhenReadingVariable(): void
49
    {
50
        $this->expectExceptionMessage('Entity not found for class `GraphQLTests\Doctrine\Blog\Model\User` and ID `non-existing-id`');
51
        $this->type->parseValue('non-existing-id')->getEntity();
52
    }
53
54
    public function testCanGetIdWhenReadingLiteral(): void
55
    {
56
        $ast = new StringValueNode(['value' => '123']);
57
        $actual = $this->type->parseLiteral($ast)->getId();
58
        self::assertSame('123', $actual);
59
    }
60
61
    public function testCanGetEntityFromRepositoryWhenReadingLiteral(): void
62
    {
63
        $ast = new StringValueNode(['value' => '123']);
64
        $actual = $this->type->parseLiteral($ast)->getEntity();
65
        self::assertInstanceOf(User::class, $actual);
66
        self::assertSame(123, $actual->getId());
67
    }
68
69
    public function testNonExistingEntityThrowErrorWhenReadingLiteral(): void
70
    {
71
        $this->expectExceptionMessage('Entity not found for class `GraphQLTests\Doctrine\Blog\Model\User` and ID `non-existing-id`');
72
        $ast = new StringValueNode(['value' => 'non-existing-id']);
73
        $this->type->parseLiteral($ast)->getEntity();
74
    }
75
76
    public function testCanGetIdFromEntity(): void
77
    {
78
        $user = new User(456);
79
80
        $actual = $this->type->serialize($user);
81
        self::assertSame('456', $actual);
82
    }
83
}
84