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