1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TheCodingMachine\GraphQL\Controllers\Integration; |
4
|
|
|
|
5
|
|
|
use function class_exists; |
6
|
|
|
use Doctrine\Common\Annotations\AnnotationReader as DoctrineAnnotationReader; |
7
|
|
|
use Exception; |
8
|
|
|
use GraphQL\GraphQL; |
9
|
|
|
use GraphQL\Type\Definition\InputType; |
10
|
|
|
use Mouf\Picotainer\Picotainer; |
11
|
|
|
use PhpParser\Comment\Doc; |
12
|
|
|
use PHPUnit\Framework\TestCase; |
13
|
|
|
use PHPUnit\Util\Type; |
14
|
|
|
use function print_r; |
15
|
|
|
use Psr\Container\ContainerInterface; |
16
|
|
|
use Psr\Container\NotFoundExceptionInterface; |
17
|
|
|
use Symfony\Component\Cache\Simple\ArrayCache; |
18
|
|
|
use Symfony\Component\Cache\Simple\NullCache; |
19
|
|
|
use TheCodingMachine\GraphQL\Controllers\AnnotationReader; |
20
|
|
|
use TheCodingMachine\GraphQL\Controllers\ControllerQueryProviderFactory; |
21
|
|
|
use TheCodingMachine\GraphQL\Controllers\Fixtures\Integration\Models\Contact; |
22
|
|
|
use TheCodingMachine\GraphQL\Controllers\GlobControllerQueryProvider; |
23
|
|
|
use TheCodingMachine\GraphQL\Controllers\HydratorInterface; |
24
|
|
|
use TheCodingMachine\GraphQL\Controllers\Mappers\GlobTypeMapper; |
25
|
|
|
use TheCodingMachine\GraphQL\Controllers\Mappers\RecursiveTypeMapper; |
26
|
|
|
use TheCodingMachine\GraphQL\Controllers\Mappers\RecursiveTypeMapperInterface; |
27
|
|
|
use TheCodingMachine\GraphQL\Controllers\Mappers\TypeMapperInterface; |
28
|
|
|
use TheCodingMachine\GraphQL\Controllers\QueryProviderInterface; |
29
|
|
|
use TheCodingMachine\GraphQL\Controllers\Containers\BasicAutoWiringContainer; |
30
|
|
|
use TheCodingMachine\GraphQL\Controllers\Containers\EmptyContainer; |
31
|
|
|
use TheCodingMachine\GraphQL\Controllers\Reflection\CachedDocBlockFactory; |
32
|
|
|
use TheCodingMachine\GraphQL\Controllers\Schema; |
33
|
|
|
use TheCodingMachine\GraphQL\Controllers\Security\AuthenticationServiceInterface; |
34
|
|
|
use TheCodingMachine\GraphQL\Controllers\Security\AuthorizationServiceInterface; |
35
|
|
|
use TheCodingMachine\GraphQL\Controllers\Security\VoidAuthenticationService; |
36
|
|
|
use TheCodingMachine\GraphQL\Controllers\Security\VoidAuthorizationService; |
37
|
|
|
use TheCodingMachine\GraphQL\Controllers\TypeGenerator; |
38
|
|
|
|
39
|
|
|
class EndToEndTest extends TestCase |
40
|
|
|
{ |
41
|
|
|
/** |
42
|
|
|
* @var ContainerInterface |
43
|
|
|
*/ |
44
|
|
|
private $mainContainer; |
45
|
|
|
|
46
|
|
|
public function setUp() |
47
|
|
|
{ |
48
|
|
|
$this->mainContainer = new Picotainer([ |
49
|
|
|
Schema::class => function(ContainerInterface $container) { |
50
|
|
|
return new Schema($container->get(QueryProviderInterface::class), $container->get(RecursiveTypeMapperInterface::class)); |
51
|
|
|
}, |
52
|
|
|
QueryProviderInterface::class => function(ContainerInterface $container) { |
53
|
|
|
return new GlobControllerQueryProvider('TheCodingMachine\\GraphQL\\Controllers\\Fixtures\\Integration\\Controllers', $container->get(ControllerQueryProviderFactory::class), |
54
|
|
|
$container->get(RecursiveTypeMapperInterface::class), $container->get(BasicAutoWiringContainer::class), new NullCache()); |
55
|
|
|
}, |
56
|
|
|
ControllerQueryProviderFactory::class => function(ContainerInterface $container) { |
57
|
|
|
return new ControllerQueryProviderFactory( |
58
|
|
|
$container->get(AnnotationReader::class), |
59
|
|
|
$container->get(HydratorInterface::class), |
60
|
|
|
$container->get(AuthenticationServiceInterface::class), |
61
|
|
|
$container->get(AuthorizationServiceInterface::class), |
62
|
|
|
$container->get(BasicAutoWiringContainer::class), |
63
|
|
|
$container->get(CachedDocBlockFactory::class) |
64
|
|
|
); |
65
|
|
|
}, |
66
|
|
|
BasicAutoWiringContainer::class => function(ContainerInterface $container) { |
|
|
|
|
67
|
|
|
return new BasicAutoWiringContainer(new EmptyContainer()); |
68
|
|
|
}, |
69
|
|
|
AuthorizationServiceInterface::class => function(ContainerInterface $container) { |
|
|
|
|
70
|
|
|
return new VoidAuthorizationService(); |
71
|
|
|
}, |
72
|
|
|
AuthenticationServiceInterface::class => function(ContainerInterface $container) { |
|
|
|
|
73
|
|
|
return new VoidAuthenticationService(); |
74
|
|
|
}, |
75
|
|
|
RecursiveTypeMapperInterface::class => function(ContainerInterface $container) { |
76
|
|
|
return new RecursiveTypeMapper($container->get(TypeMapperInterface::class)); |
77
|
|
|
}, |
78
|
|
|
TypeMapperInterface::class => function(ContainerInterface $container) { |
79
|
|
|
return new GlobTypeMapper('TheCodingMachine\\GraphQL\\Controllers\\Fixtures\\Integration\\Types', |
80
|
|
|
$container->get(TypeGenerator::class), |
81
|
|
|
$container->get(BasicAutoWiringContainer::class), |
82
|
|
|
$container->get(AnnotationReader::class), |
83
|
|
|
new NullCache() |
84
|
|
|
); |
85
|
|
|
}, |
86
|
|
|
TypeGenerator::class => function(ContainerInterface $container) { |
87
|
|
|
return new TypeGenerator( |
88
|
|
|
$container->get(AnnotationReader::class), |
89
|
|
|
$container->get(ControllerQueryProviderFactory::class) |
90
|
|
|
); |
91
|
|
|
}, |
92
|
|
|
AnnotationReader::class => function(ContainerInterface $container) { |
|
|
|
|
93
|
|
|
return new AnnotationReader(new DoctrineAnnotationReader()); |
94
|
|
|
}, |
95
|
|
|
HydratorInterface::class => function(ContainerInterface $container) { |
|
|
|
|
96
|
|
|
return new class implements HydratorInterface |
97
|
|
|
{ |
98
|
|
|
public function hydrate(array $data, InputType $type) |
99
|
|
|
{ |
100
|
|
|
return new Contact($data['name']); |
101
|
|
|
} |
102
|
|
|
}; |
103
|
|
|
}, |
104
|
|
|
CachedDocBlockFactory::class => function() { |
105
|
|
|
return new CachedDocBlockFactory(new ArrayCache()); |
106
|
|
|
} |
107
|
|
|
]); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function testEndToEnd() |
111
|
|
|
{ |
112
|
|
|
/** |
113
|
|
|
* @var Schema $schema |
114
|
|
|
*/ |
115
|
|
|
$schema = $this->mainContainer->get(Schema::class); |
116
|
|
|
$queryString = ' |
117
|
|
|
query { |
118
|
|
|
getContacts { |
119
|
|
|
name |
120
|
|
|
... on User { |
121
|
|
|
email |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
'; |
126
|
|
|
|
127
|
|
|
$result = GraphQL::executeQuery( |
128
|
|
|
$schema, |
129
|
|
|
$queryString |
130
|
|
|
); |
131
|
|
|
|
132
|
|
|
$this->assertSame([ |
133
|
|
|
'getContacts' => [ |
134
|
|
|
[ |
135
|
|
|
'name' => 'Joe' |
136
|
|
|
], |
137
|
|
|
[ |
138
|
|
|
'name' => 'Bill', |
139
|
|
|
'email' => '[email protected]' |
140
|
|
|
] |
141
|
|
|
|
142
|
|
|
] |
143
|
|
|
], $result->toArray()['data']); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
} |
147
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.