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\Security\VoidAuthenticationService; |
10
|
|
|
use TheCodingMachine\GraphQL\Controllers\Security\VoidAuthorizationService; |
11
|
|
|
use Youshido\GraphQL\Execution\ResolveInfo; |
12
|
|
|
use Youshido\GraphQL\Type\InputObject\InputObjectType; |
13
|
|
|
use Youshido\GraphQL\Type\ListType\ListType; |
14
|
|
|
use Youshido\GraphQL\Type\NonNullType; |
15
|
|
|
use Youshido\GraphQL\Type\Object\ObjectType; |
16
|
|
|
use Youshido\GraphQL\Type\Scalar\BooleanType; |
17
|
|
|
use Youshido\GraphQL\Type\Scalar\DateTimeType; |
18
|
|
|
use Youshido\GraphQL\Type\Scalar\FloatType; |
19
|
|
|
use Youshido\GraphQL\Type\Scalar\IntType; |
20
|
|
|
use Youshido\GraphQL\Type\Scalar\StringType; |
21
|
|
|
use Youshido\GraphQL\Type\TypeInterface; |
22
|
|
|
use TheCodingMachine\GraphQL\Controllers\Annotations\Query; |
23
|
|
|
|
24
|
|
|
class ControllerQueryProviderTest extends AbstractQueryProviderTest |
25
|
|
|
{ |
26
|
|
|
public function testQueryProvider() |
27
|
|
|
{ |
28
|
|
|
$controller = new TestController(); |
29
|
|
|
$reader = new AnnotationReader(); |
30
|
|
|
|
31
|
|
|
$queryProvider = new ControllerQueryProvider($controller, $reader, $this->getTypeMapper(), $this->getHydrator(), new VoidAuthenticationService(), new VoidAuthorizationService()); |
32
|
|
|
|
33
|
|
|
$queries = $queryProvider->getQueries(); |
34
|
|
|
|
35
|
|
|
$this->assertCount(1, $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->assertSame('TestObject', $usersQuery->getArgument('list')->getType()->getTypeOf()->getItemType()->getTypeOf()->getName()); |
52
|
|
|
|
53
|
|
|
$mockResolveInfo = $this->createMock(ResolveInfo::class); |
54
|
|
|
|
55
|
|
|
$context = ['int'=>42, 'string'=>'foo', 'list'=>[ |
56
|
|
|
['test'=>42], |
57
|
|
|
['test'=>12], |
58
|
|
|
], |
59
|
|
|
'boolean'=>true, |
60
|
|
|
'float'=>4.2, |
61
|
|
|
'dateTimeImmutable'=>'2017-01-01 01:01:01', |
62
|
|
|
'dateTime'=>'2017-01-01 01:01:01' |
63
|
|
|
]; |
64
|
|
|
|
65
|
|
|
$result = $usersQuery->resolve('foo', $context, $mockResolveInfo); |
66
|
|
|
|
67
|
|
|
$this->assertInstanceOf(TestObject::class, $result); |
68
|
|
|
$this->assertSame('foo424212true4.22017010101010120170101010101default', $result->getTest()); |
69
|
|
|
|
70
|
|
|
unset($context['string']); // Testing null default value |
71
|
|
|
$result = $usersQuery->resolve('foo', $context, $mockResolveInfo); |
72
|
|
|
|
73
|
|
|
$this->assertSame('424212true4.22017010101010120170101010101default', $result->getTest()); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function testMutations() |
77
|
|
|
{ |
78
|
|
|
$controller = new TestController(); |
79
|
|
|
$reader = new AnnotationReader(); |
80
|
|
|
|
81
|
|
|
$queryProvider = new ControllerQueryProvider($controller, $reader, $this->getTypeMapper(), $this->getHydrator(), new VoidAuthenticationService(), new VoidAuthorizationService()); |
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
|
|
|
* @Query |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
public function test($noTypeHint): string |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
return 'foo'; |
107
|
|
|
} |
108
|
|
|
}; |
109
|
|
|
$reader = new AnnotationReader(); |
110
|
|
|
|
111
|
|
|
$queryProvider = new ControllerQueryProvider($controller, $reader, $this->getTypeMapper(), $this->getHydrator(), new VoidAuthenticationService(), new VoidAuthorizationService()); |
112
|
|
|
|
113
|
|
|
$this->expectException(MissingTypeHintException::class); |
114
|
|
|
$queryProvider->getQueries(); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.