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