1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PrimitiveArrayControllerTest class file |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Graviton\CoreBundle\Tests\Controller; |
7
|
|
|
|
8
|
|
|
use Graviton\TestBundle\Test\RestTestCase; |
9
|
|
|
use Symfony\Component\HttpFoundation\Response; |
10
|
|
|
use GravitonDyn\TestCasePrimitiveArrayBundle\DataFixtures\MongoDB\LoadTestCasePrimitiveArrayData; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
14
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
15
|
|
|
* @link http://swisscom.ch |
16
|
|
|
*/ |
17
|
|
|
class PrimitiveArrayControllerTest extends RestTestCase |
18
|
|
|
{ |
19
|
|
|
const DATE_FORMAT = 'Y-m-d\\TH:i:sO'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* load fixtures |
23
|
|
|
* |
24
|
|
|
* @return void |
25
|
|
|
*/ |
26
|
|
|
public function setUp() |
27
|
|
|
{ |
28
|
|
|
if (!class_exists(LoadTestCasePrimitiveArrayData::class)) { |
29
|
|
|
$this->markTestSkipped('TestCasePrimitiveArray definition is not loaded'); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$this->loadFixtures( |
33
|
|
|
[LoadTestCasePrimitiveArrayData::class], |
34
|
|
|
null, |
35
|
|
|
'doctrine_mongodb' |
36
|
|
|
); |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
/********* |
40
|
|
|
* FIXES IN FIXTURES |
41
|
|
|
* Sadly - for this new approach to work, we need to fix our fixtures.. |
42
|
|
|
* they get generated wrong ({} converted to []), which leads to wrong data in db) |
43
|
|
|
*/ |
44
|
|
|
$client = static::createRestClient(); |
45
|
|
|
$client->request('GET', '/testcase/primitivearray/testdata'); |
46
|
|
|
$object = $client->getResults(); |
47
|
|
|
$object->hasharray[2] = new \stdClass(); |
48
|
|
|
$object->arrayhash[0]->hasharray[2] = new \stdClass(); |
49
|
|
|
$object->hash->hasharray[2] = new \stdClass(); |
50
|
|
|
$client = static::createRestClient(); |
51
|
|
|
$client->put('/testcase/primitivearray/testdata', $object); |
52
|
|
|
$this->assertEmpty($client->getResponse()->getContent()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Test item schema |
57
|
|
|
* |
58
|
|
|
* @return void |
59
|
|
|
*/ |
60
|
|
View Code Duplication |
public function testItemSchema() |
61
|
|
|
{ |
62
|
|
|
$client = static::createRestClient(); |
63
|
|
|
$client->request('GET', '/schema/testcase/primitivearray/item'); |
64
|
|
|
$this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode()); |
65
|
|
|
|
66
|
|
|
$schema = $client->getResults(); |
67
|
|
|
$this->assertEquals('object', $schema->type); |
68
|
|
|
$this->assertItemSchema($schema); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Test collection schema |
73
|
|
|
* |
74
|
|
|
* @return void |
75
|
|
|
*/ |
76
|
|
View Code Duplication |
public function testCollectionSchema() |
77
|
|
|
{ |
78
|
|
|
$client = static::createRestClient(); |
79
|
|
|
$client->request('GET', '/schema/testcase/primitivearray/collection'); |
80
|
|
|
$this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode()); |
81
|
|
|
|
82
|
|
|
$schema = $client->getResults(); |
83
|
|
|
$this->assertEquals('array', $schema->type); |
84
|
|
|
$this->assertEquals('object', $schema->items->type); |
85
|
|
|
$this->assertItemSchema($schema->items); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Test GET one method |
90
|
|
|
* |
91
|
|
|
* @return void |
92
|
|
|
*/ |
93
|
|
View Code Duplication |
public function testCheckGetOne() |
94
|
|
|
{ |
95
|
|
|
$client = static::createRestClient(); |
96
|
|
|
$client->request('GET', '/testcase/primitivearray/testdata'); |
97
|
|
|
$this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode()); |
98
|
|
|
$this->assertNotEmpty($client->getResults()); |
99
|
|
|
|
100
|
|
|
$this->assertFixtureData($client->getResults()); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Test GET all method |
105
|
|
|
* |
106
|
|
|
* @return void |
107
|
|
|
*/ |
108
|
|
|
public function testCheckGetAll() |
109
|
|
|
{ |
110
|
|
|
$client = static::createRestClient(); |
111
|
|
|
$client->request('GET', '/testcase/primitivearray/'); |
112
|
|
|
$this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode()); |
113
|
|
|
$this->assertCount(1, $client->getResults()); |
114
|
|
|
|
115
|
|
|
$this->assertFixtureData($client->getResults()[0]); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Test POST method |
120
|
|
|
* |
121
|
|
|
* @return void |
122
|
|
|
*/ |
123
|
|
|
public function testPostMethod() |
124
|
|
|
{ |
125
|
|
|
$data = (object) [ |
126
|
|
|
'intarray' => [10, 20], |
127
|
|
|
'strarray' => ['a', 'b'], |
128
|
|
|
'boolarray' => [true, false], |
129
|
|
|
'hasharray' => [(object) ['x' => 'y'], (object) []], |
130
|
|
|
'datearray' => ['2015-09-30T23:59:59+0000', '2015-10-01T00:00:01+0300'], |
131
|
|
|
|
132
|
|
|
'hash' => (object) [ |
133
|
|
|
'intarray' => [10, 20], |
134
|
|
|
'strarray' => ['a', 'b'], |
135
|
|
|
'boolarray' => [true, false], |
136
|
|
|
'hasharray' => [(object) ['x' => 'y'], (object) []], |
137
|
|
|
'datearray' => ['2015-09-30T23:59:59+0000', '2015-10-01T00:00:01+0300'], |
138
|
|
|
], |
139
|
|
|
|
140
|
|
|
'arrayhash' => [ |
141
|
|
|
(object) [ |
142
|
|
|
'intarray' => [10, 20], |
143
|
|
|
'strarray' => ['a', 'b'], |
144
|
|
|
'boolarray' => [true, false], |
145
|
|
|
'hasharray' => [(object) ['x' => 'y'], (object) []], |
146
|
|
|
'datearray' => ['2015-09-30T23:59:59+0000', '2015-10-01T00:00:01+0300'], |
147
|
|
|
] |
148
|
|
|
], |
149
|
|
|
|
150
|
|
|
'rawData' => (object) [ |
151
|
|
|
'hasharray' => [(object) ['x' => 'y'], (object) []], |
152
|
|
|
'emptyhash' => (object) [], |
153
|
|
|
'emptystring' => "", |
154
|
|
|
'emptyarray' => [], |
155
|
|
|
'emptyarrayhash' => [ (object) [] ] |
156
|
|
|
], |
157
|
|
|
]; |
158
|
|
|
|
159
|
|
|
$client = static::createRestClient(); |
160
|
|
|
$client->post('/testcase/primitivearray/', $data); |
161
|
|
|
$this->assertEquals(Response::HTTP_CREATED, $client->getResponse()->getStatusCode()); |
162
|
|
|
$this->assertEmpty($client->getResults()); |
163
|
|
|
|
164
|
|
|
$location = $client->getResponse()->headers->get('Location'); |
165
|
|
|
|
166
|
|
|
$client = static::createRestClient(); |
167
|
|
|
$client->request('GET', $location); |
168
|
|
|
$this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode()); |
169
|
|
|
|
170
|
|
|
$result = $client->getResults(); |
171
|
|
|
$this->assertNotNull($result->id); |
172
|
|
|
unset($result->id); |
173
|
|
|
$this->assertEquals($this->fixDateTimezone($data), $result); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Test PUT method |
178
|
|
|
* |
179
|
|
|
* @return void |
180
|
|
|
*/ |
181
|
|
|
public function testPutMethod() |
182
|
|
|
{ |
183
|
|
|
$data = (object) [ |
184
|
|
|
'id' => 'testdata', |
185
|
|
|
|
186
|
|
|
'intarray' => [10, 20], |
187
|
|
|
'strarray' => ['a', 'b'], |
188
|
|
|
'boolarray' => [true, false], |
189
|
|
|
'hasharray' => [(object) ['x' => 'y'], (object) []], |
190
|
|
|
'datearray' => ['2015-09-30T23:59:59+0000', '2015-10-01T00:00:01+0300'], |
191
|
|
|
|
192
|
|
|
'hash' => (object) [ |
193
|
|
|
'intarray' => [10, 20], |
194
|
|
|
'strarray' => ['a', 'b'], |
195
|
|
|
'boolarray' => [true, false], |
196
|
|
|
'hasharray' => [(object) ['x' => 'y'], (object) []], |
197
|
|
|
'datearray' => ['2015-09-30T23:59:59+0000', '2015-10-01T00:00:01+0300'], |
198
|
|
|
], |
199
|
|
|
|
200
|
|
|
'arrayhash' => [ |
201
|
|
|
(object) [ |
202
|
|
|
'intarray' => [10, 20], |
203
|
|
|
'strarray' => ['a', 'b'], |
204
|
|
|
'boolarray' => [true, false], |
205
|
|
|
'hasharray' => [(object) ['x' => 'y'], (object) []], |
206
|
|
|
'datearray' => ['2015-09-30T23:59:59+0000', '2015-10-01T00:00:01+0300'], |
207
|
|
|
] |
208
|
|
|
], |
209
|
|
|
|
210
|
|
|
'rawData' => (object) [], |
211
|
|
|
]; |
212
|
|
|
|
213
|
|
|
$client = static::createRestClient(); |
214
|
|
|
$client->put('/testcase/primitivearray/testdata', $data); |
215
|
|
|
$this->assertEquals(Response::HTTP_NO_CONTENT, $client->getResponse()->getStatusCode()); |
216
|
|
|
$this->assertEmpty($client->getResults()); |
217
|
|
|
|
218
|
|
|
$client = static::createRestClient(); |
219
|
|
|
$client->request('GET', '/testcase/primitivearray/testdata'); |
220
|
|
|
$this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode()); |
221
|
|
|
|
222
|
|
|
$this->assertEquals($this->fixDateTimezone($data), $client->getResults()); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Test validation |
227
|
|
|
* |
228
|
|
|
* @return void |
229
|
|
|
*/ |
230
|
|
|
public function testValidation() |
231
|
|
|
{ |
232
|
|
|
$data = (object) [ |
233
|
|
|
'id' => 'testdata', |
234
|
|
|
|
235
|
|
|
'intarray' => [1, 'a'], |
236
|
|
|
'strarray' => ['a', false], |
237
|
|
|
'boolarray' => [true, 'a'], |
238
|
|
|
'hasharray' => [(object) ['x' => 'y'], 1.5], |
239
|
|
|
'datearray' => ['2015-10-03T22:32:00+0600', 'abc'], |
240
|
|
|
|
241
|
|
|
'hash' => (object) [ |
242
|
|
|
'intarray' => [1, 'a'], |
243
|
|
|
'strarray' => ['a', false], |
244
|
|
|
'boolarray' => [true, 'a'], |
245
|
|
|
'hasharray' => [(object) ['x' => 'y'], 1.5], |
246
|
|
|
'datearray' => ['2015-10-03T22:32:00+0600', 'abc'], |
247
|
|
|
], |
248
|
|
|
|
249
|
|
|
'arrayhash' => [ |
250
|
|
|
(object) [ |
251
|
|
|
'intarray' => [1, 'a'], |
252
|
|
|
'strarray' => ['a', false], |
253
|
|
|
'boolarray' => [true, 'a'], |
254
|
|
|
'hasharray' => [(object) ['x' => 'y'], 1.5], |
255
|
|
|
'datearray' => ['2015-10-03T22:32:00+0600', 'abc'], |
256
|
|
|
] |
257
|
|
|
], |
258
|
|
|
]; |
259
|
|
|
|
260
|
|
|
$client = static::createRestClient(); |
261
|
|
|
$client->put('/testcase/primitivearray/testdata', $data); |
262
|
|
|
$this->assertEquals(Response::HTTP_BAD_REQUEST, $client->getResponse()->getStatusCode()); |
263
|
|
|
$this->assertNotNull($client->getResults()); |
264
|
|
|
|
265
|
|
|
$this->assertEquals( |
266
|
|
|
[ |
267
|
|
|
(object) [ |
268
|
|
|
'propertyPath' => 'intarray[1]', |
269
|
|
|
'message' => 'String value found, but an integer is required', |
270
|
|
|
], |
271
|
|
|
(object) [ |
272
|
|
|
'propertyPath' => 'strarray[1]', |
273
|
|
|
'message' => 'Boolean value found, but a string is required', |
274
|
|
|
], |
275
|
|
|
(object) [ |
276
|
|
|
'propertyPath' => 'boolarray[1]', |
277
|
|
|
'message' => 'String value found, but a boolean is required', |
278
|
|
|
], |
279
|
|
|
(object) [ |
280
|
|
|
'propertyPath' => 'datearray[1]', |
281
|
|
|
'message' => 'Invalid date-time "abc", expected format YYYY-MM-DDThh:mm:ssZ '. |
282
|
|
|
'or YYYY-MM-DDThh:mm:ss+hh:mm', |
283
|
|
|
], |
284
|
|
|
(object) [ |
285
|
|
|
'propertyPath' => 'hasharray[1]', |
286
|
|
|
'message' => 'Double value found, but an object is required', |
287
|
|
|
], |
288
|
|
|
(object) [ |
289
|
|
|
'propertyPath' => 'hash.intarray[1]', |
290
|
|
|
'message' => 'String value found, but an integer is required', |
291
|
|
|
], |
292
|
|
|
(object) [ |
293
|
|
|
'propertyPath' => 'hash.strarray[1]', |
294
|
|
|
'message' => 'Boolean value found, but a string is required', |
295
|
|
|
], |
296
|
|
|
(object) [ |
297
|
|
|
'propertyPath' => 'hash.boolarray[1]', |
298
|
|
|
'message' => 'String value found, but a boolean is required', |
299
|
|
|
], |
300
|
|
|
|
301
|
|
|
(object) [ |
302
|
|
|
'propertyPath' => 'hash.datearray[1]', |
303
|
|
|
'message' => 'Invalid date-time "abc", expected format YYYY-MM-DDThh:mm:ssZ '. |
304
|
|
|
'or YYYY-MM-DDThh:mm:ss+hh:mm', |
305
|
|
|
], |
306
|
|
|
(object) [ |
307
|
|
|
'propertyPath' => 'hash.hasharray[1]', |
308
|
|
|
'message' => 'Double value found, but an object is required', |
309
|
|
|
], |
310
|
|
|
(object) [ |
311
|
|
|
'propertyPath' => 'arrayhash[0].intarray[1]', |
312
|
|
|
'message' => 'String value found, but an integer is required', |
313
|
|
|
], |
314
|
|
|
(object) [ |
315
|
|
|
'propertyPath' => 'arrayhash[0].strarray[1]', |
316
|
|
|
'message' => 'Boolean value found, but a string is required', |
317
|
|
|
], |
318
|
|
|
(object) [ |
319
|
|
|
'propertyPath' => 'arrayhash[0].boolarray[1]', |
320
|
|
|
'message' => 'String value found, but a boolean is required', |
321
|
|
|
], |
322
|
|
|
(object) [ |
323
|
|
|
'propertyPath' => 'arrayhash[0].datearray[1]', |
324
|
|
|
'message' => 'Invalid date-time "abc", expected format YYYY-MM-DDThh:mm:ssZ '. |
325
|
|
|
'or YYYY-MM-DDThh:mm:ss+hh:mm', |
326
|
|
|
], |
327
|
|
|
(object) [ |
328
|
|
|
'propertyPath' => 'arrayhash[0].hasharray[1]', |
329
|
|
|
'message' => 'Double value found, but an object is required', |
330
|
|
|
] |
331
|
|
|
], |
332
|
|
|
$client->getResults() |
333
|
|
|
); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* Fix date timezone |
338
|
|
|
* |
339
|
|
|
* @param object $data Request data |
340
|
|
|
* @return object |
341
|
|
|
*/ |
342
|
|
|
private function fixDateTimezone($data) |
343
|
|
|
{ |
344
|
|
|
$converter = function (&$date) { |
345
|
|
|
$date = \DateTime::createFromFormat(self::DATE_FORMAT, $date) |
346
|
|
|
->setTimezone(new \DateTimeZone(date_default_timezone_get())) |
347
|
|
|
->format(self::DATE_FORMAT); |
348
|
|
|
}; |
349
|
|
|
|
350
|
|
|
array_walk($data->datearray, $converter); |
351
|
|
|
array_walk($data->hash->datearray, $converter); |
352
|
|
|
array_walk($data->arrayhash[0]->datearray, $converter); |
353
|
|
|
|
354
|
|
|
return $data; |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* Assert fixture data |
360
|
|
|
* |
361
|
|
|
* @param object $data Fixture data |
362
|
|
|
* @return void |
363
|
|
|
* @throws \PHPUnit_Framework_AssertionFailedError |
364
|
|
|
*/ |
365
|
|
|
private function assertFixtureData($data) |
366
|
|
|
{ |
367
|
|
|
foreach ([ |
368
|
|
|
$data, |
369
|
|
|
$data->hash, |
370
|
|
|
$data->arrayhash[0], |
371
|
|
|
] as $data) { |
372
|
|
|
$this->assertInternalType('array', $data->intarray); |
373
|
|
|
foreach ($data->intarray as $value) { |
374
|
|
|
$this->assertInternalType('integer', $value); |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
$this->assertInternalType('array', $data->strarray); |
378
|
|
|
foreach ($data->strarray as $value) { |
379
|
|
|
$this->assertInternalType('string', $value); |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
$this->assertInternalType('array', $data->boolarray); |
383
|
|
|
foreach ($data->boolarray as $value) { |
384
|
|
|
$this->assertInternalType('boolean', $value); |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
$this->assertInternalType('array', $data->datearray); |
388
|
|
|
foreach ($data->datearray as $value) { |
389
|
|
|
$this->assertInternalType('string', $value); |
390
|
|
|
$this->assertInstanceOf(\DateTime::class, \DateTime::createFromFormat(self::DATE_FORMAT, $value)); |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
$this->assertInternalType('array', $data->hasharray); |
394
|
|
|
foreach ($data->hasharray as $value) { |
395
|
|
|
$this->assertInternalType('object', $value); |
396
|
|
|
} |
397
|
|
|
} |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
/** |
401
|
|
|
* Assert item schema |
402
|
|
|
* |
403
|
|
|
* @param object $schema Item schema |
404
|
|
|
* @return void |
405
|
|
|
* @throws \PHPUnit_Framework_AssertionFailedError |
406
|
|
|
*/ |
407
|
|
|
private function assertItemSchema($schema) |
408
|
|
|
{ |
409
|
|
|
foreach ([ |
410
|
|
|
$schema->properties, |
411
|
|
|
$schema->properties->hash->properties, |
412
|
|
|
$schema->properties->arrayhash->items->properties, |
413
|
|
|
] as $schema) { |
414
|
|
|
$this->assertEquals('array', $schema->intarray->type); |
415
|
|
|
$this->assertEquals('integer', $schema->intarray->items->type); |
416
|
|
|
|
417
|
|
|
$this->assertEquals('array', $schema->strarray->type); |
418
|
|
|
$this->assertEquals('string', $schema->strarray->items->type); |
419
|
|
|
|
420
|
|
|
$this->assertEquals('array', $schema->boolarray->type); |
421
|
|
|
$this->assertEquals('boolean', $schema->boolarray->items->type); |
422
|
|
|
|
423
|
|
|
$this->assertEquals('array', $schema->datearray->type); |
424
|
|
|
$this->assertEquals('string', $schema->datearray->items->type); |
425
|
|
|
$this->assertEquals('date-time', $schema->datearray->items->format); |
426
|
|
|
|
427
|
|
|
$this->assertEquals('array', $schema->hasharray->type); |
428
|
|
|
$this->assertEquals('object', $schema->hasharray->items->type); |
429
|
|
|
} |
430
|
|
|
} |
431
|
|
|
} |
432
|
|
|
|