1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace GraphQLTests\Doctrine; |
6
|
|
|
|
7
|
|
|
use GraphQLTests\Doctrine\Blog\Model\Post; |
8
|
|
|
use GraphQLTests\Doctrine\Blog\Model\User; |
9
|
|
|
|
10
|
|
|
final class OutputTypesTest extends \PHPUnit\Framework\TestCase |
11
|
|
|
{ |
12
|
|
|
use TypesTrait; |
13
|
|
|
|
14
|
|
|
public function testCanGetTypesWithBackslashPrefix(): void |
15
|
|
|
{ |
16
|
|
|
$type = $this->types->getOutput(Post::class); |
17
|
|
|
self::assertSame($type, $this->types->getOutput('\\' . Post::class)); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function testCanGetOutputTypes(): void |
21
|
|
|
{ |
22
|
|
|
$userType = $this->types->getOutput(User::class); |
23
|
|
|
|
24
|
|
|
$this->assertType('tests/data/UserOutput.graphqls', $userType); |
25
|
|
|
self::assertSame($userType, $this->types->getOutput(User::class), 'must returns the same instance of user type'); |
26
|
|
|
|
27
|
|
|
$postType = $this->types->getOutput(Post::class); |
28
|
|
|
$this->assertType('tests/data/PostOutput.graphqls', $postType); |
29
|
|
|
self::assertSame($postType, $this->types->getOutput(Post::class), 'must returns the same instance of post type'); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function testNonPublicGetterMustBeIgnored(): void |
33
|
|
|
{ |
34
|
|
|
$actual = $this->types->getOutput(Blog\Model\Special\IgnoredGetter::class); |
35
|
|
|
$this->assertType('tests/data/IgnoredGetter.graphqls', $actual); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testCanDeclareArrayOfEntity(): void |
39
|
|
|
{ |
40
|
|
|
$actual = $this->types->getOutput(Blog\Model\Special\ArrayOfEntity::class); |
41
|
|
|
$this->assertType('tests/data/ArrayOfEntity.graphqls', $actual); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function testDefaultValuesOutput(): void |
45
|
|
|
{ |
46
|
|
|
$actual = $this->types->getOutput(Blog\Model\Special\DefaultValue::class); |
47
|
|
|
$this->assertType('tests/data/DefaultValue.graphqls', $actual); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testFieldWithoutTypeMustThrow(): void |
51
|
|
|
{ |
52
|
|
|
$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` annotation.'); |
53
|
|
|
$type = $this->types->getOutput(Blog\Model\Special\NoType::class); |
54
|
|
|
$type->getFields(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testFieldReturningCollectionWithoutTypeMustThrow(): void |
58
|
|
|
{ |
59
|
|
|
$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` annotation.'); |
60
|
|
|
$type = $this->types->getOutput(Blog\Model\Special\NoTypeCollection::class); |
61
|
|
|
$type->getFields(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testCannotGetInvalidType(): void |
65
|
|
|
{ |
66
|
|
|
$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.'); |
67
|
|
|
$this->types->getOutput(\DateTimeImmutable::class); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testArgumentWithoutTypeMustThrow(): void |
71
|
|
|
{ |
72
|
|
|
$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` annotation.'); |
73
|
|
|
$type = $this->types->getOutput(Blog\Model\Special\NoTypeArgument::class); |
74
|
|
|
$type->getFields(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testFieldWithExtraArgumentMustThrow(): void |
78
|
|
|
{ |
79
|
|
|
$this->expectExceptionMessage('The following arguments were declared via `@API\Argument` annotation but do not match actual parameter names on method `GraphQLTests\Doctrine\Blog\Model\Special\ExtraArgument::getWithParams()`. Either rename or remove the annotations: misspelled_name'); |
80
|
|
|
$type = $this->types->getOutput(Blog\Model\Special\ExtraArgument::class); |
81
|
|
|
$type->getFields(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testFieldWithArrayArgumentMustThrow(): void |
85
|
|
|
{ |
86
|
|
|
$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` annotation. Either change the type hint or specify the type with `@API\Argument` annotation.'); |
87
|
|
|
$type = $this->types->getOutput(Blog\Model\Special\ArrayArgument::class); |
88
|
|
|
$type->getFields(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testFieldWithObjectTypeArgumentMustThrow(): void |
92
|
|
|
{ |
93
|
|
|
$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` annotation to specify a custom InputType.'); |
94
|
|
|
$type = $this->types->getOutput(Blog\Model\Special\ObjectTypeArgument::class); |
95
|
|
|
$type->getFields(); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|