1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DoctrineModuleTest\Stdlib\Hydrator; |
4
|
|
|
|
5
|
|
|
use DoctrineModuleTest\Stdlib\Hydrator\Asset\ContextStrategy; |
6
|
|
|
use PHPUnit_Framework_TestCase as BaseTestCase; |
7
|
|
|
use ReflectionClass; |
8
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
9
|
|
|
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineObjectHydrator; |
10
|
|
|
use DoctrineModule\Stdlib\Hydrator\Strategy; |
11
|
|
|
use DoctrineModule\Stdlib\Hydrator\Filter; |
12
|
|
|
use DoctrineModuleTest\Stdlib\Hydrator\Asset\NamingStrategyEntity; |
13
|
|
|
use Zend\Stdlib\Hydrator\NamingStrategy\UnderscoreNamingStrategy; |
14
|
|
|
|
15
|
|
|
class DoctrineObjectTest extends BaseTestCase |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var DoctrineObjectHydrator |
19
|
|
|
*/ |
20
|
|
|
protected $hydratorByValue; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var DoctrineObjectHydrator |
24
|
|
|
*/ |
25
|
|
|
protected $hydratorByReference; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var \Doctrine\Common\Persistence\Mapping\ClassMetadata|\PHPUnit_Framework_MockObject_MockObject |
29
|
|
|
*/ |
30
|
|
|
protected $metadata; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var \Doctrine\Common\Persistence\ObjectManager|\PHPUnit_Framework_MockObject_MockObject |
34
|
|
|
*/ |
35
|
|
|
protected $objectManager; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* setUp |
39
|
|
|
*/ |
40
|
|
|
public function setUp() |
41
|
|
|
{ |
42
|
|
|
parent::setUp(); |
43
|
|
|
|
44
|
|
|
$this->metadata = $this->getMock('Doctrine\Common\Persistence\Mapping\ClassMetadata'); |
45
|
|
|
$this->objectManager = $this->getMock('Doctrine\Common\Persistence\ObjectManager'); |
46
|
|
|
|
47
|
|
|
$this->objectManager->expects($this->any()) |
48
|
|
|
->method('getClassMetadata') |
49
|
|
|
->will($this->returnValue($this->metadata)); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function configureObjectManagerForSimpleEntity() |
53
|
|
|
{ |
54
|
|
|
$refl = new ReflectionClass('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity'); |
55
|
|
|
|
56
|
|
|
$this |
|
|
|
|
57
|
|
|
->metadata |
58
|
|
|
->expects($this->any()) |
59
|
|
|
->method('getName') |
60
|
|
|
->will($this->returnValue('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity')); |
61
|
|
|
$this |
62
|
|
|
->metadata |
63
|
|
|
->expects($this->any()) |
64
|
|
|
->method('getAssociationNames') |
65
|
|
|
->will($this->returnValue(array())); |
66
|
|
|
|
67
|
|
|
$this |
68
|
|
|
->metadata |
69
|
|
|
->expects($this->any()) |
70
|
|
|
->method('getFieldNames') |
71
|
|
|
->will($this->returnValue(array('id', 'field'))); |
72
|
|
|
|
73
|
|
|
$this |
74
|
|
|
->metadata |
75
|
|
|
->expects($this->any()) |
76
|
|
|
->method('getTypeOfField') |
77
|
|
|
->with($this->logicalOr($this->equalTo('id'), $this->equalTo('field'))) |
78
|
|
|
->will( |
79
|
|
|
$this->returnCallback( |
80
|
|
|
function ($arg) { |
81
|
|
|
if ('id' === $arg) { |
82
|
|
|
return 'integer'; |
83
|
|
|
} elseif ('field' === $arg) { |
84
|
|
|
return 'string'; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
throw new \InvalidArgumentException(); |
88
|
|
|
} |
89
|
|
|
) |
90
|
|
|
); |
91
|
|
|
|
92
|
|
|
$this |
|
|
|
|
93
|
|
|
->metadata |
94
|
|
|
->expects($this->any()) |
95
|
|
|
->method('hasAssociation') |
96
|
|
|
->will($this->returnValue(false)); |
97
|
|
|
|
98
|
|
|
$this |
99
|
|
|
->metadata |
100
|
|
|
->expects($this->any()) |
101
|
|
|
->method('getIdentifierFieldNames') |
102
|
|
|
->will($this->returnValue(array('id'))); |
103
|
|
|
|
104
|
|
|
$this |
105
|
|
|
->metadata |
106
|
|
|
->expects($this->any()) |
107
|
|
|
->method('getReflectionClass') |
108
|
|
|
->will($this->returnValue($refl)); |
109
|
|
|
|
110
|
|
|
$this->hydratorByValue = new DoctrineObjectHydrator( |
111
|
|
|
$this->objectManager, |
112
|
|
|
true |
113
|
|
|
); |
114
|
|
|
$this->hydratorByReference = new DoctrineObjectHydrator( |
115
|
|
|
$this->objectManager, |
116
|
|
|
false |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function configureObjectManagerForNamingStrategyEntity() |
121
|
|
|
{ |
122
|
|
|
$refl = new ReflectionClass('DoctrineModuleTest\Stdlib\Hydrator\Asset\NamingStrategyEntity'); |
123
|
|
|
|
124
|
|
|
$this |
|
|
|
|
125
|
|
|
->metadata |
126
|
|
|
->expects($this->any()) |
127
|
|
|
->method('getName') |
128
|
|
|
->will($this->returnValue('DoctrineModuleTest\Stdlib\Hydrator\Asset\NamingStrategyEntity')); |
129
|
|
|
$this |
130
|
|
|
->metadata |
131
|
|
|
->expects($this->any()) |
132
|
|
|
->method('getAssociationNames') |
133
|
|
|
->will($this->returnValue(array())); |
134
|
|
|
|
135
|
|
|
$this |
136
|
|
|
->metadata |
137
|
|
|
->expects($this->any()) |
138
|
|
|
->method('getFieldNames') |
139
|
|
|
->will($this->returnValue(array('camelCase'))); |
140
|
|
|
|
141
|
|
|
$this |
142
|
|
|
->metadata |
143
|
|
|
->expects($this->any()) |
144
|
|
|
->method('getTypeOfField') |
145
|
|
|
->with($this->equalTo('camelCase')) |
146
|
|
|
->will($this->returnValue('string')); |
147
|
|
|
|
148
|
|
|
$this |
149
|
|
|
->metadata |
150
|
|
|
->expects($this->any()) |
151
|
|
|
->method('hasAssociation') |
152
|
|
|
->will($this->returnValue(false)); |
153
|
|
|
|
154
|
|
|
$this |
155
|
|
|
->metadata |
156
|
|
|
->expects($this->any()) |
157
|
|
|
->method('getIdentifierFieldNames') |
158
|
|
|
->will($this->returnValue(array('camelCase'))); |
159
|
|
|
|
160
|
|
|
$this |
161
|
|
|
->metadata |
162
|
|
|
->expects($this->any()) |
163
|
|
|
->method('getReflectionClass') |
164
|
|
|
->will($this->returnValue($refl)); |
165
|
|
|
|
166
|
|
|
$this->hydratorByValue = new DoctrineObjectHydrator( |
167
|
|
|
$this->objectManager, |
168
|
|
|
true |
169
|
|
|
); |
170
|
|
|
$this->hydratorByReference = new DoctrineObjectHydrator( |
171
|
|
|
$this->objectManager, |
172
|
|
|
false |
173
|
|
|
); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function configureObjectManagerForSimpleIsEntity() |
177
|
|
|
{ |
178
|
|
|
$refl = new ReflectionClass('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleIsEntity'); |
179
|
|
|
|
180
|
|
|
$this |
|
|
|
|
181
|
|
|
->metadata |
182
|
|
|
->expects($this->any()) |
183
|
|
|
->method('getName') |
184
|
|
|
->will($this->returnValue('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleIsEntity')); |
185
|
|
|
$this |
186
|
|
|
->metadata |
187
|
|
|
->expects($this->any()) |
188
|
|
|
->method('getAssociationNames') |
189
|
|
|
->will($this->returnValue(array())); |
190
|
|
|
|
191
|
|
|
$this |
192
|
|
|
->metadata |
193
|
|
|
->expects($this->any()) |
194
|
|
|
->method('getFieldNames') |
195
|
|
|
->will($this->returnValue(array('id', 'done'))); |
196
|
|
|
|
197
|
|
|
$this |
198
|
|
|
->metadata |
199
|
|
|
->expects($this->any()) |
200
|
|
|
->method('getTypeOfField') |
201
|
|
|
->with($this->logicalOr($this->equalTo('id'), $this->equalTo('done'))) |
202
|
|
|
->will( |
203
|
|
|
$this->returnCallback( |
204
|
|
|
function ($arg) { |
205
|
|
|
if ('id' === $arg) { |
206
|
|
|
return 'integer'; |
207
|
|
|
} elseif ('done' === $arg) { |
208
|
|
|
return 'boolean'; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
throw new \InvalidArgumentException(); |
212
|
|
|
} |
213
|
|
|
) |
214
|
|
|
); |
215
|
|
|
|
216
|
|
|
$this |
|
|
|
|
217
|
|
|
->metadata |
218
|
|
|
->expects($this->any()) |
219
|
|
|
->method('hasAssociation') |
220
|
|
|
->will($this->returnValue(false)); |
221
|
|
|
|
222
|
|
|
$this |
223
|
|
|
->metadata |
224
|
|
|
->expects($this->any()) |
225
|
|
|
->method('getIdentifierFieldNames') |
226
|
|
|
->will($this->returnValue(array('id'))); |
227
|
|
|
|
228
|
|
|
$this |
229
|
|
|
->metadata |
230
|
|
|
->expects($this->any()) |
231
|
|
|
->method('getReflectionClass') |
232
|
|
|
->will($this->returnValue($refl)); |
233
|
|
|
|
234
|
|
|
$this->hydratorByValue = new DoctrineObjectHydrator( |
235
|
|
|
$this->objectManager, |
236
|
|
|
true |
237
|
|
|
); |
238
|
|
|
$this->hydratorByReference = new DoctrineObjectHydrator( |
239
|
|
|
$this->objectManager, |
240
|
|
|
false |
241
|
|
|
); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
public function configureObjectManagerForSimpleEntityWithIsBoolean() |
245
|
|
|
{ |
246
|
|
|
$refl = new ReflectionClass('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntityWithIsBoolean'); |
247
|
|
|
|
248
|
|
|
$this |
|
|
|
|
249
|
|
|
->metadata |
250
|
|
|
->expects($this->any()) |
251
|
|
|
->method('getName') |
252
|
|
|
->will($this->returnValue('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntityWithIsBoolean')); |
253
|
|
|
$this |
254
|
|
|
->metadata |
255
|
|
|
->expects($this->any()) |
256
|
|
|
->method('getAssociationNames') |
257
|
|
|
->will($this->returnValue(array())); |
258
|
|
|
|
259
|
|
|
$this |
260
|
|
|
->metadata |
261
|
|
|
->expects($this->any()) |
262
|
|
|
->method('getFieldNames') |
263
|
|
|
->will($this->returnValue(array('id', 'isActive'))); |
264
|
|
|
|
265
|
|
|
$this |
266
|
|
|
->metadata |
267
|
|
|
->expects($this->any()) |
268
|
|
|
->method('getTypeOfField') |
269
|
|
|
->with($this->logicalOr($this->equalTo('id'), $this->equalTo('isActive'))) |
270
|
|
|
->will( |
271
|
|
|
$this->returnCallback( |
272
|
|
|
function ($arg) { |
273
|
|
|
if ('id' === $arg) { |
274
|
|
|
return 'integer'; |
275
|
|
|
} elseif ('isActive' === $arg) { |
276
|
|
|
return 'boolean'; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
throw new \InvalidArgumentException(); |
280
|
|
|
} |
281
|
|
|
) |
282
|
|
|
); |
283
|
|
|
|
284
|
|
|
$this |
|
|
|
|
285
|
|
|
->metadata |
286
|
|
|
->expects($this->any()) |
287
|
|
|
->method('hasAssociation') |
288
|
|
|
->will($this->returnValue(false)); |
289
|
|
|
|
290
|
|
|
$this |
291
|
|
|
->metadata |
292
|
|
|
->expects($this->any()) |
293
|
|
|
->method('getIdentifierFieldNames') |
294
|
|
|
->will($this->returnValue(array('id'))); |
295
|
|
|
|
296
|
|
|
$this |
297
|
|
|
->metadata |
298
|
|
|
->expects($this->any()) |
299
|
|
|
->method('getReflectionClass') |
300
|
|
|
->will($this->returnValue($refl)); |
301
|
|
|
|
302
|
|
|
$this->hydratorByValue = new DoctrineObjectHydrator( |
303
|
|
|
$this->objectManager, |
304
|
|
|
true |
305
|
|
|
); |
306
|
|
|
$this->hydratorByReference = new DoctrineObjectHydrator( |
307
|
|
|
$this->objectManager, |
308
|
|
|
false |
309
|
|
|
); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
public function configureObjectManagerForSimpleEntityWithStringId() |
313
|
|
|
{ |
314
|
|
|
$refl = new ReflectionClass('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity'); |
315
|
|
|
|
316
|
|
|
$this |
|
|
|
|
317
|
|
|
->metadata |
318
|
|
|
->expects($this->any()) |
319
|
|
|
->method('getName') |
320
|
|
|
->will($this->returnValue('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity')); |
321
|
|
|
$this |
322
|
|
|
->metadata |
323
|
|
|
->expects($this->any()) |
324
|
|
|
->method('getAssociationNames') |
325
|
|
|
->will($this->returnValue(array())); |
326
|
|
|
|
327
|
|
|
$this |
328
|
|
|
->metadata |
329
|
|
|
->expects($this->any()) |
330
|
|
|
->method('getFieldNames') |
331
|
|
|
->will($this->returnValue(array('id', 'field'))); |
332
|
|
|
|
333
|
|
|
$this |
334
|
|
|
->metadata |
335
|
|
|
->expects($this->any()) |
336
|
|
|
->method('getTypeOfField') |
337
|
|
|
->with($this->logicalOr($this->equalTo('id'), $this->equalTo('field'))) |
338
|
|
|
->will($this->returnValue('string')); |
339
|
|
|
|
340
|
|
|
$this |
341
|
|
|
->metadata |
342
|
|
|
->expects($this->any()) |
343
|
|
|
->method('hasAssociation') |
344
|
|
|
->will($this->returnValue(false)); |
345
|
|
|
|
346
|
|
|
$this |
347
|
|
|
->metadata |
348
|
|
|
->expects($this->any()) |
349
|
|
|
->method('getIdentifierFieldNames') |
350
|
|
|
->will($this->returnValue(array('id'))); |
351
|
|
|
|
352
|
|
|
$this |
353
|
|
|
->metadata |
354
|
|
|
->expects($this->any()) |
355
|
|
|
->method('getReflectionClass') |
356
|
|
|
->will($this->returnValue($refl)); |
357
|
|
|
|
358
|
|
|
$this->hydratorByValue = new DoctrineObjectHydrator( |
359
|
|
|
$this->objectManager, |
360
|
|
|
true |
361
|
|
|
); |
362
|
|
|
$this->hydratorByReference = new DoctrineObjectHydrator( |
363
|
|
|
$this->objectManager, |
364
|
|
|
false |
365
|
|
|
); |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
public function configureObjectManagerForSimpleEntityWithDateTime() |
369
|
|
|
{ |
370
|
|
|
$refl = new ReflectionClass('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntityWithDateTime'); |
371
|
|
|
|
372
|
|
|
$this |
|
|
|
|
373
|
|
|
->metadata |
374
|
|
|
->expects($this->any()) |
375
|
|
|
->method('getAssociationNames') |
376
|
|
|
->will($this->returnValue(array())); |
377
|
|
|
|
378
|
|
|
$this |
379
|
|
|
->metadata |
380
|
|
|
->expects($this->any()) |
381
|
|
|
->method('getFieldNames') |
382
|
|
|
->will($this->returnValue(array('id', 'date'))); |
383
|
|
|
|
384
|
|
|
$this |
385
|
|
|
->metadata |
386
|
|
|
->expects($this->any()) |
387
|
|
|
->method('getTypeOfField') |
388
|
|
|
->with($this->logicalOr($this->equalTo('id'), $this->equalTo('date'))) |
389
|
|
|
->will( |
390
|
|
|
$this->returnCallback( |
391
|
|
|
function ($arg) { |
392
|
|
|
if ($arg === 'id') { |
393
|
|
|
return 'integer'; |
394
|
|
|
} elseif ($arg === 'date') { |
395
|
|
|
return 'datetime'; |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
throw new \InvalidArgumentException(); |
399
|
|
|
} |
400
|
|
|
) |
401
|
|
|
); |
402
|
|
|
|
403
|
|
|
$this |
|
|
|
|
404
|
|
|
->metadata |
405
|
|
|
->expects($this->any()) |
406
|
|
|
->method('hasAssociation') |
407
|
|
|
->will($this->returnValue(false)); |
408
|
|
|
|
409
|
|
|
$this |
410
|
|
|
->metadata |
411
|
|
|
->expects($this->any()) |
412
|
|
|
->method('getIdentifierFieldNames') |
413
|
|
|
->will($this->returnValue(array('id'))); |
414
|
|
|
|
415
|
|
|
$this |
416
|
|
|
->metadata |
417
|
|
|
->expects($this->any()) |
418
|
|
|
->method('getReflectionClass') |
419
|
|
|
->will($this->returnValue($refl)); |
420
|
|
|
|
421
|
|
|
$this->hydratorByValue = new DoctrineObjectHydrator( |
422
|
|
|
$this->objectManager, |
423
|
|
|
true |
424
|
|
|
); |
425
|
|
|
$this->hydratorByReference = new DoctrineObjectHydrator( |
426
|
|
|
$this->objectManager, |
427
|
|
|
false |
428
|
|
|
); |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
public function configureObjectManagerForOneToOneEntity() |
432
|
|
|
{ |
433
|
|
|
$refl = new ReflectionClass('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToOneEntity'); |
434
|
|
|
|
435
|
|
|
$this |
|
|
|
|
436
|
|
|
->metadata |
437
|
|
|
->expects($this->any()) |
438
|
|
|
->method('getFieldNames') |
439
|
|
|
->will($this->returnValue(array('id'))); |
440
|
|
|
|
441
|
|
|
$this |
442
|
|
|
->metadata |
443
|
|
|
->expects($this->any()) |
444
|
|
|
->method('getAssociationNames') |
445
|
|
|
->will($this->returnValue(array('toOne'))); |
446
|
|
|
|
447
|
|
|
$this |
448
|
|
|
->metadata |
449
|
|
|
->expects($this->any()) |
450
|
|
|
->method('getTypeOfField') |
451
|
|
|
->with($this->logicalOr($this->equalTo('id'), $this->equalTo('toOne'))) |
452
|
|
|
->will( |
453
|
|
|
$this->returnCallback( |
454
|
|
|
function ($arg) { |
455
|
|
|
if ($arg === 'id') { |
456
|
|
|
return 'integer'; |
457
|
|
|
} elseif ($arg === 'toOne') { |
458
|
|
|
return 'DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity'; |
459
|
|
|
} |
460
|
|
|
|
461
|
|
|
throw new \InvalidArgumentException(); |
462
|
|
|
} |
463
|
|
|
) |
464
|
|
|
); |
465
|
|
|
|
466
|
|
|
$this |
|
|
|
|
467
|
|
|
->metadata |
468
|
|
|
->expects($this->any()) |
469
|
|
|
->method('hasAssociation') |
470
|
|
|
->with($this->logicalOr($this->equalTo('id'), $this->equalTo('toOne'))) |
471
|
|
|
->will( |
472
|
|
|
$this->returnCallback( |
473
|
|
|
function ($arg) { |
474
|
|
|
if ($arg === 'id') { |
475
|
|
|
return false; |
476
|
|
|
} elseif ($arg === 'toOne') { |
477
|
|
|
return true; |
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
throw new \InvalidArgumentException(); |
481
|
|
|
} |
482
|
|
|
) |
483
|
|
|
); |
484
|
|
|
|
485
|
|
|
$this |
|
|
|
|
486
|
|
|
->metadata |
487
|
|
|
->expects($this->any()) |
488
|
|
|
->method('isSingleValuedAssociation') |
489
|
|
|
->with('toOne') |
490
|
|
|
->will($this->returnValue(true)); |
491
|
|
|
|
492
|
|
|
$this |
493
|
|
|
->metadata |
494
|
|
|
->expects($this->any()) |
495
|
|
|
->method('getAssociationTargetClass') |
496
|
|
|
->with('toOne') |
497
|
|
|
->will($this->returnValue('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity')); |
498
|
|
|
|
499
|
|
|
$this |
500
|
|
|
->metadata |
501
|
|
|
->expects($this->any()) |
502
|
|
|
->method('getReflectionClass') |
503
|
|
|
->will($this->returnValue($refl)); |
504
|
|
|
|
505
|
|
|
$this |
506
|
|
|
->metadata |
507
|
|
|
->expects($this->any()) |
508
|
|
|
->method('getIdentifier') |
509
|
|
|
->will($this->returnValue(array("id"))); |
510
|
|
|
|
511
|
|
|
$this->hydratorByValue = new DoctrineObjectHydrator( |
512
|
|
|
$this->objectManager, |
513
|
|
|
true |
514
|
|
|
); |
515
|
|
|
$this->hydratorByReference = new DoctrineObjectHydrator( |
516
|
|
|
$this->objectManager, |
517
|
|
|
false |
518
|
|
|
); |
519
|
|
|
} |
520
|
|
|
|
521
|
|
|
public function configureObjectManagerForOneToOneEntityNotNullable() |
522
|
|
|
{ |
523
|
|
|
$refl = new ReflectionClass('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToOneEntityNotNullable'); |
524
|
|
|
|
525
|
|
|
$this |
|
|
|
|
526
|
|
|
->metadata |
527
|
|
|
->expects($this->any()) |
528
|
|
|
->method('getFieldNames') |
529
|
|
|
->will($this->returnValue(array('id'))); |
530
|
|
|
|
531
|
|
|
$this |
532
|
|
|
->metadata |
533
|
|
|
->expects($this->any()) |
534
|
|
|
->method('getAssociationNames') |
535
|
|
|
->will($this->returnValue(array('toOne'))); |
536
|
|
|
|
537
|
|
|
$this |
538
|
|
|
->metadata |
539
|
|
|
->expects($this->any()) |
540
|
|
|
->method('getTypeOfField') |
541
|
|
|
->with( |
542
|
|
|
$this->logicalOr( |
543
|
|
|
$this->equalTo('id'), |
544
|
|
|
$this->equalTo('toOne'), |
545
|
|
|
$this->equalTo('field') |
546
|
|
|
) |
547
|
|
|
) |
548
|
|
|
->will( |
549
|
|
|
$this->returnCallback( |
550
|
|
|
function ($arg) { |
551
|
|
|
if ($arg === 'id') { |
552
|
|
|
return 'integer'; |
553
|
|
|
} elseif ($arg === 'toOne') { |
554
|
|
|
return 'DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity'; |
555
|
|
|
} elseif ($arg === 'field') { |
556
|
|
|
return 'string'; |
557
|
|
|
} |
558
|
|
|
|
559
|
|
|
throw new \InvalidArgumentException(); |
560
|
|
|
} |
561
|
|
|
) |
562
|
|
|
); |
563
|
|
|
|
564
|
|
|
$this |
|
|
|
|
565
|
|
|
->metadata |
566
|
|
|
->expects($this->any()) |
567
|
|
|
->method('hasAssociation') |
568
|
|
|
->with( |
569
|
|
|
$this->logicalOr( |
570
|
|
|
$this->equalTo('id'), |
571
|
|
|
$this->equalTo('toOne'), |
572
|
|
|
$this->equalTo('field') |
573
|
|
|
) |
574
|
|
|
) |
575
|
|
|
->will( |
576
|
|
|
$this->returnCallback( |
577
|
|
|
function ($arg) { |
578
|
|
|
if ($arg === 'id' || $arg === 'field') { |
579
|
|
|
return false; |
580
|
|
|
} elseif ($arg === 'toOne') { |
581
|
|
|
return true; |
582
|
|
|
} |
583
|
|
|
|
584
|
|
|
throw new \InvalidArgumentException(); |
585
|
|
|
} |
586
|
|
|
) |
587
|
|
|
); |
588
|
|
|
|
589
|
|
|
$this |
|
|
|
|
590
|
|
|
->metadata |
591
|
|
|
->expects($this->any()) |
592
|
|
|
->method('isSingleValuedAssociation') |
593
|
|
|
->with('toOne') |
594
|
|
|
->will($this->returnValue(true)); |
595
|
|
|
|
596
|
|
|
$this |
597
|
|
|
->metadata |
598
|
|
|
->expects($this->any()) |
599
|
|
|
->method('getAssociationTargetClass') |
600
|
|
|
->with('toOne') |
601
|
|
|
->will($this->returnValue('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity')); |
602
|
|
|
|
603
|
|
|
$this |
604
|
|
|
->metadata |
605
|
|
|
->expects($this->any()) |
606
|
|
|
->method('getReflectionClass') |
607
|
|
|
->will($this->returnValue($refl)); |
608
|
|
|
|
609
|
|
|
$this |
610
|
|
|
->metadata |
611
|
|
|
->expects($this->any()) |
612
|
|
|
->method('getIdentifier') |
613
|
|
|
->will($this->returnValue(array("id"))); |
614
|
|
|
|
615
|
|
|
$this->hydratorByValue = new DoctrineObjectHydrator( |
616
|
|
|
$this->objectManager, |
617
|
|
|
true |
618
|
|
|
); |
619
|
|
|
$this->hydratorByReference = new DoctrineObjectHydrator( |
620
|
|
|
$this->objectManager, |
621
|
|
|
false |
622
|
|
|
); |
623
|
|
|
} |
624
|
|
|
|
625
|
|
|
public function configureObjectManagerForOneToManyEntity() |
626
|
|
|
{ |
627
|
|
|
$refl = new ReflectionClass('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntity'); |
628
|
|
|
|
629
|
|
|
$this |
|
|
|
|
630
|
|
|
->metadata |
631
|
|
|
->expects($this->any()) |
632
|
|
|
->method('getFieldNames') |
633
|
|
|
->will($this->returnValue(array('id'))); |
634
|
|
|
|
635
|
|
|
$this |
636
|
|
|
->metadata |
637
|
|
|
->expects($this->any()) |
638
|
|
|
->method('getAssociationNames') |
639
|
|
|
->will($this->returnValue(array('entities'))); |
640
|
|
|
|
641
|
|
|
$this |
642
|
|
|
->metadata |
643
|
|
|
->expects($this->any()) |
644
|
|
|
->method('getTypeOfField') |
645
|
|
|
->with( |
646
|
|
|
$this->logicalOr( |
647
|
|
|
$this->equalTo('id'), |
648
|
|
|
$this->equalTo('entities'), |
649
|
|
|
$this->equalTo('field') |
650
|
|
|
) |
651
|
|
|
) |
652
|
|
|
->will( |
653
|
|
|
$this->returnCallback( |
654
|
|
|
function ($arg) { |
655
|
|
|
if ($arg === 'id') { |
656
|
|
|
return 'integer'; |
657
|
|
|
} elseif ($arg === 'field') { |
658
|
|
|
return 'string'; |
659
|
|
|
} elseif ($arg === 'entities') { |
660
|
|
|
return 'Doctrine\Common\Collections\ArrayCollection'; |
661
|
|
|
} |
662
|
|
|
|
663
|
|
|
throw new \InvalidArgumentException(); |
664
|
|
|
} |
665
|
|
|
) |
666
|
|
|
); |
667
|
|
|
|
668
|
|
|
$this |
|
|
|
|
669
|
|
|
->metadata |
670
|
|
|
->expects($this->any()) |
671
|
|
|
->method('hasAssociation') |
672
|
|
|
->with($this->logicalOr($this->equalTo('id'), $this->equalTo('entities'), $this->equalTo('field'))) |
673
|
|
|
->will( |
674
|
|
|
$this->returnCallback( |
675
|
|
|
function ($arg) { |
676
|
|
|
if ($arg === 'id') { |
677
|
|
|
return false; |
678
|
|
|
} elseif ($arg === 'field') { |
679
|
|
|
return false; |
680
|
|
|
} elseif ($arg === 'entities') { |
681
|
|
|
return true; |
682
|
|
|
} |
683
|
|
|
|
684
|
|
|
throw new \InvalidArgumentException(); |
685
|
|
|
} |
686
|
|
|
) |
687
|
|
|
); |
688
|
|
|
|
689
|
|
|
$this |
|
|
|
|
690
|
|
|
->metadata |
691
|
|
|
->expects($this->any()) |
692
|
|
|
->method('isSingleValuedAssociation') |
693
|
|
|
->with('entities') |
694
|
|
|
->will($this->returnValue(false)); |
695
|
|
|
|
696
|
|
|
$this |
697
|
|
|
->metadata |
698
|
|
|
->expects($this->any()) |
699
|
|
|
->method('isCollectionValuedAssociation') |
700
|
|
|
->with('entities') |
701
|
|
|
->will($this->returnValue(true)); |
702
|
|
|
|
703
|
|
|
$this |
704
|
|
|
->metadata |
705
|
|
|
->expects($this->any()) |
706
|
|
|
->method('getAssociationTargetClass') |
707
|
|
|
->with('entities') |
708
|
|
|
->will($this->returnValue('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity')); |
709
|
|
|
|
710
|
|
|
$this |
711
|
|
|
->metadata |
712
|
|
|
->expects($this->any()) |
713
|
|
|
->method('getReflectionClass') |
714
|
|
|
->will($this->returnValue($refl)); |
715
|
|
|
|
716
|
|
|
$this->metadata |
717
|
|
|
->expects($this->any()) |
718
|
|
|
->method('getIdentifier') |
719
|
|
|
->will($this->returnValue(array("id"))); |
720
|
|
|
|
721
|
|
|
$this->hydratorByValue = new DoctrineObjectHydrator( |
722
|
|
|
$this->objectManager, |
723
|
|
|
true |
724
|
|
|
); |
725
|
|
|
$this->hydratorByReference = new DoctrineObjectHydrator( |
726
|
|
|
$this->objectManager, |
727
|
|
|
false |
728
|
|
|
); |
729
|
|
|
|
730
|
|
|
} |
731
|
|
|
|
732
|
|
|
public function configureObjectManagerForOneToManyArrayEntity() |
733
|
|
|
{ |
734
|
|
|
$refl = new ReflectionClass('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyArrayEntity'); |
735
|
|
|
|
736
|
|
|
$this |
|
|
|
|
737
|
|
|
->metadata |
738
|
|
|
->expects($this->any()) |
739
|
|
|
->method('getFieldNames') |
740
|
|
|
->will($this->returnValue(array('id'))); |
741
|
|
|
|
742
|
|
|
$this |
743
|
|
|
->metadata |
744
|
|
|
->expects($this->any()) |
745
|
|
|
->method('getAssociationNames') |
746
|
|
|
->will($this->returnValue(array('entities'))); |
747
|
|
|
|
748
|
|
|
$this |
749
|
|
|
->metadata |
750
|
|
|
->expects($this->any()) |
751
|
|
|
->method('getTypeOfField') |
752
|
|
|
->with( |
753
|
|
|
$this->logicalOr( |
754
|
|
|
$this->equalTo('id'), |
755
|
|
|
$this->equalTo('entities'), |
756
|
|
|
$this->equalTo('field') |
757
|
|
|
) |
758
|
|
|
) |
759
|
|
|
->will( |
760
|
|
|
$this->returnCallback( |
761
|
|
|
function ($arg) { |
762
|
|
|
if ($arg === 'id') { |
763
|
|
|
return 'integer'; |
764
|
|
|
} elseif ($arg === 'field') { |
765
|
|
|
return 'string'; |
766
|
|
|
} elseif ($arg === 'entities') { |
767
|
|
|
return 'Doctrine\Common\Collections\ArrayCollection'; |
768
|
|
|
} |
769
|
|
|
|
770
|
|
|
throw new \InvalidArgumentException(); |
771
|
|
|
} |
772
|
|
|
) |
773
|
|
|
); |
774
|
|
|
|
775
|
|
|
$this |
|
|
|
|
776
|
|
|
->metadata |
777
|
|
|
->expects($this->any()) |
778
|
|
|
->method('hasAssociation') |
779
|
|
|
->with($this->logicalOr($this->equalTo('id'), $this->equalTo('entities'))) |
780
|
|
|
->will( |
781
|
|
|
$this->returnCallback( |
782
|
|
|
function ($arg) { |
783
|
|
|
if ($arg === 'id') { |
784
|
|
|
return false; |
785
|
|
|
} elseif ($arg === 'field') { |
786
|
|
|
return 'string'; |
787
|
|
|
} elseif ($arg === 'entities') { |
788
|
|
|
return true; |
789
|
|
|
} |
790
|
|
|
|
791
|
|
|
throw new \InvalidArgumentException(); |
792
|
|
|
} |
793
|
|
|
) |
794
|
|
|
); |
795
|
|
|
|
796
|
|
|
$this |
|
|
|
|
797
|
|
|
->metadata |
798
|
|
|
->expects($this->any()) |
799
|
|
|
->method('isSingleValuedAssociation') |
800
|
|
|
->with('entities') |
801
|
|
|
->will($this->returnValue(false)); |
802
|
|
|
|
803
|
|
|
$this |
804
|
|
|
->metadata |
805
|
|
|
->expects($this->any()) |
806
|
|
|
->method('isCollectionValuedAssociation') |
807
|
|
|
->with('entities') |
808
|
|
|
->will($this->returnValue(true)); |
809
|
|
|
|
810
|
|
|
$this |
811
|
|
|
->metadata |
812
|
|
|
->expects($this->any()) |
813
|
|
|
->method('getAssociationTargetClass') |
814
|
|
|
->with('entities') |
815
|
|
|
->will($this->returnValue('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity')); |
816
|
|
|
|
817
|
|
|
$this |
818
|
|
|
->metadata |
819
|
|
|
->expects($this->any()) |
820
|
|
|
->method('getReflectionClass') |
821
|
|
|
->will($this->returnValue($refl)); |
822
|
|
|
|
823
|
|
|
$this->metadata |
824
|
|
|
->expects($this->any()) |
825
|
|
|
->method('getIdentifier') |
826
|
|
|
->will($this->returnValue(array("id"))); |
827
|
|
|
|
828
|
|
|
$this->hydratorByValue = new DoctrineObjectHydrator( |
829
|
|
|
$this->objectManager, |
830
|
|
|
true |
831
|
|
|
); |
832
|
|
|
$this->hydratorByReference = new DoctrineObjectHydrator( |
833
|
|
|
$this->objectManager, |
834
|
|
|
false |
835
|
|
|
); |
836
|
|
|
} |
837
|
|
|
|
838
|
|
|
public function testObjectIsPassedForContextToStrategies() |
839
|
|
|
{ |
840
|
|
|
$entity = new Asset\ContextEntity(); |
841
|
|
|
$entity->setId(2); |
842
|
|
|
$entity->setField('foo', false); |
|
|
|
|
843
|
|
|
|
844
|
|
|
$this->configureObjectManagerForSimpleEntity(); |
845
|
|
|
|
846
|
|
|
$hydrator = $this->hydratorByValue; |
847
|
|
|
$entity = $hydrator->hydrate(array('id' => 3, 'field' => 'bar'), $entity); |
848
|
|
|
$this->assertEquals(array('id' => 3, 'field' => 'bar'), $hydrator->extract($entity)); |
849
|
|
|
|
850
|
|
|
$hydrator->addStrategy('id', new ContextStrategy()); |
851
|
|
|
$entity = $hydrator->hydrate(array('id' => 3, 'field' => 'bar'), $entity); |
852
|
|
|
$this->assertEquals(array('id' => '3barbar', 'field' => 'bar'), $hydrator->extract($entity)); |
853
|
|
|
} |
854
|
|
|
|
855
|
|
|
public function testCanExtractSimpleEntityByValue() |
856
|
|
|
{ |
857
|
|
|
// When using extraction by value, it will use the public API of the entity to retrieve values (getters) |
858
|
|
|
$entity = new Asset\SimpleEntity(); |
859
|
|
|
$entity->setId(2); |
860
|
|
|
$entity->setField('foo', false); |
861
|
|
|
|
862
|
|
|
$this->configureObjectManagerForSimpleEntity(); |
863
|
|
|
|
864
|
|
|
$data = $this->hydratorByValue->extract($entity); |
865
|
|
|
$this->assertEquals(array('id' => 2, 'field' => 'From getter: foo'), $data); |
866
|
|
|
} |
867
|
|
|
|
868
|
|
|
public function testCanExtractSimpleEntityByReference() |
869
|
|
|
{ |
870
|
|
|
// When using extraction by reference, it won't use the public API of entity (getters won't be called) |
871
|
|
|
$entity = new Asset\SimpleEntity(); |
872
|
|
|
$entity->setId(2); |
873
|
|
|
$entity->setField('foo', false); |
874
|
|
|
|
875
|
|
|
$this->configureObjectManagerForSimpleEntity(); |
876
|
|
|
|
877
|
|
|
$data = $this->hydratorByReference->extract($entity); |
878
|
|
|
$this->assertEquals(array('id' => 2, 'field' => 'foo'), $data); |
879
|
|
|
} |
880
|
|
|
|
881
|
|
|
public function testCanHydrateSimpleEntityByValue() |
882
|
|
|
{ |
883
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
884
|
|
|
$entity = new Asset\SimpleEntity(); |
885
|
|
|
$this->configureObjectManagerForSimpleEntity(); |
886
|
|
|
$data = array('field' => 'foo'); |
887
|
|
|
|
888
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
889
|
|
|
|
890
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $entity); |
891
|
|
|
$this->assertEquals('From setter: foo', $entity->getField(false)); |
892
|
|
|
} |
893
|
|
|
|
894
|
|
|
/** |
895
|
|
|
* When using hydration by value, it will use the public API of the entity to set values (setters) |
896
|
|
|
* |
897
|
|
|
* @covers \DoctrineModule\Stdlib\Hydrator\DoctrineObject::hydrateByValue |
898
|
|
|
*/ |
899
|
|
|
public function testCanHydrateSimpleEntityWithStringIdByValue() |
900
|
|
|
{ |
901
|
|
|
$entity = new Asset\SimpleEntity(); |
902
|
|
|
$data = array('id' => 'bar', 'field' => 'foo'); |
903
|
|
|
|
904
|
|
|
$this->configureObjectManagerForSimpleEntityWithStringId(); |
905
|
|
|
|
906
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
907
|
|
|
|
908
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $entity); |
909
|
|
|
$this->assertEquals('From setter: foo', $entity->getField(false)); |
910
|
|
|
} |
911
|
|
|
|
912
|
|
|
public function testCanHydrateSimpleEntityByReference() |
913
|
|
|
{ |
914
|
|
|
// When using hydration by reference, it won't use the public API of the entity to set values (setters) |
915
|
|
|
$entity = new Asset\SimpleEntity(); |
916
|
|
|
$this->configureObjectManagerForSimpleEntity(); |
917
|
|
|
$data = array('field' => 'foo'); |
918
|
|
|
|
919
|
|
|
$entity = $this->hydratorByReference->hydrate($data, $entity); |
920
|
|
|
|
921
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $entity); |
922
|
|
|
$this->assertEquals('foo', $entity->getField(false)); |
923
|
|
|
} |
924
|
|
|
|
925
|
|
|
/** |
926
|
|
|
* When using hydration by reference, it won't use the public API of the entity to set values (getters) |
927
|
|
|
* |
928
|
|
|
* @covers \DoctrineModule\Stdlib\Hydrator\DoctrineObject::hydrateByReference |
929
|
|
|
*/ |
930
|
|
|
public function testCanHydrateSimpleEntityWithStringIdByReference() |
931
|
|
|
{ |
932
|
|
|
$entity = new Asset\SimpleEntity(); |
933
|
|
|
$data = array('id' => 'bar', 'field' => 'foo'); |
934
|
|
|
|
935
|
|
|
$this->configureObjectManagerForSimpleEntityWithStringId(); |
936
|
|
|
|
937
|
|
|
$entity = $this->hydratorByReference->hydrate($data, $entity); |
938
|
|
|
|
939
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $entity); |
940
|
|
|
$this->assertEquals('foo', $entity->getField(false)); |
941
|
|
|
} |
942
|
|
|
|
943
|
|
|
public function testReuseExistingEntityIfDataArrayContainsIdentifier() |
944
|
|
|
{ |
945
|
|
|
// When using hydration by reference, it won't use the public API of the entity to set values (setters) |
946
|
|
|
$entity = new Asset\SimpleEntity(); |
947
|
|
|
|
948
|
|
|
$this->configureObjectManagerForSimpleEntity(); |
949
|
|
|
$data = array('id' => 1); |
950
|
|
|
|
951
|
|
|
$entityInDatabaseWithIdOfOne = new Asset\SimpleEntity(); |
952
|
|
|
$entityInDatabaseWithIdOfOne->setId(1); |
953
|
|
|
$entityInDatabaseWithIdOfOne->setField('bar', false); |
954
|
|
|
|
955
|
|
|
$this |
|
|
|
|
956
|
|
|
->objectManager |
957
|
|
|
->expects($this->once()) |
958
|
|
|
->method('find') |
959
|
|
|
->with('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', array('id' => 1)) |
960
|
|
|
->will($this->returnValue($entityInDatabaseWithIdOfOne)); |
961
|
|
|
|
962
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
963
|
|
|
|
964
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $entity); |
965
|
|
|
$this->assertEquals('bar', $entity->getField(false)); |
966
|
|
|
} |
967
|
|
|
|
968
|
|
|
/** |
969
|
|
|
* Test for https://github.com/doctrine/DoctrineModule/issues/456 |
970
|
|
|
*/ |
971
|
|
|
public function testReuseExistingEntityIfDataArrayContainsIdentifierWithZeroIdentifier() |
972
|
|
|
{ |
973
|
|
|
// When using hydration by reference, it won't use the public API of the entity to set values (setters) |
974
|
|
|
$entity = new Asset\SimpleEntity(); |
975
|
|
|
|
976
|
|
|
$this->configureObjectManagerForSimpleEntity(); |
977
|
|
|
$data = array('id' => 0); |
978
|
|
|
|
979
|
|
|
$entityInDatabaseWithIdOfOne = new Asset\SimpleEntity(); |
980
|
|
|
$entityInDatabaseWithIdOfOne->setId(0); |
981
|
|
|
$entityInDatabaseWithIdOfOne->setField('bar', false); |
982
|
|
|
|
983
|
|
|
$this |
|
|
|
|
984
|
|
|
->objectManager |
985
|
|
|
->expects($this->once()) |
986
|
|
|
->method('find') |
987
|
|
|
->with('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', array('id' => 0)) |
988
|
|
|
->will($this->returnValue($entityInDatabaseWithIdOfOne)); |
989
|
|
|
|
990
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
991
|
|
|
|
992
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $entity); |
993
|
|
|
$this->assertEquals('bar', $entity->getField(false)); |
994
|
|
|
} |
995
|
|
|
|
996
|
|
|
public function testExtractOneToOneAssociationByValue() |
997
|
|
|
{ |
998
|
|
|
// When using extraction by value, it will use the public API of the entity to retrieve values (getters) |
999
|
|
|
$toOne = new Asset\SimpleEntity(); |
1000
|
|
|
$toOne->setId(2); |
1001
|
|
|
$toOne->setField('foo', false); |
1002
|
|
|
|
1003
|
|
|
$entity = new Asset\OneToOneEntity(); |
1004
|
|
|
$entity->setId(2); |
1005
|
|
|
$entity->setToOne($toOne); |
1006
|
|
|
|
1007
|
|
|
$this->configureObjectManagerForOneToOneEntity(); |
1008
|
|
|
|
1009
|
|
|
$data = $this->hydratorByValue->extract($entity); |
1010
|
|
|
|
1011
|
|
|
$this->assertEquals(2, $data['id']); |
1012
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $data['toOne']); |
1013
|
|
|
$this->assertEquals('Modified from getToOne getter', $data['toOne']->getField(false)); |
1014
|
|
|
$this->assertSame($toOne, $data['toOne']); |
1015
|
|
|
} |
1016
|
|
|
|
1017
|
|
|
public function testExtractOneToOneAssociationByReference() |
1018
|
|
|
{ |
1019
|
|
|
// When using extraction by value, it will use the public API of the entity to retrieve values (getters) |
1020
|
|
|
$toOne = new Asset\SimpleEntity(); |
1021
|
|
|
$toOne->setId(2); |
1022
|
|
|
$toOne->setField('foo', false); |
1023
|
|
|
|
1024
|
|
|
$entity = new Asset\OneToOneEntity(); |
1025
|
|
|
$entity->setId(2); |
1026
|
|
|
$entity->setToOne($toOne, false); |
1027
|
|
|
|
1028
|
|
|
$this->configureObjectManagerForOneToOneEntity(); |
1029
|
|
|
|
1030
|
|
|
$data = $this->hydratorByReference->extract($entity); |
1031
|
|
|
|
1032
|
|
|
$this->assertEquals(2, $data['id']); |
1033
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $data['toOne']); |
1034
|
|
|
$this->assertEquals('foo', $data['toOne']->getField(false)); |
1035
|
|
|
$this->assertSame($toOne, $data['toOne']); |
1036
|
|
|
} |
1037
|
|
|
|
1038
|
|
|
public function testHydrateOneToOneAssociationByValue() |
1039
|
|
|
{ |
1040
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
1041
|
|
|
$toOne = new Asset\SimpleEntity(); |
1042
|
|
|
$toOne->setId(2); |
1043
|
|
|
$toOne->setField('foo', false); |
1044
|
|
|
|
1045
|
|
|
$entity = new Asset\OneToOneEntity(); |
1046
|
|
|
$this->configureObjectManagerForOneToOneEntity(); |
1047
|
|
|
|
1048
|
|
|
$data = array('toOne' => $toOne); |
1049
|
|
|
|
1050
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
1051
|
|
|
|
1052
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToOneEntity', $entity); |
1053
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $entity->getToOne(false)); |
1054
|
|
|
$this->assertEquals('Modified from setToOne setter', $entity->getToOne(false)->getField(false)); |
1055
|
|
|
} |
1056
|
|
|
|
1057
|
|
|
public function testHydrateOneToOneAssociationByReference() |
1058
|
|
|
{ |
1059
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
1060
|
|
|
$toOne = new Asset\SimpleEntity(); |
1061
|
|
|
$toOne->setId(2); |
1062
|
|
|
$toOne->setField('foo', false); |
1063
|
|
|
|
1064
|
|
|
$entity = new Asset\OneToOneEntity(); |
1065
|
|
|
$this->configureObjectManagerForOneToOneEntity(); |
1066
|
|
|
|
1067
|
|
|
$data = array('toOne' => $toOne); |
1068
|
|
|
|
1069
|
|
|
$entity = $this->hydratorByReference->hydrate($data, $entity); |
1070
|
|
|
|
1071
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToOneEntity', $entity); |
1072
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $entity->getToOne(false)); |
1073
|
|
|
$this->assertEquals('foo', $entity->getToOne(false)->getField(false)); |
1074
|
|
|
} |
1075
|
|
|
|
1076
|
|
|
public function testHydrateOneToOneAssociationByValueUsingIdentifierForRelation() |
1077
|
|
|
{ |
1078
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
1079
|
|
|
$entity = new Asset\OneToOneEntity(); |
1080
|
|
|
$this->configureObjectManagerForOneToOneEntity(); |
1081
|
|
|
|
1082
|
|
|
// Use entity of id 1 as relation |
1083
|
|
|
$data = array('toOne' => 1); |
1084
|
|
|
|
1085
|
|
|
$entityInDatabaseWithIdOfOne = new Asset\SimpleEntity(); |
1086
|
|
|
$entityInDatabaseWithIdOfOne->setId(1); |
1087
|
|
|
$entityInDatabaseWithIdOfOne->setField('bar', false); |
1088
|
|
|
|
1089
|
|
|
$this |
|
|
|
|
1090
|
|
|
->objectManager |
1091
|
|
|
->expects($this->once()) |
1092
|
|
|
->method('find') |
1093
|
|
|
->with('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', 1) |
1094
|
|
|
->will($this->returnValue($entityInDatabaseWithIdOfOne)); |
1095
|
|
|
|
1096
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
1097
|
|
|
|
1098
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToOneEntity', $entity); |
1099
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $entity->getToOne(false)); |
1100
|
|
|
$this->assertSame($entityInDatabaseWithIdOfOne, $entity->getToOne(false)); |
1101
|
|
|
} |
1102
|
|
|
|
1103
|
|
|
public function testHydrateOneToOneAssociationByReferenceUsingIdentifierForRelation() |
1104
|
|
|
{ |
1105
|
|
|
// When using hydration by reference, it won't use the public API of the entity to set values (setters) |
1106
|
|
|
$entity = new Asset\OneToOneEntity(); |
1107
|
|
|
$this->configureObjectManagerForOneToOneEntity(); |
1108
|
|
|
|
1109
|
|
|
// Use entity of id 1 as relation |
1110
|
|
|
$data = array('toOne' => 1); |
1111
|
|
|
|
1112
|
|
|
$entityInDatabaseWithIdOfOne = new Asset\SimpleEntity(); |
1113
|
|
|
$entityInDatabaseWithIdOfOne->setId(1); |
1114
|
|
|
$entityInDatabaseWithIdOfOne->setField('bar', false); |
1115
|
|
|
|
1116
|
|
|
$this |
|
|
|
|
1117
|
|
|
->objectManager |
1118
|
|
|
->expects($this->once()) |
1119
|
|
|
->method('find') |
1120
|
|
|
->with('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', 1) |
1121
|
|
|
->will($this->returnValue($entityInDatabaseWithIdOfOne)); |
1122
|
|
|
|
1123
|
|
|
$entity = $this->hydratorByReference->hydrate($data, $entity); |
1124
|
|
|
|
1125
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToOneEntity', $entity); |
1126
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $entity->getToOne(false)); |
1127
|
|
|
$this->assertSame($entityInDatabaseWithIdOfOne, $entity->getToOne(false)); |
1128
|
|
|
} |
1129
|
|
|
|
1130
|
|
|
public function testHydrateOneToOneAssociationByValueUsingIdentifierArrayForRelation() |
1131
|
|
|
{ |
1132
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
1133
|
|
|
$entity = new Asset\OneToOneEntity(); |
1134
|
|
|
$this->configureObjectManagerForOneToOneEntity(); |
1135
|
|
|
|
1136
|
|
|
// Use entity of id 1 as relation |
1137
|
|
|
$data = array('toOne' => array('id' => 1)); |
1138
|
|
|
|
1139
|
|
|
$entityInDatabaseWithIdOfOne = new Asset\SimpleEntity(); |
1140
|
|
|
$entityInDatabaseWithIdOfOne->setId(1); |
1141
|
|
|
$entityInDatabaseWithIdOfOne->setField('bar', false); |
1142
|
|
|
|
1143
|
|
|
$this |
|
|
|
|
1144
|
|
|
->objectManager |
1145
|
|
|
->expects($this->once()) |
1146
|
|
|
->method('find') |
1147
|
|
|
->with('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', array('id' => 1)) |
1148
|
|
|
->will($this->returnValue($entityInDatabaseWithIdOfOne)); |
1149
|
|
|
|
1150
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
1151
|
|
|
|
1152
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToOneEntity', $entity); |
1153
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $entity->getToOne(false)); |
1154
|
|
|
$this->assertSame($entityInDatabaseWithIdOfOne, $entity->getToOne(false)); |
1155
|
|
|
} |
1156
|
|
|
|
1157
|
|
|
public function testHydrateOneToOneAssociationByValueUsingFullArrayForRelation() |
1158
|
|
|
{ |
1159
|
|
|
$entity = new Asset\OneToOneEntityNotNullable; |
1160
|
|
|
$this->configureObjectManagerForOneToOneEntityNotNullable(); |
1161
|
|
|
|
1162
|
|
|
// Use entity of id 1 as relation |
1163
|
|
|
$data = array('toOne' => array('id' => 1, 'field' => 'foo')); |
1164
|
|
|
|
1165
|
|
|
$entityInDatabaseWithIdOfOne = new Asset\SimpleEntity(); |
1166
|
|
|
$entityInDatabaseWithIdOfOne->setId(1); |
1167
|
|
|
$entityInDatabaseWithIdOfOne->setField('bar', false); |
1168
|
|
|
|
1169
|
|
|
$this |
|
|
|
|
1170
|
|
|
->objectManager |
1171
|
|
|
->expects($this->once()) |
1172
|
|
|
->method('find') |
1173
|
|
|
->with( |
1174
|
|
|
'DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', |
1175
|
|
|
array('id' => 1) |
1176
|
|
|
) |
1177
|
|
|
->will($this->returnValue($entityInDatabaseWithIdOfOne)); |
1178
|
|
|
|
1179
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
1180
|
|
|
|
1181
|
|
|
$this->assertInstanceOf( |
1182
|
|
|
'DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToOneEntityNotNullable', |
1183
|
|
|
$entity |
1184
|
|
|
); |
1185
|
|
|
$this->assertInstanceOf( |
1186
|
|
|
'DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', |
1187
|
|
|
$entity->getToOne(false) |
1188
|
|
|
); |
1189
|
|
|
$this->assertSame($entityInDatabaseWithIdOfOne, $entity->getToOne(false)); |
1190
|
|
|
$this->assertEquals( |
1191
|
|
|
'From getter: Modified from setToOne setter', |
1192
|
|
|
$entityInDatabaseWithIdOfOne->getField() |
1193
|
|
|
); |
1194
|
|
|
} |
1195
|
|
|
|
1196
|
|
|
public function testHydrateOneToOneAssociationByReferenceUsingIdentifierArrayForRelation() |
1197
|
|
|
{ |
1198
|
|
|
// When using hydration by reference, it won't use the public API of the entity to set values (setters) |
1199
|
|
|
$entity = new Asset\OneToOneEntity(); |
1200
|
|
|
$this->configureObjectManagerForOneToOneEntity(); |
1201
|
|
|
|
1202
|
|
|
// Use entity of id 1 as relation |
1203
|
|
|
$data = array('toOne' => array('id' => 1)); |
1204
|
|
|
|
1205
|
|
|
$entityInDatabaseWithIdOfOne = new Asset\SimpleEntity(); |
1206
|
|
|
$entityInDatabaseWithIdOfOne->setId(1); |
1207
|
|
|
$entityInDatabaseWithIdOfOne->setField('bar', false); |
1208
|
|
|
|
1209
|
|
|
$this |
|
|
|
|
1210
|
|
|
->objectManager |
1211
|
|
|
->expects($this->once()) |
1212
|
|
|
->method('find') |
1213
|
|
|
->with('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', array('id' => 1)) |
1214
|
|
|
->will($this->returnValue($entityInDatabaseWithIdOfOne)); |
1215
|
|
|
|
1216
|
|
|
$entity = $this->hydratorByReference->hydrate($data, $entity); |
1217
|
|
|
|
1218
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToOneEntity', $entity); |
1219
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $entity->getToOne(false)); |
1220
|
|
|
$this->assertSame($entityInDatabaseWithIdOfOne, $entity->getToOne(false)); |
1221
|
|
|
} |
1222
|
|
|
|
1223
|
|
|
public function testCanHydrateOneToOneAssociationByValueWithNullableRelation() |
1224
|
|
|
{ |
1225
|
|
|
// When using hydration by value, it will use the public API of the entity to retrieve values (setters) |
1226
|
|
|
$entity = new Asset\OneToOneEntity(); |
1227
|
|
|
$this->configureObjectManagerForOneToOneEntity(); |
1228
|
|
|
|
1229
|
|
|
$data = array('toOne' => null); |
1230
|
|
|
|
1231
|
|
|
$this->metadata->expects($this->once()) |
|
|
|
|
1232
|
|
|
->method('hasAssociation'); |
1233
|
|
|
|
1234
|
|
|
$object = $this->hydratorByValue->hydrate($data, $entity); |
1235
|
|
|
$this->assertNull($object->getToOne(false)); |
1236
|
|
|
} |
1237
|
|
|
|
1238
|
|
|
public function testCanHydrateOneToOneAssociationByReferenceWithNullableRelation() |
1239
|
|
|
{ |
1240
|
|
|
// When using hydration by reference, it won't use the public API of the entity to retrieve values (setters) |
1241
|
|
|
$entity = new Asset\OneToOneEntity(); |
1242
|
|
|
|
1243
|
|
|
$this->configureObjectManagerForOneToOneEntity(); |
1244
|
|
|
$this->objectManager->expects($this->never())->method('find'); |
|
|
|
|
1245
|
|
|
$this->metadata->expects($this->once())->method('hasAssociation'); |
|
|
|
|
1246
|
|
|
|
1247
|
|
|
$data = array('toOne' => null); |
1248
|
|
|
|
1249
|
|
|
$object = $this->hydratorByReference->hydrate($data, $entity); |
1250
|
|
|
$this->assertNull($object->getToOne(false)); |
1251
|
|
|
} |
1252
|
|
|
|
1253
|
|
|
public function testExtractOneToManyAssociationByValue() |
1254
|
|
|
{ |
1255
|
|
|
// When using extraction by value, it will use the public API of the entity to retrieve values (getters) |
1256
|
|
|
$toMany1 = new Asset\SimpleEntity(); |
1257
|
|
|
$toMany1->setId(2); |
1258
|
|
|
$toMany1->setField('foo', false); |
1259
|
|
|
|
1260
|
|
|
$toMany2 = new Asset\SimpleEntity(); |
1261
|
|
|
$toMany2->setId(3); |
1262
|
|
|
$toMany2->setField('bar', false); |
1263
|
|
|
|
1264
|
|
|
$collection = new ArrayCollection(array($toMany1, $toMany2)); |
1265
|
|
|
|
1266
|
|
|
$entity = new Asset\OneToManyEntity(); |
1267
|
|
|
$entity->setId(4); |
1268
|
|
|
$entity->addEntities($collection); |
1269
|
|
|
|
1270
|
|
|
$this->configureObjectManagerForOneToManyEntity(); |
1271
|
|
|
|
1272
|
|
|
$data = $this->hydratorByValue->extract($entity); |
1273
|
|
|
|
1274
|
|
|
$this->assertEquals(4, $data['id']); |
1275
|
|
|
$this->assertInstanceOf('Doctrine\Common\Collections\Collection', $data['entities']); |
1276
|
|
|
|
1277
|
|
|
$this->assertEquals($toMany1->getId(), $data['entities'][0]->getId()); |
1278
|
|
|
$this->assertSame($toMany1, $data['entities'][0]); |
1279
|
|
|
$this->assertEquals($toMany2->getId(), $data['entities'][1]->getId()); |
1280
|
|
|
$this->assertSame($toMany2, $data['entities'][1]); |
1281
|
|
|
} |
1282
|
|
|
|
1283
|
|
|
/** |
1284
|
|
|
* @depends testExtractOneToManyAssociationByValue |
1285
|
|
|
*/ |
1286
|
|
|
public function testExtractOneToManyByValueWithArray() |
1287
|
|
|
{ |
1288
|
|
|
// When using extraction by value, it will use the public API of the entity to retrieve values (getters) |
1289
|
|
|
$toMany1 = new Asset\SimpleEntity(); |
1290
|
|
|
$toMany1->setId(2); |
1291
|
|
|
$toMany1->setField('foo', false); |
1292
|
|
|
|
1293
|
|
|
$toMany2 = new Asset\SimpleEntity(); |
1294
|
|
|
$toMany2->setId(3); |
1295
|
|
|
$toMany2->setField('bar', false); |
1296
|
|
|
|
1297
|
|
|
$collection = new ArrayCollection(array($toMany1, $toMany2)); |
1298
|
|
|
|
1299
|
|
|
$entity = new Asset\OneToManyArrayEntity(); |
1300
|
|
|
$entity->setId(4); |
1301
|
|
|
$entity->addEntities($collection); |
1302
|
|
|
|
1303
|
|
|
$this->configureObjectManagerForOneToManyArrayEntity(); |
1304
|
|
|
|
1305
|
|
|
$data = $this->hydratorByValue->extract($entity); |
1306
|
|
|
|
1307
|
|
|
$this->assertEquals(4, $data['id']); |
1308
|
|
|
$this->assertInternalType('array', $data['entities']); |
1309
|
|
|
|
1310
|
|
|
$this->assertEquals($toMany1->getId(), $data['entities'][0]->getId()); |
1311
|
|
|
$this->assertSame($toMany1, $data['entities'][0]); |
1312
|
|
|
$this->assertEquals($toMany2->getId(), $data['entities'][1]->getId()); |
1313
|
|
|
$this->assertSame($toMany2, $data['entities'][1]); |
1314
|
|
|
} |
1315
|
|
|
|
1316
|
|
|
public function testExtractOneToManyAssociationByReference() |
1317
|
|
|
{ |
1318
|
|
|
// When using extraction by reference, it won't use the public API of the entity to retrieve values (getters) |
1319
|
|
|
$toMany1 = new Asset\SimpleEntity(); |
1320
|
|
|
$toMany1->setId(2); |
1321
|
|
|
$toMany1->setField('foo', false); |
1322
|
|
|
|
1323
|
|
|
$toMany2 = new Asset\SimpleEntity(); |
1324
|
|
|
$toMany2->setId(3); |
1325
|
|
|
$toMany2->setField('bar', false); |
1326
|
|
|
|
1327
|
|
|
$collection = new ArrayCollection(array($toMany1, $toMany2)); |
1328
|
|
|
|
1329
|
|
|
$entity = new Asset\OneToManyEntity(); |
1330
|
|
|
$entity->setId(4); |
1331
|
|
|
$entity->addEntities($collection); |
1332
|
|
|
|
1333
|
|
|
$this->configureObjectManagerForOneToManyEntity(); |
1334
|
|
|
|
1335
|
|
|
$data = $this->hydratorByReference->extract($entity); |
1336
|
|
|
|
1337
|
|
|
$this->assertEquals(4, $data['id']); |
1338
|
|
|
$this->assertInstanceOf('Doctrine\Common\Collections\Collection', $data['entities']); |
1339
|
|
|
|
1340
|
|
|
$this->assertEquals($toMany1->getId(), $data['entities'][0]->getId()); |
1341
|
|
|
$this->assertSame($toMany1, $data['entities'][0]); |
1342
|
|
|
$this->assertEquals($toMany2->getId(), $data['entities'][1]->getId()); |
1343
|
|
|
$this->assertSame($toMany2, $data['entities'][1]); |
1344
|
|
|
} |
1345
|
|
|
|
1346
|
|
|
/** |
1347
|
|
|
* @depends testExtractOneToManyAssociationByReference |
1348
|
|
|
*/ |
1349
|
|
|
public function testExtractOneToManyArrayByReference() |
1350
|
|
|
{ |
1351
|
|
|
// When using extraction by reference, it won't use the public API of the entity to retrieve values (getters) |
1352
|
|
|
$toMany1 = new Asset\SimpleEntity(); |
1353
|
|
|
$toMany1->setId(2); |
1354
|
|
|
$toMany1->setField('foo', false); |
1355
|
|
|
|
1356
|
|
|
$toMany2 = new Asset\SimpleEntity(); |
1357
|
|
|
$toMany2->setId(3); |
1358
|
|
|
$toMany2->setField('bar', false); |
1359
|
|
|
|
1360
|
|
|
$collection = new ArrayCollection(array($toMany1, $toMany2)); |
1361
|
|
|
|
1362
|
|
|
$entity = new Asset\OneToManyArrayEntity(); |
1363
|
|
|
$entity->setId(4); |
1364
|
|
|
$entity->addEntities($collection); |
1365
|
|
|
|
1366
|
|
|
$this->configureObjectManagerForOneToManyArrayEntity(); |
1367
|
|
|
|
1368
|
|
|
$data = $this->hydratorByReference->extract($entity); |
1369
|
|
|
|
1370
|
|
|
$this->assertEquals(4, $data['id']); |
1371
|
|
|
$this->assertInstanceOf('Doctrine\Common\Collections\Collection', $data['entities']); |
1372
|
|
|
|
1373
|
|
|
$this->assertEquals($toMany1->getId(), $data['entities'][0]->getId()); |
1374
|
|
|
$this->assertSame($toMany1, $data['entities'][0]); |
1375
|
|
|
$this->assertEquals($toMany2->getId(), $data['entities'][1]->getId()); |
1376
|
|
|
$this->assertSame($toMany2, $data['entities'][1]); |
1377
|
|
|
} |
1378
|
|
|
|
1379
|
|
|
public function testHydrateOneToManyAssociationByValue() |
1380
|
|
|
{ |
1381
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
1382
|
|
|
$toMany1 = new Asset\SimpleEntity(); |
1383
|
|
|
$toMany1->setId(2); |
1384
|
|
|
$toMany1->setField('foo', false); |
1385
|
|
|
|
1386
|
|
|
$toMany2 = new Asset\SimpleEntity(); |
1387
|
|
|
$toMany2->setId(3); |
1388
|
|
|
$toMany2->setField('bar', false); |
1389
|
|
|
|
1390
|
|
|
$entity = new Asset\OneToManyEntity(); |
1391
|
|
|
$this->configureObjectManagerForOneToManyEntity(); |
1392
|
|
|
|
1393
|
|
|
$data = array( |
1394
|
|
|
'entities' => array( |
1395
|
|
|
$toMany1, $toMany2 |
1396
|
|
|
) |
1397
|
|
|
); |
1398
|
|
|
|
1399
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
1400
|
|
|
|
1401
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntity', $entity); |
1402
|
|
|
|
1403
|
|
|
$entities = $entity->getEntities(false); |
1404
|
|
|
|
1405
|
|
|
foreach ($entities as $en) { |
1406
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $en); |
1407
|
|
|
$this->assertInternalType('integer', $en->getId()); |
1408
|
|
|
$this->assertContains('Modified from addEntities adder', $en->getField(false)); |
1409
|
|
|
} |
1410
|
|
|
|
1411
|
|
|
$this->assertEquals(2, $entities[0]->getId()); |
1412
|
|
|
$this->assertSame($toMany1, $entities[0]); |
1413
|
|
|
|
1414
|
|
|
$this->assertEquals(3, $entities[1]->getId()); |
1415
|
|
|
$this->assertSame($toMany2, $entities[1]); |
1416
|
|
|
} |
1417
|
|
|
|
1418
|
|
|
/** |
1419
|
|
|
* @depends testHydrateOneToManyAssociationByValue |
1420
|
|
|
*/ |
1421
|
|
|
public function testHydrateOneToManyArrayByValue() |
1422
|
|
|
{ |
1423
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
1424
|
|
|
$toMany1 = new Asset\SimpleEntity(); |
1425
|
|
|
$toMany1->setId(2); |
1426
|
|
|
$toMany1->setField('foo', false); |
1427
|
|
|
|
1428
|
|
|
$toMany2 = new Asset\SimpleEntity(); |
1429
|
|
|
$toMany2->setId(3); |
1430
|
|
|
$toMany2->setField('bar', false); |
1431
|
|
|
|
1432
|
|
|
$entity = new Asset\OneToManyArrayEntity(); |
1433
|
|
|
$this->configureObjectManagerForOneToManyArrayEntity(); |
1434
|
|
|
|
1435
|
|
|
$data = array( |
1436
|
|
|
'entities' => array( |
1437
|
|
|
$toMany1, $toMany2 |
1438
|
|
|
) |
1439
|
|
|
); |
1440
|
|
|
|
1441
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
1442
|
|
|
|
1443
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyArrayEntity', $entity); |
1444
|
|
|
|
1445
|
|
|
$entities = $entity->getEntities(false); |
1446
|
|
|
|
1447
|
|
|
foreach ($entities as $en) { |
1448
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $en); |
1449
|
|
|
$this->assertInternalType('integer', $en->getId()); |
1450
|
|
|
$this->assertContains('Modified from addEntities adder', $en->getField(false)); |
1451
|
|
|
} |
1452
|
|
|
|
1453
|
|
|
$this->assertEquals(2, $entities[0]->getId()); |
1454
|
|
|
$this->assertSame($toMany1, $entities[0]); |
1455
|
|
|
|
1456
|
|
|
$this->assertEquals(3, $entities[1]->getId()); |
1457
|
|
|
$this->assertSame($toMany2, $entities[1]); |
1458
|
|
|
} |
1459
|
|
|
|
1460
|
|
|
public function testHydrateOneToManyAssociationByReference() |
1461
|
|
|
{ |
1462
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
1463
|
|
|
$toMany1 = new Asset\SimpleEntity(); |
1464
|
|
|
$toMany1->setId(2); |
1465
|
|
|
$toMany1->setField('foo', false); |
1466
|
|
|
|
1467
|
|
|
$toMany2 = new Asset\SimpleEntity(); |
1468
|
|
|
$toMany2->setId(3); |
1469
|
|
|
$toMany2->setField('bar', false); |
1470
|
|
|
|
1471
|
|
|
$entity = new Asset\OneToManyEntity(); |
1472
|
|
|
$this->configureObjectManagerForOneToManyEntity(); |
1473
|
|
|
|
1474
|
|
|
$data = array( |
1475
|
|
|
'entities' => array( |
1476
|
|
|
$toMany1, $toMany2 |
1477
|
|
|
) |
1478
|
|
|
); |
1479
|
|
|
|
1480
|
|
|
$entity = $this->hydratorByReference->hydrate($data, $entity); |
1481
|
|
|
|
1482
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntity', $entity); |
1483
|
|
|
|
1484
|
|
|
$entities = $entity->getEntities(false); |
1485
|
|
|
|
1486
|
|
|
foreach ($entities as $en) { |
1487
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $en); |
1488
|
|
|
$this->assertInternalType('integer', $en->getId()); |
1489
|
|
|
$this->assertNotContains('Modified from addEntities adder', $en->getField(false)); |
1490
|
|
|
} |
1491
|
|
|
|
1492
|
|
|
$this->assertEquals(2, $entities[0]->getId()); |
1493
|
|
|
$this->assertSame($toMany1, $entities[0]); |
1494
|
|
|
|
1495
|
|
|
$this->assertEquals(3, $entities[1]->getId()); |
1496
|
|
|
$this->assertSame($toMany2, $entities[1]); |
1497
|
|
|
} |
1498
|
|
|
|
1499
|
|
|
/** |
1500
|
|
|
* @depends testHydrateOneToManyAssociationByReference |
1501
|
|
|
*/ |
1502
|
|
|
public function testHydrateOneToManyArrayByReference() |
1503
|
|
|
{ |
1504
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
1505
|
|
|
$toMany1 = new Asset\SimpleEntity(); |
1506
|
|
|
$toMany1->setId(2); |
1507
|
|
|
$toMany1->setField('foo', false); |
1508
|
|
|
|
1509
|
|
|
$toMany2 = new Asset\SimpleEntity(); |
1510
|
|
|
$toMany2->setId(3); |
1511
|
|
|
$toMany2->setField('bar', false); |
1512
|
|
|
|
1513
|
|
|
$entity = new Asset\OneToManyArrayEntity(); |
1514
|
|
|
$this->configureObjectManagerForOneToManyArrayEntity(); |
1515
|
|
|
|
1516
|
|
|
$data = array( |
1517
|
|
|
'entities' => array( |
1518
|
|
|
$toMany1, $toMany2 |
1519
|
|
|
) |
1520
|
|
|
); |
1521
|
|
|
|
1522
|
|
|
$entity = $this->hydratorByReference->hydrate($data, $entity); |
1523
|
|
|
|
1524
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyArrayEntity', $entity); |
1525
|
|
|
|
1526
|
|
|
$entities = $entity->getEntities(false); |
1527
|
|
|
|
1528
|
|
|
foreach ($entities as $en) { |
1529
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $en); |
1530
|
|
|
$this->assertInternalType('integer', $en->getId()); |
1531
|
|
|
$this->assertNotContains('Modified from addEntities adder', $en->getField(false)); |
1532
|
|
|
} |
1533
|
|
|
|
1534
|
|
|
$this->assertEquals(2, $entities[0]->getId()); |
1535
|
|
|
$this->assertSame($toMany1, $entities[0]); |
1536
|
|
|
|
1537
|
|
|
$this->assertEquals(3, $entities[1]->getId()); |
1538
|
|
|
$this->assertSame($toMany2, $entities[1]); |
1539
|
|
|
} |
1540
|
|
|
|
1541
|
|
|
public function testHydrateOneToManyAssociationByValueUsingIdentifiersForRelations() |
1542
|
|
|
{ |
1543
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
1544
|
|
|
$entity = new Asset\OneToManyEntity(); |
1545
|
|
|
$this->configureObjectManagerForOneToManyEntity(); |
1546
|
|
|
|
1547
|
|
|
$data = array( |
1548
|
|
|
'entities' => array( |
1549
|
|
|
2, 3 |
1550
|
|
|
) |
1551
|
|
|
); |
1552
|
|
|
|
1553
|
|
|
$entityInDatabaseWithIdOfTwo = new Asset\SimpleEntity(); |
1554
|
|
|
$entityInDatabaseWithIdOfTwo->setId(2); |
1555
|
|
|
$entityInDatabaseWithIdOfTwo->setField('foo', false); |
1556
|
|
|
|
1557
|
|
|
$entityInDatabaseWithIdOfThree = new Asset\SimpleEntity(); |
1558
|
|
|
$entityInDatabaseWithIdOfThree->setId(3); |
1559
|
|
|
$entityInDatabaseWithIdOfThree->setField('bar', false); |
1560
|
|
|
|
1561
|
|
|
$this |
|
|
|
|
1562
|
|
|
->objectManager |
1563
|
|
|
->expects($this->exactly(2)) |
1564
|
|
|
->method('find') |
1565
|
|
|
->with( |
1566
|
|
|
'DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', |
1567
|
|
|
$this->logicalOr($this->equalTo(array('id' => 2)), $this->equalTo(array('id' => 3))) |
1568
|
|
|
) |
1569
|
|
|
->will( |
1570
|
|
|
$this->returnCallback( |
1571
|
|
|
function ($target, $arg) use ($entityInDatabaseWithIdOfTwo, $entityInDatabaseWithIdOfThree) { |
1572
|
|
|
if ($arg['id'] === 2) { |
1573
|
|
|
return $entityInDatabaseWithIdOfTwo; |
1574
|
|
|
} elseif ($arg['id'] === 3) { |
1575
|
|
|
return $entityInDatabaseWithIdOfThree; |
1576
|
|
|
} |
1577
|
|
|
|
1578
|
|
|
throw new \InvalidArgumentException(); |
1579
|
|
|
} |
1580
|
|
|
) |
1581
|
|
|
); |
1582
|
|
|
|
1583
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
1584
|
|
|
|
1585
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntity', $entity); |
1586
|
|
|
|
1587
|
|
|
/* @var $entity \DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntity */ |
1588
|
|
|
$entities = $entity->getEntities(false); |
1589
|
|
|
|
1590
|
|
|
foreach ($entities as $en) { |
1591
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $en); |
1592
|
|
|
$this->assertInternalType('integer', $en->getId()); |
1593
|
|
|
$this->assertContains('Modified from addEntities adder', $en->getField(false)); |
1594
|
|
|
} |
1595
|
|
|
|
1596
|
|
|
$this->assertEquals(2, $entities[0]->getId()); |
1597
|
|
|
$this->assertSame($entityInDatabaseWithIdOfTwo, $entities[0]); |
1598
|
|
|
|
1599
|
|
|
$this->assertEquals(3, $entities[1]->getId()); |
1600
|
|
|
$this->assertSame($entityInDatabaseWithIdOfThree, $entities[1]); |
1601
|
|
|
} |
1602
|
|
|
|
1603
|
|
|
public function testHydrateOneToManyAssociationByValueUsingIdentifiersArrayForRelations() |
1604
|
|
|
{ |
1605
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
1606
|
|
|
$entity = new Asset\OneToManyEntity(); |
1607
|
|
|
$this->configureObjectManagerForOneToManyEntity(); |
1608
|
|
|
|
1609
|
|
|
$data = array( |
1610
|
|
|
'entities' => array( |
1611
|
|
|
array('id' => 2), |
1612
|
|
|
array('id' => 3) |
1613
|
|
|
) |
1614
|
|
|
); |
1615
|
|
|
|
1616
|
|
|
$entityInDatabaseWithIdOfTwo = new Asset\SimpleEntity(); |
1617
|
|
|
$entityInDatabaseWithIdOfTwo->setId(2); |
1618
|
|
|
$entityInDatabaseWithIdOfTwo->setField('foo', false); |
1619
|
|
|
|
1620
|
|
|
$entityInDatabaseWithIdOfThree = new Asset\SimpleEntity(); |
1621
|
|
|
$entityInDatabaseWithIdOfThree->setId(3); |
1622
|
|
|
$entityInDatabaseWithIdOfThree->setField('bar', false); |
1623
|
|
|
|
1624
|
|
|
$this |
|
|
|
|
1625
|
|
|
->objectManager |
1626
|
|
|
->expects($this->exactly(2)) |
1627
|
|
|
->method('find') |
1628
|
|
|
->with( |
1629
|
|
|
'DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', |
1630
|
|
|
$this->logicalOr($this->equalTo(array('id' => 2)), $this->equalTo(array('id' => 3))) |
1631
|
|
|
) |
1632
|
|
|
->will( |
1633
|
|
|
$this->returnCallback( |
1634
|
|
|
function ($target, $arg) use ($entityInDatabaseWithIdOfTwo, $entityInDatabaseWithIdOfThree) { |
1635
|
|
|
if ($arg['id'] === 2) { |
1636
|
|
|
return $entityInDatabaseWithIdOfTwo; |
1637
|
|
|
} elseif ($arg['id'] === 3) { |
1638
|
|
|
return $entityInDatabaseWithIdOfThree; |
1639
|
|
|
} |
1640
|
|
|
|
1641
|
|
|
throw new \InvalidArgumentException(); |
1642
|
|
|
} |
1643
|
|
|
) |
1644
|
|
|
); |
1645
|
|
|
|
1646
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
1647
|
|
|
|
1648
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntity', $entity); |
1649
|
|
|
|
1650
|
|
|
/* @var $entity \DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntity */ |
1651
|
|
|
$entities = $entity->getEntities(false); |
1652
|
|
|
|
1653
|
|
|
foreach ($entities as $en) { |
1654
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $en); |
1655
|
|
|
$this->assertInternalType('integer', $en->getId()); |
1656
|
|
|
$this->assertContains('Modified from addEntities adder', $en->getField(false)); |
1657
|
|
|
} |
1658
|
|
|
|
1659
|
|
|
$this->assertEquals(2, $entities[0]->getId()); |
1660
|
|
|
$this->assertSame($entityInDatabaseWithIdOfTwo, $entities[0]); |
1661
|
|
|
|
1662
|
|
|
$this->assertEquals(3, $entities[1]->getId()); |
1663
|
|
|
$this->assertSame($entityInDatabaseWithIdOfThree, $entities[1]); |
1664
|
|
|
} |
1665
|
|
|
|
1666
|
|
|
public function testHydrateOneToManyAssociationByReferenceUsingIdentifiersArrayForRelations() |
1667
|
|
|
{ |
1668
|
|
|
|
1669
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
1670
|
|
|
$entity = new Asset\OneToManyEntity(); |
1671
|
|
|
$this->configureObjectManagerForOneToManyEntity(); |
1672
|
|
|
|
1673
|
|
|
$data = array( |
1674
|
|
|
'entities' => array( |
1675
|
|
|
array('id' => 2), |
1676
|
|
|
array('id' => 3) |
1677
|
|
|
) |
1678
|
|
|
); |
1679
|
|
|
|
1680
|
|
|
$entityInDatabaseWithIdOfTwo = new Asset\SimpleEntity(); |
1681
|
|
|
$entityInDatabaseWithIdOfTwo->setId(2); |
1682
|
|
|
$entityInDatabaseWithIdOfTwo->setField('foo', false); |
1683
|
|
|
|
1684
|
|
|
$entityInDatabaseWithIdOfThree = new Asset\SimpleEntity(); |
1685
|
|
|
$entityInDatabaseWithIdOfThree->setId(3); |
1686
|
|
|
$entityInDatabaseWithIdOfThree->setField('bar', false); |
1687
|
|
|
|
1688
|
|
|
$this |
|
|
|
|
1689
|
|
|
->objectManager |
1690
|
|
|
->expects($this->exactly(2)) |
1691
|
|
|
->method('find') |
1692
|
|
|
->with( |
1693
|
|
|
'DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', |
1694
|
|
|
$this->logicalOr($this->equalTo(array('id' => 2)), $this->equalTo(array('id' => 3))) |
1695
|
|
|
) |
1696
|
|
|
->will( |
1697
|
|
|
$this->returnCallback( |
1698
|
|
|
function ($target, $arg) use ($entityInDatabaseWithIdOfTwo, $entityInDatabaseWithIdOfThree) { |
1699
|
|
|
if ($arg['id'] === 2) { |
1700
|
|
|
return $entityInDatabaseWithIdOfTwo; |
1701
|
|
|
} elseif ($arg['id'] === 3) { |
1702
|
|
|
return $entityInDatabaseWithIdOfThree; |
1703
|
|
|
} |
1704
|
|
|
|
1705
|
|
|
throw new \InvalidArgumentException(); |
1706
|
|
|
} |
1707
|
|
|
) |
1708
|
|
|
); |
1709
|
|
|
|
1710
|
|
|
$entity = $this->hydratorByReference->hydrate($data, $entity); |
1711
|
|
|
|
1712
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntity', $entity); |
1713
|
|
|
|
1714
|
|
|
$entities = $entity->getEntities(false); |
1715
|
|
|
|
1716
|
|
|
foreach ($entities as $en) { |
1717
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $en); |
1718
|
|
|
$this->assertInternalType('integer', $en->getId()); |
1719
|
|
|
$this->assertNotContains('Modified from addEntities adder', $en->getField(false)); |
1720
|
|
|
} |
1721
|
|
|
|
1722
|
|
|
$this->assertEquals(2, $entities[0]->getId()); |
1723
|
|
|
$this->assertSame($entityInDatabaseWithIdOfTwo, $entities[0]); |
1724
|
|
|
|
1725
|
|
|
$this->assertEquals(3, $entities[1]->getId()); |
1726
|
|
|
$this->assertSame($entityInDatabaseWithIdOfThree, $entities[1]); |
1727
|
|
|
} |
1728
|
|
|
|
1729
|
|
|
public function testHydrateOneToManyAssociationByReferenceUsingIdentifiersForRelations() |
1730
|
|
|
{ |
1731
|
|
|
// When using hydration by reference, it won't use the public API of the entity to set values (setters) |
1732
|
|
|
$entity = new Asset\OneToManyEntity(); |
1733
|
|
|
$this->configureObjectManagerForOneToManyEntity(); |
1734
|
|
|
|
1735
|
|
|
$data = array( |
1736
|
|
|
'entities' => array( |
1737
|
|
|
2, 3 |
1738
|
|
|
) |
1739
|
|
|
); |
1740
|
|
|
|
1741
|
|
|
$entityInDatabaseWithIdOfTwo = new Asset\SimpleEntity(); |
1742
|
|
|
$entityInDatabaseWithIdOfTwo->setId(2); |
1743
|
|
|
$entityInDatabaseWithIdOfTwo->setField('foo', false); |
1744
|
|
|
|
1745
|
|
|
$entityInDatabaseWithIdOfThree = new Asset\SimpleEntity(); |
1746
|
|
|
$entityInDatabaseWithIdOfThree->setId(3); |
1747
|
|
|
$entityInDatabaseWithIdOfThree->setField('bar', false); |
1748
|
|
|
|
1749
|
|
|
$this |
|
|
|
|
1750
|
|
|
->objectManager |
1751
|
|
|
->expects($this->any()) |
1752
|
|
|
->method('find') |
1753
|
|
|
->with( |
1754
|
|
|
'DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', |
1755
|
|
|
$this->logicalOr($this->equalTo(array('id' => 2)), $this->equalTo(array('id' => 3))) |
1756
|
|
|
) |
1757
|
|
|
->will( |
1758
|
|
|
$this->returnCallback( |
1759
|
|
|
function ($target, $arg) use ($entityInDatabaseWithIdOfTwo, $entityInDatabaseWithIdOfThree) { |
1760
|
|
|
if ($arg['id'] === 2) { |
1761
|
|
|
return $entityInDatabaseWithIdOfTwo; |
1762
|
|
|
} elseif ($arg['id'] === 3) { |
1763
|
|
|
return $entityInDatabaseWithIdOfThree; |
1764
|
|
|
} |
1765
|
|
|
|
1766
|
|
|
throw new \InvalidArgumentException(); |
1767
|
|
|
} |
1768
|
|
|
) |
1769
|
|
|
); |
1770
|
|
|
|
1771
|
|
|
$entity = $this->hydratorByReference->hydrate($data, $entity); |
1772
|
|
|
|
1773
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntity', $entity); |
1774
|
|
|
|
1775
|
|
|
$entities = $entity->getEntities(false); |
1776
|
|
|
|
1777
|
|
|
foreach ($entities as $en) { |
1778
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $en); |
1779
|
|
|
$this->assertInternalType('integer', $en->getId()); |
1780
|
|
|
$this->assertNotContains('Modified from addEntities adder', $en->getField(false)); |
1781
|
|
|
} |
1782
|
|
|
|
1783
|
|
|
$this->assertEquals(2, $entities[0]->getId()); |
1784
|
|
|
$this->assertSame($entityInDatabaseWithIdOfTwo, $entities[0]); |
1785
|
|
|
|
1786
|
|
|
$this->assertEquals(3, $entities[1]->getId()); |
1787
|
|
|
$this->assertSame($entityInDatabaseWithIdOfThree, $entities[1]); |
1788
|
|
|
} |
1789
|
|
|
|
1790
|
|
|
public function testHydrateOneToManyAssociationByValueUsingDisallowRemoveStrategy() |
1791
|
|
|
{ |
1792
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
1793
|
|
|
$toMany1 = new Asset\SimpleEntity(); |
1794
|
|
|
$toMany1->setId(2); |
1795
|
|
|
$toMany1->setField('foo', false); |
1796
|
|
|
|
1797
|
|
|
$toMany2 = new Asset\SimpleEntity(); |
1798
|
|
|
$toMany2->setId(3); |
1799
|
|
|
$toMany2->setField('bar', false); |
1800
|
|
|
|
1801
|
|
|
$toMany3 = new Asset\SimpleEntity(); |
1802
|
|
|
$toMany3->setId(8); |
1803
|
|
|
$toMany3->setField('baz', false); |
1804
|
|
|
|
1805
|
|
|
$entity = new Asset\OneToManyEntity(); |
1806
|
|
|
$this->configureObjectManagerForOneToManyEntity(); |
1807
|
|
|
|
1808
|
|
|
// Initially add two elements |
1809
|
|
|
$entity->addEntities(new ArrayCollection(array($toMany1, $toMany2))); |
1810
|
|
|
|
1811
|
|
|
// The hydrated collection contains two other elements, one of them is new, and one of them is missing |
1812
|
|
|
// in the new strategy |
1813
|
|
|
$data = array( |
1814
|
|
|
'entities' => array( |
1815
|
|
|
$toMany2, $toMany3 |
1816
|
|
|
) |
1817
|
|
|
); |
1818
|
|
|
|
1819
|
|
|
// Use a DisallowRemove strategy |
1820
|
|
|
$this->hydratorByValue->addStrategy('entities', new Strategy\DisallowRemoveByValue()); |
1821
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
1822
|
|
|
|
1823
|
|
|
$entities = $entity->getEntities(false); |
1824
|
|
|
|
1825
|
|
|
// DisallowStrategy should not remove existing entities in Collection even if it's not in the new collection |
1826
|
|
|
$this->assertEquals(3, count($entities)); |
1827
|
|
|
|
1828
|
|
|
foreach ($entities as $en) { |
1829
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $en); |
1830
|
|
|
$this->assertInternalType('integer', $en->getId()); |
1831
|
|
|
} |
1832
|
|
|
|
1833
|
|
|
$this->assertEquals(2, $entities[0]->getId()); |
1834
|
|
|
$this->assertSame($toMany1, $entities[0]); |
1835
|
|
|
|
1836
|
|
|
$this->assertEquals(3, $entities[1]->getId()); |
1837
|
|
|
$this->assertSame($toMany2, $entities[1]); |
1838
|
|
|
|
1839
|
|
|
$this->assertEquals(8, $entities[2]->getId()); |
1840
|
|
|
$this->assertSame($toMany3, $entities[2]); |
1841
|
|
|
} |
1842
|
|
|
|
1843
|
|
|
public function testHydrateOneToManyAssociationByReferenceUsingDisallowRemoveStrategy() |
1844
|
|
|
{ |
1845
|
|
|
// When using hydration by reference, it won't use the public API of the entity to set values (setters) |
1846
|
|
|
$toMany1 = new Asset\SimpleEntity(); |
1847
|
|
|
$toMany1->setId(2); |
1848
|
|
|
$toMany1->setField('foo', false); |
1849
|
|
|
|
1850
|
|
|
$toMany2 = new Asset\SimpleEntity(); |
1851
|
|
|
$toMany2->setId(3); |
1852
|
|
|
$toMany2->setField('bar', false); |
1853
|
|
|
|
1854
|
|
|
$toMany3 = new Asset\SimpleEntity(); |
1855
|
|
|
$toMany3->setId(8); |
1856
|
|
|
$toMany3->setField('baz', false); |
1857
|
|
|
|
1858
|
|
|
$entity = new Asset\OneToManyEntity(); |
1859
|
|
|
$this->configureObjectManagerForOneToManyEntity(); |
1860
|
|
|
|
1861
|
|
|
// Initially add two elements |
1862
|
|
|
$entity->addEntities(new ArrayCollection(array($toMany1, $toMany2))); |
1863
|
|
|
|
1864
|
|
|
// The hydrated collection contains two other elements, one of them is new, and one of them is missing |
1865
|
|
|
// in the new strategy |
1866
|
|
|
$data = array( |
1867
|
|
|
'entities' => array( |
1868
|
|
|
$toMany2, $toMany3 |
1869
|
|
|
) |
1870
|
|
|
); |
1871
|
|
|
|
1872
|
|
|
// Use a DisallowRemove strategy |
1873
|
|
|
$this->hydratorByReference->addStrategy('entities', new Strategy\DisallowRemoveByReference()); |
1874
|
|
|
$entity = $this->hydratorByReference->hydrate($data, $entity); |
1875
|
|
|
|
1876
|
|
|
$entities = $entity->getEntities(false); |
1877
|
|
|
|
1878
|
|
|
// DisallowStrategy should not remove existing entities in Collection even if it's not in the new collection |
1879
|
|
|
$this->assertEquals(3, count($entities)); |
1880
|
|
|
|
1881
|
|
|
foreach ($entities as $en) { |
1882
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $en); |
1883
|
|
|
$this->assertInternalType('integer', $en->getId()); |
1884
|
|
|
|
1885
|
|
|
// Only the third element is new so the adder has not been called on it |
1886
|
|
|
if ($en === $toMany3) { |
1887
|
|
|
$this->assertNotContains('Modified from addEntities adder', $en->getField(false)); |
1888
|
|
|
} |
1889
|
|
|
} |
1890
|
|
|
|
1891
|
|
|
$this->assertEquals(2, $entities[0]->getId()); |
1892
|
|
|
$this->assertSame($toMany1, $entities[0]); |
1893
|
|
|
|
1894
|
|
|
$this->assertEquals(3, $entities[1]->getId()); |
1895
|
|
|
$this->assertSame($toMany2, $entities[1]); |
1896
|
|
|
|
1897
|
|
|
$this->assertEquals(8, $entities[2]->getId()); |
1898
|
|
|
$this->assertSame($toMany3, $entities[2]); |
1899
|
|
|
} |
1900
|
|
|
|
1901
|
|
|
public function testHydrateOneToManyAssociationByValueWithArrayCausingDataModifications() |
1902
|
|
|
{ |
1903
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
1904
|
|
|
$data = array( |
1905
|
|
|
'entities' => array( |
1906
|
|
|
array('id' => 2, 'field' => 'Modified By Hydrate'), |
1907
|
|
|
array('id' => 3, 'field' => 'Modified By Hydrate') |
1908
|
|
|
) |
1909
|
|
|
); |
1910
|
|
|
|
1911
|
|
|
$entityInDatabaseWithIdOfTwo = new Asset\SimpleEntity(); |
1912
|
|
|
$entityInDatabaseWithIdOfTwo->setId(2); |
1913
|
|
|
$entityInDatabaseWithIdOfTwo->setField('foo', false); |
1914
|
|
|
|
1915
|
|
|
$entityInDatabaseWithIdOfThree = new Asset\SimpleEntity(); |
1916
|
|
|
$entityInDatabaseWithIdOfThree->setId(3); |
1917
|
|
|
$entityInDatabaseWithIdOfThree->setField('bar', false); |
1918
|
|
|
|
1919
|
|
|
$entity = new Asset\OneToManyEntityWithEntities( |
1920
|
|
|
new ArrayCollection(array( |
1921
|
|
|
$entityInDatabaseWithIdOfTwo, |
1922
|
|
|
$entityInDatabaseWithIdOfThree |
1923
|
|
|
)) |
1924
|
|
|
); |
1925
|
|
|
$this->configureObjectManagerForOneToManyEntity(); |
1926
|
|
|
|
1927
|
|
|
$this |
|
|
|
|
1928
|
|
|
->objectManager |
1929
|
|
|
->expects($this->exactly(2)) |
1930
|
|
|
->method('find') |
1931
|
|
|
->with( |
1932
|
|
|
'DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', |
1933
|
|
|
$this->logicalOr($this->equalTo(array('id' => 2)), $this->equalTo(array('id' => 3))) |
1934
|
|
|
) |
1935
|
|
|
->will( |
1936
|
|
|
$this->returnCallback( |
1937
|
|
|
function ($target, $arg) use ($entityInDatabaseWithIdOfTwo, $entityInDatabaseWithIdOfThree) { |
1938
|
|
|
if ($arg['id'] === 2) { |
1939
|
|
|
return $entityInDatabaseWithIdOfTwo; |
1940
|
|
|
} elseif ($arg['id'] === 3) { |
1941
|
|
|
return $entityInDatabaseWithIdOfThree; |
1942
|
|
|
} |
1943
|
|
|
|
1944
|
|
|
throw new \InvalidArgumentException(); |
1945
|
|
|
} |
1946
|
|
|
) |
1947
|
|
|
); |
1948
|
|
|
|
1949
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
1950
|
|
|
|
1951
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntityWithEntities', $entity); |
1952
|
|
|
|
1953
|
|
|
/* @var $entity \DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntity */ |
1954
|
|
|
$entities = $entity->getEntities(false); |
1955
|
|
|
|
1956
|
|
|
foreach ($entities as $en) { |
1957
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $en); |
1958
|
|
|
$this->assertInternalType('integer', $en->getId()); |
1959
|
|
|
$this->assertInternalType('string', $en->getField()); |
1960
|
|
|
$this->assertContains('Modified By Hydrate', $en->getField(false)); |
1961
|
|
|
} |
1962
|
|
|
|
1963
|
|
|
$this->assertEquals(2, $entities[0]->getId()); |
1964
|
|
|
$this->assertSame($entityInDatabaseWithIdOfTwo, $entities[0]); |
1965
|
|
|
|
1966
|
|
|
$this->assertEquals(3, $entities[1]->getId()); |
1967
|
|
|
$this->assertSame($entityInDatabaseWithIdOfThree, $entities[1]); |
1968
|
|
|
} |
1969
|
|
|
|
1970
|
|
|
|
1971
|
|
|
public function testHydrateOneToManyAssociationByValueWithTraversableCausingDataModifications() |
1972
|
|
|
{ |
1973
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
1974
|
|
|
$data = array( |
1975
|
|
|
'entities' => new ArrayCollection( |
1976
|
|
|
array( |
1977
|
|
|
array('id' => 2, 'field' => 'Modified By Hydrate'), |
1978
|
|
|
array('id' => 3, 'field' => 'Modified By Hydrate') |
1979
|
|
|
) |
1980
|
|
|
) |
1981
|
|
|
); |
1982
|
|
|
|
1983
|
|
|
$entityInDatabaseWithIdOfTwo = new Asset\SimpleEntity(); |
1984
|
|
|
$entityInDatabaseWithIdOfTwo->setId(2); |
1985
|
|
|
$entityInDatabaseWithIdOfTwo->setField('foo', false); |
1986
|
|
|
|
1987
|
|
|
$entityInDatabaseWithIdOfThree = new Asset\SimpleEntity(); |
1988
|
|
|
$entityInDatabaseWithIdOfThree->setId(3); |
1989
|
|
|
$entityInDatabaseWithIdOfThree->setField('bar', false); |
1990
|
|
|
|
1991
|
|
|
$entity = new Asset\OneToManyEntityWithEntities( |
1992
|
|
|
new ArrayCollection(array( |
1993
|
|
|
$entityInDatabaseWithIdOfTwo, |
1994
|
|
|
$entityInDatabaseWithIdOfThree |
1995
|
|
|
)) |
1996
|
|
|
); |
1997
|
|
|
$this->configureObjectManagerForOneToManyEntity(); |
1998
|
|
|
|
1999
|
|
|
$this |
|
|
|
|
2000
|
|
|
->objectManager |
2001
|
|
|
->expects($this->exactly(2)) |
2002
|
|
|
->method('find') |
2003
|
|
|
->with( |
2004
|
|
|
'DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', |
2005
|
|
|
$this->logicalOr($this->equalTo(array('id' => 2)), $this->equalTo(array('id' => 3))) |
2006
|
|
|
) |
2007
|
|
|
->will( |
2008
|
|
|
$this->returnCallback( |
2009
|
|
|
function ($target, $arg) use ($entityInDatabaseWithIdOfTwo, $entityInDatabaseWithIdOfThree) { |
2010
|
|
|
if ($arg['id'] === 2) { |
2011
|
|
|
return $entityInDatabaseWithIdOfTwo; |
2012
|
|
|
} elseif ($arg['id'] === 3) { |
2013
|
|
|
return $entityInDatabaseWithIdOfThree; |
2014
|
|
|
} |
2015
|
|
|
|
2016
|
|
|
throw new \InvalidArgumentException(); |
2017
|
|
|
} |
2018
|
|
|
) |
2019
|
|
|
); |
2020
|
|
|
|
2021
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
2022
|
|
|
|
2023
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntityWithEntities', $entity); |
2024
|
|
|
|
2025
|
|
|
/* @var $entity \DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntity */ |
2026
|
|
|
$entities = $entity->getEntities(false); |
2027
|
|
|
|
2028
|
|
|
foreach ($entities as $en) { |
2029
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $en); |
2030
|
|
|
$this->assertInternalType('integer', $en->getId()); |
2031
|
|
|
$this->assertInternalType('string', $en->getField()); |
2032
|
|
|
$this->assertContains('Modified By Hydrate', $en->getField(false)); |
2033
|
|
|
} |
2034
|
|
|
|
2035
|
|
|
$this->assertEquals(2, $entities[0]->getId()); |
2036
|
|
|
$this->assertSame($entityInDatabaseWithIdOfTwo, $entities[0]); |
2037
|
|
|
|
2038
|
|
|
$this->assertEquals(3, $entities[1]->getId()); |
2039
|
|
|
$this->assertSame($entityInDatabaseWithIdOfThree, $entities[1]); |
2040
|
|
|
} |
2041
|
|
|
|
2042
|
|
|
public function testHydrateOneToManyAssociationByValueWithStdClass() |
2043
|
|
|
{ |
2044
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
2045
|
|
|
$stdClass1 = new \StdClass(); |
2046
|
|
|
$stdClass1->id = 2; |
2047
|
|
|
|
2048
|
|
|
$stdClass2 = new \StdClass(); |
2049
|
|
|
$stdClass2->id = 3; |
2050
|
|
|
|
2051
|
|
|
$data = array('entities' => array($stdClass1, $stdClass2)); |
2052
|
|
|
|
2053
|
|
|
$entityInDatabaseWithIdOfTwo = new Asset\SimpleEntity(); |
2054
|
|
|
$entityInDatabaseWithIdOfTwo->setId(2); |
2055
|
|
|
$entityInDatabaseWithIdOfTwo->setField('foo', false); |
2056
|
|
|
|
2057
|
|
|
$entityInDatabaseWithIdOfThree = new Asset\SimpleEntity(); |
2058
|
|
|
$entityInDatabaseWithIdOfThree->setId(3); |
2059
|
|
|
$entityInDatabaseWithIdOfThree->setField('bar', false); |
2060
|
|
|
|
2061
|
|
|
$entity = new Asset\OneToManyEntityWithEntities( |
2062
|
|
|
new ArrayCollection(array( |
2063
|
|
|
$entityInDatabaseWithIdOfTwo, |
2064
|
|
|
$entityInDatabaseWithIdOfThree |
2065
|
|
|
)) |
2066
|
|
|
); |
2067
|
|
|
$this->configureObjectManagerForOneToManyEntity(); |
2068
|
|
|
|
2069
|
|
|
$this |
|
|
|
|
2070
|
|
|
->objectManager |
2071
|
|
|
->expects($this->exactly(2)) |
2072
|
|
|
->method('find') |
2073
|
|
|
->with( |
2074
|
|
|
'DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', |
2075
|
|
|
$this->logicalOr($this->equalTo(array('id' => 2)), $this->equalTo(array('id' => 3))) |
2076
|
|
|
) |
2077
|
|
|
->will( |
2078
|
|
|
$this->returnCallback( |
2079
|
|
|
function ($target, $arg) use ($entityInDatabaseWithIdOfTwo, $entityInDatabaseWithIdOfThree) { |
2080
|
|
|
if ($arg['id'] === 2) { |
2081
|
|
|
return $entityInDatabaseWithIdOfTwo; |
2082
|
|
|
} elseif ($arg['id'] === 3) { |
2083
|
|
|
return $entityInDatabaseWithIdOfThree; |
2084
|
|
|
} |
2085
|
|
|
|
2086
|
|
|
throw new \InvalidArgumentException(); |
2087
|
|
|
} |
2088
|
|
|
) |
2089
|
|
|
); |
2090
|
|
|
|
2091
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
2092
|
|
|
|
2093
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntityWithEntities', $entity); |
2094
|
|
|
|
2095
|
|
|
/* @var $entity \DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntity */ |
2096
|
|
|
$entities = $entity->getEntities(false); |
2097
|
|
|
|
2098
|
|
|
foreach ($entities as $en) { |
2099
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $en); |
2100
|
|
|
$this->assertInternalType('integer', $en->getId()); |
2101
|
|
|
} |
2102
|
|
|
|
2103
|
|
|
$this->assertEquals(2, $entities[0]->getId()); |
2104
|
|
|
$this->assertSame($entityInDatabaseWithIdOfTwo, $entities[0]); |
2105
|
|
|
|
2106
|
|
|
$this->assertEquals(3, $entities[1]->getId()); |
2107
|
|
|
$this->assertSame($entityInDatabaseWithIdOfThree, $entities[1]); |
2108
|
|
|
} |
2109
|
|
|
|
2110
|
|
|
public function testHydrateOneToManyAssociationByReferenceWithArrayCausingDataModifications() |
2111
|
|
|
{ |
2112
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
2113
|
|
|
$data = array( |
2114
|
|
|
'entities' => array( |
2115
|
|
|
array('id' => 2, 'field' => 'Modified By Hydrate'), |
2116
|
|
|
array('id' => 3, 'field' => 'Modified By Hydrate') |
2117
|
|
|
) |
2118
|
|
|
); |
2119
|
|
|
|
2120
|
|
|
$entityInDatabaseWithIdOfTwo = new Asset\SimpleEntity(); |
2121
|
|
|
$entityInDatabaseWithIdOfTwo->setId(2); |
2122
|
|
|
$entityInDatabaseWithIdOfTwo->setField('Unmodified Value', false); |
2123
|
|
|
|
2124
|
|
|
$entityInDatabaseWithIdOfThree = new Asset\SimpleEntity(); |
2125
|
|
|
$entityInDatabaseWithIdOfThree->setId(3); |
2126
|
|
|
$entityInDatabaseWithIdOfThree->setField('Unmodified Value', false); |
2127
|
|
|
|
2128
|
|
|
$entity = new Asset\OneToManyEntityWithEntities( |
2129
|
|
|
new ArrayCollection(array( |
2130
|
|
|
$entityInDatabaseWithIdOfTwo, |
2131
|
|
|
$entityInDatabaseWithIdOfThree |
2132
|
|
|
)) |
2133
|
|
|
); |
2134
|
|
|
|
2135
|
|
|
$reflSteps = array( |
2136
|
|
|
new ReflectionClass('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntityWithEntities'), |
2137
|
|
|
new ReflectionClass('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity'), |
2138
|
|
|
new ReflectionClass('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity'), |
2139
|
|
|
new ReflectionClass('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntityWithEntities'), |
2140
|
|
|
); |
2141
|
|
|
$this |
|
|
|
|
2142
|
|
|
->metadata |
2143
|
|
|
->expects($this->any()) |
2144
|
|
|
->method('getReflectionClass') |
2145
|
|
|
->will($this->returnCallback( |
2146
|
|
|
function () use (&$reflSteps) { |
2147
|
|
|
$refl = array_shift($reflSteps); |
2148
|
|
|
return $refl; |
2149
|
|
|
} |
2150
|
|
|
)); |
2151
|
|
|
|
2152
|
|
|
$this->configureObjectManagerForOneToManyEntity(); |
2153
|
|
|
|
2154
|
|
|
$this |
|
|
|
|
2155
|
|
|
->objectManager |
2156
|
|
|
->expects($this->exactly(2)) |
2157
|
|
|
->method('find') |
2158
|
|
|
->with( |
2159
|
|
|
'DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', |
2160
|
|
|
$this->logicalOr($this->equalTo(array('id' => 2)), $this->equalTo(array('id' => 3))) |
2161
|
|
|
) |
2162
|
|
|
->will( |
2163
|
|
|
$this->returnCallback( |
2164
|
|
|
function ($target, $arg) use ($entityInDatabaseWithIdOfTwo, $entityInDatabaseWithIdOfThree) { |
2165
|
|
|
if ($arg['id'] === 2) { |
2166
|
|
|
return $entityInDatabaseWithIdOfTwo; |
2167
|
|
|
} elseif ($arg['id'] === 3) { |
2168
|
|
|
return $entityInDatabaseWithIdOfThree; |
2169
|
|
|
} |
2170
|
|
|
|
2171
|
|
|
throw new \InvalidArgumentException(); |
2172
|
|
|
} |
2173
|
|
|
) |
2174
|
|
|
); |
2175
|
|
|
|
2176
|
|
|
$entity = $this->hydratorByReference->hydrate($data, $entity); |
2177
|
|
|
|
2178
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntityWithEntities', $entity); |
2179
|
|
|
|
2180
|
|
|
/* @var $entity \DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntity */ |
2181
|
|
|
$entities = $entity->getEntities(false); |
2182
|
|
|
|
2183
|
|
|
foreach ($entities as $en) { |
2184
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $en); |
2185
|
|
|
$this->assertInternalType('integer', $en->getId()); |
2186
|
|
|
$this->assertInternalType('string', $en->getField()); |
2187
|
|
|
$this->assertContains('Modified By Hydrate', $en->getField(false)); |
2188
|
|
|
} |
2189
|
|
|
|
2190
|
|
|
$this->assertEquals(2, $entities[0]->getId()); |
2191
|
|
|
$this->assertSame($entityInDatabaseWithIdOfTwo, $entities[0]); |
2192
|
|
|
|
2193
|
|
|
$this->assertEquals(3, $entities[1]->getId()); |
2194
|
|
|
$this->assertSame($entityInDatabaseWithIdOfThree, $entities[1]); |
2195
|
|
|
|
2196
|
|
|
|
2197
|
|
|
} |
2198
|
|
|
|
2199
|
|
|
public function testAssertCollectionsAreNotSwappedDuringHydration() |
2200
|
|
|
{ |
2201
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
2202
|
|
|
$entity = new Asset\OneToManyEntity(); |
2203
|
|
|
$this->configureObjectManagerForOneToManyEntity(); |
2204
|
|
|
|
2205
|
|
|
$toMany1 = new Asset\SimpleEntity(); |
2206
|
|
|
$toMany1->setId(2); |
2207
|
|
|
$toMany1->setField('foo', false); |
2208
|
|
|
|
2209
|
|
|
$toMany2 = new Asset\SimpleEntity(); |
2210
|
|
|
$toMany2->setId(3); |
2211
|
|
|
$toMany2->setField('bar', false); |
2212
|
|
|
|
2213
|
|
|
$data = array( |
2214
|
|
|
'entities' => array( |
2215
|
|
|
$toMany1, $toMany2 |
2216
|
|
|
) |
2217
|
|
|
); |
2218
|
|
|
|
2219
|
|
|
// Set the initial collection |
2220
|
|
|
$entity->addEntities(new ArrayCollection(array($toMany1, $toMany2))); |
2221
|
|
|
$initialCollection = $entity->getEntities(false); |
2222
|
|
|
|
2223
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
2224
|
|
|
|
2225
|
|
|
$modifiedCollection = $entity->getEntities(false); |
2226
|
|
|
$this->assertSame($initialCollection, $modifiedCollection); |
2227
|
|
|
} |
2228
|
|
|
|
2229
|
|
|
public function testAssertCollectionsAreNotSwappedDuringHydrationUsingIdentifiersForRelations() |
2230
|
|
|
{ |
2231
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
2232
|
|
|
$entity = new Asset\OneToManyEntity(); |
2233
|
|
|
$this->configureObjectManagerForOneToManyEntity(); |
2234
|
|
|
|
2235
|
|
|
$data = array( |
2236
|
|
|
'entities' => array( |
2237
|
|
|
2, 3 |
2238
|
|
|
) |
2239
|
|
|
); |
2240
|
|
|
|
2241
|
|
|
$entityInDatabaseWithIdOfTwo = new Asset\SimpleEntity(); |
2242
|
|
|
$entityInDatabaseWithIdOfTwo->setId(2); |
2243
|
|
|
$entityInDatabaseWithIdOfTwo->setField('foo', false); |
2244
|
|
|
|
2245
|
|
|
$entityInDatabaseWithIdOfThree = new Asset\SimpleEntity(); |
2246
|
|
|
$entityInDatabaseWithIdOfThree->setId(3); |
2247
|
|
|
$entityInDatabaseWithIdOfThree->setField('bar', false); |
2248
|
|
|
|
2249
|
|
|
// Set the initial collection |
2250
|
|
|
$entity->addEntities(new ArrayCollection(array($entityInDatabaseWithIdOfTwo, $entityInDatabaseWithIdOfThree))); |
2251
|
|
|
$initialCollection = $entity->getEntities(false); |
2252
|
|
|
|
2253
|
|
|
$this |
|
|
|
|
2254
|
|
|
->objectManager |
2255
|
|
|
->expects($this->any()) |
2256
|
|
|
->method('find') |
2257
|
|
|
->with( |
2258
|
|
|
'DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', |
2259
|
|
|
$this->logicalOr($this->equalTo(array('id' =>2)), $this->equalTo(array('id' => 3))) |
2260
|
|
|
) |
2261
|
|
|
->will( |
2262
|
|
|
$this->returnCallback( |
2263
|
|
|
function ($target, $arg) use ($entityInDatabaseWithIdOfTwo, $entityInDatabaseWithIdOfThree) { |
2264
|
|
|
if ($arg['id'] === 2) { |
2265
|
|
|
return $entityInDatabaseWithIdOfTwo; |
2266
|
|
|
} |
2267
|
|
|
|
2268
|
|
|
return $entityInDatabaseWithIdOfThree; |
2269
|
|
|
} |
2270
|
|
|
) |
2271
|
|
|
); |
2272
|
|
|
|
2273
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
2274
|
|
|
|
2275
|
|
|
$modifiedCollection = $entity->getEntities(false); |
2276
|
|
|
$this->assertSame($initialCollection, $modifiedCollection); |
2277
|
|
|
} |
2278
|
|
|
|
2279
|
|
|
public function testCanLookupsForEmptyIdentifiers() |
2280
|
|
|
{ |
2281
|
|
|
// When using hydration by reference, it won't use the public API of the entity to set values (setters) |
2282
|
|
|
$entity = new Asset\OneToManyEntity(); |
2283
|
|
|
$this->configureObjectManagerForOneToManyEntity(); |
2284
|
|
|
|
2285
|
|
|
$data = array( |
2286
|
|
|
'entities' => array( |
2287
|
|
|
'' |
2288
|
|
|
) |
2289
|
|
|
); |
2290
|
|
|
|
2291
|
|
|
$entityInDatabaseWithEmptyId = new Asset\SimpleEntity(); |
2292
|
|
|
$entityInDatabaseWithEmptyId->setId(''); |
2293
|
|
|
$entityInDatabaseWithEmptyId->setField('baz', false); |
2294
|
|
|
|
2295
|
|
|
$this |
|
|
|
|
2296
|
|
|
->objectManager |
2297
|
|
|
->expects($this->any()) |
2298
|
|
|
->method('find') |
2299
|
|
|
->with('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', '') |
2300
|
|
|
->will($this->returnValue($entityInDatabaseWithEmptyId)); |
2301
|
|
|
|
2302
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
2303
|
|
|
|
2304
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntity', $entity); |
2305
|
|
|
|
2306
|
|
|
$entities = $entity->getEntities(false); |
2307
|
|
|
$entity = $entities[0]; |
2308
|
|
|
|
2309
|
|
|
$this->assertEquals(1, count($entities)); |
2310
|
|
|
|
2311
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $entity); |
2312
|
|
|
$this->assertSame($entityInDatabaseWithEmptyId, $entity); |
2313
|
|
|
} |
2314
|
|
|
|
2315
|
|
|
public function testHandleDateTimeConversionUsingByValue() |
2316
|
|
|
{ |
2317
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
2318
|
|
|
$entity = new Asset\SimpleEntityWithDateTime(); |
2319
|
|
|
$this->configureObjectManagerForSimpleEntityWithDateTime(); |
2320
|
|
|
|
2321
|
|
|
$now = time(); |
2322
|
|
|
$data = array('date' => $now); |
2323
|
|
|
|
2324
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
2325
|
|
|
|
2326
|
|
|
$this->assertInstanceOf('DateTime', $entity->getDate()); |
2327
|
|
|
$this->assertEquals($now, $entity->getDate()->getTimestamp()); |
2328
|
|
|
} |
2329
|
|
|
|
2330
|
|
|
public function testEmptyStringIsNotConvertedToDateTime() |
2331
|
|
|
{ |
2332
|
|
|
$entity = new Asset\SimpleEntityWithDateTime(); |
2333
|
|
|
$this->configureObjectManagerForSimpleEntityWithDateTime(); |
2334
|
|
|
|
2335
|
|
|
$data = array('date' => ''); |
2336
|
|
|
|
2337
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
2338
|
|
|
|
2339
|
|
|
$this->assertNull($entity->getDate()); |
2340
|
|
|
} |
2341
|
|
|
|
2342
|
|
|
public function testAssertNullValueHydratedForOneToOneWithOptionalMethodSignature() |
2343
|
|
|
{ |
2344
|
|
|
$entity = new Asset\OneToOneEntity(); |
2345
|
|
|
|
2346
|
|
|
$this->configureObjectManagerForOneToOneEntity(); |
2347
|
|
|
$this->objectManager->expects($this->never())->method('find'); |
|
|
|
|
2348
|
|
|
|
2349
|
|
|
$data = array('toOne' => null); |
2350
|
|
|
|
2351
|
|
|
|
2352
|
|
|
$object = $this->hydratorByValue->hydrate($data, $entity); |
2353
|
|
|
$this->assertNull($object->getToOne(false)); |
2354
|
|
|
} |
2355
|
|
|
|
2356
|
|
|
public function testAssertNullValueNotUsedAsIdentifierForOneToOneWithNonOptionalMethodSignature() |
2357
|
|
|
{ |
2358
|
|
|
$entity = new Asset\OneToOneEntityNotNullable(); |
2359
|
|
|
|
2360
|
|
|
$entity->setToOne(new Asset\SimpleEntity()); |
2361
|
|
|
$this->configureObjectManagerForOneToOneEntityNotNullable(); |
2362
|
|
|
$this->objectManager->expects($this->never())->method('find'); |
|
|
|
|
2363
|
|
|
|
2364
|
|
|
$data = array('toOne' => null); |
2365
|
|
|
|
2366
|
|
|
$object = $this->hydratorByValue->hydrate($data, $entity); |
2367
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $object->getToOne(false)); |
2368
|
|
|
} |
2369
|
|
|
|
2370
|
|
|
public function testUsesStrategyOnSimpleFieldsWhenHydratingByValue() |
2371
|
|
|
{ |
2372
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
2373
|
|
|
$entity = new Asset\SimpleEntity(); |
2374
|
|
|
$this->configureObjectManagerForSimpleEntity(); |
2375
|
|
|
$data = array('field' => 'foo'); |
2376
|
|
|
|
2377
|
|
|
$this->hydratorByValue->addStrategy('field', new Asset\SimpleStrategy()); |
2378
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
2379
|
|
|
|
2380
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $entity); |
2381
|
|
|
$this->assertEquals('From setter: modified while hydrating', $entity->getField(false)); |
2382
|
|
|
} |
2383
|
|
|
|
2384
|
|
|
public function testUsesStrategyOnSimpleFieldsWhenHydratingByReference() |
2385
|
|
|
{ |
2386
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
2387
|
|
|
$entity = new Asset\SimpleEntity(); |
2388
|
|
|
$this->configureObjectManagerForSimpleEntity(); |
2389
|
|
|
$data = array('field' => 'foo'); |
2390
|
|
|
|
2391
|
|
|
$this->hydratorByReference->addStrategy('field', new Asset\SimpleStrategy()); |
2392
|
|
|
$entity = $this->hydratorByReference->hydrate($data, $entity); |
2393
|
|
|
|
2394
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $entity); |
2395
|
|
|
$this->assertEquals('modified while hydrating', $entity->getField(false)); |
2396
|
|
|
} |
2397
|
|
|
|
2398
|
|
|
public function testUsesStrategyOnSimpleFieldsWhenExtractingByValue() |
2399
|
|
|
{ |
2400
|
|
|
$entity = new Asset\SimpleEntity(); |
2401
|
|
|
$entity->setId(2); |
2402
|
|
|
$entity->setField('foo', false); |
2403
|
|
|
|
2404
|
|
|
$this->configureObjectManagerForSimpleEntity(); |
2405
|
|
|
|
2406
|
|
|
$this->hydratorByValue->addStrategy('field', new Asset\SimpleStrategy()); |
2407
|
|
|
$data = $this->hydratorByValue->extract($entity); |
2408
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $entity); |
2409
|
|
|
$this->assertEquals(array('id' => 2, 'field' => 'modified while extracting'), $data); |
2410
|
|
|
} |
2411
|
|
|
|
2412
|
|
|
public function testUsesStrategyOnSimpleFieldsWhenExtractingByReference() |
2413
|
|
|
{ |
2414
|
|
|
$entity = new Asset\SimpleEntity(); |
2415
|
|
|
$entity->setId(2); |
2416
|
|
|
$entity->setField('foo', false); |
2417
|
|
|
|
2418
|
|
|
$this->configureObjectManagerForSimpleEntity(); |
2419
|
|
|
|
2420
|
|
|
$this->hydratorByReference->addStrategy('field', new Asset\SimpleStrategy()); |
2421
|
|
|
$data = $this->hydratorByReference->extract($entity); |
2422
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $entity); |
2423
|
|
|
$this->assertEquals(array('id' => 2, 'field' => 'modified while extracting'), $data); |
2424
|
|
|
} |
2425
|
|
|
|
2426
|
|
|
public function testCanExtractIsserByValue() |
2427
|
|
|
{ |
2428
|
|
|
$entity = new Asset\SimpleIsEntity(); |
2429
|
|
|
$entity->setId(2); |
2430
|
|
|
$entity->setDone(true); |
2431
|
|
|
|
2432
|
|
|
$this->configureObjectManagerForSimpleIsEntity(); |
2433
|
|
|
|
2434
|
|
|
$data = $this->hydratorByValue->extract($entity); |
2435
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleIsEntity', $entity); |
2436
|
|
|
$this->assertEquals(array('id' => 2, 'done' => true), $data); |
2437
|
|
|
} |
2438
|
|
|
|
2439
|
|
|
public function testCanExtractIsserThatStartsWithIsByValue() |
2440
|
|
|
{ |
2441
|
|
|
$entity = new Asset\SimpleEntityWithIsBoolean(); |
2442
|
|
|
$entity->setId(2); |
2443
|
|
|
$entity->setIsActive(true); |
2444
|
|
|
|
2445
|
|
|
$this->configureObjectManagerForSimpleEntityWithIsBoolean(); |
2446
|
|
|
|
2447
|
|
|
$data = $this->hydratorByValue->extract($entity); |
2448
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntityWithIsBoolean', $entity); |
2449
|
|
|
$this->assertEquals(array('id' => 2, 'isActive' => true), $data); |
2450
|
|
|
} |
2451
|
|
|
|
2452
|
|
|
public function testExtractWithPropertyNameFilterByValue() |
2453
|
|
|
{ |
2454
|
|
|
$entity = new Asset\SimpleEntity(); |
2455
|
|
|
$entity->setId(2); |
2456
|
|
|
$entity->setField('foo', false); |
2457
|
|
|
|
2458
|
|
|
$filter = new Filter\PropertyName(array('id'), false); |
2459
|
|
|
|
2460
|
|
|
$this->configureObjectManagerForSimpleEntity(); |
2461
|
|
|
|
2462
|
|
|
$this->hydratorByValue->addFilter('propertyname', $filter); |
|
|
|
|
2463
|
|
|
$data = $this->hydratorByValue->extract($entity); |
2464
|
|
|
|
2465
|
|
|
$this->assertEquals(2, $data['id']); |
2466
|
|
|
$this->assertEquals(array('id'), array_keys($data), 'Only the "id" field should have been extracted.'); |
2467
|
|
|
} |
2468
|
|
|
|
2469
|
|
|
public function testExtractWithPropertyNameFilterByReference() |
2470
|
|
|
{ |
2471
|
|
|
$entity = new Asset\SimpleEntity(); |
2472
|
|
|
$entity->setId(2); |
2473
|
|
|
$entity->setField('foo', false); |
2474
|
|
|
|
2475
|
|
|
$filter = new Filter\PropertyName(array('id'), false); |
2476
|
|
|
|
2477
|
|
|
$this->configureObjectManagerForSimpleEntity(); |
2478
|
|
|
|
2479
|
|
|
$this->hydratorByReference->addFilter('propertyname', $filter); |
|
|
|
|
2480
|
|
|
$data = $this->hydratorByReference->extract($entity); |
2481
|
|
|
|
2482
|
|
|
$this->assertEquals(2, $data['id']); |
2483
|
|
|
$this->assertEquals(array('id'), array_keys($data), 'Only the "id" field should have been extracted.'); |
2484
|
|
|
} |
2485
|
|
|
|
2486
|
|
|
public function testExtractByReferenceUsesNamingStrategy() |
2487
|
|
|
{ |
2488
|
|
|
$this->configureObjectManagerForNamingStrategyEntity(); |
2489
|
|
|
$name = 'Foo'; |
2490
|
|
|
$this->hydratorByReference->setNamingStrategy(new UnderscoreNamingStrategy()); |
2491
|
|
|
$data = $this->hydratorByReference->extract(new NamingStrategyEntity($name)); |
2492
|
|
|
$this->assertEquals($name, $data['camel_case']); |
2493
|
|
|
} |
2494
|
|
|
|
2495
|
|
|
public function testExtractByValueUsesNamingStrategy() |
2496
|
|
|
{ |
2497
|
|
|
$this->configureObjectManagerForNamingStrategyEntity(); |
2498
|
|
|
$name = 'Bar'; |
2499
|
|
|
$this->hydratorByValue->setNamingStrategy(new UnderscoreNamingStrategy()); |
2500
|
|
|
$data = $this->hydratorByValue->extract(new NamingStrategyEntity($name)); |
2501
|
|
|
$this->assertEquals($name, $data['camel_case']); |
2502
|
|
|
} |
2503
|
|
|
|
2504
|
|
|
public function testHydrateByReferenceUsesNamingStrategy() |
2505
|
|
|
{ |
2506
|
|
|
$this->configureObjectManagerForNamingStrategyEntity(); |
2507
|
|
|
$name = 'Baz'; |
2508
|
|
|
$this->hydratorByReference->setNamingStrategy(new UnderscoreNamingStrategy()); |
2509
|
|
|
$entity = $this->hydratorByReference->hydrate(array('camel_case' => $name), new NamingStrategyEntity()); |
2510
|
|
|
$this->assertEquals($name, $entity->getCamelCase()); |
2511
|
|
|
} |
2512
|
|
|
|
2513
|
|
|
public function testHydrateByValueUsesNamingStrategy() |
2514
|
|
|
{ |
2515
|
|
|
$this->configureObjectManagerForNamingStrategyEntity(); |
2516
|
|
|
$name = 'Qux'; |
2517
|
|
|
$this->hydratorByValue->setNamingStrategy(new UnderscoreNamingStrategy()); |
2518
|
|
|
$entity = $this->hydratorByValue->hydrate(array('camel_case' => $name), new NamingStrategyEntity()); |
2519
|
|
|
$this->assertEquals($name, $entity->getCamelCase()); |
2520
|
|
|
} |
2521
|
|
|
} |
2522
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: