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 $expectedData = [ |
19
|
|
|
'user' => [ |
20
|
|
|
'friends' => [ |
21
|
|
|
'totalCount' => 4, |
22
|
|
|
'edges' => [ |
23
|
|
|
[ |
24
|
|
|
'friendshipTime' => 'Yesterday', |
25
|
|
|
'node' => [ |
26
|
|
|
'name' => 'Nick', |
27
|
|
|
], |
28
|
|
|
], |
29
|
|
|
[ |
30
|
|
|
'friendshipTime' => 'Yesterday', |
31
|
|
|
'node' => [ |
32
|
|
|
'name' => 'Lee', |
33
|
|
|
], |
34
|
|
|
], |
35
|
|
|
], |
36
|
|
|
], |
37
|
|
|
], |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
View Code Duplication |
public function testEndpointAction() |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
$client = static::createClient(['test_case' => 'connection']); |
43
|
|
|
|
44
|
|
|
$query = <<<EOF |
45
|
|
|
query FriendsQuery { |
46
|
|
|
user { |
47
|
|
|
friends(first: 2) { |
48
|
|
|
totalCount |
49
|
|
|
edges { |
50
|
|
|
friendshipTime |
51
|
|
|
node { |
52
|
|
|
name |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
EOF; |
59
|
|
|
$client->request('GET', '/', ['query' => $query], [], ['CONTENT_TYPE' => 'application/graphql']); |
60
|
|
|
$result = $client->getResponse()->getContent(); |
61
|
|
|
$this->assertEquals(['data' => $this->expectedData], json_decode($result, true), $result); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException |
66
|
|
|
* @expectedExceptionMessage Must provide query parameter |
67
|
|
|
*/ |
68
|
|
|
public function testEndpointWithEmptyQuery() |
69
|
|
|
{ |
70
|
|
|
$client = static::createClient(); |
71
|
|
|
$client->request('GET', '/', []); |
72
|
|
|
$client->getResponse()->getContent(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException |
77
|
|
|
* @expectedExceptionMessage POST body sent invalid JSON [Syntax error] |
78
|
|
|
*/ |
79
|
|
|
public function testEndpointWithInvalidBodyQuery() |
80
|
|
|
{ |
81
|
|
|
$client = static::createClient(); |
82
|
|
|
$client->request('GET', '/', [], [], ['CONTENT_TYPE' => 'application/json'], '{'); |
83
|
|
|
$client->getResponse()->getContent(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
View Code Duplication |
public function testEndpointActionWithVariables() |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
$client = static::createClient(['test_case' => 'connection']); |
89
|
|
|
|
90
|
|
|
$query = <<<EOF |
91
|
|
|
query FriendsQuery(\$firstFriends: Int) { |
92
|
|
|
user { |
93
|
|
|
friends(first: \$firstFriends) { |
94
|
|
|
totalCount |
95
|
|
|
edges { |
96
|
|
|
friendshipTime |
97
|
|
|
node { |
98
|
|
|
name |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
EOF; |
105
|
|
|
|
106
|
|
|
$client->request('GET', '/', [], [], ['CONTENT_TYPE' => 'application/json'], json_encode(['query' => $query, 'variables' => '{"firstFriends": 2}'])); |
107
|
|
|
$result = $client->getResponse()->getContent(); |
108
|
|
|
$this->assertEquals(['data' => $this->expectedData], json_decode($result, true), $result); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException |
113
|
|
|
* @expectedExceptionMessage Variables are invalid JSON |
114
|
|
|
*/ |
115
|
|
View Code Duplication |
public function testEndpointActionWithInvalidVariables() |
|
|
|
|
116
|
|
|
{ |
117
|
|
|
$client = static::createClient(['test_case' => 'connection']); |
118
|
|
|
|
119
|
|
|
$query = <<<EOF |
120
|
|
|
query { |
121
|
|
|
user |
122
|
|
|
} |
123
|
|
|
EOF; |
124
|
|
|
|
125
|
|
|
$client->request('GET', '/', ['query' => $query, 'variables' => '"firstFriends": 2}']); |
126
|
|
|
$result = $client->getResponse()->getContent(); |
127
|
|
|
$this->assertEquals(['data' => $this->expectedData], json_decode($result, true), $result); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
View Code Duplication |
public function testEndpointActionWithOperationName() |
|
|
|
|
131
|
|
|
{ |
132
|
|
|
$client = static::createClient(['test_case' => 'connection']); |
133
|
|
|
|
134
|
|
|
$query = <<<EOF |
135
|
|
|
query FriendsQuery { |
136
|
|
|
user { |
137
|
|
|
friends(first: 2) { |
138
|
|
|
totalCount |
139
|
|
|
edges { |
140
|
|
|
friendshipTime |
141
|
|
|
node { |
142
|
|
|
name |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
query FriendsQuery2 { |
150
|
|
|
user { |
151
|
|
|
friends { |
152
|
|
|
totalCount |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
EOF; |
157
|
|
|
|
158
|
|
|
$client->request('POST', '/', ['query' => $query, 'operationName' => 'FriendsQuery'], [], ['CONTENT_TYPE' => 'application/x-www-form-urlencoded']); |
159
|
|
|
$result = $client->getResponse()->getContent(); |
160
|
|
|
$this->assertEquals(['data' => $this->expectedData], json_decode($result, true), $result); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
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.