1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TheCodingMachine\GraphQL\Controllers; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestController; |
8
|
|
|
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestObject; |
9
|
|
|
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestType; |
10
|
|
|
use TheCodingMachine\GraphQL\Controllers\Security\VoidAuthenticationService; |
11
|
|
|
use TheCodingMachine\GraphQL\Controllers\Security\VoidAuthorizationService; |
12
|
|
|
use Youshido\GraphQL\Execution\ResolveInfo; |
13
|
|
|
use Youshido\GraphQL\Type\InputObject\InputObjectType; |
14
|
|
|
use Youshido\GraphQL\Type\ListType\ListType; |
15
|
|
|
use Youshido\GraphQL\Type\NonNullType; |
16
|
|
|
use Youshido\GraphQL\Type\Object\ObjectType; |
17
|
|
|
use Youshido\GraphQL\Type\Scalar\BooleanType; |
18
|
|
|
use Youshido\GraphQL\Type\Scalar\DateTimeType; |
19
|
|
|
use Youshido\GraphQL\Type\Scalar\FloatType; |
20
|
|
|
use Youshido\GraphQL\Type\Scalar\IntType; |
21
|
|
|
use Youshido\GraphQL\Type\Scalar\StringType; |
22
|
|
|
use Youshido\GraphQL\Type\TypeInterface; |
23
|
|
|
use TheCodingMachine\GraphQL\Controllers\Annotations\Query; |
24
|
|
|
|
25
|
|
|
class ControllerQueryProviderTest extends AbstractQueryProviderTest |
26
|
|
|
{ |
27
|
|
|
public function testQueryProvider() |
28
|
|
|
{ |
29
|
|
|
$controller = new TestController(); |
30
|
|
|
|
31
|
|
|
$queryProvider = new ControllerQueryProvider($controller, $this->getRegistry()); |
32
|
|
|
|
33
|
|
|
$queries = $queryProvider->getQueries(); |
34
|
|
|
|
35
|
|
|
$this->assertCount(3, $queries); |
36
|
|
|
$usersQuery = $queries[0]; |
37
|
|
|
$this->assertSame('test', $usersQuery->getName()); |
38
|
|
|
|
39
|
|
|
$this->assertCount(8, $usersQuery->getArguments()); |
40
|
|
|
$this->assertInstanceOf(NonNullType::class, $usersQuery->getArgument('int')->getType()); |
41
|
|
|
$this->assertInstanceOf(IntType::class, $usersQuery->getArgument('int')->getType()->getTypeOf()); |
42
|
|
|
$this->assertInstanceOf(StringType::class, $usersQuery->getArgument('string')->getType()); |
43
|
|
|
$this->assertInstanceOf(NonNullType::class, $usersQuery->getArgument('list')->getType()); |
44
|
|
|
$this->assertInstanceOf(ListType::class, $usersQuery->getArgument('list')->getType()->getTypeOf()); |
45
|
|
|
$this->assertInstanceOf(NonNullType::class, $usersQuery->getArgument('list')->getType()->getTypeOf()->getItemType()); |
46
|
|
|
$this->assertInstanceOf(InputObjectType::class, $usersQuery->getArgument('list')->getType()->getTypeOf()->getItemType()->getTypeOf()); |
47
|
|
|
$this->assertInstanceOf(BooleanType::class, $usersQuery->getArgument('boolean')->getType()); |
48
|
|
|
$this->assertInstanceOf(FloatType::class, $usersQuery->getArgument('float')->getType()); |
49
|
|
|
$this->assertInstanceOf(DateTimeType::class, $usersQuery->getArgument('dateTimeImmutable')->getType()); |
50
|
|
|
$this->assertInstanceOf(DateTimeType::class, $usersQuery->getArgument('dateTime')->getType()); |
51
|
|
|
$this->assertInstanceOf(StringType::class, $usersQuery->getArgument('withDefault')->getType()); |
52
|
|
|
$this->assertSame('TestObject', $usersQuery->getArgument('list')->getType()->getTypeOf()->getItemType()->getTypeOf()->getName()); |
53
|
|
|
|
54
|
|
|
$mockResolveInfo = $this->createMock(ResolveInfo::class); |
55
|
|
|
|
56
|
|
|
$context = ['int' => 42, 'string' => 'foo', 'list' => [ |
57
|
|
|
['test' => 42], |
58
|
|
|
['test' => 12], |
59
|
|
|
], |
60
|
|
|
'boolean' => true, |
61
|
|
|
'float' => 4.2, |
62
|
|
|
'dateTimeImmutable' => '2017-01-01 01:01:01', |
63
|
|
|
'dateTime' => '2017-01-01 01:01:01' |
64
|
|
|
]; |
65
|
|
|
|
66
|
|
|
$result = $usersQuery->resolve('foo', $context, $mockResolveInfo); |
67
|
|
|
|
68
|
|
|
$this->assertInstanceOf(TestObject::class, $result); |
69
|
|
|
$this->assertSame('foo424212true4.22017010101010120170101010101default', $result->getTest()); |
70
|
|
|
|
71
|
|
|
unset($context['string']); // Testing null default value |
72
|
|
|
$result = $usersQuery->resolve('foo', $context, $mockResolveInfo); |
73
|
|
|
|
74
|
|
|
$this->assertSame('424212true4.22017010101010120170101010101default', $result->getTest()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testMutations() |
78
|
|
|
{ |
79
|
|
|
$controller = new TestController(); |
80
|
|
|
|
81
|
|
|
$queryProvider = new ControllerQueryProvider($controller, $this->getRegistry()); |
82
|
|
|
|
83
|
|
|
$mutations = $queryProvider->getMutations(); |
84
|
|
|
|
85
|
|
|
$this->assertCount(1, $mutations); |
86
|
|
|
$mutation = $mutations[0]; |
87
|
|
|
$this->assertSame('mutation', $mutation->getName()); |
88
|
|
|
|
89
|
|
|
$mockResolveInfo = $this->createMock(ResolveInfo::class); |
90
|
|
|
|
91
|
|
|
$result = $mutation->resolve('foo', ['testObject' => ['test' => 42]], $mockResolveInfo); |
92
|
|
|
|
93
|
|
|
$this->assertInstanceOf(TestObject::class, $result); |
94
|
|
|
$this->assertEquals('42', $result->getTest()); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function testErrors() |
98
|
|
|
{ |
99
|
|
|
$controller = new class |
100
|
|
|
{ |
101
|
|
|
/** |
102
|
|
|
* @Query |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
public function test($noTypeHint): string |
|
|
|
|
106
|
|
|
{ |
107
|
|
|
return 'foo'; |
108
|
|
|
} |
109
|
|
|
}; |
110
|
|
|
|
111
|
|
|
$queryProvider = new ControllerQueryProvider($controller, $this->getRegistry()); |
112
|
|
|
|
113
|
|
|
$this->expectException(MissingTypeHintException::class); |
114
|
|
|
$queryProvider->getQueries(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function testQueryProviderWithFixedReturnType() |
118
|
|
|
{ |
119
|
|
|
$controller = new TestController(); |
120
|
|
|
|
121
|
|
|
$queryProvider = new ControllerQueryProvider($controller, $this->getRegistry()); |
122
|
|
|
|
123
|
|
|
$queries = $queryProvider->getQueries(); |
124
|
|
|
|
125
|
|
|
$this->assertCount(3, $queries); |
126
|
|
|
$fixedQuery = $queries[1]; |
127
|
|
|
|
128
|
|
|
$this->assertInstanceOf(TestType::class, $fixedQuery->getType()); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function testNameFromAnnotation() |
132
|
|
|
{ |
133
|
|
|
$controller = new TestController(); |
134
|
|
|
|
135
|
|
|
$queryProvider = new ControllerQueryProvider($controller, $this->getRegistry()); |
136
|
|
|
|
137
|
|
|
$queries = $queryProvider->getQueries(); |
138
|
|
|
|
139
|
|
|
$query = $queries[2]; |
140
|
|
|
|
141
|
|
|
$this->assertSame('nameFromAnnotation', $query->getName()); |
142
|
|
|
} |
143
|
|
|
} |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.