|
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
|
|
|
], |
|
155
|
|
|
]; |
|
156
|
|
|
|
|
157
|
|
|
$client = static::createRestClient(); |
|
158
|
|
|
$client->post('/testcase/primitivearray/', $data); |
|
159
|
|
|
$this->assertEquals(Response::HTTP_CREATED, $client->getResponse()->getStatusCode()); |
|
160
|
|
|
$this->assertEmpty($client->getResults()); |
|
161
|
|
|
|
|
162
|
|
|
$location = $client->getResponse()->headers->get('Location'); |
|
163
|
|
|
|
|
164
|
|
|
$client = static::createRestClient(); |
|
165
|
|
|
$client->request('GET', $location); |
|
166
|
|
|
$this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode()); |
|
167
|
|
|
|
|
168
|
|
|
$result = $client->getResults(); |
|
169
|
|
|
$this->assertNotNull($result->id); |
|
170
|
|
|
unset($result->id); |
|
171
|
|
|
unset($data->rawData->hasharray[1]); |
|
172
|
|
|
unset($data->rawData->emptyhash); |
|
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
|
|
|
var_dump(json_encode($this->fixDateTimezone($data), JSON_PRETTY_PRINT)); |
|
|
|
|
|
|
219
|
|
|
die; |
|
|
|
|
|
|
220
|
|
|
|
|
221
|
|
|
$client = static::createRestClient(); |
|
222
|
|
|
$client->request('GET', '/testcase/primitivearray/testdata'); |
|
223
|
|
|
$this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode()); |
|
224
|
|
|
|
|
225
|
|
|
var_dump($client->getResults()->hasharray); |
|
226
|
|
|
|
|
227
|
|
|
die; |
|
|
|
|
|
|
228
|
|
|
|
|
229
|
|
|
$this->assertEquals($this->fixDateTimezone($data), $client->getResults()); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Test validation |
|
234
|
|
|
* |
|
235
|
|
|
* @return void |
|
236
|
|
|
*/ |
|
237
|
|
|
public function testValidation() |
|
238
|
|
|
{ |
|
239
|
|
|
$data = (object) [ |
|
240
|
|
|
'id' => 'testdata', |
|
241
|
|
|
|
|
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
|
|
|
'hash' => (object) [ |
|
249
|
|
|
'intarray' => [1, 'a'], |
|
250
|
|
|
'strarray' => ['a', false], |
|
251
|
|
|
'boolarray' => [true, 'a'], |
|
252
|
|
|
'hasharray' => [(object) ['x' => 'y'], 1.5], |
|
253
|
|
|
'datearray' => ['2015-10-03T22:32:00+0600', 'abc'], |
|
254
|
|
|
], |
|
255
|
|
|
|
|
256
|
|
|
'arrayhash' => [ |
|
257
|
|
|
(object) [ |
|
258
|
|
|
'intarray' => [1, 'a'], |
|
259
|
|
|
'strarray' => ['a', false], |
|
260
|
|
|
'boolarray' => [true, 'a'], |
|
261
|
|
|
'hasharray' => [(object) ['x' => 'y'], 1.5], |
|
262
|
|
|
'datearray' => ['2015-10-03T22:32:00+0600', 'abc'], |
|
263
|
|
|
] |
|
264
|
|
|
], |
|
265
|
|
|
]; |
|
266
|
|
|
|
|
267
|
|
|
$client = static::createRestClient(); |
|
268
|
|
|
$client->put('/testcase/primitivearray/testdata', $data); |
|
269
|
|
|
$this->assertEquals(Response::HTTP_BAD_REQUEST, $client->getResponse()->getStatusCode()); |
|
270
|
|
|
$this->assertNotNull($client->getResults()); |
|
271
|
|
|
|
|
272
|
|
|
$this->assertEquals( |
|
273
|
|
|
[ |
|
274
|
|
|
(object) [ |
|
275
|
|
|
'propertyPath' => 'intarray[1]', |
|
276
|
|
|
'message' => 'String value found, but an integer is required', |
|
277
|
|
|
], |
|
278
|
|
|
(object) [ |
|
279
|
|
|
'propertyPath' => 'strarray[1]', |
|
280
|
|
|
'message' => 'Boolean value found, but a string is required', |
|
281
|
|
|
], |
|
282
|
|
|
(object) [ |
|
283
|
|
|
'propertyPath' => 'boolarray[1]', |
|
284
|
|
|
'message' => 'String value found, but a boolean is required', |
|
285
|
|
|
], |
|
286
|
|
|
(object) [ |
|
287
|
|
|
'propertyPath' => 'datearray[1]', |
|
288
|
|
|
'message' => 'Invalid date-time "abc", expected format YYYY-MM-DDThh:mm:ssZ '. |
|
289
|
|
|
'or YYYY-MM-DDThh:mm:ss+hh:mm', |
|
290
|
|
|
], |
|
291
|
|
|
(object) [ |
|
292
|
|
|
'propertyPath' => 'hasharray[1]', |
|
293
|
|
|
'message' => 'Double value found, but an object is required', |
|
294
|
|
|
], |
|
295
|
|
|
(object) [ |
|
296
|
|
|
'propertyPath' => 'hash.intarray[1]', |
|
297
|
|
|
'message' => 'String value found, but an integer is required', |
|
298
|
|
|
], |
|
299
|
|
|
(object) [ |
|
300
|
|
|
'propertyPath' => 'hash.strarray[1]', |
|
301
|
|
|
'message' => 'Boolean value found, but a string is required', |
|
302
|
|
|
], |
|
303
|
|
|
(object) [ |
|
304
|
|
|
'propertyPath' => 'hash.boolarray[1]', |
|
305
|
|
|
'message' => 'String value found, but a boolean is required', |
|
306
|
|
|
], |
|
307
|
|
|
|
|
308
|
|
|
(object) [ |
|
309
|
|
|
'propertyPath' => 'hash.datearray[1]', |
|
310
|
|
|
'message' => 'Invalid date-time "abc", expected format YYYY-MM-DDThh:mm:ssZ '. |
|
311
|
|
|
'or YYYY-MM-DDThh:mm:ss+hh:mm', |
|
312
|
|
|
], |
|
313
|
|
|
(object) [ |
|
314
|
|
|
'propertyPath' => 'hash.hasharray[1]', |
|
315
|
|
|
'message' => 'Double value found, but an object is required', |
|
316
|
|
|
], |
|
317
|
|
|
(object) [ |
|
318
|
|
|
'propertyPath' => 'arrayhash[0].intarray[1]', |
|
319
|
|
|
'message' => 'String value found, but an integer is required', |
|
320
|
|
|
], |
|
321
|
|
|
(object) [ |
|
322
|
|
|
'propertyPath' => 'arrayhash[0].strarray[1]', |
|
323
|
|
|
'message' => 'Boolean value found, but a string is required', |
|
324
|
|
|
], |
|
325
|
|
|
(object) [ |
|
326
|
|
|
'propertyPath' => 'arrayhash[0].boolarray[1]', |
|
327
|
|
|
'message' => 'String value found, but a boolean is required', |
|
328
|
|
|
], |
|
329
|
|
|
(object) [ |
|
330
|
|
|
'propertyPath' => 'arrayhash[0].datearray[1]', |
|
331
|
|
|
'message' => 'Invalid date-time "abc", expected format YYYY-MM-DDThh:mm:ssZ '. |
|
332
|
|
|
'or YYYY-MM-DDThh:mm:ss+hh:mm', |
|
333
|
|
|
], |
|
334
|
|
|
(object) [ |
|
335
|
|
|
'propertyPath' => 'arrayhash[0].hasharray[1]', |
|
336
|
|
|
'message' => 'Double value found, but an object is required', |
|
337
|
|
|
] |
|
338
|
|
|
], |
|
339
|
|
|
$client->getResults() |
|
340
|
|
|
); |
|
341
|
|
|
} |
|
342
|
|
|
|
|
343
|
|
|
/** |
|
344
|
|
|
* Fix date timezone |
|
345
|
|
|
* |
|
346
|
|
|
* @param object $data Request data |
|
347
|
|
|
* @return object |
|
348
|
|
|
*/ |
|
349
|
|
|
private function fixDateTimezone($data) |
|
350
|
|
|
{ |
|
351
|
|
|
$converter = function (&$date) { |
|
352
|
|
|
$date = \DateTime::createFromFormat(self::DATE_FORMAT, $date) |
|
353
|
|
|
->setTimezone(new \DateTimeZone(date_default_timezone_get())) |
|
354
|
|
|
->format(self::DATE_FORMAT); |
|
355
|
|
|
}; |
|
356
|
|
|
|
|
357
|
|
|
array_walk($data->datearray, $converter); |
|
358
|
|
|
array_walk($data->hash->datearray, $converter); |
|
359
|
|
|
array_walk($data->arrayhash[0]->datearray, $converter); |
|
360
|
|
|
|
|
361
|
|
|
return $data; |
|
362
|
|
|
} |
|
363
|
|
|
|
|
364
|
|
|
|
|
365
|
|
|
/** |
|
366
|
|
|
* Assert fixture data |
|
367
|
|
|
* |
|
368
|
|
|
* @param object $data Fixture data |
|
369
|
|
|
* @return void |
|
370
|
|
|
* @throws \PHPUnit_Framework_AssertionFailedError |
|
371
|
|
|
*/ |
|
372
|
|
|
private function assertFixtureData($data) |
|
373
|
|
|
{ |
|
374
|
|
|
foreach ([ |
|
375
|
|
|
$data, |
|
376
|
|
|
$data->hash, |
|
377
|
|
|
$data->arrayhash[0], |
|
378
|
|
|
] as $data) { |
|
379
|
|
|
$this->assertInternalType('array', $data->intarray); |
|
380
|
|
|
foreach ($data->intarray as $value) { |
|
381
|
|
|
$this->assertInternalType('integer', $value); |
|
382
|
|
|
} |
|
383
|
|
|
|
|
384
|
|
|
$this->assertInternalType('array', $data->strarray); |
|
385
|
|
|
foreach ($data->strarray as $value) { |
|
386
|
|
|
$this->assertInternalType('string', $value); |
|
387
|
|
|
} |
|
388
|
|
|
|
|
389
|
|
|
$this->assertInternalType('array', $data->boolarray); |
|
390
|
|
|
foreach ($data->boolarray as $value) { |
|
391
|
|
|
$this->assertInternalType('boolean', $value); |
|
392
|
|
|
} |
|
393
|
|
|
|
|
394
|
|
|
$this->assertInternalType('array', $data->datearray); |
|
395
|
|
|
foreach ($data->datearray as $value) { |
|
396
|
|
|
$this->assertInternalType('string', $value); |
|
397
|
|
|
$this->assertInstanceOf(\DateTime::class, \DateTime::createFromFormat(self::DATE_FORMAT, $value)); |
|
398
|
|
|
} |
|
399
|
|
|
|
|
400
|
|
|
$this->assertInternalType('array', $data->hasharray); |
|
401
|
|
|
foreach ($data->hasharray as $value) { |
|
402
|
|
|
$this->assertInternalType('object', $value); |
|
403
|
|
|
} |
|
404
|
|
|
} |
|
405
|
|
|
} |
|
406
|
|
|
|
|
407
|
|
|
/** |
|
408
|
|
|
* Assert item schema |
|
409
|
|
|
* |
|
410
|
|
|
* @param object $schema Item schema |
|
411
|
|
|
* @return void |
|
412
|
|
|
* @throws \PHPUnit_Framework_AssertionFailedError |
|
413
|
|
|
*/ |
|
414
|
|
|
private function assertItemSchema($schema) |
|
415
|
|
|
{ |
|
416
|
|
|
foreach ([ |
|
417
|
|
|
$schema->properties, |
|
418
|
|
|
$schema->properties->hash->properties, |
|
419
|
|
|
$schema->properties->arrayhash->items->properties, |
|
420
|
|
|
] as $schema) { |
|
421
|
|
|
$this->assertEquals('array', $schema->intarray->type); |
|
422
|
|
|
$this->assertEquals('integer', $schema->intarray->items->type); |
|
423
|
|
|
|
|
424
|
|
|
$this->assertEquals('array', $schema->strarray->type); |
|
425
|
|
|
$this->assertEquals('string', $schema->strarray->items->type); |
|
426
|
|
|
|
|
427
|
|
|
$this->assertEquals('array', $schema->boolarray->type); |
|
428
|
|
|
$this->assertEquals('boolean', $schema->boolarray->items->type); |
|
429
|
|
|
|
|
430
|
|
|
$this->assertEquals('array', $schema->datearray->type); |
|
431
|
|
|
$this->assertEquals('string', $schema->datearray->items->type); |
|
432
|
|
|
$this->assertEquals('date-time', $schema->datearray->items->format); |
|
433
|
|
|
|
|
434
|
|
|
$this->assertEquals('array', $schema->hasharray->type); |
|
435
|
|
|
$this->assertEquals('object', $schema->hasharray->items->type); |
|
436
|
|
|
} |
|
437
|
|
|
} |
|
438
|
|
|
} |
|
439
|
|
|
|