EntityIDTypeTest   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 11
eloc 34
c 5
b 0
f 0
dl 0
loc 82
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A testCanGetEntityFromRepositoryWhenReadingVariable() 0 5 1
A testWillThrowIfParsingInvalidValue() 0 4 1
A testCanGetIdWhenReadingVariable() 0 4 1
A testCanGetIdWhenReadingLiteral() 0 5 1
A testCanGetEntityFromRepositoryWhenReadingLiteral() 0 6 1
A testWillThrowIfParsingInvalidLiteralValue() 0 6 1
A testNonExistingEntityThrowErrorWhenReadingLiteral() 0 8 1
A setUp() 0 4 1
A testNonExistingEntityThrowErrorWhenReadingVariable() 0 4 1
A testMetadata() 0 4 1
A testCanGetIdFromEntity() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GraphQLTests\Doctrine\Definition;
6
7
use GraphQL\Doctrine\Definition\EntityIDType;
8
use GraphQL\Error\Error;
9
use GraphQL\Error\UserError;
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
use PHPUnit\Framework\TestCase;
15
16
final class EntityIDTypeTest extends TestCase
17
{
18
    use EntityManagerTrait;
19
20
    private EntityIDType $type;
21
22
    protected function setUp(): void
23
    {
24
        $this->setUpEntityManager();
25
        $this->type = new EntityIDType($this->entityManager, User::class, 'UserID');
26
    }
27
28
    public function testMetadata(): void
29
    {
30
        self::assertSame('UserID', $this->type->name);
31
        self::assertSame('Automatically generated type to be used as input where an object of type `User` is needed', $this->type->description);
32
    }
33
34
    public function testCanGetIdWhenReadingVariable(): void
35
    {
36
        $actual = $this->type->parseValue('123')->getId();
37
        self::assertSame('123', $actual);
38
    }
39
40
    public function testWillThrowIfParsingInvalidValue(): void
41
    {
42
        $this->expectExceptionMessage('EntityID cannot represent value: false');
43
        $this->type->parseValue(false);
44
    }
45
46
    public function testCanGetEntityFromRepositoryWhenReadingVariable(): void
47
    {
48
        $actual = $this->type->parseValue('123')->getEntity();
49
        self::assertInstanceOf(User::class, $actual);
50
        self::assertSame(123, $actual->getId());
51
    }
52
53
    public function testNonExistingEntityThrowErrorWhenReadingVariable(): void
54
    {
55
        $this->expectExceptionMessage('Entity not found for class `GraphQLTests\Doctrine\Blog\Model\User` and ID `non-existing-id`');
56
        $this->type->parseValue('non-existing-id')->getEntity();
57
    }
58
59
    public function testCanGetIdWhenReadingLiteral(): void
60
    {
61
        $ast = new StringValueNode(['value' => '123']);
62
        $actual = $this->type->parseLiteral($ast)->getId();
63
        self::assertSame('123', $actual);
64
    }
65
66
    public function testCanGetEntityFromRepositoryWhenReadingLiteral(): void
67
    {
68
        $ast = new StringValueNode(['value' => '123']);
69
        $actual = $this->type->parseLiteral($ast)->getEntity();
70
        self::assertInstanceOf(User::class, $actual);
71
        self::assertSame(123, $actual->getId());
72
    }
73
74
    public function testNonExistingEntityThrowErrorWhenReadingLiteral(): void
75
    {
76
        $ast = new StringValueNode(['value' => 'non-existing-id']);
77
        $value = $this->type->parseLiteral($ast);
78
79
        $this->expectException(UserError::class);
80
        $this->expectExceptionMessage('Entity not found for class `GraphQLTests\Doctrine\Blog\Model\User` and ID `non-existing-id`');
81
        $value->getEntity();
82
    }
83
84
    public function testWillThrowIfParsingInvalidLiteralValue(): void
85
    {
86
        $ast = new BooleanValueNode(['value' => false]);
87
88
        $this->expectException(Error::class);
89
        $this->type->parseLiteral($ast);
90
    }
91
92
    public function testCanGetIdFromEntity(): void
93
    {
94
        $user = new User(456);
95
96
        $actual = $this->type->serialize($user);
97
        self::assertSame('456', $actual);
98
    }
99
}
100