1
|
|
|
<?php |
2
|
|
|
namespace Ajir\RabbitMqSqlBundle\Tests\DataTransformer; |
3
|
|
|
|
4
|
|
|
use Ajir\RabbitMqSqlBundle\DataMapper\DataMapper; |
5
|
|
|
use Ajir\RabbitMqSqlBundle\DataTransformer\DataTransformer; |
6
|
|
|
use Ajir\RabbitMqSqlBundle\DataValidator\DataValidator; |
7
|
|
|
use Ajir\RabbitMqSqlBundle\Model\Entity; |
8
|
|
|
use PHPUnit_Framework_TestCase; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class DataTransformerTest |
12
|
|
|
* |
13
|
|
|
* @author Florian Ajir <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class DataTransformerTest extends PHPUnit_Framework_TestCase |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The simple mapping fixture |
20
|
|
|
* |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $mapping; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The relationnal mapping fixture |
27
|
|
|
* |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected $relationnalMapping; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The relationnal collection mapping fixture |
34
|
|
|
* |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
protected $relationnalCollectionMapping; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The message data |
41
|
|
|
* |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
protected $data; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* |
48
|
|
|
*/ |
49
|
|
|
public function testPrepare() |
50
|
|
|
{ |
51
|
|
|
$mapper = new DataMapper($this->mapping); |
52
|
|
|
$dataTransformer = new DataTransformer($mapper); |
53
|
|
|
$data = array( |
54
|
|
|
'identifier' => '1', |
55
|
|
|
'label' => 'label_de_test', |
56
|
|
|
'amount' => '10.01', |
57
|
|
|
'birthday' => '1989-11-10', |
58
|
|
|
'subscribe' => '2015-01-02T09:00:00+0200' |
59
|
|
|
); |
60
|
|
|
$expected = array( |
61
|
|
|
'user' => array( |
62
|
|
|
'id' => '1', |
63
|
|
|
'name' => 'label_de_test', |
64
|
|
|
'amount' => '10.01', |
65
|
|
|
'birthdate' => '1989-11-10', |
66
|
|
|
'created_at' => '2015-01-02T09:00:00+0200', |
67
|
|
|
'_identifier' => 'id', |
68
|
|
|
'_table' => 'users' |
69
|
|
|
) |
70
|
|
|
); |
71
|
|
|
$this->assertEquals($expected, $dataTransformer->prepare('user', $data)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* |
76
|
|
|
*/ |
77
|
|
|
public function testPrepareMissingNotNullableProperty() |
78
|
|
|
{ |
79
|
|
|
$mapper = new DataMapper($this->mapping); |
80
|
|
|
$validator = new DataValidator(); |
81
|
|
|
$dataTransformer = new DataTransformer($mapper, $validator); |
82
|
|
|
$data = array( |
83
|
|
|
'label' => 'label_de_test', |
84
|
|
|
'amount' => '10.01', |
85
|
|
|
'birthday' => '1989-11-10', |
86
|
|
|
'subscribe' => '2015-01-02T09:00:00+0200' |
87
|
|
|
); |
88
|
|
|
$this->setExpectedException('InvalidArgumentException', 'user.identifier is not nullable.'); |
89
|
|
|
$dataTransformer->prepare('user', $data); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* |
94
|
|
|
*/ |
95
|
|
|
public function testPrepareMissingNullableProperty() |
96
|
|
|
{ |
97
|
|
|
$mapper = new DataMapper($this->mapping); |
98
|
|
|
$validator = new DataValidator(); |
99
|
|
|
$dataTransformer = new DataTransformer($mapper, $validator); |
100
|
|
|
$data = array( |
101
|
|
|
'identifier' => '1234567', |
102
|
|
|
); |
103
|
|
|
$result = $dataTransformer->prepare('user', $data); |
104
|
|
|
$this->assertEquals( |
105
|
|
|
array( |
106
|
|
|
'user' => array( |
107
|
|
|
'_identifier' => "id", |
108
|
|
|
'_table' => "users", |
109
|
|
|
'id' => "1234567" |
110
|
|
|
) |
111
|
|
|
), |
112
|
|
|
$result |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* |
118
|
|
|
*/ |
119
|
|
|
public function testPrepareWrongType() |
120
|
|
|
{ |
121
|
|
|
$mapper = new DataMapper($this->mapping); |
122
|
|
|
$validator = new DataValidator(); |
123
|
|
|
$dataTransformer = new DataTransformer($mapper, $validator); |
124
|
|
|
|
125
|
|
|
$data = array( |
126
|
|
|
'identifier' => '1', |
127
|
|
|
'amount' => '10,01' |
128
|
|
|
); |
129
|
|
|
$this->setExpectedException('InvalidArgumentException', 'user.amount type not valid.'); |
130
|
|
|
$dataTransformer->prepare('user', $data); |
131
|
|
|
$data = array( |
132
|
|
|
'identifier' => '1', |
133
|
|
|
'birthday' => '11/10/1989' |
134
|
|
|
); |
135
|
|
|
$this->setExpectedException('InvalidArgumentException', 'user.birthday type not valid.'); |
136
|
|
|
$dataTransformer->prepare('user', $data); |
137
|
|
|
$data = array( |
138
|
|
|
'identifier' => '1', |
139
|
|
|
'subscribe' => '2015-01-02' |
140
|
|
|
); |
141
|
|
|
$this->setExpectedException('InvalidArgumentException', 'user.subscribe type not valid.'); |
142
|
|
|
$dataTransformer->prepare('user', $data); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* |
147
|
|
|
*/ |
148
|
|
|
public function testPrepareLengthExceed() |
149
|
|
|
{ |
150
|
|
|
$mapper = new DataMapper($this->mapping); |
151
|
|
|
$validator = new DataValidator(); |
152
|
|
|
$dataTransformer = new DataTransformer($mapper, $validator); |
153
|
|
|
|
154
|
|
|
$data = array( |
155
|
|
|
'identifier' => '123456789' |
156
|
|
|
); |
157
|
|
|
$this->setExpectedException( |
158
|
|
|
'InvalidArgumentException', |
159
|
|
|
'user.identifier value length exceed database max length.' |
160
|
|
|
); |
161
|
|
|
$dataTransformer->prepare('user', $data); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* |
166
|
|
|
*/ |
167
|
|
|
public function testPrepareRelationnal() |
168
|
|
|
{ |
169
|
|
|
$mapper = new DataMapper($this->relationnalMapping); |
170
|
|
|
$dataTransformer = new DataTransformer($mapper); |
171
|
|
|
$data = array( |
172
|
|
|
'identifier' => '1', |
173
|
|
|
'label' => 'label_de_test', |
174
|
|
|
'Address' => array( |
175
|
|
|
'identifier' => '2', |
176
|
|
|
'postal_code' => '34000', |
177
|
|
|
'city' => 'Montpellier' |
178
|
|
|
) |
179
|
|
|
); |
180
|
|
|
$expected = array( |
181
|
|
|
'User' => array( |
182
|
|
|
'sku' => '1', |
183
|
|
|
'_identifier' => 'sku', |
184
|
|
|
'_table' => 'users', |
185
|
|
|
'_related' => array( |
186
|
|
|
'manyToOne' => array( |
187
|
|
|
'Address' => array( |
188
|
|
|
'_relation' => array( |
189
|
|
|
'targetEntity' => 'Address', |
190
|
|
|
'joinColumn' => array( |
191
|
|
|
'name' => 'address_id', |
192
|
|
|
'referencedColumnName' => 'id' |
193
|
|
|
), |
194
|
|
|
'table' => 'address' |
195
|
|
|
), |
196
|
|
|
'_data' => array( |
197
|
|
|
'Address' => array( |
198
|
|
|
'_identifier' => 'sku', |
199
|
|
|
'_table' => 'address', |
200
|
|
|
'sku' => '2', |
201
|
|
|
'postal_code' => '34000', |
202
|
|
|
'city' => 'Montpellier' |
203
|
|
|
) |
204
|
|
|
) |
205
|
|
|
) |
206
|
|
|
) |
207
|
|
|
) |
208
|
|
|
) |
209
|
|
|
); |
210
|
|
|
$result = $dataTransformer->prepare('User', $data); |
211
|
|
|
$this->assertEquals($expected, $result); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* |
216
|
|
|
*/ |
217
|
|
|
public function testPrepareWithDiscriminator() |
218
|
|
|
{ |
219
|
|
|
$mapper = new DataMapper(array( |
220
|
|
|
'supplier' => array( |
221
|
|
|
'discriminator' => 'dtype', |
222
|
|
|
'fields' => array( |
223
|
|
|
'sku' => array( |
224
|
|
|
'column' => 'sku', |
225
|
|
|
'type' => 'string', |
226
|
|
|
) |
227
|
|
|
) |
228
|
|
|
) |
229
|
|
|
)); |
230
|
|
|
$dataTransformer = new DataTransformer($mapper); |
231
|
|
|
$data = array( |
232
|
|
|
'sku' => '1234567', |
233
|
|
|
'dtype' => 'brand' |
234
|
|
|
); |
235
|
|
|
$expected = array( |
236
|
|
|
'supplier' => array( |
237
|
|
|
'sku' => '1234567', |
238
|
|
|
'_table' => 'brand' |
239
|
|
|
) |
240
|
|
|
); |
241
|
|
|
$result = $dataTransformer->prepare('supplier', $data); |
242
|
|
|
$this->assertEquals($expected, $result); |
243
|
|
|
|
244
|
|
|
$entity = new Entity($result['supplier']); |
245
|
|
|
$this->assertEquals('brand', $entity->getTable()); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* |
250
|
|
|
*/ |
251
|
|
|
public function testValidateLengthExceed() |
252
|
|
|
{ |
253
|
|
|
$mapper = new DataMapper(array( |
254
|
|
|
'user' => array( |
255
|
|
|
'table' => 'user', |
256
|
|
|
'fields' => array( |
257
|
|
|
'sku' => array( |
258
|
|
|
'column' => 'sku', |
259
|
|
|
'type' => 'string', |
260
|
|
|
'length' => '7' |
261
|
|
|
) |
262
|
|
|
) |
263
|
|
|
) |
264
|
|
|
)); |
265
|
|
|
$dataTransformer = new DataTransformer($mapper, new DataValidator()); |
266
|
|
|
$data = array( |
267
|
|
|
'sku' => '12345678' |
268
|
|
|
); |
269
|
|
|
$this->setExpectedException('InvalidArgumentException'); |
270
|
|
|
$dataTransformer->prepare('user', $data); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* |
275
|
|
|
*/ |
276
|
|
|
public function testPrepareRelationnalCollection() |
277
|
|
|
{ |
278
|
|
|
$mapper = new DataMapper($this->relationnalCollectionMapping); |
279
|
|
|
$dataTransformer = new DataTransformer($mapper); |
280
|
|
|
$data = array( |
281
|
|
|
'sku' => '1234567', |
282
|
|
|
'fr' => array( |
283
|
|
|
array( |
284
|
|
|
'name' => 'test' |
285
|
|
|
) |
286
|
|
|
) |
287
|
|
|
); |
288
|
|
|
$expected = array( |
289
|
|
|
'selection' => array( |
290
|
|
|
'sku' => '1234567', |
291
|
|
|
'_identifier' => 'sku', |
292
|
|
|
'_table' => 'selection', |
293
|
|
|
'_related' => array( |
294
|
|
|
'oneToMany' => array( |
295
|
|
|
'selection_lang' => array( |
296
|
|
|
'_relation' => array( |
297
|
|
|
'targetEntity' => 'selection_lang', |
298
|
|
|
'joinColumn' => array( |
299
|
|
|
'referencedColumnName' => 'id', |
300
|
|
|
'name' => 'selection_id', |
301
|
|
|
), |
302
|
|
|
'references' => array( |
303
|
|
|
'lang_id' => array( |
304
|
|
|
'table' => 'lang', |
305
|
|
|
'referencedColumnName' => 'id', |
306
|
|
|
'where' => array( |
307
|
|
|
'iso_code' => 'fr' |
308
|
|
|
) |
309
|
|
|
) |
310
|
|
|
), |
311
|
|
|
'table' => 'selection_lang' |
312
|
|
|
), |
313
|
|
|
'_data' => array( |
314
|
|
|
array( |
315
|
|
|
'selection_lang' => array( |
316
|
|
|
'_table' => 'selection_lang', |
317
|
|
|
'_identifier' => 'name', |
318
|
|
|
'name' => 'test', |
319
|
|
|
) |
320
|
|
|
) |
321
|
|
|
) |
322
|
|
|
) |
323
|
|
|
) |
324
|
|
|
) |
325
|
|
|
) |
326
|
|
|
); |
327
|
|
|
$result = $dataTransformer->prepare('selection', $data); |
328
|
|
|
$this->assertEquals($expected, $result); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* |
333
|
|
|
*/ |
334
|
|
|
public function testPrepareWithFixedFieldMapping() |
335
|
|
|
{ |
336
|
|
|
$mapper = new DataMapper(array( |
337
|
|
|
'supplier' => array( |
338
|
|
|
'fields' => array( |
339
|
|
|
'sku' => array( |
340
|
|
|
'column' => 'sku', |
341
|
|
|
), |
342
|
|
|
'type' => array( |
343
|
|
|
'column' => 'type', |
344
|
|
|
'value' => 'brand' |
345
|
|
|
) |
346
|
|
|
) |
347
|
|
|
) |
348
|
|
|
)); |
349
|
|
|
$dataTransformer = new DataTransformer($mapper); |
350
|
|
|
$data = array( |
351
|
|
|
'sku' => '1234567' |
352
|
|
|
); |
353
|
|
|
$expected = array( |
354
|
|
|
'supplier' => array( |
355
|
|
|
'sku' => '1234567', |
356
|
|
|
'type' => 'brand' |
357
|
|
|
) |
358
|
|
|
); |
359
|
|
|
$result = $dataTransformer->prepare('supplier', $data); |
360
|
|
|
$this->assertEquals($expected, $result); |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
/** |
364
|
|
|
* Sets up the fixture |
365
|
|
|
*/ |
366
|
|
|
protected function setUp() |
367
|
|
|
{ |
368
|
|
|
$this->setUpMapping(); |
369
|
|
|
$this->setUpRelationnalMapping(); |
370
|
|
|
$this->setUpRelationnalCollectionMapping(); |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
private function setUpMapping() |
374
|
|
|
{ |
375
|
|
|
$this->mapping = array( |
376
|
|
|
'user' => array( |
377
|
|
|
'table' => 'users', |
378
|
|
|
'identifier' => 'id', |
379
|
|
|
'fields' => array( |
380
|
|
|
'identifier' => array( |
381
|
|
|
'column' => 'id', |
382
|
|
|
'type' => 'int', |
383
|
|
|
'length' => '8', |
384
|
|
|
'nullable' => 'false' |
385
|
|
|
), |
386
|
|
|
'label' => array( |
387
|
|
|
'column' => 'name', |
388
|
|
|
'type' => 'string', |
389
|
|
|
), |
390
|
|
|
'amount' => array( |
391
|
|
|
'column' => 'amount', |
392
|
|
|
'type' => 'decimal', |
393
|
|
|
), |
394
|
|
|
'birthday' => array( |
395
|
|
|
'column' => 'birthdate', |
396
|
|
|
'type' => 'date', |
397
|
|
|
), |
398
|
|
|
'subscribe' => array( |
399
|
|
|
'column' => 'created_at', |
400
|
|
|
'type' => 'datetime', |
401
|
|
|
), |
402
|
|
|
'missing' => array( |
403
|
|
|
'column' => 'missing', |
404
|
|
|
'type' => 'string', |
405
|
|
|
'nullable' => 'true' |
406
|
|
|
) |
407
|
|
|
) |
408
|
|
|
) |
409
|
|
|
); |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
private function setUpRelationnalMapping() |
413
|
|
|
{ |
414
|
|
|
$this->relationnalMapping = array( |
415
|
|
|
'User' => array( |
416
|
|
|
'table' => 'users', |
417
|
|
|
'identifier' => 'sku', |
418
|
|
|
'fields' => array( |
419
|
|
|
'identifier' => array( |
420
|
|
|
'column' => 'sku', |
421
|
|
|
'type' => 'int', |
422
|
|
|
'length' => '8', |
423
|
|
|
'nullable' => 'false' |
424
|
|
|
) |
425
|
|
|
), |
426
|
|
|
'manyToOne' => |
427
|
|
|
array( |
428
|
|
|
'Address' => |
429
|
|
|
array( |
430
|
|
|
'joinColumn' => array( |
431
|
|
|
'referencedColumnName' => 'id', |
432
|
|
|
'name' => 'address_id', |
433
|
|
|
), |
434
|
|
|
'targetEntity' => 'Address', |
435
|
|
|
), |
436
|
|
|
) |
437
|
|
|
), |
438
|
|
|
'Address' => array( |
439
|
|
|
'table' => 'address', |
440
|
|
|
'identifier' => 'sku', |
441
|
|
|
'fields' => array( |
442
|
|
|
'identifier' => array( |
443
|
|
|
'column' => 'sku', |
444
|
|
|
'type' => 'string' |
445
|
|
|
), |
446
|
|
|
'postal_code' => array( |
447
|
|
|
'column' => 'postal_code', |
448
|
|
|
'type' => 'string', |
449
|
|
|
'length' => '5' |
450
|
|
|
), |
451
|
|
|
'city' => array( |
452
|
|
|
'column' => 'city', |
453
|
|
|
'type' => 'string', |
454
|
|
|
), |
455
|
|
|
) |
456
|
|
|
) |
457
|
|
|
); |
458
|
|
|
} |
459
|
|
|
|
460
|
|
|
/** |
461
|
|
|
* |
462
|
|
|
*/ |
463
|
|
|
private function setUpRelationnalCollectionMapping() |
464
|
|
|
{ |
465
|
|
|
$this->relationnalCollectionMapping = array( |
466
|
|
|
'selection' => array( |
467
|
|
|
'table' => 'selection', |
468
|
|
|
'identifier' => 'sku', |
469
|
|
|
'fields' => array( |
470
|
|
|
'sku' => array( |
471
|
|
|
'column' => 'sku', |
472
|
|
|
'length' => '7', |
473
|
|
|
'nullable' => 'false' |
474
|
|
|
) |
475
|
|
|
), |
476
|
|
|
'oneToMany' => array( |
477
|
|
|
'fr' => array( |
478
|
|
|
'targetEntity' => 'selection_lang', |
479
|
|
|
'joinColumn' => array( |
480
|
|
|
'referencedColumnName' => 'id', |
481
|
|
|
'name' => 'selection_id', |
482
|
|
|
), |
483
|
|
|
'references' => array( |
484
|
|
|
'lang_id' => array( |
485
|
|
|
'table' => 'lang', |
486
|
|
|
'referencedColumnName' => 'id', |
487
|
|
|
'where' => array( |
488
|
|
|
'iso_code' => 'fr' |
489
|
|
|
) |
490
|
|
|
) |
491
|
|
|
) |
492
|
|
|
) |
493
|
|
|
) |
494
|
|
|
), |
495
|
|
|
'selection_lang' => array( |
496
|
|
|
'table' => 'selection_lang', |
497
|
|
|
'identifier' => 'name', |
498
|
|
|
'fields' => array( |
499
|
|
|
'name' => array( |
500
|
|
|
'column' => 'name' |
501
|
|
|
) |
502
|
|
|
) |
503
|
|
|
) |
504
|
|
|
); |
505
|
|
|
} |
506
|
|
|
} |
507
|
|
|
|