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
|
|
|
|