Completed
Push — master ( 6cc2de...ecdd96 )
by Adrien
02:40
created

EntityIDTypeTest::testCanGetIdFromEntity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
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 GraphQL\Doctrine\Definition\EntityIDType;
8
use GraphQL\Language\AST\StringValueNode;
9
use GraphQLTests\Doctrine\Blog\Model\User;
10
use GraphQLTests\Doctrine\EntityManagerTrait;
11
12
class EntityIDTypeTest extends \PHPUnit\Framework\TestCase
13
{
14
    use EntityManagerTrait;
15
16
    /**
17
     * @var EntityIDType
18
     */
19
    private $type;
20
21
    public function setUp(): void
22
    {
23
        $this->setUpEntityManager();
24
        $this->type = new EntityIDType($this->entityManager, User::class);
25
    }
26
27
    public function testMetadata(): void
28
    {
29
        self::assertSame('UserID', $this->type->name);
30
        self::assertSame('Automatically generated type to be used as input where an object of type `User` is needed', $this->type->description);
31
    }
32
33
    public function testCanGetEntityFromRepositoryWhenReadingVariable(): void
34
    {
35
        $actual = $this->type->parseValue('123')->getEntity();
36
        self::assertInstanceOf(User::class, $actual);
37
        self::assertSame(123, $actual->getId());
38
    }
39
40
    public function testNonExistingEntityThrowErrorWhenReadingVariable(): void
41
    {
42
        $this->expectExceptionMessage('Entity not found for class `GraphQLTests\Doctrine\Blog\Model\User` and ID `non-existing-id`');
43
        $this->type->parseValue('non-existing-id')->getEntity();
44
    }
45
46
    public function testCanGetEntityFromRepositoryWhenReadingLiteral(): void
47
    {
48
        $ast = new StringValueNode(['value' => '123']);
49
        $actual = $this->type->parseLiteral($ast)->getEntity();
50
        self::assertInstanceOf(User::class, $actual);
51
        self::assertSame(123, $actual->getId());
52
    }
53
54
    public function testNonExistingEntityThrowErrorWhenReadingLiteral(): void
55
    {
56
        $this->expectExceptionMessage('Entity not found for class `GraphQLTests\Doctrine\Blog\Model\User` and ID `non-existing-id`');
57
        $ast = new StringValueNode(['value' => 'non-existing-id']);
58
        $this->type->parseLiteral($ast)->getEntity();
59
    }
60
61
    public function testCanGetIdFromEntity(): void
62
    {
63
        $user = new User(456);
64
65
        $actual = $this->type->serialize($user);
66
        self::assertSame('456', $actual);
67
    }
68
}
69