1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the OverblogGraphQLBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Overblog <http://github.com/overblog/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Overblog\GraphQLBundle\Tests\Functional\Controller; |
13
|
|
|
|
14
|
|
|
use Overblog\GraphQLBundle\Tests\Functional\TestCase; |
15
|
|
|
|
16
|
|
|
class GraphControllerTest extends TestCase |
17
|
|
|
{ |
18
|
|
|
private $friendsQuery = <<<EOF |
19
|
|
|
query FriendsQuery { |
20
|
|
|
user { |
21
|
|
|
friends(first: 2) { |
22
|
|
|
totalCount |
23
|
|
|
edges { |
24
|
|
|
friendshipTime |
25
|
|
|
node { |
26
|
|
|
name |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
EOF; |
33
|
|
|
|
34
|
|
|
private $friendsTotalCountQuery = <<<EOF |
35
|
|
|
query FriendsTotalCountQuery { |
36
|
|
|
user { |
37
|
|
|
friends { |
38
|
|
|
totalCount |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
EOF; |
43
|
|
|
|
44
|
|
|
private $expectedData = [ |
45
|
|
|
'user' => [ |
46
|
|
|
'friends' => [ |
47
|
|
|
'totalCount' => 4, |
48
|
|
|
'edges' => [ |
49
|
|
|
[ |
50
|
|
|
'friendshipTime' => 'Yesterday', |
51
|
|
|
'node' => [ |
52
|
|
|
'name' => 'Nick', |
53
|
|
|
], |
54
|
|
|
], |
55
|
|
|
[ |
56
|
|
|
'friendshipTime' => 'Yesterday', |
57
|
|
|
'node' => [ |
58
|
|
|
'name' => 'Lee', |
59
|
|
|
], |
60
|
|
|
], |
61
|
|
|
], |
62
|
|
|
], |
63
|
|
|
], |
64
|
|
|
]; |
65
|
|
|
|
66
|
|
|
public function testEndpointAction() |
67
|
|
|
{ |
68
|
|
|
$client = static::createClient(['test_case' => 'connection']); |
69
|
|
|
|
70
|
|
|
$client->request('GET', '/', ['query' => $this->friendsQuery], [], ['CONTENT_TYPE' => 'application/graphql']); |
71
|
|
|
$result = $client->getResponse()->getContent(); |
72
|
|
|
$this->assertEquals(['data' => $this->expectedData], json_decode($result, true), $result); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException |
77
|
|
|
* @expectedExceptionMessage Must provide query parameter |
78
|
|
|
*/ |
79
|
|
|
public function testEndpointWithEmptyQuery() |
80
|
|
|
{ |
81
|
|
|
$client = static::createClient(); |
82
|
|
|
$client->request('GET', '/', []); |
83
|
|
|
$client->getResponse()->getContent(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException |
88
|
|
|
* @expectedExceptionMessage POST body sent invalid JSON |
89
|
|
|
*/ |
90
|
|
|
public function testEndpointWithInvalidBodyQuery() |
91
|
|
|
{ |
92
|
|
|
$client = static::createClient(); |
93
|
|
|
$client->request('GET', '/', [], [], ['CONTENT_TYPE' => 'application/json'], '{'); |
94
|
|
|
$client->getResponse()->getContent(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
View Code Duplication |
public function testEndpointActionWithVariables() |
|
|
|
|
98
|
|
|
{ |
99
|
|
|
$client = static::createClient(['test_case' => 'connection']); |
100
|
|
|
|
101
|
|
|
$query = <<<EOF |
102
|
|
|
query FriendsQuery(\$firstFriends: Int) { |
103
|
|
|
user { |
104
|
|
|
friends(first: \$firstFriends) { |
105
|
|
|
totalCount |
106
|
|
|
edges { |
107
|
|
|
friendshipTime |
108
|
|
|
node { |
109
|
|
|
name |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
EOF; |
116
|
|
|
|
117
|
|
|
$client->request('GET', '/', [], [], ['CONTENT_TYPE' => 'application/json'], json_encode(['query' => $query, 'variables' => '{"firstFriends": 2}'])); |
118
|
|
|
$result = $client->getResponse()->getContent(); |
119
|
|
|
$this->assertEquals(['data' => $this->expectedData], json_decode($result, true), $result); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException |
124
|
|
|
* @expectedExceptionMessage Variables are invalid JSON |
125
|
|
|
*/ |
126
|
|
View Code Duplication |
public function testEndpointActionWithInvalidVariables() |
|
|
|
|
127
|
|
|
{ |
128
|
|
|
$client = static::createClient(['test_case' => 'connection']); |
129
|
|
|
|
130
|
|
|
$query = <<<EOF |
131
|
|
|
query { |
132
|
|
|
user |
133
|
|
|
} |
134
|
|
|
EOF; |
135
|
|
|
|
136
|
|
|
$client->request('GET', '/', ['query' => $query, 'variables' => '"firstFriends": 2}']); |
137
|
|
|
$result = $client->getResponse()->getContent(); |
138
|
|
|
$this->assertEquals(['data' => $this->expectedData], json_decode($result, true), $result); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function testEndpointActionWithOperationName() |
142
|
|
|
{ |
143
|
|
|
$client = static::createClient(['test_case' => 'connection']); |
144
|
|
|
|
145
|
|
|
$query = $this->friendsQuery . "\n" .$this->friendsTotalCountQuery; |
146
|
|
|
|
147
|
|
|
$client->request('POST', '/', ['query' => $query, 'operationName' => 'FriendsQuery'], [], ['CONTENT_TYPE' => 'application/x-www-form-urlencoded']); |
148
|
|
|
$result = $client->getResponse()->getContent(); |
149
|
|
|
$this->assertEquals(['data' => $this->expectedData], json_decode($result, true), $result); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function testBatchEndpointAction() |
153
|
|
|
{ |
154
|
|
|
$client = static::createClient(['test_case' => 'connection']); |
155
|
|
|
|
156
|
|
|
$data = [ |
157
|
|
|
'friends' => [ |
158
|
|
|
'query' => $this->friendsQuery, |
159
|
|
|
], |
160
|
|
|
'friendsTotalCount' => [ |
161
|
|
|
'query' => $this->friendsTotalCountQuery, |
162
|
|
|
], |
163
|
|
|
]; |
164
|
|
|
|
165
|
|
|
$client->request('POST', '/?batch', [], [], ['CONTENT_TYPE' => 'application/json'], json_encode($data)); |
166
|
|
|
$result = $client->getResponse()->getContent(); |
167
|
|
|
|
168
|
|
|
$expected = [ |
169
|
|
|
'friends' => ['data' => $this->expectedData], |
170
|
|
|
'friendsTotalCount' => ['data' => ['user' => ['friends' => ['totalCount' => 4]]]], |
171
|
|
|
]; |
172
|
|
|
$this->assertEquals($expected, json_decode($result, true), $result); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException |
177
|
|
|
* @expectedExceptionMessage Must provide at least one valid query. |
178
|
|
|
*/ |
179
|
|
|
public function testBatchEndpointWithEmptyQuery() |
180
|
|
|
{ |
181
|
|
|
$client = static::createClient(); |
182
|
|
|
$client->request('GET', '/?batch', [], [], ['CONTENT_TYPE' => 'application/json']); |
183
|
|
|
$client->getResponse()->getContent(); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException |
188
|
|
|
* @expectedExceptionMessage Only request with content type " is accepted. |
189
|
|
|
*/ |
190
|
|
|
public function testBatchEndpointWrongContentType() |
191
|
|
|
{ |
192
|
|
|
$client = static::createClient(); |
193
|
|
|
$client->request('GET', '/?batch'); |
194
|
|
|
$client->getResponse()->getContent(); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException |
199
|
|
|
* @expectedExceptionMessage POST body sent invalid JSON |
200
|
|
|
*/ |
201
|
|
|
public function testBatchEndpointWithInvalidJson() |
202
|
|
|
{ |
203
|
|
|
$client = static::createClient(); |
204
|
|
|
$client->request('GET', '/?batch', [], [], ['CONTENT_TYPE' => 'application/json'], '{'); |
205
|
|
|
$client->getResponse()->getContent(); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException |
210
|
|
|
* @expectedExceptionMessage No valid query found in node "test" |
211
|
|
|
*/ |
212
|
|
|
public function testBatchEndpointWithInvalidQuery() |
213
|
|
|
{ |
214
|
|
|
$client = static::createClient(); |
215
|
|
|
$client->request('GET', '/?batch', [], [], ['CONTENT_TYPE' => 'application/json'], '{"test" : {"query": 1}}'); |
216
|
|
|
$client->getResponse()->getContent(); |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.