1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TheCodingMachine\GraphQL\Controllers; |
4
|
|
|
|
5
|
|
|
use GraphQL\Error\Debug; |
6
|
|
|
use GraphQL\GraphQL; |
7
|
|
|
use GraphQL\Type\SchemaConfig; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
use Symfony\Component\Cache\Simple\PhpFilesCache; |
10
|
|
|
use TheCodingMachine\GraphQL\Controllers\Containers\BasicAutoWiringContainer; |
11
|
|
|
use TheCodingMachine\GraphQL\Controllers\Containers\EmptyContainer; |
12
|
|
|
use TheCodingMachine\GraphQL\Controllers\Hydrators\FactoryHydrator; |
13
|
|
|
use TheCodingMachine\GraphQL\Controllers\Mappers\CompositeTypeMapper; |
14
|
|
|
use TheCodingMachine\GraphQL\Controllers\Security\VoidAuthenticationService; |
15
|
|
|
use TheCodingMachine\GraphQL\Controllers\Security\VoidAuthorizationService; |
16
|
|
|
|
17
|
|
|
class SchemaFactoryTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
public function testCreateSchema(): void |
21
|
|
|
{ |
22
|
|
|
$container = new BasicAutoWiringContainer(new EmptyContainer()); |
23
|
|
|
$cache = new PhpFilesCache(); |
24
|
|
|
|
25
|
|
|
$factory = new SchemaFactory($cache, $container); |
26
|
|
|
|
27
|
|
|
$factory->addControllerNamespace('TheCodingMachine\\GraphQL\\Controllers\\Fixtures\\Integration\\Controllers'); |
28
|
|
|
$factory->addTypeNamespace('TheCodingMachine\\GraphQL\\Controllers\\Fixtures\\Integration'); |
29
|
|
|
$factory->addQueryProvider(new AggregateQueryProvider([])); |
30
|
|
|
|
31
|
|
|
$schema = $factory->createSchema(); |
32
|
|
|
|
33
|
|
|
$this->doTestSchema($schema); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function testSetters(): void |
37
|
|
|
{ |
38
|
|
|
$container = new BasicAutoWiringContainer(new EmptyContainer()); |
39
|
|
|
$cache = new PhpFilesCache(); |
40
|
|
|
|
41
|
|
|
$factory = new SchemaFactory($cache, $container); |
42
|
|
|
|
43
|
|
|
$factory->addControllerNamespace('TheCodingMachine\\GraphQL\\Controllers\\Fixtures\\Integration\\Controllers'); |
44
|
|
|
$factory->addTypeNamespace('TheCodingMachine\\GraphQL\\Controllers\\Fixtures\\Integration'); |
45
|
|
|
$factory->setDoctrineAnnotationReader(new \Doctrine\Common\Annotations\AnnotationReader()) |
46
|
|
|
->setHydrator(new FactoryHydrator()) |
47
|
|
|
->setAuthenticationService(new VoidAuthenticationService()) |
48
|
|
|
->setAuthorizationService(new VoidAuthorizationService()) |
49
|
|
|
->setNamingStrategy(new NamingStrategy()) |
50
|
|
|
->addTypeMapper(new CompositeTypeMapper([])) |
51
|
|
|
->setSchemaConfig(new SchemaConfig()); |
52
|
|
|
|
53
|
|
|
$schema = $factory->createSchema(); |
54
|
|
|
|
55
|
|
|
$this->doTestSchema($schema); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testException(): void |
59
|
|
|
{ |
60
|
|
|
$container = new BasicAutoWiringContainer(new EmptyContainer()); |
61
|
|
|
$cache = new PhpFilesCache(); |
62
|
|
|
|
63
|
|
|
$factory = new SchemaFactory($cache, $container); |
64
|
|
|
|
65
|
|
|
$this->expectException(GraphQLException::class); |
66
|
|
|
$factory->createSchema(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testException2(): void |
70
|
|
|
{ |
71
|
|
|
$container = new BasicAutoWiringContainer(new EmptyContainer()); |
72
|
|
|
$cache = new PhpFilesCache(); |
73
|
|
|
|
74
|
|
|
$factory = new SchemaFactory($cache, $container); |
75
|
|
|
$factory->addTypeNamespace('TheCodingMachine\\GraphQL\\Controllers\\Fixtures\\Integration'); |
76
|
|
|
|
77
|
|
|
$this->expectException(GraphQLException::class); |
78
|
|
|
$factory->createSchema(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
private function doTestSchema(Schema $schema): void |
82
|
|
|
{ |
83
|
|
|
|
84
|
|
|
$schema->assertValid(); |
85
|
|
|
|
86
|
|
|
$queryString = ' |
87
|
|
|
query { |
88
|
|
|
contacts { |
89
|
|
|
name |
90
|
|
|
uppercaseName |
91
|
|
|
... on User { |
92
|
|
|
email |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
'; |
97
|
|
|
|
98
|
|
|
$result = GraphQL::executeQuery( |
99
|
|
|
$schema, |
100
|
|
|
$queryString |
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
$this->assertSame([ |
104
|
|
|
'contacts' => [ |
105
|
|
|
[ |
106
|
|
|
'name' => 'Joe', |
107
|
|
|
'uppercaseName' => 'JOE' |
108
|
|
|
], |
109
|
|
|
[ |
110
|
|
|
'name' => 'Bill', |
111
|
|
|
'uppercaseName' => 'BILL', |
112
|
|
|
'email' => '[email protected]' |
113
|
|
|
] |
114
|
|
|
|
115
|
|
|
] |
116
|
|
|
], $result->toArray(Debug::RETHROW_INTERNAL_EXCEPTIONS)['data']); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|