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