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\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([])); |
66
|
|
|
|
67
|
|
|
$this |
68
|
|
|
->metadata |
69
|
|
|
->expects($this->any()) |
70
|
|
|
->method('getFieldNames') |
71
|
|
|
->will($this->returnValue(['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(['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([])); |
134
|
|
|
|
135
|
|
|
$this |
136
|
|
|
->metadata |
137
|
|
|
->expects($this->any()) |
138
|
|
|
->method('getFieldNames') |
139
|
|
|
->will($this->returnValue(['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(['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([])); |
190
|
|
|
|
191
|
|
|
$this |
192
|
|
|
->metadata |
193
|
|
|
->expects($this->any()) |
194
|
|
|
->method('getFieldNames') |
195
|
|
|
->will($this->returnValue(['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(['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([])); |
258
|
|
|
|
259
|
|
|
$this |
260
|
|
|
->metadata |
261
|
|
|
->expects($this->any()) |
262
|
|
|
->method('getFieldNames') |
263
|
|
|
->will($this->returnValue(['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(['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([])); |
326
|
|
|
|
327
|
|
|
$this |
328
|
|
|
->metadata |
329
|
|
|
->expects($this->any()) |
330
|
|
|
->method('getFieldNames') |
331
|
|
|
->will($this->returnValue(['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(['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([])); |
377
|
|
|
|
378
|
|
|
$this |
379
|
|
|
->metadata |
380
|
|
|
->expects($this->any()) |
381
|
|
|
->method('getFieldNames') |
382
|
|
|
->will($this->returnValue(['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(['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(['id'])); |
440
|
|
|
|
441
|
|
|
$this |
442
|
|
|
->metadata |
443
|
|
|
->expects($this->any()) |
444
|
|
|
->method('getAssociationNames') |
445
|
|
|
->will($this->returnValue(['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(["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(['id'])); |
530
|
|
|
|
531
|
|
|
$this |
532
|
|
|
->metadata |
533
|
|
|
->expects($this->any()) |
534
|
|
|
->method('getAssociationNames') |
535
|
|
|
->will($this->returnValue(['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(["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(['id'])); |
634
|
|
|
|
635
|
|
|
$this |
636
|
|
|
->metadata |
637
|
|
|
->expects($this->any()) |
638
|
|
|
->method('getAssociationNames') |
639
|
|
|
->will($this->returnValue(['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(["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
|
|
|
public function configureObjectManagerForOneToManyArrayEntity() |
732
|
|
|
{ |
733
|
|
|
$refl = new ReflectionClass('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyArrayEntity'); |
734
|
|
|
|
735
|
|
|
$this |
|
|
|
|
736
|
|
|
->metadata |
737
|
|
|
->expects($this->any()) |
738
|
|
|
->method('getFieldNames') |
739
|
|
|
->will($this->returnValue(['id'])); |
740
|
|
|
|
741
|
|
|
$this |
742
|
|
|
->metadata |
743
|
|
|
->expects($this->any()) |
744
|
|
|
->method('getAssociationNames') |
745
|
|
|
->will($this->returnValue(['entities'])); |
746
|
|
|
|
747
|
|
|
$this |
748
|
|
|
->metadata |
749
|
|
|
->expects($this->any()) |
750
|
|
|
->method('getTypeOfField') |
751
|
|
|
->with( |
752
|
|
|
$this->logicalOr( |
753
|
|
|
$this->equalTo('id'), |
754
|
|
|
$this->equalTo('entities'), |
755
|
|
|
$this->equalTo('field') |
756
|
|
|
) |
757
|
|
|
) |
758
|
|
|
->will( |
759
|
|
|
$this->returnCallback( |
760
|
|
|
function ($arg) { |
761
|
|
|
if ($arg === 'id') { |
762
|
|
|
return 'integer'; |
763
|
|
|
} elseif ($arg === 'field') { |
764
|
|
|
return 'string'; |
765
|
|
|
} elseif ($arg === 'entities') { |
766
|
|
|
return 'Doctrine\Common\Collections\ArrayCollection'; |
767
|
|
|
} |
768
|
|
|
|
769
|
|
|
throw new \InvalidArgumentException(); |
770
|
|
|
} |
771
|
|
|
) |
772
|
|
|
); |
773
|
|
|
|
774
|
|
|
$this |
|
|
|
|
775
|
|
|
->metadata |
776
|
|
|
->expects($this->any()) |
777
|
|
|
->method('hasAssociation') |
778
|
|
|
->with($this->logicalOr($this->equalTo('id'), $this->equalTo('entities'))) |
779
|
|
|
->will( |
780
|
|
|
$this->returnCallback( |
781
|
|
|
function ($arg) { |
782
|
|
|
if ($arg === 'id') { |
783
|
|
|
return false; |
784
|
|
|
} elseif ($arg === 'field') { |
785
|
|
|
return 'string'; |
786
|
|
|
} elseif ($arg === 'entities') { |
787
|
|
|
return true; |
788
|
|
|
} |
789
|
|
|
|
790
|
|
|
throw new \InvalidArgumentException(); |
791
|
|
|
} |
792
|
|
|
) |
793
|
|
|
); |
794
|
|
|
|
795
|
|
|
$this |
|
|
|
|
796
|
|
|
->metadata |
797
|
|
|
->expects($this->any()) |
798
|
|
|
->method('isSingleValuedAssociation') |
799
|
|
|
->with('entities') |
800
|
|
|
->will($this->returnValue(false)); |
801
|
|
|
|
802
|
|
|
$this |
803
|
|
|
->metadata |
804
|
|
|
->expects($this->any()) |
805
|
|
|
->method('isCollectionValuedAssociation') |
806
|
|
|
->with('entities') |
807
|
|
|
->will($this->returnValue(true)); |
808
|
|
|
|
809
|
|
|
$this |
810
|
|
|
->metadata |
811
|
|
|
->expects($this->any()) |
812
|
|
|
->method('getAssociationTargetClass') |
813
|
|
|
->with('entities') |
814
|
|
|
->will($this->returnValue('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity')); |
815
|
|
|
|
816
|
|
|
$this |
817
|
|
|
->metadata |
818
|
|
|
->expects($this->any()) |
819
|
|
|
->method('getReflectionClass') |
820
|
|
|
->will($this->returnValue($refl)); |
821
|
|
|
|
822
|
|
|
$this->metadata |
823
|
|
|
->expects($this->any()) |
824
|
|
|
->method('getIdentifier') |
825
|
|
|
->will($this->returnValue(["id"])); |
826
|
|
|
|
827
|
|
|
$this->hydratorByValue = new DoctrineObjectHydrator( |
828
|
|
|
$this->objectManager, |
829
|
|
|
true |
830
|
|
|
); |
831
|
|
|
$this->hydratorByReference = new DoctrineObjectHydrator( |
832
|
|
|
$this->objectManager, |
833
|
|
|
false |
834
|
|
|
); |
835
|
|
|
} |
836
|
|
|
|
837
|
|
|
public function testObjectIsPassedForContextToStrategies() |
838
|
|
|
{ |
839
|
|
|
$entity = new Asset\ContextEntity(); |
840
|
|
|
$entity->setId(2); |
841
|
|
|
$entity->setField('foo', false); |
|
|
|
|
842
|
|
|
|
843
|
|
|
$this->configureObjectManagerForSimpleEntity(); |
844
|
|
|
|
845
|
|
|
$hydrator = $this->hydratorByValue; |
846
|
|
|
$entity = $hydrator->hydrate(['id' => 3, 'field' => 'bar'], $entity); |
847
|
|
|
$this->assertEquals(['id' => 3, 'field' => 'bar'], $hydrator->extract($entity)); |
848
|
|
|
|
849
|
|
|
$hydrator->addStrategy('id', new ContextStrategy()); |
850
|
|
|
$entity = $hydrator->hydrate(['id' => 3, 'field' => 'bar'], $entity); |
851
|
|
|
$this->assertEquals(['id' => '3barbar', 'field' => 'bar'], $hydrator->extract($entity)); |
852
|
|
|
} |
853
|
|
|
|
854
|
|
|
public function testCanExtractSimpleEntityByValue() |
855
|
|
|
{ |
856
|
|
|
// When using extraction by value, it will use the public API of the entity to retrieve values (getters) |
857
|
|
|
$entity = new Asset\SimpleEntity(); |
858
|
|
|
$entity->setId(2); |
859
|
|
|
$entity->setField('foo', false); |
860
|
|
|
|
861
|
|
|
$this->configureObjectManagerForSimpleEntity(); |
862
|
|
|
|
863
|
|
|
$data = $this->hydratorByValue->extract($entity); |
864
|
|
|
$this->assertEquals(['id' => 2, 'field' => 'From getter: foo'], $data); |
865
|
|
|
} |
866
|
|
|
|
867
|
|
|
public function testCanExtractSimpleEntityByReference() |
868
|
|
|
{ |
869
|
|
|
// When using extraction by reference, it won't use the public API of entity (getters won't be called) |
870
|
|
|
$entity = new Asset\SimpleEntity(); |
871
|
|
|
$entity->setId(2); |
872
|
|
|
$entity->setField('foo', false); |
873
|
|
|
|
874
|
|
|
$this->configureObjectManagerForSimpleEntity(); |
875
|
|
|
|
876
|
|
|
$data = $this->hydratorByReference->extract($entity); |
877
|
|
|
$this->assertEquals(['id' => 2, 'field' => 'foo'], $data); |
878
|
|
|
} |
879
|
|
|
|
880
|
|
|
public function testCanHydrateSimpleEntityByValue() |
881
|
|
|
{ |
882
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
883
|
|
|
$entity = new Asset\SimpleEntity(); |
884
|
|
|
$this->configureObjectManagerForSimpleEntity(); |
885
|
|
|
$data = ['field' => 'foo']; |
886
|
|
|
|
887
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
888
|
|
|
|
889
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $entity); |
890
|
|
|
$this->assertEquals('From setter: foo', $entity->getField(false)); |
891
|
|
|
} |
892
|
|
|
|
893
|
|
|
/** |
894
|
|
|
* When using hydration by value, it will use the public API of the entity to set values (setters) |
895
|
|
|
* |
896
|
|
|
* @covers \DoctrineModule\Stdlib\Hydrator\DoctrineObject::hydrateByValue |
897
|
|
|
*/ |
898
|
|
|
public function testCanHydrateSimpleEntityWithStringIdByValue() |
899
|
|
|
{ |
900
|
|
|
$entity = new Asset\SimpleEntity(); |
901
|
|
|
$data = ['id' => 'bar', 'field' => 'foo']; |
902
|
|
|
|
903
|
|
|
$this->configureObjectManagerForSimpleEntityWithStringId(); |
904
|
|
|
|
905
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
906
|
|
|
|
907
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $entity); |
908
|
|
|
$this->assertEquals('From setter: foo', $entity->getField(false)); |
909
|
|
|
} |
910
|
|
|
|
911
|
|
|
public function testCanHydrateSimpleEntityByReference() |
912
|
|
|
{ |
913
|
|
|
// When using hydration by reference, it won't use the public API of the entity to set values (setters) |
914
|
|
|
$entity = new Asset\SimpleEntity(); |
915
|
|
|
$this->configureObjectManagerForSimpleEntity(); |
916
|
|
|
$data = ['field' => 'foo']; |
917
|
|
|
|
918
|
|
|
$entity = $this->hydratorByReference->hydrate($data, $entity); |
919
|
|
|
|
920
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $entity); |
921
|
|
|
$this->assertEquals('foo', $entity->getField(false)); |
922
|
|
|
} |
923
|
|
|
|
924
|
|
|
/** |
925
|
|
|
* When using hydration by reference, it won't use the public API of the entity to set values (getters) |
926
|
|
|
* |
927
|
|
|
* @covers \DoctrineModule\Stdlib\Hydrator\DoctrineObject::hydrateByReference |
928
|
|
|
*/ |
929
|
|
|
public function testCanHydrateSimpleEntityWithStringIdByReference() |
930
|
|
|
{ |
931
|
|
|
$entity = new Asset\SimpleEntity(); |
932
|
|
|
$data = ['id' => 'bar', 'field' => 'foo']; |
933
|
|
|
|
934
|
|
|
$this->configureObjectManagerForSimpleEntityWithStringId(); |
935
|
|
|
|
936
|
|
|
$entity = $this->hydratorByReference->hydrate($data, $entity); |
937
|
|
|
|
938
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $entity); |
939
|
|
|
$this->assertEquals('foo', $entity->getField(false)); |
940
|
|
|
} |
941
|
|
|
|
942
|
|
|
public function testReuseExistingEntityIfDataArrayContainsIdentifier() |
943
|
|
|
{ |
944
|
|
|
// When using hydration by reference, it won't use the public API of the entity to set values (setters) |
945
|
|
|
$entity = new Asset\SimpleEntity(); |
946
|
|
|
|
947
|
|
|
$this->configureObjectManagerForSimpleEntity(); |
948
|
|
|
$data = ['id' => 1]; |
949
|
|
|
|
950
|
|
|
$entityInDatabaseWithIdOfOne = new Asset\SimpleEntity(); |
951
|
|
|
$entityInDatabaseWithIdOfOne->setId(1); |
952
|
|
|
$entityInDatabaseWithIdOfOne->setField('bar', false); |
953
|
|
|
|
954
|
|
|
$this |
|
|
|
|
955
|
|
|
->objectManager |
956
|
|
|
->expects($this->once()) |
957
|
|
|
->method('find') |
958
|
|
|
->with('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', ['id' => 1]) |
959
|
|
|
->will($this->returnValue($entityInDatabaseWithIdOfOne)); |
960
|
|
|
|
961
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
962
|
|
|
|
963
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $entity); |
964
|
|
|
$this->assertEquals('bar', $entity->getField(false)); |
965
|
|
|
} |
966
|
|
|
|
967
|
|
|
/** |
968
|
|
|
* Test for https://github.com/doctrine/DoctrineModule/issues/456 |
969
|
|
|
*/ |
970
|
|
|
public function testReuseExistingEntityIfDataArrayContainsIdentifierWithZeroIdentifier() |
971
|
|
|
{ |
972
|
|
|
// When using hydration by reference, it won't use the public API of the entity to set values (setters) |
973
|
|
|
$entity = new Asset\SimpleEntity(); |
974
|
|
|
|
975
|
|
|
$this->configureObjectManagerForSimpleEntity(); |
976
|
|
|
$data = ['id' => 0]; |
977
|
|
|
|
978
|
|
|
$entityInDatabaseWithIdOfOne = new Asset\SimpleEntity(); |
979
|
|
|
$entityInDatabaseWithIdOfOne->setId(0); |
980
|
|
|
$entityInDatabaseWithIdOfOne->setField('bar', false); |
981
|
|
|
|
982
|
|
|
$this |
|
|
|
|
983
|
|
|
->objectManager |
984
|
|
|
->expects($this->once()) |
985
|
|
|
->method('find') |
986
|
|
|
->with('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', ['id' => 0]) |
987
|
|
|
->will($this->returnValue($entityInDatabaseWithIdOfOne)); |
988
|
|
|
|
989
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
990
|
|
|
|
991
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $entity); |
992
|
|
|
$this->assertEquals('bar', $entity->getField(false)); |
993
|
|
|
} |
994
|
|
|
|
995
|
|
|
public function testExtractOneToOneAssociationByValue() |
996
|
|
|
{ |
997
|
|
|
// When using extraction by value, it will use the public API of the entity to retrieve values (getters) |
998
|
|
|
$toOne = new Asset\SimpleEntity(); |
999
|
|
|
$toOne->setId(2); |
1000
|
|
|
$toOne->setField('foo', false); |
1001
|
|
|
|
1002
|
|
|
$entity = new Asset\OneToOneEntity(); |
1003
|
|
|
$entity->setId(2); |
1004
|
|
|
$entity->setToOne($toOne); |
1005
|
|
|
|
1006
|
|
|
$this->configureObjectManagerForOneToOneEntity(); |
1007
|
|
|
|
1008
|
|
|
$data = $this->hydratorByValue->extract($entity); |
1009
|
|
|
|
1010
|
|
|
$this->assertEquals(2, $data['id']); |
1011
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $data['toOne']); |
1012
|
|
|
$this->assertEquals('Modified from getToOne getter', $data['toOne']->getField(false)); |
1013
|
|
|
$this->assertSame($toOne, $data['toOne']); |
1014
|
|
|
} |
1015
|
|
|
|
1016
|
|
|
public function testExtractOneToOneAssociationByReference() |
1017
|
|
|
{ |
1018
|
|
|
// When using extraction by value, it will use the public API of the entity to retrieve values (getters) |
1019
|
|
|
$toOne = new Asset\SimpleEntity(); |
1020
|
|
|
$toOne->setId(2); |
1021
|
|
|
$toOne->setField('foo', false); |
1022
|
|
|
|
1023
|
|
|
$entity = new Asset\OneToOneEntity(); |
1024
|
|
|
$entity->setId(2); |
1025
|
|
|
$entity->setToOne($toOne, false); |
1026
|
|
|
|
1027
|
|
|
$this->configureObjectManagerForOneToOneEntity(); |
1028
|
|
|
|
1029
|
|
|
$data = $this->hydratorByReference->extract($entity); |
1030
|
|
|
|
1031
|
|
|
$this->assertEquals(2, $data['id']); |
1032
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $data['toOne']); |
1033
|
|
|
$this->assertEquals('foo', $data['toOne']->getField(false)); |
1034
|
|
|
$this->assertSame($toOne, $data['toOne']); |
1035
|
|
|
} |
1036
|
|
|
|
1037
|
|
|
public function testHydrateOneToOneAssociationByValue() |
1038
|
|
|
{ |
1039
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
1040
|
|
|
$toOne = new Asset\SimpleEntity(); |
1041
|
|
|
$toOne->setId(2); |
1042
|
|
|
$toOne->setField('foo', false); |
1043
|
|
|
|
1044
|
|
|
$entity = new Asset\OneToOneEntity(); |
1045
|
|
|
$this->configureObjectManagerForOneToOneEntity(); |
1046
|
|
|
|
1047
|
|
|
$data = ['toOne' => $toOne]; |
1048
|
|
|
|
1049
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
1050
|
|
|
|
1051
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToOneEntity', $entity); |
1052
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $entity->getToOne(false)); |
1053
|
|
|
$this->assertEquals('Modified from setToOne setter', $entity->getToOne(false)->getField(false)); |
1054
|
|
|
} |
1055
|
|
|
|
1056
|
|
|
public function testHydrateOneToOneAssociationByReference() |
1057
|
|
|
{ |
1058
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
1059
|
|
|
$toOne = new Asset\SimpleEntity(); |
1060
|
|
|
$toOne->setId(2); |
1061
|
|
|
$toOne->setField('foo', false); |
1062
|
|
|
|
1063
|
|
|
$entity = new Asset\OneToOneEntity(); |
1064
|
|
|
$this->configureObjectManagerForOneToOneEntity(); |
1065
|
|
|
|
1066
|
|
|
$data = ['toOne' => $toOne]; |
1067
|
|
|
|
1068
|
|
|
$entity = $this->hydratorByReference->hydrate($data, $entity); |
1069
|
|
|
|
1070
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToOneEntity', $entity); |
1071
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $entity->getToOne(false)); |
1072
|
|
|
$this->assertEquals('foo', $entity->getToOne(false)->getField(false)); |
1073
|
|
|
} |
1074
|
|
|
|
1075
|
|
|
public function testHydrateOneToOneAssociationByValueUsingIdentifierForRelation() |
1076
|
|
|
{ |
1077
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
1078
|
|
|
$entity = new Asset\OneToOneEntity(); |
1079
|
|
|
$this->configureObjectManagerForOneToOneEntity(); |
1080
|
|
|
|
1081
|
|
|
// Use entity of id 1 as relation |
1082
|
|
|
$data = ['toOne' => 1]; |
1083
|
|
|
|
1084
|
|
|
$entityInDatabaseWithIdOfOne = new Asset\SimpleEntity(); |
1085
|
|
|
$entityInDatabaseWithIdOfOne->setId(1); |
1086
|
|
|
$entityInDatabaseWithIdOfOne->setField('bar', false); |
1087
|
|
|
|
1088
|
|
|
$this |
|
|
|
|
1089
|
|
|
->objectManager |
1090
|
|
|
->expects($this->once()) |
1091
|
|
|
->method('find') |
1092
|
|
|
->with('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', 1) |
1093
|
|
|
->will($this->returnValue($entityInDatabaseWithIdOfOne)); |
1094
|
|
|
|
1095
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
1096
|
|
|
|
1097
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToOneEntity', $entity); |
1098
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $entity->getToOne(false)); |
1099
|
|
|
$this->assertSame($entityInDatabaseWithIdOfOne, $entity->getToOne(false)); |
1100
|
|
|
} |
1101
|
|
|
|
1102
|
|
|
public function testHydrateOneToOneAssociationByReferenceUsingIdentifierForRelation() |
1103
|
|
|
{ |
1104
|
|
|
// When using hydration by reference, it won't use the public API of the entity to set values (setters) |
1105
|
|
|
$entity = new Asset\OneToOneEntity(); |
1106
|
|
|
$this->configureObjectManagerForOneToOneEntity(); |
1107
|
|
|
|
1108
|
|
|
// Use entity of id 1 as relation |
1109
|
|
|
$data = ['toOne' => 1]; |
1110
|
|
|
|
1111
|
|
|
$entityInDatabaseWithIdOfOne = new Asset\SimpleEntity(); |
1112
|
|
|
$entityInDatabaseWithIdOfOne->setId(1); |
1113
|
|
|
$entityInDatabaseWithIdOfOne->setField('bar', false); |
1114
|
|
|
|
1115
|
|
|
$this |
|
|
|
|
1116
|
|
|
->objectManager |
1117
|
|
|
->expects($this->once()) |
1118
|
|
|
->method('find') |
1119
|
|
|
->with('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', 1) |
1120
|
|
|
->will($this->returnValue($entityInDatabaseWithIdOfOne)); |
1121
|
|
|
|
1122
|
|
|
$entity = $this->hydratorByReference->hydrate($data, $entity); |
1123
|
|
|
|
1124
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToOneEntity', $entity); |
1125
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $entity->getToOne(false)); |
1126
|
|
|
$this->assertSame($entityInDatabaseWithIdOfOne, $entity->getToOne(false)); |
1127
|
|
|
} |
1128
|
|
|
|
1129
|
|
|
public function testHydrateOneToOneAssociationByValueUsingIdentifierArrayForRelation() |
1130
|
|
|
{ |
1131
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
1132
|
|
|
$entity = new Asset\OneToOneEntity(); |
1133
|
|
|
$this->configureObjectManagerForOneToOneEntity(); |
1134
|
|
|
|
1135
|
|
|
// Use entity of id 1 as relation |
1136
|
|
|
$data = ['toOne' => ['id' => 1]]; |
1137
|
|
|
|
1138
|
|
|
$entityInDatabaseWithIdOfOne = new Asset\SimpleEntity(); |
1139
|
|
|
$entityInDatabaseWithIdOfOne->setId(1); |
1140
|
|
|
$entityInDatabaseWithIdOfOne->setField('bar', false); |
1141
|
|
|
|
1142
|
|
|
$this |
|
|
|
|
1143
|
|
|
->objectManager |
1144
|
|
|
->expects($this->once()) |
1145
|
|
|
->method('find') |
1146
|
|
|
->with('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', ['id' => 1]) |
1147
|
|
|
->will($this->returnValue($entityInDatabaseWithIdOfOne)); |
1148
|
|
|
|
1149
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
1150
|
|
|
|
1151
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToOneEntity', $entity); |
1152
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $entity->getToOne(false)); |
1153
|
|
|
$this->assertSame($entityInDatabaseWithIdOfOne, $entity->getToOne(false)); |
1154
|
|
|
} |
1155
|
|
|
|
1156
|
|
|
public function testHydrateOneToOneAssociationByValueUsingFullArrayForRelation() |
1157
|
|
|
{ |
1158
|
|
|
$entity = new Asset\OneToOneEntityNotNullable; |
1159
|
|
|
$this->configureObjectManagerForOneToOneEntityNotNullable(); |
1160
|
|
|
|
1161
|
|
|
// Use entity of id 1 as relation |
1162
|
|
|
$data = ['toOne' => ['id' => 1, 'field' => 'foo']]; |
1163
|
|
|
|
1164
|
|
|
$entityInDatabaseWithIdOfOne = new Asset\SimpleEntity(); |
1165
|
|
|
$entityInDatabaseWithIdOfOne->setId(1); |
1166
|
|
|
$entityInDatabaseWithIdOfOne->setField('bar', false); |
1167
|
|
|
|
1168
|
|
|
$this |
|
|
|
|
1169
|
|
|
->objectManager |
1170
|
|
|
->expects($this->once()) |
1171
|
|
|
->method('find') |
1172
|
|
|
->with( |
1173
|
|
|
'DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', |
1174
|
|
|
['id' => 1] |
1175
|
|
|
) |
1176
|
|
|
->will($this->returnValue($entityInDatabaseWithIdOfOne)); |
1177
|
|
|
|
1178
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
1179
|
|
|
|
1180
|
|
|
$this->assertInstanceOf( |
1181
|
|
|
'DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToOneEntityNotNullable', |
1182
|
|
|
$entity |
1183
|
|
|
); |
1184
|
|
|
$this->assertInstanceOf( |
1185
|
|
|
'DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', |
1186
|
|
|
$entity->getToOne(false) |
1187
|
|
|
); |
1188
|
|
|
$this->assertSame($entityInDatabaseWithIdOfOne, $entity->getToOne(false)); |
1189
|
|
|
$this->assertEquals( |
1190
|
|
|
'From getter: Modified from setToOne setter', |
1191
|
|
|
$entityInDatabaseWithIdOfOne->getField() |
1192
|
|
|
); |
1193
|
|
|
} |
1194
|
|
|
|
1195
|
|
|
public function testHydrateOneToOneAssociationByReferenceUsingIdentifierArrayForRelation() |
1196
|
|
|
{ |
1197
|
|
|
// When using hydration by reference, it won't use the public API of the entity to set values (setters) |
1198
|
|
|
$entity = new Asset\OneToOneEntity(); |
1199
|
|
|
$this->configureObjectManagerForOneToOneEntity(); |
1200
|
|
|
|
1201
|
|
|
// Use entity of id 1 as relation |
1202
|
|
|
$data = ['toOne' => ['id' => 1]]; |
1203
|
|
|
|
1204
|
|
|
$entityInDatabaseWithIdOfOne = new Asset\SimpleEntity(); |
1205
|
|
|
$entityInDatabaseWithIdOfOne->setId(1); |
1206
|
|
|
$entityInDatabaseWithIdOfOne->setField('bar', false); |
1207
|
|
|
|
1208
|
|
|
$this |
|
|
|
|
1209
|
|
|
->objectManager |
1210
|
|
|
->expects($this->once()) |
1211
|
|
|
->method('find') |
1212
|
|
|
->with('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', ['id' => 1]) |
1213
|
|
|
->will($this->returnValue($entityInDatabaseWithIdOfOne)); |
1214
|
|
|
|
1215
|
|
|
$entity = $this->hydratorByReference->hydrate($data, $entity); |
1216
|
|
|
|
1217
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToOneEntity', $entity); |
1218
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $entity->getToOne(false)); |
1219
|
|
|
$this->assertSame($entityInDatabaseWithIdOfOne, $entity->getToOne(false)); |
1220
|
|
|
} |
1221
|
|
|
|
1222
|
|
|
public function testCanHydrateOneToOneAssociationByValueWithNullableRelation() |
1223
|
|
|
{ |
1224
|
|
|
// When using hydration by value, it will use the public API of the entity to retrieve values (setters) |
1225
|
|
|
$entity = new Asset\OneToOneEntity(); |
1226
|
|
|
$this->configureObjectManagerForOneToOneEntity(); |
1227
|
|
|
|
1228
|
|
|
$data = ['toOne' => null]; |
1229
|
|
|
|
1230
|
|
|
$this->metadata->expects($this->once()) |
|
|
|
|
1231
|
|
|
->method('hasAssociation'); |
1232
|
|
|
|
1233
|
|
|
$object = $this->hydratorByValue->hydrate($data, $entity); |
1234
|
|
|
$this->assertNull($object->getToOne(false)); |
1235
|
|
|
} |
1236
|
|
|
|
1237
|
|
|
public function testCanHydrateOneToOneAssociationByReferenceWithNullableRelation() |
1238
|
|
|
{ |
1239
|
|
|
// When using hydration by reference, it won't use the public API of the entity to retrieve values (setters) |
1240
|
|
|
$entity = new Asset\OneToOneEntity(); |
1241
|
|
|
|
1242
|
|
|
$this->configureObjectManagerForOneToOneEntity(); |
1243
|
|
|
$this->objectManager->expects($this->never())->method('find'); |
|
|
|
|
1244
|
|
|
$this->metadata->expects($this->once())->method('hasAssociation'); |
|
|
|
|
1245
|
|
|
|
1246
|
|
|
$data = ['toOne' => null]; |
1247
|
|
|
|
1248
|
|
|
$object = $this->hydratorByReference->hydrate($data, $entity); |
1249
|
|
|
$this->assertNull($object->getToOne(false)); |
1250
|
|
|
} |
1251
|
|
|
|
1252
|
|
|
public function testExtractOneToManyAssociationByValue() |
1253
|
|
|
{ |
1254
|
|
|
// When using extraction by value, it will use the public API of the entity to retrieve values (getters) |
1255
|
|
|
$toMany1 = new Asset\SimpleEntity(); |
1256
|
|
|
$toMany1->setId(2); |
1257
|
|
|
$toMany1->setField('foo', false); |
1258
|
|
|
|
1259
|
|
|
$toMany2 = new Asset\SimpleEntity(); |
1260
|
|
|
$toMany2->setId(3); |
1261
|
|
|
$toMany2->setField('bar', false); |
1262
|
|
|
|
1263
|
|
|
$collection = new ArrayCollection([$toMany1, $toMany2]); |
1264
|
|
|
|
1265
|
|
|
$entity = new Asset\OneToManyEntity(); |
1266
|
|
|
$entity->setId(4); |
1267
|
|
|
$entity->addEntities($collection); |
1268
|
|
|
|
1269
|
|
|
$this->configureObjectManagerForOneToManyEntity(); |
1270
|
|
|
|
1271
|
|
|
$data = $this->hydratorByValue->extract($entity); |
1272
|
|
|
|
1273
|
|
|
$this->assertEquals(4, $data['id']); |
1274
|
|
|
$this->assertInstanceOf('Doctrine\Common\Collections\Collection', $data['entities']); |
1275
|
|
|
|
1276
|
|
|
$this->assertEquals($toMany1->getId(), $data['entities'][0]->getId()); |
1277
|
|
|
$this->assertSame($toMany1, $data['entities'][0]); |
1278
|
|
|
$this->assertEquals($toMany2->getId(), $data['entities'][1]->getId()); |
1279
|
|
|
$this->assertSame($toMany2, $data['entities'][1]); |
1280
|
|
|
} |
1281
|
|
|
|
1282
|
|
|
/** |
1283
|
|
|
* @depends testExtractOneToManyAssociationByValue |
1284
|
|
|
*/ |
1285
|
|
|
public function testExtractOneToManyByValueWithArray() |
1286
|
|
|
{ |
1287
|
|
|
// When using extraction by value, it will use the public API of the entity to retrieve values (getters) |
1288
|
|
|
$toMany1 = new Asset\SimpleEntity(); |
1289
|
|
|
$toMany1->setId(2); |
1290
|
|
|
$toMany1->setField('foo', false); |
1291
|
|
|
|
1292
|
|
|
$toMany2 = new Asset\SimpleEntity(); |
1293
|
|
|
$toMany2->setId(3); |
1294
|
|
|
$toMany2->setField('bar', false); |
1295
|
|
|
|
1296
|
|
|
$collection = new ArrayCollection([$toMany1, $toMany2]); |
1297
|
|
|
|
1298
|
|
|
$entity = new Asset\OneToManyArrayEntity(); |
1299
|
|
|
$entity->setId(4); |
1300
|
|
|
$entity->addEntities($collection); |
1301
|
|
|
|
1302
|
|
|
$this->configureObjectManagerForOneToManyArrayEntity(); |
1303
|
|
|
|
1304
|
|
|
$data = $this->hydratorByValue->extract($entity); |
1305
|
|
|
|
1306
|
|
|
$this->assertEquals(4, $data['id']); |
1307
|
|
|
$this->assertInternalType('array', $data['entities']); |
1308
|
|
|
|
1309
|
|
|
$this->assertEquals($toMany1->getId(), $data['entities'][0]->getId()); |
1310
|
|
|
$this->assertSame($toMany1, $data['entities'][0]); |
1311
|
|
|
$this->assertEquals($toMany2->getId(), $data['entities'][1]->getId()); |
1312
|
|
|
$this->assertSame($toMany2, $data['entities'][1]); |
1313
|
|
|
} |
1314
|
|
|
|
1315
|
|
|
public function testExtractOneToManyAssociationByReference() |
1316
|
|
|
{ |
1317
|
|
|
// When using extraction by reference, it won't use the public API of the entity to retrieve values (getters) |
1318
|
|
|
$toMany1 = new Asset\SimpleEntity(); |
1319
|
|
|
$toMany1->setId(2); |
1320
|
|
|
$toMany1->setField('foo', false); |
1321
|
|
|
|
1322
|
|
|
$toMany2 = new Asset\SimpleEntity(); |
1323
|
|
|
$toMany2->setId(3); |
1324
|
|
|
$toMany2->setField('bar', false); |
1325
|
|
|
|
1326
|
|
|
$collection = new ArrayCollection([$toMany1, $toMany2]); |
1327
|
|
|
|
1328
|
|
|
$entity = new Asset\OneToManyEntity(); |
1329
|
|
|
$entity->setId(4); |
1330
|
|
|
$entity->addEntities($collection); |
1331
|
|
|
|
1332
|
|
|
$this->configureObjectManagerForOneToManyEntity(); |
1333
|
|
|
|
1334
|
|
|
$data = $this->hydratorByReference->extract($entity); |
1335
|
|
|
|
1336
|
|
|
$this->assertEquals(4, $data['id']); |
1337
|
|
|
$this->assertInstanceOf('Doctrine\Common\Collections\Collection', $data['entities']); |
1338
|
|
|
|
1339
|
|
|
$this->assertEquals($toMany1->getId(), $data['entities'][0]->getId()); |
1340
|
|
|
$this->assertSame($toMany1, $data['entities'][0]); |
1341
|
|
|
$this->assertEquals($toMany2->getId(), $data['entities'][1]->getId()); |
1342
|
|
|
$this->assertSame($toMany2, $data['entities'][1]); |
1343
|
|
|
} |
1344
|
|
|
|
1345
|
|
|
/** |
1346
|
|
|
* @depends testExtractOneToManyAssociationByReference |
1347
|
|
|
*/ |
1348
|
|
|
public function testExtractOneToManyArrayByReference() |
1349
|
|
|
{ |
1350
|
|
|
// When using extraction by reference, it won't use the public API of the entity to retrieve values (getters) |
1351
|
|
|
$toMany1 = new Asset\SimpleEntity(); |
1352
|
|
|
$toMany1->setId(2); |
1353
|
|
|
$toMany1->setField('foo', false); |
1354
|
|
|
|
1355
|
|
|
$toMany2 = new Asset\SimpleEntity(); |
1356
|
|
|
$toMany2->setId(3); |
1357
|
|
|
$toMany2->setField('bar', false); |
1358
|
|
|
|
1359
|
|
|
$collection = new ArrayCollection([$toMany1, $toMany2]); |
1360
|
|
|
|
1361
|
|
|
$entity = new Asset\OneToManyArrayEntity(); |
1362
|
|
|
$entity->setId(4); |
1363
|
|
|
$entity->addEntities($collection); |
1364
|
|
|
|
1365
|
|
|
$this->configureObjectManagerForOneToManyArrayEntity(); |
1366
|
|
|
|
1367
|
|
|
$data = $this->hydratorByReference->extract($entity); |
1368
|
|
|
|
1369
|
|
|
$this->assertEquals(4, $data['id']); |
1370
|
|
|
$this->assertInstanceOf('Doctrine\Common\Collections\Collection', $data['entities']); |
1371
|
|
|
|
1372
|
|
|
$this->assertEquals($toMany1->getId(), $data['entities'][0]->getId()); |
1373
|
|
|
$this->assertSame($toMany1, $data['entities'][0]); |
1374
|
|
|
$this->assertEquals($toMany2->getId(), $data['entities'][1]->getId()); |
1375
|
|
|
$this->assertSame($toMany2, $data['entities'][1]); |
1376
|
|
|
} |
1377
|
|
|
|
1378
|
|
|
public function testHydrateOneToManyAssociationByValue() |
1379
|
|
|
{ |
1380
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
1381
|
|
|
$toMany1 = new Asset\SimpleEntity(); |
1382
|
|
|
$toMany1->setId(2); |
1383
|
|
|
$toMany1->setField('foo', false); |
1384
|
|
|
|
1385
|
|
|
$toMany2 = new Asset\SimpleEntity(); |
1386
|
|
|
$toMany2->setId(3); |
1387
|
|
|
$toMany2->setField('bar', false); |
1388
|
|
|
|
1389
|
|
|
$entity = new Asset\OneToManyEntity(); |
1390
|
|
|
$this->configureObjectManagerForOneToManyEntity(); |
1391
|
|
|
|
1392
|
|
|
$data = [ |
1393
|
|
|
'entities' => [$toMany1, $toMany2], |
1394
|
|
|
]; |
1395
|
|
|
|
1396
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
1397
|
|
|
|
1398
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntity', $entity); |
1399
|
|
|
|
1400
|
|
|
$entities = $entity->getEntities(false); |
1401
|
|
|
|
1402
|
|
|
foreach ($entities as $en) { |
1403
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $en); |
1404
|
|
|
$this->assertInternalType('integer', $en->getId()); |
1405
|
|
|
$this->assertContains('Modified from addEntities adder', $en->getField(false)); |
1406
|
|
|
} |
1407
|
|
|
|
1408
|
|
|
$this->assertEquals(2, $entities[0]->getId()); |
1409
|
|
|
$this->assertSame($toMany1, $entities[0]); |
1410
|
|
|
|
1411
|
|
|
$this->assertEquals(3, $entities[1]->getId()); |
1412
|
|
|
$this->assertSame($toMany2, $entities[1]); |
1413
|
|
|
} |
1414
|
|
|
|
1415
|
|
|
/** |
1416
|
|
|
* @depends testHydrateOneToManyAssociationByValue |
1417
|
|
|
*/ |
1418
|
|
|
public function testHydrateOneToManyArrayByValue() |
1419
|
|
|
{ |
1420
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
1421
|
|
|
$toMany1 = new Asset\SimpleEntity(); |
1422
|
|
|
$toMany1->setId(2); |
1423
|
|
|
$toMany1->setField('foo', false); |
1424
|
|
|
|
1425
|
|
|
$toMany2 = new Asset\SimpleEntity(); |
1426
|
|
|
$toMany2->setId(3); |
1427
|
|
|
$toMany2->setField('bar', false); |
1428
|
|
|
|
1429
|
|
|
$entity = new Asset\OneToManyArrayEntity(); |
1430
|
|
|
$this->configureObjectManagerForOneToManyArrayEntity(); |
1431
|
|
|
|
1432
|
|
|
$data = [ |
1433
|
|
|
'entities' => [$toMany1, $toMany2], |
1434
|
|
|
]; |
1435
|
|
|
|
1436
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
1437
|
|
|
|
1438
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyArrayEntity', $entity); |
1439
|
|
|
|
1440
|
|
|
$entities = $entity->getEntities(false); |
1441
|
|
|
|
1442
|
|
|
foreach ($entities as $en) { |
1443
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $en); |
1444
|
|
|
$this->assertInternalType('integer', $en->getId()); |
1445
|
|
|
$this->assertContains('Modified from addEntities adder', $en->getField(false)); |
1446
|
|
|
} |
1447
|
|
|
|
1448
|
|
|
$this->assertEquals(2, $entities[0]->getId()); |
1449
|
|
|
$this->assertSame($toMany1, $entities[0]); |
1450
|
|
|
|
1451
|
|
|
$this->assertEquals(3, $entities[1]->getId()); |
1452
|
|
|
$this->assertSame($toMany2, $entities[1]); |
1453
|
|
|
} |
1454
|
|
|
|
1455
|
|
|
public function testHydrateOneToManyAssociationByReference() |
1456
|
|
|
{ |
1457
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
1458
|
|
|
$toMany1 = new Asset\SimpleEntity(); |
1459
|
|
|
$toMany1->setId(2); |
1460
|
|
|
$toMany1->setField('foo', false); |
1461
|
|
|
|
1462
|
|
|
$toMany2 = new Asset\SimpleEntity(); |
1463
|
|
|
$toMany2->setId(3); |
1464
|
|
|
$toMany2->setField('bar', false); |
1465
|
|
|
|
1466
|
|
|
$entity = new Asset\OneToManyEntity(); |
1467
|
|
|
$this->configureObjectManagerForOneToManyEntity(); |
1468
|
|
|
|
1469
|
|
|
$data = [ |
1470
|
|
|
'entities' => [$toMany1, $toMany2], |
1471
|
|
|
]; |
1472
|
|
|
|
1473
|
|
|
$entity = $this->hydratorByReference->hydrate($data, $entity); |
1474
|
|
|
|
1475
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntity', $entity); |
1476
|
|
|
|
1477
|
|
|
$entities = $entity->getEntities(false); |
1478
|
|
|
|
1479
|
|
|
foreach ($entities as $en) { |
1480
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $en); |
1481
|
|
|
$this->assertInternalType('integer', $en->getId()); |
1482
|
|
|
$this->assertNotContains('Modified from addEntities adder', $en->getField(false)); |
1483
|
|
|
} |
1484
|
|
|
|
1485
|
|
|
$this->assertEquals(2, $entities[0]->getId()); |
1486
|
|
|
$this->assertSame($toMany1, $entities[0]); |
1487
|
|
|
|
1488
|
|
|
$this->assertEquals(3, $entities[1]->getId()); |
1489
|
|
|
$this->assertSame($toMany2, $entities[1]); |
1490
|
|
|
} |
1491
|
|
|
|
1492
|
|
|
/** |
1493
|
|
|
* @depends testHydrateOneToManyAssociationByReference |
1494
|
|
|
*/ |
1495
|
|
|
public function testHydrateOneToManyArrayByReference() |
1496
|
|
|
{ |
1497
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
1498
|
|
|
$toMany1 = new Asset\SimpleEntity(); |
1499
|
|
|
$toMany1->setId(2); |
1500
|
|
|
$toMany1->setField('foo', false); |
1501
|
|
|
|
1502
|
|
|
$toMany2 = new Asset\SimpleEntity(); |
1503
|
|
|
$toMany2->setId(3); |
1504
|
|
|
$toMany2->setField('bar', false); |
1505
|
|
|
|
1506
|
|
|
$entity = new Asset\OneToManyArrayEntity(); |
1507
|
|
|
$this->configureObjectManagerForOneToManyArrayEntity(); |
1508
|
|
|
|
1509
|
|
|
$data = [ |
1510
|
|
|
'entities' => [$toMany1, $toMany2], |
1511
|
|
|
]; |
1512
|
|
|
|
1513
|
|
|
$entity = $this->hydratorByReference->hydrate($data, $entity); |
1514
|
|
|
|
1515
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyArrayEntity', $entity); |
1516
|
|
|
|
1517
|
|
|
$entities = $entity->getEntities(false); |
1518
|
|
|
|
1519
|
|
|
foreach ($entities as $en) { |
1520
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $en); |
1521
|
|
|
$this->assertInternalType('integer', $en->getId()); |
1522
|
|
|
$this->assertNotContains('Modified from addEntities adder', $en->getField(false)); |
1523
|
|
|
} |
1524
|
|
|
|
1525
|
|
|
$this->assertEquals(2, $entities[0]->getId()); |
1526
|
|
|
$this->assertSame($toMany1, $entities[0]); |
1527
|
|
|
|
1528
|
|
|
$this->assertEquals(3, $entities[1]->getId()); |
1529
|
|
|
$this->assertSame($toMany2, $entities[1]); |
1530
|
|
|
} |
1531
|
|
|
|
1532
|
|
|
public function testHydrateOneToManyAssociationByValueUsingIdentifiersForRelations() |
1533
|
|
|
{ |
1534
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
1535
|
|
|
$entity = new Asset\OneToManyEntity(); |
1536
|
|
|
$this->configureObjectManagerForOneToManyEntity(); |
1537
|
|
|
|
1538
|
|
|
$data = [ |
1539
|
|
|
'entities' => [2, 3], |
1540
|
|
|
]; |
1541
|
|
|
|
1542
|
|
|
$entityInDatabaseWithIdOfTwo = new Asset\SimpleEntity(); |
1543
|
|
|
$entityInDatabaseWithIdOfTwo->setId(2); |
1544
|
|
|
$entityInDatabaseWithIdOfTwo->setField('foo', false); |
1545
|
|
|
|
1546
|
|
|
$entityInDatabaseWithIdOfThree = new Asset\SimpleEntity(); |
1547
|
|
|
$entityInDatabaseWithIdOfThree->setId(3); |
1548
|
|
|
$entityInDatabaseWithIdOfThree->setField('bar', false); |
1549
|
|
|
|
1550
|
|
|
$this |
|
|
|
|
1551
|
|
|
->objectManager |
1552
|
|
|
->expects($this->exactly(2)) |
1553
|
|
|
->method('find') |
1554
|
|
|
->with( |
1555
|
|
|
'DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', |
1556
|
|
|
$this->logicalOr($this->equalTo(['id' => 2]), $this->equalTo(['id' => 3])) |
1557
|
|
|
) |
1558
|
|
|
->will( |
1559
|
|
|
$this->returnCallback( |
1560
|
|
|
function ($target, $arg) use ($entityInDatabaseWithIdOfTwo, $entityInDatabaseWithIdOfThree) { |
1561
|
|
|
if ($arg['id'] === 2) { |
1562
|
|
|
return $entityInDatabaseWithIdOfTwo; |
1563
|
|
|
} elseif ($arg['id'] === 3) { |
1564
|
|
|
return $entityInDatabaseWithIdOfThree; |
1565
|
|
|
} |
1566
|
|
|
|
1567
|
|
|
throw new \InvalidArgumentException(); |
1568
|
|
|
} |
1569
|
|
|
) |
1570
|
|
|
); |
1571
|
|
|
|
1572
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
1573
|
|
|
|
1574
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntity', $entity); |
1575
|
|
|
|
1576
|
|
|
/* @var $entity \DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntity */ |
1577
|
|
|
$entities = $entity->getEntities(false); |
1578
|
|
|
|
1579
|
|
|
foreach ($entities as $en) { |
1580
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $en); |
1581
|
|
|
$this->assertInternalType('integer', $en->getId()); |
1582
|
|
|
$this->assertContains('Modified from addEntities adder', $en->getField(false)); |
1583
|
|
|
} |
1584
|
|
|
|
1585
|
|
|
$this->assertEquals(2, $entities[0]->getId()); |
1586
|
|
|
$this->assertSame($entityInDatabaseWithIdOfTwo, $entities[0]); |
1587
|
|
|
|
1588
|
|
|
$this->assertEquals(3, $entities[1]->getId()); |
1589
|
|
|
$this->assertSame($entityInDatabaseWithIdOfThree, $entities[1]); |
1590
|
|
|
} |
1591
|
|
|
|
1592
|
|
|
public function testHydrateOneToManyAssociationByValueUsingIdentifiersArrayForRelations() |
1593
|
|
|
{ |
1594
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
1595
|
|
|
$entity = new Asset\OneToManyEntity(); |
1596
|
|
|
$this->configureObjectManagerForOneToManyEntity(); |
1597
|
|
|
|
1598
|
|
|
$data = [ |
1599
|
|
|
'entities' => [ |
1600
|
|
|
['id' => 2], |
1601
|
|
|
['id' => 3], |
1602
|
|
|
], |
1603
|
|
|
]; |
1604
|
|
|
|
1605
|
|
|
$entityInDatabaseWithIdOfTwo = new Asset\SimpleEntity(); |
1606
|
|
|
$entityInDatabaseWithIdOfTwo->setId(2); |
1607
|
|
|
$entityInDatabaseWithIdOfTwo->setField('foo', false); |
1608
|
|
|
|
1609
|
|
|
$entityInDatabaseWithIdOfThree = new Asset\SimpleEntity(); |
1610
|
|
|
$entityInDatabaseWithIdOfThree->setId(3); |
1611
|
|
|
$entityInDatabaseWithIdOfThree->setField('bar', false); |
1612
|
|
|
|
1613
|
|
|
$this |
|
|
|
|
1614
|
|
|
->objectManager |
1615
|
|
|
->expects($this->exactly(2)) |
1616
|
|
|
->method('find') |
1617
|
|
|
->with( |
1618
|
|
|
'DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', |
1619
|
|
|
$this->logicalOr($this->equalTo(['id' => 2]), $this->equalTo(['id' => 3])) |
1620
|
|
|
) |
1621
|
|
|
->will( |
1622
|
|
|
$this->returnCallback( |
1623
|
|
|
function ($target, $arg) use ($entityInDatabaseWithIdOfTwo, $entityInDatabaseWithIdOfThree) { |
1624
|
|
|
if ($arg['id'] === 2) { |
1625
|
|
|
return $entityInDatabaseWithIdOfTwo; |
1626
|
|
|
} elseif ($arg['id'] === 3) { |
1627
|
|
|
return $entityInDatabaseWithIdOfThree; |
1628
|
|
|
} |
1629
|
|
|
|
1630
|
|
|
throw new \InvalidArgumentException(); |
1631
|
|
|
} |
1632
|
|
|
) |
1633
|
|
|
); |
1634
|
|
|
|
1635
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
1636
|
|
|
|
1637
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntity', $entity); |
1638
|
|
|
|
1639
|
|
|
/* @var $entity \DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntity */ |
1640
|
|
|
$entities = $entity->getEntities(false); |
1641
|
|
|
|
1642
|
|
|
foreach ($entities as $en) { |
1643
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $en); |
1644
|
|
|
$this->assertInternalType('integer', $en->getId()); |
1645
|
|
|
$this->assertContains('Modified from addEntities adder', $en->getField(false)); |
1646
|
|
|
} |
1647
|
|
|
|
1648
|
|
|
$this->assertEquals(2, $entities[0]->getId()); |
1649
|
|
|
$this->assertSame($entityInDatabaseWithIdOfTwo, $entities[0]); |
1650
|
|
|
|
1651
|
|
|
$this->assertEquals(3, $entities[1]->getId()); |
1652
|
|
|
$this->assertSame($entityInDatabaseWithIdOfThree, $entities[1]); |
1653
|
|
|
} |
1654
|
|
|
|
1655
|
|
|
public function testHydrateOneToManyAssociationByReferenceUsingIdentifiersArrayForRelations() |
1656
|
|
|
{ |
1657
|
|
|
|
1658
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
1659
|
|
|
$entity = new Asset\OneToManyEntity(); |
1660
|
|
|
$this->configureObjectManagerForOneToManyEntity(); |
1661
|
|
|
|
1662
|
|
|
$data = [ |
1663
|
|
|
'entities' => [ |
1664
|
|
|
['id' => 2], |
1665
|
|
|
['id' => 3], |
1666
|
|
|
], |
1667
|
|
|
]; |
1668
|
|
|
|
1669
|
|
|
$entityInDatabaseWithIdOfTwo = new Asset\SimpleEntity(); |
1670
|
|
|
$entityInDatabaseWithIdOfTwo->setId(2); |
1671
|
|
|
$entityInDatabaseWithIdOfTwo->setField('foo', false); |
1672
|
|
|
|
1673
|
|
|
$entityInDatabaseWithIdOfThree = new Asset\SimpleEntity(); |
1674
|
|
|
$entityInDatabaseWithIdOfThree->setId(3); |
1675
|
|
|
$entityInDatabaseWithIdOfThree->setField('bar', false); |
1676
|
|
|
|
1677
|
|
|
$this |
|
|
|
|
1678
|
|
|
->objectManager |
1679
|
|
|
->expects($this->exactly(2)) |
1680
|
|
|
->method('find') |
1681
|
|
|
->with( |
1682
|
|
|
'DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', |
1683
|
|
|
$this->logicalOr($this->equalTo(['id' => 2]), $this->equalTo(['id' => 3])) |
1684
|
|
|
) |
1685
|
|
|
->will( |
1686
|
|
|
$this->returnCallback( |
1687
|
|
|
function ($target, $arg) use ($entityInDatabaseWithIdOfTwo, $entityInDatabaseWithIdOfThree) { |
1688
|
|
|
if ($arg['id'] === 2) { |
1689
|
|
|
return $entityInDatabaseWithIdOfTwo; |
1690
|
|
|
} elseif ($arg['id'] === 3) { |
1691
|
|
|
return $entityInDatabaseWithIdOfThree; |
1692
|
|
|
} |
1693
|
|
|
|
1694
|
|
|
throw new \InvalidArgumentException(); |
1695
|
|
|
} |
1696
|
|
|
) |
1697
|
|
|
); |
1698
|
|
|
|
1699
|
|
|
$entity = $this->hydratorByReference->hydrate($data, $entity); |
1700
|
|
|
|
1701
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntity', $entity); |
1702
|
|
|
|
1703
|
|
|
$entities = $entity->getEntities(false); |
1704
|
|
|
|
1705
|
|
|
foreach ($entities as $en) { |
1706
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $en); |
1707
|
|
|
$this->assertInternalType('integer', $en->getId()); |
1708
|
|
|
$this->assertNotContains('Modified from addEntities adder', $en->getField(false)); |
1709
|
|
|
} |
1710
|
|
|
|
1711
|
|
|
$this->assertEquals(2, $entities[0]->getId()); |
1712
|
|
|
$this->assertSame($entityInDatabaseWithIdOfTwo, $entities[0]); |
1713
|
|
|
|
1714
|
|
|
$this->assertEquals(3, $entities[1]->getId()); |
1715
|
|
|
$this->assertSame($entityInDatabaseWithIdOfThree, $entities[1]); |
1716
|
|
|
} |
1717
|
|
|
|
1718
|
|
|
public function testHydrateOneToManyAssociationByReferenceUsingIdentifiersForRelations() |
1719
|
|
|
{ |
1720
|
|
|
// When using hydration by reference, it won't use the public API of the entity to set values (setters) |
1721
|
|
|
$entity = new Asset\OneToManyEntity(); |
1722
|
|
|
$this->configureObjectManagerForOneToManyEntity(); |
1723
|
|
|
|
1724
|
|
|
$data = [ |
1725
|
|
|
'entities' => [2, 3], |
1726
|
|
|
]; |
1727
|
|
|
|
1728
|
|
|
$entityInDatabaseWithIdOfTwo = new Asset\SimpleEntity(); |
1729
|
|
|
$entityInDatabaseWithIdOfTwo->setId(2); |
1730
|
|
|
$entityInDatabaseWithIdOfTwo->setField('foo', false); |
1731
|
|
|
|
1732
|
|
|
$entityInDatabaseWithIdOfThree = new Asset\SimpleEntity(); |
1733
|
|
|
$entityInDatabaseWithIdOfThree->setId(3); |
1734
|
|
|
$entityInDatabaseWithIdOfThree->setField('bar', false); |
1735
|
|
|
|
1736
|
|
|
$this |
|
|
|
|
1737
|
|
|
->objectManager |
1738
|
|
|
->expects($this->any()) |
1739
|
|
|
->method('find') |
1740
|
|
|
->with( |
1741
|
|
|
'DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', |
1742
|
|
|
$this->logicalOr($this->equalTo(['id' => 2]), $this->equalTo(['id' => 3])) |
1743
|
|
|
) |
1744
|
|
|
->will( |
1745
|
|
|
$this->returnCallback( |
1746
|
|
|
function ($target, $arg) use ($entityInDatabaseWithIdOfTwo, $entityInDatabaseWithIdOfThree) { |
1747
|
|
|
if ($arg['id'] === 2) { |
1748
|
|
|
return $entityInDatabaseWithIdOfTwo; |
1749
|
|
|
} elseif ($arg['id'] === 3) { |
1750
|
|
|
return $entityInDatabaseWithIdOfThree; |
1751
|
|
|
} |
1752
|
|
|
|
1753
|
|
|
throw new \InvalidArgumentException(); |
1754
|
|
|
} |
1755
|
|
|
) |
1756
|
|
|
); |
1757
|
|
|
|
1758
|
|
|
$entity = $this->hydratorByReference->hydrate($data, $entity); |
1759
|
|
|
|
1760
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntity', $entity); |
1761
|
|
|
|
1762
|
|
|
$entities = $entity->getEntities(false); |
1763
|
|
|
|
1764
|
|
|
foreach ($entities as $en) { |
1765
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $en); |
1766
|
|
|
$this->assertInternalType('integer', $en->getId()); |
1767
|
|
|
$this->assertNotContains('Modified from addEntities adder', $en->getField(false)); |
1768
|
|
|
} |
1769
|
|
|
|
1770
|
|
|
$this->assertEquals(2, $entities[0]->getId()); |
1771
|
|
|
$this->assertSame($entityInDatabaseWithIdOfTwo, $entities[0]); |
1772
|
|
|
|
1773
|
|
|
$this->assertEquals(3, $entities[1]->getId()); |
1774
|
|
|
$this->assertSame($entityInDatabaseWithIdOfThree, $entities[1]); |
1775
|
|
|
} |
1776
|
|
|
|
1777
|
|
|
public function testHydrateOneToManyAssociationByValueUsingDisallowRemoveStrategy() |
1778
|
|
|
{ |
1779
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
1780
|
|
|
$toMany1 = new Asset\SimpleEntity(); |
1781
|
|
|
$toMany1->setId(2); |
1782
|
|
|
$toMany1->setField('foo', false); |
1783
|
|
|
|
1784
|
|
|
$toMany2 = new Asset\SimpleEntity(); |
1785
|
|
|
$toMany2->setId(3); |
1786
|
|
|
$toMany2->setField('bar', false); |
1787
|
|
|
|
1788
|
|
|
$toMany3 = new Asset\SimpleEntity(); |
1789
|
|
|
$toMany3->setId(8); |
1790
|
|
|
$toMany3->setField('baz', false); |
1791
|
|
|
|
1792
|
|
|
$entity = new Asset\OneToManyEntity(); |
1793
|
|
|
$this->configureObjectManagerForOneToManyEntity(); |
1794
|
|
|
|
1795
|
|
|
// Initially add two elements |
1796
|
|
|
$entity->addEntities(new ArrayCollection([$toMany1, $toMany2])); |
1797
|
|
|
|
1798
|
|
|
// The hydrated collection contains two other elements, one of them is new, and one of them is missing |
1799
|
|
|
// in the new strategy |
1800
|
|
|
$data = [ |
1801
|
|
|
'entities' => [$toMany2, $toMany3], |
1802
|
|
|
]; |
1803
|
|
|
|
1804
|
|
|
// Use a DisallowRemove strategy |
1805
|
|
|
$this->hydratorByValue->addStrategy('entities', new Strategy\DisallowRemoveByValue()); |
1806
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
1807
|
|
|
|
1808
|
|
|
$entities = $entity->getEntities(false); |
1809
|
|
|
|
1810
|
|
|
// DisallowStrategy should not remove existing entities in Collection even if it's not in the new collection |
1811
|
|
|
$this->assertEquals(3, count($entities)); |
1812
|
|
|
|
1813
|
|
|
foreach ($entities as $en) { |
1814
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $en); |
1815
|
|
|
$this->assertInternalType('integer', $en->getId()); |
1816
|
|
|
} |
1817
|
|
|
|
1818
|
|
|
$this->assertEquals(2, $entities[0]->getId()); |
1819
|
|
|
$this->assertSame($toMany1, $entities[0]); |
1820
|
|
|
|
1821
|
|
|
$this->assertEquals(3, $entities[1]->getId()); |
1822
|
|
|
$this->assertSame($toMany2, $entities[1]); |
1823
|
|
|
|
1824
|
|
|
$this->assertEquals(8, $entities[2]->getId()); |
1825
|
|
|
$this->assertSame($toMany3, $entities[2]); |
1826
|
|
|
} |
1827
|
|
|
|
1828
|
|
|
public function testHydrateOneToManyAssociationByReferenceUsingDisallowRemoveStrategy() |
1829
|
|
|
{ |
1830
|
|
|
// When using hydration by reference, it won't use the public API of the entity to set values (setters) |
1831
|
|
|
$toMany1 = new Asset\SimpleEntity(); |
1832
|
|
|
$toMany1->setId(2); |
1833
|
|
|
$toMany1->setField('foo', false); |
1834
|
|
|
|
1835
|
|
|
$toMany2 = new Asset\SimpleEntity(); |
1836
|
|
|
$toMany2->setId(3); |
1837
|
|
|
$toMany2->setField('bar', false); |
1838
|
|
|
|
1839
|
|
|
$toMany3 = new Asset\SimpleEntity(); |
1840
|
|
|
$toMany3->setId(8); |
1841
|
|
|
$toMany3->setField('baz', false); |
1842
|
|
|
|
1843
|
|
|
$entity = new Asset\OneToManyEntity(); |
1844
|
|
|
$this->configureObjectManagerForOneToManyEntity(); |
1845
|
|
|
|
1846
|
|
|
// Initially add two elements |
1847
|
|
|
$entity->addEntities(new ArrayCollection([$toMany1, $toMany2])); |
1848
|
|
|
|
1849
|
|
|
// The hydrated collection contains two other elements, one of them is new, and one of them is missing |
1850
|
|
|
// in the new strategy |
1851
|
|
|
$data = [ |
1852
|
|
|
'entities' => [$toMany2, $toMany3], |
1853
|
|
|
]; |
1854
|
|
|
|
1855
|
|
|
// Use a DisallowRemove strategy |
1856
|
|
|
$this->hydratorByReference->addStrategy('entities', new Strategy\DisallowRemoveByReference()); |
1857
|
|
|
$entity = $this->hydratorByReference->hydrate($data, $entity); |
1858
|
|
|
|
1859
|
|
|
$entities = $entity->getEntities(false); |
1860
|
|
|
|
1861
|
|
|
// DisallowStrategy should not remove existing entities in Collection even if it's not in the new collection |
1862
|
|
|
$this->assertEquals(3, count($entities)); |
1863
|
|
|
|
1864
|
|
|
foreach ($entities as $en) { |
1865
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $en); |
1866
|
|
|
$this->assertInternalType('integer', $en->getId()); |
1867
|
|
|
|
1868
|
|
|
// Only the third element is new so the adder has not been called on it |
1869
|
|
|
if ($en === $toMany3) { |
1870
|
|
|
$this->assertNotContains('Modified from addEntities adder', $en->getField(false)); |
1871
|
|
|
} |
1872
|
|
|
} |
1873
|
|
|
|
1874
|
|
|
$this->assertEquals(2, $entities[0]->getId()); |
1875
|
|
|
$this->assertSame($toMany1, $entities[0]); |
1876
|
|
|
|
1877
|
|
|
$this->assertEquals(3, $entities[1]->getId()); |
1878
|
|
|
$this->assertSame($toMany2, $entities[1]); |
1879
|
|
|
|
1880
|
|
|
$this->assertEquals(8, $entities[2]->getId()); |
1881
|
|
|
$this->assertSame($toMany3, $entities[2]); |
1882
|
|
|
} |
1883
|
|
|
|
1884
|
|
|
public function testHydrateOneToManyAssociationByValueWithArrayCausingDataModifications() |
1885
|
|
|
{ |
1886
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
1887
|
|
|
$data = [ |
1888
|
|
|
'entities' => [ |
1889
|
|
|
['id' => 2, 'field' => 'Modified By Hydrate'], |
1890
|
|
|
['id' => 3, 'field' => 'Modified By Hydrate'], |
1891
|
|
|
], |
1892
|
|
|
]; |
1893
|
|
|
|
1894
|
|
|
$entityInDatabaseWithIdOfTwo = new Asset\SimpleEntity(); |
1895
|
|
|
$entityInDatabaseWithIdOfTwo->setId(2); |
1896
|
|
|
$entityInDatabaseWithIdOfTwo->setField('foo', false); |
1897
|
|
|
|
1898
|
|
|
$entityInDatabaseWithIdOfThree = new Asset\SimpleEntity(); |
1899
|
|
|
$entityInDatabaseWithIdOfThree->setId(3); |
1900
|
|
|
$entityInDatabaseWithIdOfThree->setField('bar', false); |
1901
|
|
|
|
1902
|
|
|
$entity = new Asset\OneToManyEntityWithEntities( |
1903
|
|
|
new ArrayCollection([ |
1904
|
|
|
$entityInDatabaseWithIdOfTwo, |
1905
|
|
|
$entityInDatabaseWithIdOfThree, |
1906
|
|
|
]) |
1907
|
|
|
); |
1908
|
|
|
$this->configureObjectManagerForOneToManyEntity(); |
1909
|
|
|
|
1910
|
|
|
$this |
|
|
|
|
1911
|
|
|
->objectManager |
1912
|
|
|
->expects($this->exactly(2)) |
1913
|
|
|
->method('find') |
1914
|
|
|
->with( |
1915
|
|
|
'DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', |
1916
|
|
|
$this->logicalOr($this->equalTo(['id' => 2]), $this->equalTo(['id' => 3])) |
1917
|
|
|
) |
1918
|
|
|
->will( |
1919
|
|
|
$this->returnCallback( |
1920
|
|
|
function ($target, $arg) use ($entityInDatabaseWithIdOfTwo, $entityInDatabaseWithIdOfThree) { |
1921
|
|
|
if ($arg['id'] === 2) { |
1922
|
|
|
return $entityInDatabaseWithIdOfTwo; |
1923
|
|
|
} elseif ($arg['id'] === 3) { |
1924
|
|
|
return $entityInDatabaseWithIdOfThree; |
1925
|
|
|
} |
1926
|
|
|
|
1927
|
|
|
throw new \InvalidArgumentException(); |
1928
|
|
|
} |
1929
|
|
|
) |
1930
|
|
|
); |
1931
|
|
|
|
1932
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
1933
|
|
|
|
1934
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntityWithEntities', $entity); |
1935
|
|
|
|
1936
|
|
|
/* @var $entity \DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntity */ |
1937
|
|
|
$entities = $entity->getEntities(false); |
1938
|
|
|
|
1939
|
|
|
foreach ($entities as $en) { |
1940
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $en); |
1941
|
|
|
$this->assertInternalType('integer', $en->getId()); |
1942
|
|
|
$this->assertInternalType('string', $en->getField()); |
1943
|
|
|
$this->assertContains('Modified By Hydrate', $en->getField(false)); |
1944
|
|
|
} |
1945
|
|
|
|
1946
|
|
|
$this->assertEquals(2, $entities[0]->getId()); |
1947
|
|
|
$this->assertSame($entityInDatabaseWithIdOfTwo, $entities[0]); |
1948
|
|
|
|
1949
|
|
|
$this->assertEquals(3, $entities[1]->getId()); |
1950
|
|
|
$this->assertSame($entityInDatabaseWithIdOfThree, $entities[1]); |
1951
|
|
|
} |
1952
|
|
|
|
1953
|
|
|
|
1954
|
|
|
public function testHydrateOneToManyAssociationByValueWithTraversableCausingDataModifications() |
1955
|
|
|
{ |
1956
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
1957
|
|
|
$data = [ |
1958
|
|
|
'entities' => new ArrayCollection([ |
1959
|
|
|
['id' => 2, 'field' => 'Modified By Hydrate'], |
1960
|
|
|
['id' => 3, 'field' => 'Modified By Hydrate'], |
1961
|
|
|
]), |
1962
|
|
|
]; |
1963
|
|
|
|
1964
|
|
|
$entityInDatabaseWithIdOfTwo = new Asset\SimpleEntity(); |
1965
|
|
|
$entityInDatabaseWithIdOfTwo->setId(2); |
1966
|
|
|
$entityInDatabaseWithIdOfTwo->setField('foo', false); |
1967
|
|
|
|
1968
|
|
|
$entityInDatabaseWithIdOfThree = new Asset\SimpleEntity(); |
1969
|
|
|
$entityInDatabaseWithIdOfThree->setId(3); |
1970
|
|
|
$entityInDatabaseWithIdOfThree->setField('bar', false); |
1971
|
|
|
|
1972
|
|
|
$entity = new Asset\OneToManyEntityWithEntities( |
1973
|
|
|
new ArrayCollection([ |
1974
|
|
|
$entityInDatabaseWithIdOfTwo, |
1975
|
|
|
$entityInDatabaseWithIdOfThree, |
1976
|
|
|
]) |
1977
|
|
|
); |
1978
|
|
|
$this->configureObjectManagerForOneToManyEntity(); |
1979
|
|
|
|
1980
|
|
|
$this |
|
|
|
|
1981
|
|
|
->objectManager |
1982
|
|
|
->expects($this->exactly(2)) |
1983
|
|
|
->method('find') |
1984
|
|
|
->with( |
1985
|
|
|
'DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', |
1986
|
|
|
$this->logicalOr($this->equalTo(['id' => 2]), $this->equalTo(['id' => 3])) |
1987
|
|
|
) |
1988
|
|
|
->will( |
1989
|
|
|
$this->returnCallback( |
1990
|
|
|
function ($target, $arg) use ($entityInDatabaseWithIdOfTwo, $entityInDatabaseWithIdOfThree) { |
1991
|
|
|
if ($arg['id'] === 2) { |
1992
|
|
|
return $entityInDatabaseWithIdOfTwo; |
1993
|
|
|
} elseif ($arg['id'] === 3) { |
1994
|
|
|
return $entityInDatabaseWithIdOfThree; |
1995
|
|
|
} |
1996
|
|
|
|
1997
|
|
|
throw new \InvalidArgumentException(); |
1998
|
|
|
} |
1999
|
|
|
) |
2000
|
|
|
); |
2001
|
|
|
|
2002
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
2003
|
|
|
|
2004
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntityWithEntities', $entity); |
2005
|
|
|
|
2006
|
|
|
/* @var $entity \DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntity */ |
2007
|
|
|
$entities = $entity->getEntities(false); |
2008
|
|
|
|
2009
|
|
|
foreach ($entities as $en) { |
2010
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $en); |
2011
|
|
|
$this->assertInternalType('integer', $en->getId()); |
2012
|
|
|
$this->assertInternalType('string', $en->getField()); |
2013
|
|
|
$this->assertContains('Modified By Hydrate', $en->getField(false)); |
2014
|
|
|
} |
2015
|
|
|
|
2016
|
|
|
$this->assertEquals(2, $entities[0]->getId()); |
2017
|
|
|
$this->assertSame($entityInDatabaseWithIdOfTwo, $entities[0]); |
2018
|
|
|
|
2019
|
|
|
$this->assertEquals(3, $entities[1]->getId()); |
2020
|
|
|
$this->assertSame($entityInDatabaseWithIdOfThree, $entities[1]); |
2021
|
|
|
} |
2022
|
|
|
|
2023
|
|
|
public function testHydrateOneToManyAssociationByValueWithStdClass() |
2024
|
|
|
{ |
2025
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
2026
|
|
|
$stdClass1 = new \StdClass(); |
2027
|
|
|
$stdClass1->id = 2; |
2028
|
|
|
|
2029
|
|
|
$stdClass2 = new \StdClass(); |
2030
|
|
|
$stdClass2->id = 3; |
2031
|
|
|
|
2032
|
|
|
$data = ['entities' => [$stdClass1, $stdClass2]]; |
2033
|
|
|
|
2034
|
|
|
$entityInDatabaseWithIdOfTwo = new Asset\SimpleEntity(); |
2035
|
|
|
$entityInDatabaseWithIdOfTwo->setId(2); |
2036
|
|
|
$entityInDatabaseWithIdOfTwo->setField('foo', false); |
2037
|
|
|
|
2038
|
|
|
$entityInDatabaseWithIdOfThree = new Asset\SimpleEntity(); |
2039
|
|
|
$entityInDatabaseWithIdOfThree->setId(3); |
2040
|
|
|
$entityInDatabaseWithIdOfThree->setField('bar', false); |
2041
|
|
|
|
2042
|
|
|
$entity = new Asset\OneToManyEntityWithEntities( |
2043
|
|
|
new ArrayCollection([ |
2044
|
|
|
$entityInDatabaseWithIdOfTwo, |
2045
|
|
|
$entityInDatabaseWithIdOfThree, |
2046
|
|
|
]) |
2047
|
|
|
); |
2048
|
|
|
$this->configureObjectManagerForOneToManyEntity(); |
2049
|
|
|
|
2050
|
|
|
$this |
|
|
|
|
2051
|
|
|
->objectManager |
2052
|
|
|
->expects($this->exactly(2)) |
2053
|
|
|
->method('find') |
2054
|
|
|
->with( |
2055
|
|
|
'DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', |
2056
|
|
|
$this->logicalOr($this->equalTo(['id' => 2]), $this->equalTo(['id' => 3])) |
2057
|
|
|
) |
2058
|
|
|
->will( |
2059
|
|
|
$this->returnCallback( |
2060
|
|
|
function ($target, $arg) use ($entityInDatabaseWithIdOfTwo, $entityInDatabaseWithIdOfThree) { |
2061
|
|
|
if ($arg['id'] === 2) { |
2062
|
|
|
return $entityInDatabaseWithIdOfTwo; |
2063
|
|
|
} elseif ($arg['id'] === 3) { |
2064
|
|
|
return $entityInDatabaseWithIdOfThree; |
2065
|
|
|
} |
2066
|
|
|
|
2067
|
|
|
throw new \InvalidArgumentException(); |
2068
|
|
|
} |
2069
|
|
|
) |
2070
|
|
|
); |
2071
|
|
|
|
2072
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
2073
|
|
|
|
2074
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntityWithEntities', $entity); |
2075
|
|
|
|
2076
|
|
|
/* @var $entity \DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntity */ |
2077
|
|
|
$entities = $entity->getEntities(false); |
2078
|
|
|
|
2079
|
|
|
foreach ($entities as $en) { |
2080
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $en); |
2081
|
|
|
$this->assertInternalType('integer', $en->getId()); |
2082
|
|
|
} |
2083
|
|
|
|
2084
|
|
|
$this->assertEquals(2, $entities[0]->getId()); |
2085
|
|
|
$this->assertSame($entityInDatabaseWithIdOfTwo, $entities[0]); |
2086
|
|
|
|
2087
|
|
|
$this->assertEquals(3, $entities[1]->getId()); |
2088
|
|
|
$this->assertSame($entityInDatabaseWithIdOfThree, $entities[1]); |
2089
|
|
|
} |
2090
|
|
|
|
2091
|
|
|
public function testHydrateOneToManyAssociationByReferenceWithArrayCausingDataModifications() |
2092
|
|
|
{ |
2093
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
2094
|
|
|
$data = [ |
2095
|
|
|
'entities' => [ |
2096
|
|
|
['id' => 2, 'field' => 'Modified By Hydrate'], |
2097
|
|
|
['id' => 3, 'field' => 'Modified By Hydrate'], |
2098
|
|
|
], |
2099
|
|
|
]; |
2100
|
|
|
|
2101
|
|
|
$entityInDatabaseWithIdOfTwo = new Asset\SimpleEntity(); |
2102
|
|
|
$entityInDatabaseWithIdOfTwo->setId(2); |
2103
|
|
|
$entityInDatabaseWithIdOfTwo->setField('Unmodified Value', false); |
2104
|
|
|
|
2105
|
|
|
$entityInDatabaseWithIdOfThree = new Asset\SimpleEntity(); |
2106
|
|
|
$entityInDatabaseWithIdOfThree->setId(3); |
2107
|
|
|
$entityInDatabaseWithIdOfThree->setField('Unmodified Value', false); |
2108
|
|
|
|
2109
|
|
|
$entity = new Asset\OneToManyEntityWithEntities( |
2110
|
|
|
new ArrayCollection([ |
2111
|
|
|
$entityInDatabaseWithIdOfTwo, |
2112
|
|
|
$entityInDatabaseWithIdOfThree, |
2113
|
|
|
]) |
2114
|
|
|
); |
2115
|
|
|
|
2116
|
|
|
$reflSteps = [ |
2117
|
|
|
new ReflectionClass('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntityWithEntities'), |
2118
|
|
|
new ReflectionClass('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity'), |
2119
|
|
|
new ReflectionClass('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity'), |
2120
|
|
|
new ReflectionClass('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntityWithEntities'), |
2121
|
|
|
]; |
2122
|
|
|
$this |
|
|
|
|
2123
|
|
|
->metadata |
2124
|
|
|
->expects($this->any()) |
2125
|
|
|
->method('getReflectionClass') |
2126
|
|
|
->will($this->returnCallback( |
2127
|
|
|
function () use (&$reflSteps) { |
2128
|
|
|
$refl = array_shift($reflSteps); |
2129
|
|
|
return $refl; |
2130
|
|
|
} |
2131
|
|
|
)); |
2132
|
|
|
|
2133
|
|
|
$this->configureObjectManagerForOneToManyEntity(); |
2134
|
|
|
|
2135
|
|
|
$this |
|
|
|
|
2136
|
|
|
->objectManager |
2137
|
|
|
->expects($this->exactly(2)) |
2138
|
|
|
->method('find') |
2139
|
|
|
->with( |
2140
|
|
|
'DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', |
2141
|
|
|
$this->logicalOr($this->equalTo(['id' => 2]), $this->equalTo(['id' => 3])) |
2142
|
|
|
) |
2143
|
|
|
->will( |
2144
|
|
|
$this->returnCallback( |
2145
|
|
|
function ($target, $arg) use ($entityInDatabaseWithIdOfTwo, $entityInDatabaseWithIdOfThree) { |
2146
|
|
|
if ($arg['id'] === 2) { |
2147
|
|
|
return $entityInDatabaseWithIdOfTwo; |
2148
|
|
|
} elseif ($arg['id'] === 3) { |
2149
|
|
|
return $entityInDatabaseWithIdOfThree; |
2150
|
|
|
} |
2151
|
|
|
|
2152
|
|
|
throw new \InvalidArgumentException(); |
2153
|
|
|
} |
2154
|
|
|
) |
2155
|
|
|
); |
2156
|
|
|
|
2157
|
|
|
$entity = $this->hydratorByReference->hydrate($data, $entity); |
2158
|
|
|
|
2159
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntityWithEntities', $entity); |
2160
|
|
|
|
2161
|
|
|
/* @var $entity \DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntity */ |
2162
|
|
|
$entities = $entity->getEntities(false); |
2163
|
|
|
|
2164
|
|
|
foreach ($entities as $en) { |
2165
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $en); |
2166
|
|
|
$this->assertInternalType('integer', $en->getId()); |
2167
|
|
|
$this->assertInternalType('string', $en->getField()); |
2168
|
|
|
$this->assertContains('Modified By Hydrate', $en->getField(false)); |
2169
|
|
|
} |
2170
|
|
|
|
2171
|
|
|
$this->assertEquals(2, $entities[0]->getId()); |
2172
|
|
|
$this->assertSame($entityInDatabaseWithIdOfTwo, $entities[0]); |
2173
|
|
|
|
2174
|
|
|
$this->assertEquals(3, $entities[1]->getId()); |
2175
|
|
|
$this->assertSame($entityInDatabaseWithIdOfThree, $entities[1]); |
2176
|
|
|
} |
2177
|
|
|
|
2178
|
|
|
public function testAssertCollectionsAreNotSwappedDuringHydration() |
2179
|
|
|
{ |
2180
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
2181
|
|
|
$entity = new Asset\OneToManyEntity(); |
2182
|
|
|
$this->configureObjectManagerForOneToManyEntity(); |
2183
|
|
|
|
2184
|
|
|
$toMany1 = new Asset\SimpleEntity(); |
2185
|
|
|
$toMany1->setId(2); |
2186
|
|
|
$toMany1->setField('foo', false); |
2187
|
|
|
|
2188
|
|
|
$toMany2 = new Asset\SimpleEntity(); |
2189
|
|
|
$toMany2->setId(3); |
2190
|
|
|
$toMany2->setField('bar', false); |
2191
|
|
|
|
2192
|
|
|
$data = [ |
2193
|
|
|
'entities' => [$toMany1, $toMany2], |
2194
|
|
|
]; |
2195
|
|
|
|
2196
|
|
|
// Set the initial collection |
2197
|
|
|
$entity->addEntities(new ArrayCollection([$toMany1, $toMany2])); |
2198
|
|
|
$initialCollection = $entity->getEntities(false); |
2199
|
|
|
|
2200
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
2201
|
|
|
|
2202
|
|
|
$modifiedCollection = $entity->getEntities(false); |
2203
|
|
|
$this->assertSame($initialCollection, $modifiedCollection); |
2204
|
|
|
} |
2205
|
|
|
|
2206
|
|
|
public function testAssertCollectionsAreNotSwappedDuringHydrationUsingIdentifiersForRelations() |
2207
|
|
|
{ |
2208
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
2209
|
|
|
$entity = new Asset\OneToManyEntity(); |
2210
|
|
|
$this->configureObjectManagerForOneToManyEntity(); |
2211
|
|
|
|
2212
|
|
|
$data = [ |
2213
|
|
|
'entities' => [2, 3], |
2214
|
|
|
]; |
2215
|
|
|
|
2216
|
|
|
$entityInDatabaseWithIdOfTwo = new Asset\SimpleEntity(); |
2217
|
|
|
$entityInDatabaseWithIdOfTwo->setId(2); |
2218
|
|
|
$entityInDatabaseWithIdOfTwo->setField('foo', false); |
2219
|
|
|
|
2220
|
|
|
$entityInDatabaseWithIdOfThree = new Asset\SimpleEntity(); |
2221
|
|
|
$entityInDatabaseWithIdOfThree->setId(3); |
2222
|
|
|
$entityInDatabaseWithIdOfThree->setField('bar', false); |
2223
|
|
|
|
2224
|
|
|
// Set the initial collection |
2225
|
|
|
$entity->addEntities(new ArrayCollection([$entityInDatabaseWithIdOfTwo, $entityInDatabaseWithIdOfThree])); |
2226
|
|
|
$initialCollection = $entity->getEntities(false); |
2227
|
|
|
|
2228
|
|
|
$this |
|
|
|
|
2229
|
|
|
->objectManager |
2230
|
|
|
->expects($this->any()) |
2231
|
|
|
->method('find') |
2232
|
|
|
->with( |
2233
|
|
|
'DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', |
2234
|
|
|
$this->logicalOr($this->equalTo(['id' => 2]), $this->equalTo(['id' => 3])) |
2235
|
|
|
) |
2236
|
|
|
->will( |
2237
|
|
|
$this->returnCallback( |
2238
|
|
|
function ($target, $arg) use ($entityInDatabaseWithIdOfTwo, $entityInDatabaseWithIdOfThree) { |
2239
|
|
|
if ($arg['id'] === 2) { |
2240
|
|
|
return $entityInDatabaseWithIdOfTwo; |
2241
|
|
|
} |
2242
|
|
|
|
2243
|
|
|
return $entityInDatabaseWithIdOfThree; |
2244
|
|
|
} |
2245
|
|
|
) |
2246
|
|
|
); |
2247
|
|
|
|
2248
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
2249
|
|
|
|
2250
|
|
|
$modifiedCollection = $entity->getEntities(false); |
2251
|
|
|
$this->assertSame($initialCollection, $modifiedCollection); |
2252
|
|
|
} |
2253
|
|
|
|
2254
|
|
|
public function testCanLookupsForEmptyIdentifiers() |
2255
|
|
|
{ |
2256
|
|
|
// When using hydration by reference, it won't use the public API of the entity to set values (setters) |
2257
|
|
|
$entity = new Asset\OneToManyEntity(); |
2258
|
|
|
$this->configureObjectManagerForOneToManyEntity(); |
2259
|
|
|
|
2260
|
|
|
$data = [ |
2261
|
|
|
'entities' => [ |
2262
|
|
|
'', |
2263
|
|
|
], |
2264
|
|
|
]; |
2265
|
|
|
|
2266
|
|
|
$entityInDatabaseWithEmptyId = new Asset\SimpleEntity(); |
2267
|
|
|
$entityInDatabaseWithEmptyId->setId(''); |
2268
|
|
|
$entityInDatabaseWithEmptyId->setField('baz', false); |
2269
|
|
|
|
2270
|
|
|
$this |
|
|
|
|
2271
|
|
|
->objectManager |
2272
|
|
|
->expects($this->any()) |
2273
|
|
|
->method('find') |
2274
|
|
|
->with('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', '') |
2275
|
|
|
->will($this->returnValue($entityInDatabaseWithEmptyId)); |
2276
|
|
|
|
2277
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
2278
|
|
|
|
2279
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntity', $entity); |
2280
|
|
|
|
2281
|
|
|
$entities = $entity->getEntities(false); |
2282
|
|
|
$entity = $entities[0]; |
2283
|
|
|
|
2284
|
|
|
$this->assertEquals(1, count($entities)); |
2285
|
|
|
|
2286
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $entity); |
2287
|
|
|
$this->assertSame($entityInDatabaseWithEmptyId, $entity); |
2288
|
|
|
} |
2289
|
|
|
|
2290
|
|
|
public function testHandleDateTimeConversionUsingByValue() |
2291
|
|
|
{ |
2292
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
2293
|
|
|
$entity = new Asset\SimpleEntityWithDateTime(); |
2294
|
|
|
$this->configureObjectManagerForSimpleEntityWithDateTime(); |
2295
|
|
|
|
2296
|
|
|
$now = time(); |
2297
|
|
|
$data = ['date' => $now]; |
2298
|
|
|
|
2299
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
2300
|
|
|
|
2301
|
|
|
$this->assertInstanceOf('DateTime', $entity->getDate()); |
2302
|
|
|
$this->assertEquals($now, $entity->getDate()->getTimestamp()); |
2303
|
|
|
} |
2304
|
|
|
|
2305
|
|
|
public function testEmptyStringIsNotConvertedToDateTime() |
2306
|
|
|
{ |
2307
|
|
|
$entity = new Asset\SimpleEntityWithDateTime(); |
2308
|
|
|
$this->configureObjectManagerForSimpleEntityWithDateTime(); |
2309
|
|
|
|
2310
|
|
|
$data = ['date' => '']; |
2311
|
|
|
|
2312
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
2313
|
|
|
|
2314
|
|
|
$this->assertNull($entity->getDate()); |
2315
|
|
|
} |
2316
|
|
|
|
2317
|
|
|
public function testAssertNullValueHydratedForOneToOneWithOptionalMethodSignature() |
2318
|
|
|
{ |
2319
|
|
|
$entity = new Asset\OneToOneEntity(); |
2320
|
|
|
|
2321
|
|
|
$this->configureObjectManagerForOneToOneEntity(); |
2322
|
|
|
$this->objectManager->expects($this->never())->method('find'); |
|
|
|
|
2323
|
|
|
|
2324
|
|
|
$data = ['toOne' => null]; |
2325
|
|
|
|
2326
|
|
|
|
2327
|
|
|
$object = $this->hydratorByValue->hydrate($data, $entity); |
2328
|
|
|
$this->assertNull($object->getToOne(false)); |
2329
|
|
|
} |
2330
|
|
|
|
2331
|
|
|
public function testAssertNullValueNotUsedAsIdentifierForOneToOneWithNonOptionalMethodSignature() |
2332
|
|
|
{ |
2333
|
|
|
$entity = new Asset\OneToOneEntityNotNullable(); |
2334
|
|
|
|
2335
|
|
|
$entity->setToOne(new Asset\SimpleEntity()); |
2336
|
|
|
$this->configureObjectManagerForOneToOneEntityNotNullable(); |
2337
|
|
|
$this->objectManager->expects($this->never())->method('find'); |
|
|
|
|
2338
|
|
|
|
2339
|
|
|
$data = ['toOne' => null]; |
2340
|
|
|
|
2341
|
|
|
$object = $this->hydratorByValue->hydrate($data, $entity); |
2342
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $object->getToOne(false)); |
2343
|
|
|
} |
2344
|
|
|
|
2345
|
|
|
public function testUsesStrategyOnSimpleFieldsWhenHydratingByValue() |
2346
|
|
|
{ |
2347
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
2348
|
|
|
$entity = new Asset\SimpleEntity(); |
2349
|
|
|
$this->configureObjectManagerForSimpleEntity(); |
2350
|
|
|
$data = ['field' => 'foo']; |
2351
|
|
|
|
2352
|
|
|
$this->hydratorByValue->addStrategy('field', new Asset\SimpleStrategy()); |
2353
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
2354
|
|
|
|
2355
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $entity); |
2356
|
|
|
$this->assertEquals('From setter: modified while hydrating', $entity->getField(false)); |
2357
|
|
|
} |
2358
|
|
|
|
2359
|
|
|
public function testUsesStrategyOnSimpleFieldsWhenHydratingByReference() |
2360
|
|
|
{ |
2361
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
2362
|
|
|
$entity = new Asset\SimpleEntity(); |
2363
|
|
|
$this->configureObjectManagerForSimpleEntity(); |
2364
|
|
|
$data = ['field' => 'foo']; |
2365
|
|
|
|
2366
|
|
|
$this->hydratorByReference->addStrategy('field', new Asset\SimpleStrategy()); |
2367
|
|
|
$entity = $this->hydratorByReference->hydrate($data, $entity); |
2368
|
|
|
|
2369
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $entity); |
2370
|
|
|
$this->assertEquals('modified while hydrating', $entity->getField(false)); |
2371
|
|
|
} |
2372
|
|
|
|
2373
|
|
|
public function testUsesStrategyOnSimpleFieldsWhenExtractingByValue() |
2374
|
|
|
{ |
2375
|
|
|
$entity = new Asset\SimpleEntity(); |
2376
|
|
|
$entity->setId(2); |
2377
|
|
|
$entity->setField('foo', false); |
2378
|
|
|
|
2379
|
|
|
$this->configureObjectManagerForSimpleEntity(); |
2380
|
|
|
|
2381
|
|
|
$this->hydratorByValue->addStrategy('field', new Asset\SimpleStrategy()); |
2382
|
|
|
$data = $this->hydratorByValue->extract($entity); |
2383
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $entity); |
2384
|
|
|
$this->assertEquals(['id' => 2, 'field' => 'modified while extracting'], $data); |
2385
|
|
|
} |
2386
|
|
|
|
2387
|
|
|
public function testUsesStrategyOnSimpleFieldsWhenExtractingByReference() |
2388
|
|
|
{ |
2389
|
|
|
$entity = new Asset\SimpleEntity(); |
2390
|
|
|
$entity->setId(2); |
2391
|
|
|
$entity->setField('foo', false); |
2392
|
|
|
|
2393
|
|
|
$this->configureObjectManagerForSimpleEntity(); |
2394
|
|
|
|
2395
|
|
|
$this->hydratorByReference->addStrategy('field', new Asset\SimpleStrategy()); |
2396
|
|
|
$data = $this->hydratorByReference->extract($entity); |
2397
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $entity); |
2398
|
|
|
$this->assertEquals(['id' => 2, 'field' => 'modified while extracting'], $data); |
2399
|
|
|
} |
2400
|
|
|
|
2401
|
|
|
public function testCanExtractIsserByValue() |
2402
|
|
|
{ |
2403
|
|
|
$entity = new Asset\SimpleIsEntity(); |
2404
|
|
|
$entity->setId(2); |
2405
|
|
|
$entity->setDone(true); |
2406
|
|
|
|
2407
|
|
|
$this->configureObjectManagerForSimpleIsEntity(); |
2408
|
|
|
|
2409
|
|
|
$data = $this->hydratorByValue->extract($entity); |
2410
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleIsEntity', $entity); |
2411
|
|
|
$this->assertEquals(['id' => 2, 'done' => true], $data); |
2412
|
|
|
} |
2413
|
|
|
|
2414
|
|
|
public function testCanExtractIsserThatStartsWithIsByValue() |
2415
|
|
|
{ |
2416
|
|
|
$entity = new Asset\SimpleEntityWithIsBoolean(); |
2417
|
|
|
$entity->setId(2); |
2418
|
|
|
$entity->setIsActive(true); |
2419
|
|
|
|
2420
|
|
|
$this->configureObjectManagerForSimpleEntityWithIsBoolean(); |
2421
|
|
|
|
2422
|
|
|
$data = $this->hydratorByValue->extract($entity); |
2423
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntityWithIsBoolean', $entity); |
2424
|
|
|
$this->assertEquals(['id' => 2, 'isActive' => true], $data); |
2425
|
|
|
} |
2426
|
|
|
|
2427
|
|
|
public function testExtractWithPropertyNameFilterByValue() |
2428
|
|
|
{ |
2429
|
|
|
$entity = new Asset\SimpleEntity(); |
2430
|
|
|
$entity->setId(2); |
2431
|
|
|
$entity->setField('foo', false); |
2432
|
|
|
|
2433
|
|
|
$filter = new Filter\PropertyName(['id'], false); |
2434
|
|
|
|
2435
|
|
|
$this->configureObjectManagerForSimpleEntity(); |
2436
|
|
|
|
2437
|
|
|
$this->hydratorByValue->addFilter('propertyname', $filter); |
|
|
|
|
2438
|
|
|
$data = $this->hydratorByValue->extract($entity); |
2439
|
|
|
|
2440
|
|
|
$this->assertEquals(2, $data['id']); |
2441
|
|
|
$this->assertEquals(['id'], array_keys($data), 'Only the "id" field should have been extracted.'); |
2442
|
|
|
} |
2443
|
|
|
|
2444
|
|
|
public function testExtractWithPropertyNameFilterByReference() |
2445
|
|
|
{ |
2446
|
|
|
$entity = new Asset\SimpleEntity(); |
2447
|
|
|
$entity->setId(2); |
2448
|
|
|
$entity->setField('foo', false); |
2449
|
|
|
|
2450
|
|
|
$filter = new Filter\PropertyName(['id'], false); |
2451
|
|
|
|
2452
|
|
|
$this->configureObjectManagerForSimpleEntity(); |
2453
|
|
|
|
2454
|
|
|
$this->hydratorByReference->addFilter('propertyname', $filter); |
|
|
|
|
2455
|
|
|
$data = $this->hydratorByReference->extract($entity); |
2456
|
|
|
|
2457
|
|
|
$this->assertEquals(2, $data['id']); |
2458
|
|
|
$this->assertEquals(['id'], array_keys($data), 'Only the "id" field should have been extracted.'); |
2459
|
|
|
} |
2460
|
|
|
|
2461
|
|
|
public function testExtractByReferenceUsesNamingStrategy() |
2462
|
|
|
{ |
2463
|
|
|
$this->configureObjectManagerForNamingStrategyEntity(); |
2464
|
|
|
$name = 'Foo'; |
2465
|
|
|
$this->hydratorByReference->setNamingStrategy(new UnderscoreNamingStrategy()); |
2466
|
|
|
$data = $this->hydratorByReference->extract(new NamingStrategyEntity($name)); |
2467
|
|
|
$this->assertEquals($name, $data['camel_case']); |
2468
|
|
|
} |
2469
|
|
|
|
2470
|
|
|
public function testExtractByValueUsesNamingStrategy() |
2471
|
|
|
{ |
2472
|
|
|
$this->configureObjectManagerForNamingStrategyEntity(); |
2473
|
|
|
$name = 'Bar'; |
2474
|
|
|
$this->hydratorByValue->setNamingStrategy(new UnderscoreNamingStrategy()); |
2475
|
|
|
$data = $this->hydratorByValue->extract(new NamingStrategyEntity($name)); |
2476
|
|
|
$this->assertEquals($name, $data['camel_case']); |
2477
|
|
|
} |
2478
|
|
|
|
2479
|
|
|
public function testHydrateByReferenceUsesNamingStrategy() |
2480
|
|
|
{ |
2481
|
|
|
$this->configureObjectManagerForNamingStrategyEntity(); |
2482
|
|
|
$name = 'Baz'; |
2483
|
|
|
$this->hydratorByReference->setNamingStrategy(new UnderscoreNamingStrategy()); |
2484
|
|
|
$entity = $this->hydratorByReference->hydrate(['camel_case' => $name], new NamingStrategyEntity()); |
2485
|
|
|
$this->assertEquals($name, $entity->getCamelCase()); |
2486
|
|
|
} |
2487
|
|
|
|
2488
|
|
|
public function testHydrateByValueUsesNamingStrategy() |
2489
|
|
|
{ |
2490
|
|
|
$this->configureObjectManagerForNamingStrategyEntity(); |
2491
|
|
|
$name = 'Qux'; |
2492
|
|
|
$this->hydratorByValue->setNamingStrategy(new UnderscoreNamingStrategy()); |
2493
|
|
|
$entity = $this->hydratorByValue->hydrate(['camel_case' => $name], new NamingStrategyEntity()); |
2494
|
|
|
$this->assertEquals($name, $entity->getCamelCase()); |
2495
|
|
|
} |
2496
|
|
|
|
2497
|
|
|
public function configureObjectManagerForSimplePrivateEntity() |
2498
|
|
|
{ |
2499
|
|
|
$refl = new ReflectionClass('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimplePrivateEntity'); |
2500
|
|
|
|
2501
|
|
|
$this |
|
|
|
|
2502
|
|
|
->metadata |
2503
|
|
|
->expects($this->any()) |
2504
|
|
|
->method('getName') |
2505
|
|
|
->will($this->returnValue('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimplePrivateEntity')); |
2506
|
|
|
$this |
2507
|
|
|
->metadata |
2508
|
|
|
->expects($this->any()) |
2509
|
|
|
->method('getAssociationNames') |
2510
|
|
|
->will($this->returnValue([])); |
2511
|
|
|
|
2512
|
|
|
$this |
2513
|
|
|
->metadata |
2514
|
|
|
->expects($this->any()) |
2515
|
|
|
->method('getFieldNames') |
2516
|
|
|
->will($this->returnValue(['private', 'protected'])); |
2517
|
|
|
|
2518
|
|
|
$this |
2519
|
|
|
->metadata |
2520
|
|
|
->expects($this->any()) |
2521
|
|
|
->method('getTypeOfField') |
2522
|
|
|
->with($this->logicalOr($this->equalTo('private'), $this->equalTo('protected'))) |
2523
|
|
|
->will($this->returnValue('integer')); |
2524
|
|
|
|
2525
|
|
|
$this |
2526
|
|
|
->metadata |
2527
|
|
|
->expects($this->any()) |
2528
|
|
|
->method('hasAssociation') |
2529
|
|
|
->will($this->returnValue(false)); |
2530
|
|
|
|
2531
|
|
|
$this |
2532
|
|
|
->metadata |
2533
|
|
|
->expects($this->any()) |
2534
|
|
|
->method('getIdentifierFieldNames') |
2535
|
|
|
->will($this->returnValue(['private'])); |
2536
|
|
|
|
2537
|
|
|
$this |
2538
|
|
|
->metadata |
2539
|
|
|
->expects($this->any()) |
2540
|
|
|
->method('getReflectionClass') |
2541
|
|
|
->will($this->returnValue($refl)); |
2542
|
|
|
|
2543
|
|
|
$this->hydratorByValue = new DoctrineObjectHydrator( |
2544
|
|
|
$this->objectManager, |
2545
|
|
|
true |
2546
|
|
|
); |
2547
|
|
|
$this->hydratorByReference = new DoctrineObjectHydrator( |
2548
|
|
|
$this->objectManager, |
2549
|
|
|
false |
2550
|
|
|
); |
2551
|
|
|
} |
2552
|
|
|
|
2553
|
|
|
public function testCannotHydratePrivateByValue() |
2554
|
|
|
{ |
2555
|
|
|
$entity = new Asset\SimplePrivateEntity(); |
2556
|
|
|
$this->configureObjectManagerForSimplePrivateEntity(); |
2557
|
|
|
$data = ['private' => 123, 'protected' => 456]; |
2558
|
|
|
|
2559
|
|
|
$this->hydratorByValue->hydrate($data, $entity); |
2560
|
|
|
|
2561
|
|
|
$this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimplePrivateEntity', $entity); |
2562
|
|
|
} |
2563
|
|
|
} |
2564
|
|
|
|
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: