|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* TranslatableArrayControllerTest class file |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Graviton\CoreBundle\Tests\Controller; |
|
7
|
|
|
|
|
8
|
|
|
use Graviton\I18nBundle\DataFixtures\MongoDB\LoadLanguageData; |
|
9
|
|
|
use Graviton\I18nBundle\DataFixtures\MongoDB\LoadMultiLanguageData; |
|
10
|
|
|
use Graviton\TestBundle\Client; |
|
11
|
|
|
use Graviton\TestBundle\Test\RestTestCase; |
|
12
|
|
|
use GravitonDyn\TestCaseTranslatableArrayBundle\Document\TestCaseTranslatableArray; |
|
13
|
|
|
use GravitonDyn\TestCaseTranslatableArrayBundle\DataFixtures\MongoDB\LoadTestCaseTranslatableArrayData; |
|
14
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
|
18
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
|
19
|
|
|
* @link http://swisscom.ch |
|
20
|
|
|
*/ |
|
21
|
|
|
class TranslatableArrayControllerTest extends RestTestCase |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* load fixtures |
|
25
|
|
|
* |
|
26
|
|
|
* @return void |
|
27
|
|
|
*/ |
|
28
|
|
|
public function setUp() |
|
29
|
|
|
{ |
|
30
|
|
|
if (!class_exists(TestCaseTranslatableArray::class)) { |
|
31
|
|
|
$this->markTestSkipped(sprintf('%s definition is not loaded', TestCaseTranslatableArray::class)); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
$this->loadFixtures( |
|
35
|
|
|
[ |
|
36
|
|
|
LoadLanguageData::class, |
|
37
|
|
|
LoadMultiLanguageData::class, |
|
38
|
|
|
LoadTestCaseTranslatableArrayData::class, |
|
39
|
|
|
], |
|
40
|
|
|
null, |
|
41
|
|
|
'doctrine_mongodb' |
|
42
|
|
|
); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Test item schema |
|
47
|
|
|
* |
|
48
|
|
|
* @return void |
|
49
|
|
|
*/ |
|
50
|
|
View Code Duplication |
public function testItemSchema() |
|
|
|
|
|
|
51
|
|
|
{ |
|
52
|
|
|
$client = static::createRestClient(); |
|
53
|
|
|
$this->getRequest($client, '/schema/testcase/translatable-array/item'); |
|
54
|
|
|
$this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode()); |
|
55
|
|
|
|
|
56
|
|
|
$schema = $client->getResults(); |
|
57
|
|
|
$this->assertEquals('object', $schema->type); |
|
58
|
|
|
$this->assertItemSchema($schema); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Test collection schema |
|
63
|
|
|
* |
|
64
|
|
|
* @return void |
|
65
|
|
|
*/ |
|
66
|
|
View Code Duplication |
public function testCollectionSchema() |
|
|
|
|
|
|
67
|
|
|
{ |
|
68
|
|
|
$client = static::createRestClient(); |
|
69
|
|
|
$this->getRequest($client, '/schema/testcase/translatable-array/collection'); |
|
70
|
|
|
$this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode()); |
|
71
|
|
|
|
|
72
|
|
|
$schema = $client->getResults(); |
|
73
|
|
|
$this->assertEquals('array', $schema->type); |
|
74
|
|
|
$this->assertEquals('object', $schema->items->type); |
|
75
|
|
|
$this->assertItemSchema($schema->items); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Test GET one method |
|
80
|
|
|
* |
|
81
|
|
|
* @return void |
|
82
|
|
|
*/ |
|
83
|
|
View Code Duplication |
public function testCheckGetOne() |
|
|
|
|
|
|
84
|
|
|
{ |
|
85
|
|
|
$client = static::createRestClient(); |
|
86
|
|
|
$this->getRequest($client, '/testcase/translatable-array/testdata'); |
|
87
|
|
|
$this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode()); |
|
88
|
|
|
$this->assertNotEmpty($client->getResults()); |
|
89
|
|
|
|
|
90
|
|
|
$this->assertFixtureData($client->getResults()); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Test GET all method |
|
95
|
|
|
* |
|
96
|
|
|
* @return void |
|
97
|
|
|
*/ |
|
98
|
|
View Code Duplication |
public function testCheckGetAll() |
|
|
|
|
|
|
99
|
|
|
{ |
|
100
|
|
|
$client = static::createRestClient(); |
|
101
|
|
|
$this->getRequest($client, '/testcase/translatable-array/'); |
|
102
|
|
|
$this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode()); |
|
103
|
|
|
$this->assertCount(1, $client->getResults()); |
|
104
|
|
|
|
|
105
|
|
|
$this->assertFixtureData($client->getResults()[0]); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Test POST method |
|
110
|
|
|
* |
|
111
|
|
|
* @return void |
|
112
|
|
|
*/ |
|
113
|
|
|
public function testPostMethod() |
|
114
|
|
|
{ |
|
115
|
|
|
$data = $this->getPostData(); |
|
116
|
|
|
|
|
117
|
|
|
$client = static::createRestClient(); |
|
118
|
|
|
$client->post('/testcase/translatable-array/', $data); |
|
119
|
|
|
$this->assertEquals(Response::HTTP_CREATED, $client->getResponse()->getStatusCode()); |
|
120
|
|
|
$this->assertEmpty($client->getResults()); |
|
121
|
|
|
|
|
122
|
|
|
$location = $client->getResponse()->headers->get('Location'); |
|
123
|
|
|
|
|
124
|
|
|
$client = static::createRestClient(); |
|
125
|
|
|
$this->getRequest($client, $location, ['en', 'de']); |
|
126
|
|
|
$this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode()); |
|
127
|
|
|
|
|
128
|
|
|
$result = $client->getResults(); |
|
129
|
|
|
$this->assertNotNull($result->id); |
|
130
|
|
|
unset($result->id); |
|
131
|
|
|
$this->assertEquals($data, $result); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Test PUT method |
|
136
|
|
|
* |
|
137
|
|
|
* @return void |
|
138
|
|
|
*/ |
|
139
|
|
|
public function testPutMethod() |
|
140
|
|
|
{ |
|
141
|
|
|
$data = $this->getPostData(); |
|
142
|
|
|
|
|
143
|
|
|
$client = static::createRestClient(); |
|
144
|
|
|
$client->put('/testcase/translatable-array/testdata', $data); |
|
145
|
|
|
$this->assertEquals(Response::HTTP_NO_CONTENT, $client->getResponse()->getStatusCode()); |
|
146
|
|
|
$this->assertEmpty($client->getResults()); |
|
147
|
|
|
|
|
148
|
|
|
$client = static::createRestClient(); |
|
149
|
|
|
$this->getRequest($client, '/testcase/translatable-array/testdata', ['en', 'de']); |
|
150
|
|
|
$this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode()); |
|
151
|
|
|
|
|
152
|
|
|
$result = $client->getResults(); |
|
153
|
|
|
$this->assertNotNull($result->id); |
|
154
|
|
|
unset($result->id); |
|
155
|
|
|
$this->assertEquals($data, $result); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Test PUT method with an empty translatable that is NOT required |
|
160
|
|
|
* |
|
161
|
|
|
* @return void |
|
162
|
|
|
*/ |
|
163
|
|
View Code Duplication |
public function testPutMethodEmptyTranslatable() |
|
|
|
|
|
|
164
|
|
|
{ |
|
165
|
|
|
$data = $this->getPostData(); |
|
166
|
|
|
unset($data->field); |
|
167
|
|
|
|
|
168
|
|
|
$client = static::createRestClient(); |
|
169
|
|
|
$client->put('/testcase/translatable-array/testdata', $data); |
|
170
|
|
|
|
|
171
|
|
|
$this->assertEquals(Response::HTTP_NO_CONTENT, $client->getResponse()->getStatusCode()); |
|
172
|
|
|
$this->assertEmpty($client->getResults()); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Test validation |
|
177
|
|
|
* |
|
178
|
|
|
* @return void |
|
179
|
|
|
*/ |
|
180
|
|
|
public function testValidation() |
|
181
|
|
|
{ |
|
182
|
|
|
$data = (object) [ |
|
183
|
|
|
'id' => 'testdata', |
|
184
|
|
|
'field' => (object) [ |
|
185
|
|
|
'de' => 'No "en" translation', |
|
186
|
|
|
], |
|
187
|
|
|
'array' => [ |
|
188
|
|
|
'Invalid value', |
|
189
|
|
|
(object) ['Invalid' => 'value'], |
|
190
|
|
|
], |
|
191
|
|
|
'deep' => (object) [ |
|
192
|
|
|
'deep' => [ |
|
193
|
|
|
(object) [ |
|
194
|
|
|
'field' => 'Invalid value', |
|
195
|
|
|
'array' => 'Invalid value', |
|
196
|
|
|
], |
|
197
|
|
|
(object) [ |
|
198
|
|
|
'field' => (object) [ |
|
199
|
|
|
'en' => 'Valid value', |
|
200
|
|
|
], |
|
201
|
|
|
'array' => [ |
|
202
|
|
|
(object) [], |
|
203
|
|
|
(object) [ |
|
204
|
|
|
'en' => 'Valid value', |
|
205
|
|
|
], |
|
206
|
|
|
], |
|
207
|
|
|
], |
|
208
|
|
|
], |
|
209
|
|
|
], |
|
210
|
|
|
]; |
|
211
|
|
|
|
|
212
|
|
|
$client = static::createRestClient(); |
|
213
|
|
|
$client->put('/testcase/translatable-array/testdata', $data); |
|
214
|
|
|
$this->assertEquals(Response::HTTP_BAD_REQUEST, $client->getResponse()->getStatusCode()); |
|
215
|
|
|
$this->assertEquals( |
|
216
|
|
|
[ |
|
217
|
|
|
(object) [ |
|
218
|
|
|
'propertyPath' => 'children[field]', |
|
219
|
|
|
'message' => 'This value is not valid.' |
|
220
|
|
|
], |
|
221
|
|
|
(object) [ |
|
222
|
|
|
'propertyPath' => 'children[array].children[0]', |
|
223
|
|
|
'message' => 'This value is not valid.', |
|
224
|
|
|
], |
|
225
|
|
|
(object) [ |
|
226
|
|
|
'propertyPath' => 'children[array].children[1]', |
|
227
|
|
|
'message' => 'This value is not valid.', |
|
228
|
|
|
], |
|
229
|
|
|
|
|
230
|
|
|
(object) [ |
|
231
|
|
|
'propertyPath' => 'children[deep].children[deep].children[0].children[field]', |
|
232
|
|
|
'message' => 'This value is not valid.', |
|
233
|
|
|
], |
|
234
|
|
|
(object) [ |
|
235
|
|
|
'propertyPath' => 'children[deep].children[deep].children[0].children[array]', |
|
236
|
|
|
'message' => 'This value is not valid.', |
|
237
|
|
|
], |
|
238
|
|
|
|
|
239
|
|
|
(object) [ |
|
240
|
|
|
'propertyPath' => 'children[deep].children[deep].children[1].children[array].children[0]', |
|
241
|
|
|
'message' => 'This value is not valid.', |
|
242
|
|
|
], |
|
243
|
|
|
], |
|
244
|
|
|
$client->getResults() |
|
245
|
|
|
); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* Assert fixture data |
|
250
|
|
|
* |
|
251
|
|
|
* @param object $data Fixture data |
|
252
|
|
|
* @return void |
|
253
|
|
|
* @throws \PHPUnit_Framework_AssertionFailedError |
|
254
|
|
|
*/ |
|
255
|
|
|
private function assertFixtureData($data) |
|
256
|
|
|
{ |
|
257
|
|
|
$this->assertEquals( |
|
258
|
|
|
(object) [ |
|
259
|
|
|
'id' => 'testdata', |
|
260
|
|
|
'field' => (object) [ |
|
261
|
|
|
'en' => 'EN-1', |
|
262
|
|
|
], |
|
263
|
|
|
'array' => [ |
|
264
|
|
|
(object) [ |
|
265
|
|
|
'en' => 'EN-2', |
|
266
|
|
|
], |
|
267
|
|
|
(object) [ |
|
268
|
|
|
'en' => 'EN-3', |
|
269
|
|
|
], |
|
270
|
|
|
], |
|
271
|
|
|
'deep' => (object) [ |
|
272
|
|
|
'deep' => [ |
|
273
|
|
|
(object) [ |
|
274
|
|
|
'field' => (object) [ |
|
275
|
|
|
'en' => 'EN-4', |
|
276
|
|
|
], |
|
277
|
|
|
'array' => [ |
|
278
|
|
|
(object) [ |
|
279
|
|
|
'en' => 'EN-5', |
|
280
|
|
|
], |
|
281
|
|
|
(object) [ |
|
282
|
|
|
'en' => 'EN-6', |
|
283
|
|
|
], |
|
284
|
|
|
], |
|
285
|
|
|
], |
|
286
|
|
|
(object) [ |
|
287
|
|
|
'field' => (object) [ |
|
288
|
|
|
'en' => 'EN-7', |
|
289
|
|
|
], |
|
290
|
|
|
'array' => [ |
|
291
|
|
|
(object) [ |
|
292
|
|
|
'en' => 'EN-8', |
|
293
|
|
|
], |
|
294
|
|
|
], |
|
295
|
|
|
], |
|
296
|
|
|
], |
|
297
|
|
|
], |
|
298
|
|
|
], |
|
299
|
|
|
$data |
|
300
|
|
|
); |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
/** |
|
304
|
|
|
* Assert item schema |
|
305
|
|
|
* |
|
306
|
|
|
* @param object $schema Item schema |
|
307
|
|
|
* @return void |
|
308
|
|
|
* @throws \PHPUnit_Framework_AssertionFailedError |
|
309
|
|
|
*/ |
|
310
|
|
|
private function assertItemSchema($schema) |
|
311
|
|
|
{ |
|
312
|
|
|
foreach ([ |
|
313
|
|
|
$schema->properties, |
|
314
|
|
|
$schema->properties->deep->properties->deep->items->properties, |
|
315
|
|
|
] as $schema) { |
|
316
|
|
|
$this->assertEquals('object', $schema->field->type); |
|
317
|
|
|
$this->assertEquals('string', $schema->field->properties->de->type); |
|
318
|
|
|
$this->assertEquals('string', $schema->field->properties->en->type); |
|
319
|
|
|
$this->assertEquals('string', $schema->field->properties->fr->type); |
|
320
|
|
|
|
|
321
|
|
|
$this->assertEquals('array', $schema->array->type); |
|
322
|
|
|
$this->assertEquals('object', $schema->array->items->type); |
|
323
|
|
|
$this->assertEquals('string', $schema->array->items->properties->de->type); |
|
324
|
|
|
$this->assertEquals('string', $schema->array->items->properties->en->type); |
|
325
|
|
|
$this->assertEquals('string', $schema->array->items->properties->fr->type); |
|
326
|
|
|
} |
|
327
|
|
|
} |
|
328
|
|
|
|
|
329
|
|
|
/** |
|
330
|
|
|
* Make a get request |
|
331
|
|
|
* |
|
332
|
|
|
* @param Client $client HTTP client |
|
333
|
|
|
* @param string $url URL |
|
334
|
|
|
* @param array $languages Languages |
|
335
|
|
|
* @return void |
|
336
|
|
|
*/ |
|
337
|
|
|
private function getRequest(Client $client, $url, array $languages = ['en']) |
|
338
|
|
|
{ |
|
339
|
|
|
$client->request('GET', $url, [], [], ['HTTP_ACCEPT_LANGUAGE' => implode(',', $languages)]); |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
|
|
/** |
|
343
|
|
|
* Get post JSON |
|
344
|
|
|
* |
|
345
|
|
|
* @return string |
|
346
|
|
|
*/ |
|
347
|
|
|
private function getPostData() |
|
348
|
|
|
{ |
|
349
|
|
|
return (object) [ |
|
350
|
|
|
'field' => (object) [ |
|
351
|
|
|
'en' => 'EN-10', |
|
352
|
|
|
'de' => 'DE-10', |
|
353
|
|
|
], |
|
354
|
|
|
'array' => [ |
|
355
|
|
|
(object) [ |
|
356
|
|
|
'en' => 'EN-20', |
|
357
|
|
|
'de' => 'DE-20', |
|
358
|
|
|
], |
|
359
|
|
|
], |
|
360
|
|
|
'deep' => (object) [ |
|
361
|
|
|
'deep' => [ |
|
362
|
|
|
(object) [ |
|
363
|
|
|
'field' => (object) [ |
|
364
|
|
|
'en' => 'EN-30', |
|
365
|
|
|
'de' => 'DE-30', |
|
366
|
|
|
], |
|
367
|
|
|
'array' => [ |
|
368
|
|
|
(object) [ |
|
369
|
|
|
'en' => 'EN-40', |
|
370
|
|
|
'de' => 'DE-40', |
|
371
|
|
|
], |
|
372
|
|
|
(object) [ |
|
373
|
|
|
'en' => 'EN-50', |
|
374
|
|
|
'de' => 'DE-50', |
|
375
|
|
|
], |
|
376
|
|
|
(object) [ |
|
377
|
|
|
'en' => 'EN-60', |
|
378
|
|
|
'de' => 'DE-60', |
|
379
|
|
|
], |
|
380
|
|
|
], |
|
381
|
|
|
], |
|
382
|
|
|
(object) [ |
|
383
|
|
|
'field' => (object) [ |
|
384
|
|
|
'en' => 'EN-70', |
|
385
|
|
|
'de' => 'DE-70', |
|
386
|
|
|
], |
|
387
|
|
|
'array' => [], |
|
388
|
|
|
], |
|
389
|
|
|
] |
|
390
|
|
|
] |
|
391
|
|
|
]; |
|
392
|
|
|
} |
|
393
|
|
|
} |
|
394
|
|
|
|
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.