1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace GraphQLTests\Doctrine; |
6
|
|
|
|
7
|
|
|
use DateTimeImmutable; |
8
|
|
|
use GraphQLTests\Doctrine\Blog\Model\Post; |
9
|
|
|
use GraphQLTests\Doctrine\Blog\Model\User; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
|
12
|
|
|
final class OutputTypesTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
use TypesTrait; |
15
|
|
|
|
16
|
|
|
public function testCanGetTypesWithBackslashPrefix(): void |
17
|
|
|
{ |
18
|
|
|
$type = $this->types->getOutput(Post::class); |
19
|
|
|
self::assertSame($type, $this->types->getOutput('\\' . Post::class)); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function testCanGetOutputTypes(): void |
23
|
|
|
{ |
24
|
|
|
$userType = $this->types->getOutput(User::class); |
25
|
|
|
|
26
|
|
|
$this->assertType('tests/data/UserOutput.graphqls', $userType); |
27
|
|
|
self::assertSame($userType, $this->types->getOutput(User::class), 'must returns the same instance of user type'); |
28
|
|
|
|
29
|
|
|
$postType = $this->types->getOutput(Post::class); |
30
|
|
|
$this->assertType('tests/data/PostOutput.graphqls', $postType); |
31
|
|
|
self::assertSame($postType, $this->types->getOutput(Post::class), 'must returns the same instance of post type'); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testNonPublicGetterMustBeIgnored(): void |
35
|
|
|
{ |
36
|
|
|
$actual = $this->types->getOutput(Blog\Model\Special\IgnoredGetter::class); |
37
|
|
|
$this->assertType('tests/data/IgnoredGetter.graphqls', $actual); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testCanDeclareArrayOfEntity(): void |
41
|
|
|
{ |
42
|
|
|
$actual = $this->types->getOutput(Blog\Model\Special\ArrayOfEntity::class); |
43
|
|
|
$this->assertType('tests/data/ArrayOfEntity.graphqls', $actual); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testDefaultValuesOutput(): void |
47
|
|
|
{ |
48
|
|
|
$actual = $this->types->getOutput(Blog\Model\Special\DefaultValue::class); |
49
|
|
|
$this->assertType('tests/data/DefaultValue.graphqls', $actual); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function testSelfSupportOutput(): void |
53
|
|
|
{ |
54
|
|
|
$actual = $this->types->getOutput(Blog\Model\Special\SelfSupport::class); |
55
|
|
|
$this->assertType('tests/data/SelfSupport.graphqls', $actual); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testNamespaceSupportOutput(): void |
59
|
|
|
{ |
60
|
|
|
$actual = $this->types->getOutput(Blog\Model\Special\NamespaceSupport::class); |
61
|
|
|
$this->assertType('tests/data/NamespaceSupport.graphqls', $actual); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testFieldWithoutTypeMustThrow(): void |
65
|
|
|
{ |
66
|
|
|
$this->expectExceptionMessage('Could not find type for method `GraphQLTests\Doctrine\Blog\Model\Special\NoType::getWithoutTypeHint()`. Either type hint the return value, or specify the type with `#[API\Field]` attribute.'); |
67
|
|
|
$type = $this->types->getOutput(Blog\Model\Special\NoType::class); |
68
|
|
|
$type->getFields(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testFieldReturningCollectionWithoutTypeMustThrow(): void |
72
|
|
|
{ |
73
|
|
|
$this->expectExceptionMessage('The method `GraphQLTests\Doctrine\Blog\Model\Special\NoTypeCollection::getFoos()` is type hinted with a return type of `Doctrine\Common\Collections\Collection`, but the entity contained in that collection could not be automatically detected. Either fix the type hint, fix the doctrine mapping, or specify the type with `#[API\Field]` attribute.'); |
74
|
|
|
$type = $this->types->getOutput(Blog\Model\Special\NoTypeCollection::class); |
75
|
|
|
$type->getFields(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function testCannotGetInvalidType(): void |
79
|
|
|
{ |
80
|
|
|
$this->expectExceptionMessage('Given class name `DateTimeImmutable` is not a Doctrine entity. Either register a custom GraphQL type for `DateTimeImmutable` when instantiating `GraphQL\Doctrine\Types`, or change the usage of that class to something else.'); |
81
|
|
|
$this->types->getOutput(DateTimeImmutable::class); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testArgumentWithoutTypeMustThrow(): void |
85
|
|
|
{ |
86
|
|
|
$this->expectExceptionMessage('Could not find type for parameter `$bar` for method `GraphQLTests\Doctrine\Blog\Model\Special\NoTypeArgument::getFoo()`. Either type hint the parameter, or specify the type with `#[API\Argument]` attribute.'); |
87
|
|
|
$type = $this->types->getOutput(Blog\Model\Special\NoTypeArgument::class); |
88
|
|
|
$type->getFields(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testFieldWithArrayArgumentMustThrow(): void |
92
|
|
|
{ |
93
|
|
|
$this->expectExceptionMessage('The parameter `$arg1` on method `GraphQLTests\Doctrine\Blog\Model\Special\ArrayArgument::getWithParams()` is type hinted as `array` and is not overridden via `#[API\Argument]` attribute. Either change the type hint or specify the type with `#[API\Argument]` attribute.'); |
94
|
|
|
$type = $this->types->getOutput(Blog\Model\Special\ArrayArgument::class); |
95
|
|
|
$type->getFields(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function testFieldWithObjectTypeArgumentMustThrow(): void |
99
|
|
|
{ |
100
|
|
|
$this->expectExceptionMessage('Type for parameter `$user` for method `GraphQLTests\Doctrine\Blog\Model\Special\ObjectTypeArgument::getWithParams()` must be an instance of `GraphQL\Type\Definition\InputType`, but was `GraphQL\Type\Definition\ObjectType`. Use `#[API\Argument]` attribute to specify a custom InputType.'); |
101
|
|
|
$type = $this->types->getOutput(Blog\Model\Special\ObjectTypeArgument::class); |
102
|
|
|
$type->getFields(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function testCanOverrideArgumentDefaultValue(): void |
106
|
|
|
{ |
107
|
|
|
$actual = $this->types->getOutput(Blog\Model\Special\ArgumentOverrideDefaultValue::class); |
108
|
|
|
$this->assertType('tests/data/ArgumentOverrideDefaultValue.graphqls', $actual); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|