1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DoctrineModuleTest\Stdlib\Hydrator; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use ReflectionClass; |
7
|
|
|
use PHPUnit\Framework\TestCase as BaseTestCase; |
8
|
|
|
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineObjectHydrator; |
9
|
|
|
|
10
|
|
|
class DoctrineObjectTypeConversionsTest extends BaseTestCase |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var DoctrineObjectHydrator |
14
|
|
|
*/ |
15
|
|
|
protected $hydratorByValue; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var DoctrineObjectHydrator |
19
|
|
|
*/ |
20
|
|
|
protected $hydratorByReference; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var \Doctrine\Common\Persistence\Mapping\ClassMetadata|\PHPUnit_Framework_MockObject_MockObject |
24
|
|
|
*/ |
25
|
|
|
protected $metadata; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var \Doctrine\Common\Persistence\ObjectManager|\PHPUnit_Framework_MockObject_MockObject |
29
|
|
|
*/ |
30
|
|
|
protected $objectManager; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* setUp |
34
|
|
|
*/ |
35
|
|
|
public function setUp() |
36
|
|
|
{ |
37
|
|
|
parent::setUp(); |
38
|
|
|
|
39
|
|
|
$this->metadata = $this->createMock('Doctrine\Common\Persistence\Mapping\ClassMetadata'); |
40
|
|
|
$this->objectManager = $this->createMock('Doctrine\Common\Persistence\ObjectManager'); |
41
|
|
|
|
42
|
|
|
$this->objectManager->expects($this->any()) |
43
|
|
|
->method('getClassMetadata') |
44
|
|
|
->will($this->returnValue($this->metadata)); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $genericFieldType |
49
|
|
|
*/ |
50
|
|
|
public function configureObjectManagerForSimpleEntityWithGenericField($genericFieldType) |
51
|
|
|
{ |
52
|
|
|
$refl = new ReflectionClass('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntityWithGenericField'); |
53
|
|
|
|
54
|
|
|
$this |
|
|
|
|
55
|
|
|
->metadata |
56
|
|
|
->expects($this->any()) |
57
|
|
|
->method('getName') |
58
|
|
|
->will($this->returnValue('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntityWithGenericField')); |
59
|
|
|
$this |
60
|
|
|
->metadata |
61
|
|
|
->expects($this->any()) |
62
|
|
|
->method('getAssociationNames') |
63
|
|
|
->will($this->returnValue([])); |
64
|
|
|
|
65
|
|
|
$this |
66
|
|
|
->metadata |
67
|
|
|
->expects($this->any()) |
68
|
|
|
->method('getFieldNames') |
69
|
|
|
->will($this->returnValue(['id', 'genericField'])); |
70
|
|
|
|
71
|
|
|
$this |
72
|
|
|
->metadata |
73
|
|
|
->expects($this->any()) |
74
|
|
|
->method('getTypeOfField') |
75
|
|
|
->with($this->logicalOr($this->equalTo('id'), $this->equalTo('genericField'))) |
76
|
|
|
->will( |
77
|
|
|
$this->returnCallback( |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param string $arg |
81
|
|
|
*/ |
82
|
|
|
function ($arg) use ($genericFieldType) { |
83
|
|
|
if ('id' === $arg) { |
84
|
|
|
return 'integer'; |
85
|
|
|
} elseif ('genericField' === $arg) { |
86
|
|
|
return $genericFieldType; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
throw new \InvalidArgumentException(); |
90
|
|
|
} |
91
|
|
|
) |
92
|
|
|
); |
93
|
|
|
|
94
|
|
|
$this |
|
|
|
|
95
|
|
|
->metadata |
96
|
|
|
->expects($this->any()) |
97
|
|
|
->method('hasAssociation') |
98
|
|
|
->will($this->returnValue(false)); |
99
|
|
|
|
100
|
|
|
$this |
101
|
|
|
->metadata |
102
|
|
|
->expects($this->any()) |
103
|
|
|
->method('getIdentifierFieldNames') |
104
|
|
|
->will($this->returnValue(['id'])); |
105
|
|
|
|
106
|
|
|
$this |
107
|
|
|
->metadata |
108
|
|
|
->expects($this->any()) |
109
|
|
|
->method('getReflectionClass') |
110
|
|
|
->will($this->returnValue($refl)); |
111
|
|
|
|
112
|
|
|
$this->hydratorByValue = new DoctrineObjectHydrator( |
113
|
|
|
$this->objectManager, |
114
|
|
|
true |
115
|
|
|
); |
116
|
|
|
$this->hydratorByReference = new DoctrineObjectHydrator( |
117
|
|
|
$this->objectManager, |
118
|
|
|
false |
119
|
|
|
); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function testHandleTypeConversionsDatetime() |
123
|
|
|
{ |
124
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
125
|
|
|
$this->configureObjectManagerForSimpleEntityWithGenericField('datetime'); |
126
|
|
|
|
127
|
|
|
$entity = new Asset\SimpleEntityWithGenericField(); |
128
|
|
|
$now = new DateTime(); |
129
|
|
|
$now->setTimestamp(1522353676); |
130
|
|
|
$data = ['genericField' => 1522353676]; |
131
|
|
|
|
132
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
133
|
|
|
|
134
|
|
|
$this->assertInstanceOf('DateTime', $entity->getGenericField()); |
135
|
|
|
$this->assertEquals($now, $entity->getGenericField()); |
136
|
|
|
|
137
|
|
|
$entity = $this->hydratorByReference->hydrate($data, $entity); |
138
|
|
|
|
139
|
|
|
$this->assertInstanceOf('DateTime', $entity->getGenericField()); |
140
|
|
|
$this->assertEquals($now, $entity->getGenericField()); |
141
|
|
|
|
142
|
|
|
|
143
|
|
|
$entity = new Asset\SimpleEntityWithGenericField(); |
144
|
|
|
$now = new DateTime(); |
145
|
|
|
$data = ['genericField' => $now->format('Y-m-d\TH:i:s\.u')]; |
146
|
|
|
|
147
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
148
|
|
|
|
149
|
|
|
$this->assertInstanceOf('DateTime', $entity->getGenericField()); |
150
|
|
|
$this->assertEquals($now, $entity->getGenericField()); |
151
|
|
|
|
152
|
|
|
$entity = $this->hydratorByReference->hydrate($data, $entity); |
153
|
|
|
|
154
|
|
|
$this->assertInstanceOf('DateTime', $entity->getGenericField()); |
155
|
|
|
$this->assertEquals($now, $entity->getGenericField()); |
156
|
|
|
|
157
|
|
|
|
158
|
|
|
$entity = new Asset\SimpleEntityWithGenericField(); |
159
|
|
|
$now = new DateTime(); |
160
|
|
|
$data = ['genericField' => clone $now]; |
161
|
|
|
|
162
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
163
|
|
|
|
164
|
|
|
$this->assertInstanceOf('DateTime', $entity->getGenericField()); |
165
|
|
|
$this->assertEquals($now, $entity->getGenericField()); |
166
|
|
|
|
167
|
|
|
$entity = $this->hydratorByReference->hydrate($data, $entity); |
168
|
|
|
|
169
|
|
|
$this->assertInstanceOf('DateTime', $entity->getGenericField()); |
170
|
|
|
$this->assertEquals($now, $entity->getGenericField()); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function testHandleTypeConversionsDatetimetz() |
174
|
|
|
{ |
175
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
176
|
|
|
$this->configureObjectManagerForSimpleEntityWithGenericField('datetimetz'); |
177
|
|
|
|
178
|
|
|
$entity = new Asset\SimpleEntityWithGenericField(); |
179
|
|
|
$now = new DateTime(); |
180
|
|
|
$now->setTimestamp(1522353676); |
181
|
|
|
$data = ['genericField' => 1522353676]; |
182
|
|
|
|
183
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
184
|
|
|
|
185
|
|
|
$this->assertInstanceOf('DateTime', $entity->getGenericField()); |
186
|
|
|
$this->assertEquals($now, $entity->getGenericField()); |
187
|
|
|
|
188
|
|
|
$entity = $this->hydratorByReference->hydrate($data, $entity); |
189
|
|
|
|
190
|
|
|
$this->assertInstanceOf('DateTime', $entity->getGenericField()); |
191
|
|
|
$this->assertEquals($now, $entity->getGenericField()); |
192
|
|
|
|
193
|
|
|
|
194
|
|
|
$entity = new Asset\SimpleEntityWithGenericField(); |
195
|
|
|
$now = new DateTime(); |
196
|
|
|
$data = ['genericField' => $now->format('Y-m-d\TH:i:s\.u')]; |
197
|
|
|
|
198
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
199
|
|
|
|
200
|
|
|
$this->assertInstanceOf('DateTime', $entity->getGenericField()); |
201
|
|
|
$this->assertEquals($now, $entity->getGenericField()); |
202
|
|
|
|
203
|
|
|
$entity = $this->hydratorByReference->hydrate($data, $entity); |
204
|
|
|
|
205
|
|
|
$this->assertInstanceOf('DateTime', $entity->getGenericField()); |
206
|
|
|
$this->assertEquals($now, $entity->getGenericField()); |
207
|
|
|
|
208
|
|
|
|
209
|
|
|
$entity = new Asset\SimpleEntityWithGenericField(); |
210
|
|
|
$now = new DateTime(); |
211
|
|
|
$data = ['genericField' => clone $now]; |
212
|
|
|
|
213
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
214
|
|
|
|
215
|
|
|
$this->assertInstanceOf('DateTime', $entity->getGenericField()); |
216
|
|
|
$this->assertEquals($now, $entity->getGenericField()); |
217
|
|
|
|
218
|
|
|
$entity = $this->hydratorByReference->hydrate($data, $entity); |
219
|
|
|
|
220
|
|
|
$this->assertInstanceOf('DateTime', $entity->getGenericField()); |
221
|
|
|
$this->assertEquals($now, $entity->getGenericField()); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
public function testHandleTypeConversionsTime() |
225
|
|
|
{ |
226
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
227
|
|
|
$this->configureObjectManagerForSimpleEntityWithGenericField('time'); |
228
|
|
|
|
229
|
|
|
$entity = new Asset\SimpleEntityWithGenericField(); |
230
|
|
|
$now = new DateTime(); |
231
|
|
|
$now->setTimestamp(1522353676); |
232
|
|
|
$data = ['genericField' => 1522353676]; |
233
|
|
|
|
234
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
235
|
|
|
|
236
|
|
|
$this->assertInstanceOf('DateTime', $entity->getGenericField()); |
237
|
|
|
$this->assertEquals($now, $entity->getGenericField()); |
238
|
|
|
|
239
|
|
|
$entity = $this->hydratorByReference->hydrate($data, $entity); |
240
|
|
|
|
241
|
|
|
$this->assertInstanceOf('DateTime', $entity->getGenericField()); |
242
|
|
|
$this->assertEquals($now, $entity->getGenericField()); |
243
|
|
|
|
244
|
|
|
|
245
|
|
|
$entity = new Asset\SimpleEntityWithGenericField(); |
246
|
|
|
$now = new DateTime(); |
247
|
|
|
$data = ['genericField' => $now->format('Y-m-d\TH:i:s\.u')]; |
248
|
|
|
|
249
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
250
|
|
|
|
251
|
|
|
$this->assertInstanceOf('DateTime', $entity->getGenericField()); |
252
|
|
|
$this->assertEquals($now, $entity->getGenericField()); |
253
|
|
|
|
254
|
|
|
$entity = $this->hydratorByReference->hydrate($data, $entity); |
255
|
|
|
|
256
|
|
|
$this->assertInstanceOf('DateTime', $entity->getGenericField()); |
257
|
|
|
$this->assertEquals($now, $entity->getGenericField()); |
258
|
|
|
|
259
|
|
|
|
260
|
|
|
$entity = new Asset\SimpleEntityWithGenericField(); |
261
|
|
|
$now = new DateTime(); |
262
|
|
|
$data = ['genericField' => clone $now]; |
263
|
|
|
|
264
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
265
|
|
|
|
266
|
|
|
$this->assertInstanceOf('DateTime', $entity->getGenericField()); |
267
|
|
|
$this->assertEquals($now, $entity->getGenericField()); |
268
|
|
|
|
269
|
|
|
$entity = $this->hydratorByReference->hydrate($data, $entity); |
270
|
|
|
|
271
|
|
|
$this->assertInstanceOf('DateTime', $entity->getGenericField()); |
272
|
|
|
$this->assertEquals($now, $entity->getGenericField()); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
public function testHandleTypeConversionsDate() |
276
|
|
|
{ |
277
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
278
|
|
|
$this->configureObjectManagerForSimpleEntityWithGenericField('date'); |
279
|
|
|
|
280
|
|
|
$entity = new Asset\SimpleEntityWithGenericField(); |
281
|
|
|
$now = new DateTime(); |
282
|
|
|
$now->setTimestamp(1522353676); |
283
|
|
|
$data = ['genericField' => 1522353676]; |
284
|
|
|
|
285
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
286
|
|
|
|
287
|
|
|
$this->assertInstanceOf('DateTime', $entity->getGenericField()); |
288
|
|
|
$this->assertEquals($now, $entity->getGenericField()); |
289
|
|
|
|
290
|
|
|
$entity = $this->hydratorByReference->hydrate($data, $entity); |
291
|
|
|
|
292
|
|
|
$this->assertInstanceOf('DateTime', $entity->getGenericField()); |
293
|
|
|
$this->assertEquals($now, $entity->getGenericField()); |
294
|
|
|
|
295
|
|
|
|
296
|
|
|
$entity = new Asset\SimpleEntityWithGenericField(); |
297
|
|
|
$now = new DateTime(); |
298
|
|
|
$data = ['genericField' => $now->format('Y-m-d\TH:i:s\.u')]; |
299
|
|
|
|
300
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
301
|
|
|
|
302
|
|
|
$this->assertInstanceOf('DateTime', $entity->getGenericField()); |
303
|
|
|
$this->assertEquals($now, $entity->getGenericField()); |
304
|
|
|
|
305
|
|
|
$entity = $this->hydratorByReference->hydrate($data, $entity); |
306
|
|
|
|
307
|
|
|
$this->assertInstanceOf('DateTime', $entity->getGenericField()); |
308
|
|
|
$this->assertEquals($now, $entity->getGenericField()); |
309
|
|
|
|
310
|
|
|
|
311
|
|
|
$entity = new Asset\SimpleEntityWithGenericField(); |
312
|
|
|
$now = new DateTime(); |
313
|
|
|
$data = ['genericField' => clone $now]; |
314
|
|
|
|
315
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
316
|
|
|
|
317
|
|
|
$this->assertInstanceOf('DateTime', $entity->getGenericField()); |
318
|
|
|
$this->assertEquals($now, $entity->getGenericField()); |
319
|
|
|
|
320
|
|
|
$entity = $this->hydratorByReference->hydrate($data, $entity); |
321
|
|
|
|
322
|
|
|
$this->assertInstanceOf('DateTime', $entity->getGenericField()); |
323
|
|
|
$this->assertEquals($now, $entity->getGenericField()); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
public function testHandleTypeConversionsInteger() |
327
|
|
|
{ |
328
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
329
|
|
|
$this->configureObjectManagerForSimpleEntityWithGenericField('integer'); |
330
|
|
|
|
331
|
|
|
$entity = new Asset\SimpleEntityWithGenericField(); |
332
|
|
|
$value = 123465; |
333
|
|
|
$data = ['genericField' => '123465']; |
334
|
|
|
|
335
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
336
|
|
|
|
337
|
|
|
$this->assertTrue(is_integer($entity->getGenericField())); |
338
|
|
|
$this->assertEquals($value, $entity->getGenericField()); |
339
|
|
|
|
340
|
|
|
$entity = new Asset\SimpleEntityWithGenericField(); |
341
|
|
|
$value = 123465; |
342
|
|
|
$data = ['genericField' => '123465']; |
343
|
|
|
|
344
|
|
|
$entity = $this->hydratorByReference->hydrate($data, $entity); |
345
|
|
|
|
346
|
|
|
$this->assertTrue(is_integer($entity->getGenericField())); |
347
|
|
|
$this->assertEquals($value, $entity->getGenericField()); |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
public function testHandleTypeConversionsSmallint() |
351
|
|
|
{ |
352
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
353
|
|
|
$this->configureObjectManagerForSimpleEntityWithGenericField('smallint'); |
354
|
|
|
|
355
|
|
|
$entity = new Asset\SimpleEntityWithGenericField(); |
356
|
|
|
$value = 123465; |
357
|
|
|
$data = ['genericField' => '123465']; |
358
|
|
|
|
359
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
360
|
|
|
|
361
|
|
|
$this->assertTrue(is_integer($entity->getGenericField())); |
362
|
|
|
$this->assertEquals($value, $entity->getGenericField()); |
363
|
|
|
|
364
|
|
|
$entity = new Asset\SimpleEntityWithGenericField(); |
365
|
|
|
$value = 123465; |
366
|
|
|
$data = ['genericField' => '123465']; |
367
|
|
|
|
368
|
|
|
$entity = $this->hydratorByReference->hydrate($data, $entity); |
369
|
|
|
|
370
|
|
|
$this->assertTrue(is_integer($entity->getGenericField())); |
371
|
|
|
$this->assertEquals($value, $entity->getGenericField()); |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
public function testHandleTypeConversionsFloat() |
375
|
|
|
{ |
376
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
377
|
|
|
$this->configureObjectManagerForSimpleEntityWithGenericField('float'); |
378
|
|
|
|
379
|
|
|
$entity = new Asset\SimpleEntityWithGenericField(); |
380
|
|
|
$value = 123.465; |
381
|
|
|
$data = ['genericField' => '123.465']; |
382
|
|
|
|
383
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
384
|
|
|
|
385
|
|
|
$this->assertTrue(is_float($entity->getGenericField())); |
386
|
|
|
$this->assertEquals($value, $entity->getGenericField()); |
387
|
|
|
|
388
|
|
|
$entity = new Asset\SimpleEntityWithGenericField(); |
389
|
|
|
$value = 123.465; |
390
|
|
|
$data = ['genericField' => '123.465']; |
391
|
|
|
|
392
|
|
|
$entity = $this->hydratorByReference->hydrate($data, $entity); |
393
|
|
|
|
394
|
|
|
$this->assertTrue(is_float($entity->getGenericField())); |
395
|
|
|
$this->assertEquals($value, $entity->getGenericField()); |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
public function testHandleTypeConversionsBoolean() |
399
|
|
|
{ |
400
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
401
|
|
|
$this->configureObjectManagerForSimpleEntityWithGenericField('boolean'); |
402
|
|
|
|
403
|
|
|
$entity = new Asset\SimpleEntityWithGenericField(); |
404
|
|
|
$data = ['genericField' => true]; |
405
|
|
|
|
406
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
407
|
|
|
|
408
|
|
|
$this->assertTrue(is_bool($entity->getGenericField())); |
409
|
|
|
$this->assertEquals(true, $entity->getGenericField()); |
410
|
|
|
|
411
|
|
|
$entity = new Asset\SimpleEntityWithGenericField(); |
412
|
|
|
$data = ['genericField' => true]; |
413
|
|
|
|
414
|
|
|
$entity = $this->hydratorByReference->hydrate($data, $entity); |
415
|
|
|
|
416
|
|
|
$this->assertTrue(is_bool($entity->getGenericField())); |
417
|
|
|
$this->assertEquals(true, $entity->getGenericField()); |
418
|
|
|
|
419
|
|
|
|
420
|
|
|
$entity = new Asset\SimpleEntityWithGenericField(); |
421
|
|
|
$data = ['genericField' => 1]; |
422
|
|
|
|
423
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
424
|
|
|
|
425
|
|
|
$this->assertTrue(is_bool($entity->getGenericField())); |
426
|
|
|
$this->assertEquals(true, $entity->getGenericField()); |
427
|
|
|
|
428
|
|
|
$entity = new Asset\SimpleEntityWithGenericField(); |
429
|
|
|
$data = ['genericField' => 1]; |
430
|
|
|
|
431
|
|
|
$entity = $this->hydratorByReference->hydrate($data, $entity); |
432
|
|
|
|
433
|
|
|
$this->assertTrue(is_bool($entity->getGenericField())); |
434
|
|
|
$this->assertEquals(true, $entity->getGenericField()); |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
public function testHandleTypeConversionsString() |
438
|
|
|
{ |
439
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
440
|
|
|
$this->configureObjectManagerForSimpleEntityWithGenericField('string'); |
441
|
|
|
|
442
|
|
|
$entity = new Asset\SimpleEntityWithGenericField(); |
443
|
|
|
$data = ['genericField' => 'stringvalue']; |
444
|
|
|
|
445
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
446
|
|
|
|
447
|
|
|
$this->assertTrue(is_string($entity->getGenericField())); |
448
|
|
|
$this->assertEquals('stringvalue', $entity->getGenericField()); |
449
|
|
|
|
450
|
|
|
$entity = new Asset\SimpleEntityWithGenericField(); |
451
|
|
|
$data = ['genericField' => 'stringvalue']; |
452
|
|
|
|
453
|
|
|
$entity = $this->hydratorByReference->hydrate($data, $entity); |
454
|
|
|
|
455
|
|
|
$this->assertTrue(is_string($entity->getGenericField())); |
456
|
|
|
$this->assertEquals('stringvalue', $entity->getGenericField()); |
457
|
|
|
|
458
|
|
|
|
459
|
|
|
$entity = new Asset\SimpleEntityWithGenericField(); |
460
|
|
|
$data = ['genericField' => 'stringvalue']; |
461
|
|
|
|
462
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
463
|
|
|
|
464
|
|
|
$this->assertTrue(is_string($entity->getGenericField())); |
465
|
|
|
$this->assertEquals('stringvalue', $entity->getGenericField()); |
466
|
|
|
|
467
|
|
|
$entity = new Asset\SimpleEntityWithGenericField(); |
468
|
|
|
$data = ['genericField' => 'stringvalue']; |
469
|
|
|
|
470
|
|
|
$entity = $this->hydratorByReference->hydrate($data, $entity); |
471
|
|
|
|
472
|
|
|
$this->assertTrue(is_string($entity->getGenericField())); |
473
|
|
|
$this->assertEquals('stringvalue', $entity->getGenericField()); |
474
|
|
|
|
475
|
|
|
$entity = new Asset\SimpleEntityWithGenericField(); |
476
|
|
|
$data = ['genericField' => 12345]; |
477
|
|
|
|
478
|
|
|
$entity = $this->hydratorByReference->hydrate($data, $entity); |
479
|
|
|
|
480
|
|
|
$this->assertTrue(is_string($entity->getGenericField())); |
481
|
|
|
$this->assertEquals('12345', $entity->getGenericField()); |
482
|
|
|
} |
483
|
|
|
|
484
|
|
|
public function testHandleTypeConversionsText() |
485
|
|
|
{ |
486
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
487
|
|
|
$this->configureObjectManagerForSimpleEntityWithGenericField('text'); |
488
|
|
|
|
489
|
|
|
$entity = new Asset\SimpleEntityWithGenericField(); |
490
|
|
|
$data = ['genericField' => 'stringvalue']; |
491
|
|
|
|
492
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
493
|
|
|
|
494
|
|
|
$this->assertTrue(is_string($entity->getGenericField())); |
495
|
|
|
$this->assertEquals('stringvalue', $entity->getGenericField()); |
496
|
|
|
|
497
|
|
|
$entity = new Asset\SimpleEntityWithGenericField(); |
498
|
|
|
$data = ['genericField' => 'stringvalue']; |
499
|
|
|
|
500
|
|
|
$entity = $this->hydratorByReference->hydrate($data, $entity); |
501
|
|
|
|
502
|
|
|
$this->assertTrue(is_string($entity->getGenericField())); |
503
|
|
|
$this->assertEquals('stringvalue', $entity->getGenericField()); |
504
|
|
|
|
505
|
|
|
|
506
|
|
|
$entity = new Asset\SimpleEntityWithGenericField(); |
507
|
|
|
$data = ['genericField' => 'stringvalue']; |
508
|
|
|
|
509
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
510
|
|
|
|
511
|
|
|
$this->assertTrue(is_string($entity->getGenericField())); |
512
|
|
|
$this->assertEquals('stringvalue', $entity->getGenericField()); |
513
|
|
|
|
514
|
|
|
$entity = new Asset\SimpleEntityWithGenericField(); |
515
|
|
|
$data = ['genericField' => 'stringvalue']; |
516
|
|
|
|
517
|
|
|
$entity = $this->hydratorByReference->hydrate($data, $entity); |
518
|
|
|
|
519
|
|
|
$this->assertTrue(is_string($entity->getGenericField())); |
520
|
|
|
$this->assertEquals('stringvalue', $entity->getGenericField()); |
521
|
|
|
|
522
|
|
|
$entity = new Asset\SimpleEntityWithGenericField(); |
523
|
|
|
$data = ['genericField' => 12345]; |
524
|
|
|
|
525
|
|
|
$entity = $this->hydratorByReference->hydrate($data, $entity); |
526
|
|
|
|
527
|
|
|
$this->assertTrue(is_string($entity->getGenericField())); |
528
|
|
|
$this->assertEquals('12345', $entity->getGenericField()); |
529
|
|
|
} |
530
|
|
|
|
531
|
|
|
public function testHandleTypeConversionsBigint() |
532
|
|
|
{ |
533
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
534
|
|
|
$this->configureObjectManagerForSimpleEntityWithGenericField('bigint'); |
535
|
|
|
|
536
|
|
|
$entity = new Asset\SimpleEntityWithGenericField(); |
537
|
|
|
$data = ['genericField' => 'stringvalue']; |
538
|
|
|
|
539
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
540
|
|
|
|
541
|
|
|
$this->assertTrue(is_string($entity->getGenericField())); |
542
|
|
|
$this->assertEquals('stringvalue', $entity->getGenericField()); |
543
|
|
|
|
544
|
|
|
$entity = new Asset\SimpleEntityWithGenericField(); |
545
|
|
|
$data = ['genericField' => 'stringvalue']; |
546
|
|
|
|
547
|
|
|
$entity = $this->hydratorByReference->hydrate($data, $entity); |
548
|
|
|
|
549
|
|
|
$this->assertTrue(is_string($entity->getGenericField())); |
550
|
|
|
$this->assertEquals('stringvalue', $entity->getGenericField()); |
551
|
|
|
|
552
|
|
|
|
553
|
|
|
$entity = new Asset\SimpleEntityWithGenericField(); |
554
|
|
|
$data = ['genericField' => 'stringvalue']; |
555
|
|
|
|
556
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
557
|
|
|
|
558
|
|
|
$this->assertTrue(is_string($entity->getGenericField())); |
559
|
|
|
$this->assertEquals('stringvalue', $entity->getGenericField()); |
560
|
|
|
|
561
|
|
|
$entity = new Asset\SimpleEntityWithGenericField(); |
562
|
|
|
$data = ['genericField' => 'stringvalue']; |
563
|
|
|
|
564
|
|
|
$entity = $this->hydratorByReference->hydrate($data, $entity); |
565
|
|
|
|
566
|
|
|
$this->assertTrue(is_string($entity->getGenericField())); |
567
|
|
|
$this->assertEquals('stringvalue', $entity->getGenericField()); |
568
|
|
|
|
569
|
|
|
$entity = new Asset\SimpleEntityWithGenericField(); |
570
|
|
|
$data = ['genericField' => 12345]; |
571
|
|
|
|
572
|
|
|
$entity = $this->hydratorByReference->hydrate($data, $entity); |
573
|
|
|
|
574
|
|
|
$this->assertTrue(is_string($entity->getGenericField())); |
575
|
|
|
$this->assertEquals('12345', $entity->getGenericField()); |
576
|
|
|
} |
577
|
|
|
|
578
|
|
|
public function testHandleTypeConversionsDecimal() |
579
|
|
|
{ |
580
|
|
|
// When using hydration by value, it will use the public API of the entity to set values (setters) |
581
|
|
|
$this->configureObjectManagerForSimpleEntityWithGenericField('decimal'); |
582
|
|
|
|
583
|
|
|
$entity = new Asset\SimpleEntityWithGenericField(); |
584
|
|
|
$data = ['genericField' => '123.45']; |
585
|
|
|
|
586
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
587
|
|
|
|
588
|
|
|
$this->assertTrue(is_string($entity->getGenericField())); |
589
|
|
|
$this->assertEquals('123.45', $entity->getGenericField()); |
590
|
|
|
|
591
|
|
|
$entity = new Asset\SimpleEntityWithGenericField(); |
592
|
|
|
$data = ['genericField' => '123.45']; |
593
|
|
|
|
594
|
|
|
$entity = $this->hydratorByReference->hydrate($data, $entity); |
595
|
|
|
|
596
|
|
|
$this->assertTrue(is_string($entity->getGenericField())); |
597
|
|
|
$this->assertEquals('123.45', $entity->getGenericField()); |
598
|
|
|
|
599
|
|
|
|
600
|
|
|
$entity = new Asset\SimpleEntityWithGenericField(); |
601
|
|
|
$data = ['genericField' => '123.45']; |
602
|
|
|
|
603
|
|
|
$entity = $this->hydratorByValue->hydrate($data, $entity); |
604
|
|
|
|
605
|
|
|
$this->assertTrue(is_string($entity->getGenericField())); |
606
|
|
|
$this->assertEquals('123.45', $entity->getGenericField()); |
607
|
|
|
|
608
|
|
|
$entity = new Asset\SimpleEntityWithGenericField(); |
609
|
|
|
$data = ['genericField' => '123.45']; |
610
|
|
|
|
611
|
|
|
$entity = $this->hydratorByReference->hydrate($data, $entity); |
612
|
|
|
|
613
|
|
|
$this->assertTrue(is_string($entity->getGenericField())); |
614
|
|
|
$this->assertEquals('123.45', $entity->getGenericField()); |
615
|
|
|
|
616
|
|
|
$entity = new Asset\SimpleEntityWithGenericField(); |
617
|
|
|
$data = ['genericField' => 12345]; |
618
|
|
|
|
619
|
|
|
$entity = $this->hydratorByReference->hydrate($data, $entity); |
620
|
|
|
|
621
|
|
|
$this->assertTrue(is_string($entity->getGenericField())); |
622
|
|
|
$this->assertEquals('12345', $entity->getGenericField()); |
623
|
|
|
} |
624
|
|
|
} |
625
|
|
|
|
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: