Completed
Push — master ( 854c26...66addc )
by Tom
15s
created

configureObjectManagerForOneToOneEntity()   B

Complexity

Conditions 5
Paths 1

Size

Total Lines 89

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 89
rs 7.9288
c 0
b 0
f 0
cc 5
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\Common\Persistence\Mapping\ClassMetadata.

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:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\Common\Persistence\Mapping\ClassMetadata.

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:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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 configureObjectManagerForOneToOneEntity()
123
    {
124
        $refl = new ReflectionClass('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToOneEntity');
125
126
        $this
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\Common\Persistence\Mapping\ClassMetadata.

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:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
127
            ->metadata
128
            ->expects($this->any())
129
            ->method('getFieldNames')
130
            ->will($this->returnValue(['id']));
131
132
        $this
133
            ->metadata
134
            ->expects($this->any())
135
            ->method('getAssociationNames')
136
            ->will($this->returnValue(['toOne']));
137
138
        $this
139
            ->metadata
140
            ->expects($this->any())
141
            ->method('getTypeOfField')
142
            ->with($this->logicalOr($this->equalTo('id'), $this->equalTo('toOne')))
143
            ->will(
144
                $this->returnCallback(
145
                    function ($arg) {
146
                        if ($arg === 'id') {
147
                            return 'integer';
148
                        } elseif ($arg === 'toOne') {
149
                            return 'DoctrineModuleTest\Stdlib\Hydrator\Asset\ByValueDifferentiatorEntity';
150
                        }
151
152
                        throw new \InvalidArgumentException();
153
                    }
154
                )
155
            );
156
157
        $this
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\Common\Persistence\Mapping\ClassMetadata.

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:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
158
            ->metadata
159
            ->expects($this->any())
160
            ->method('hasAssociation')
161
            ->with($this->logicalOr($this->equalTo('id'), $this->equalTo('toOne')))
162
            ->will(
163
                $this->returnCallback(
164
                    function ($arg) {
165
                        if ($arg === 'id') {
166
                            return false;
167
                        } elseif ($arg === 'toOne') {
168
                            return true;
169
                        }
170
171
                        throw new \InvalidArgumentException();
172
                    }
173
                )
174
            );
175
176
        $this
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\Common\Persistence\Mapping\ClassMetadata.

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:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
177
            ->metadata
178
            ->expects($this->any())
179
            ->method('isSingleValuedAssociation')
180
            ->with('toOne')
181
            ->will($this->returnValue(true));
182
183
        $this
184
            ->metadata
185
            ->expects($this->any())
186
            ->method('getAssociationTargetClass')
187
            ->with('toOne')
188
            ->will($this->returnValue('DoctrineModuleTest\Stdlib\Hydrator\Asset\ByValueDifferentiatorEntity'));
189
190
        $this
191
            ->metadata
192
            ->expects($this->any())
193
            ->method('getReflectionClass')
194
            ->will($this->returnValue($refl));
195
196
        $this
197
            ->metadata
198
            ->expects($this->any())
199
            ->method('getIdentifier')
200
            ->will($this->returnValue(["id"]));
201
202
        $this->hydratorByValue     = new DoctrineObjectHydrator(
203
            $this->objectManager,
204
            true
205
        );
206
        $this->hydratorByReference = new DoctrineObjectHydrator(
207
            $this->objectManager,
208
            false
209
        );
210
    }
211
212
    public function testHandleTypeConversionsDatetime()
213
    {
214
        // When using hydration by value, it will use the public API of the entity to set values (setters)
215
        $this->configureObjectManagerForSimpleEntityWithGenericField('datetime');
216
217
        $entity = new Asset\SimpleEntityWithGenericField();
218
        $now = new DateTime();
219
        $now->setTimestamp(1522353676);
220
        $data = ['genericField' => 1522353676];
221
222
        $entity = $this->hydratorByValue->hydrate($data, $entity);
223
224
        $this->assertInstanceOf('DateTime', $entity->getGenericField());
225
        $this->assertEquals($now, $entity->getGenericField());
226
227
        $entity = $this->hydratorByReference->hydrate($data, $entity);
228
229
        $this->assertInstanceOf('DateTime', $entity->getGenericField());
230
        $this->assertEquals($now, $entity->getGenericField());
231
232
233
        $entity = new Asset\SimpleEntityWithGenericField();
234
        $now = new DateTime();
235
        $data = ['genericField' => $now->format('Y-m-d\TH:i:s\.u')];
236
237
        $entity = $this->hydratorByValue->hydrate($data, $entity);
238
239
        $this->assertInstanceOf('DateTime', $entity->getGenericField());
240
        $this->assertEquals($now, $entity->getGenericField());
241
242
        $entity = $this->hydratorByReference->hydrate($data, $entity);
243
244
        $this->assertInstanceOf('DateTime', $entity->getGenericField());
245
        $this->assertEquals($now, $entity->getGenericField());
246
247
248
        $entity = new Asset\SimpleEntityWithGenericField();
249
        $now = new DateTime();
250
        $data = ['genericField' => clone $now];
251
252
        $entity = $this->hydratorByValue->hydrate($data, $entity);
253
254
        $this->assertInstanceOf('DateTime', $entity->getGenericField());
255
        $this->assertEquals($now, $entity->getGenericField());
256
257
        $entity = $this->hydratorByReference->hydrate($data, $entity);
258
259
        $this->assertInstanceOf('DateTime', $entity->getGenericField());
260
        $this->assertEquals($now, $entity->getGenericField());
261
    }
262
263
    public function testHandleTypeConversionsDatetimetz()
264
    {
265
        // When using hydration by value, it will use the public API of the entity to set values (setters)
266
        $this->configureObjectManagerForSimpleEntityWithGenericField('datetimetz');
267
268
        $entity = new Asset\SimpleEntityWithGenericField();
269
        $now = new DateTime();
270
        $now->setTimestamp(1522353676);
271
        $data = ['genericField' => 1522353676];
272
273
        $entity = $this->hydratorByValue->hydrate($data, $entity);
274
275
        $this->assertInstanceOf('DateTime', $entity->getGenericField());
276
        $this->assertEquals($now, $entity->getGenericField());
277
278
        $entity = $this->hydratorByReference->hydrate($data, $entity);
279
280
        $this->assertInstanceOf('DateTime', $entity->getGenericField());
281
        $this->assertEquals($now, $entity->getGenericField());
282
283
284
        $entity = new Asset\SimpleEntityWithGenericField();
285
        $now = new DateTime();
286
        $data = ['genericField' => $now->format('Y-m-d\TH:i:s\.u')];
287
288
        $entity = $this->hydratorByValue->hydrate($data, $entity);
289
290
        $this->assertInstanceOf('DateTime', $entity->getGenericField());
291
        $this->assertEquals($now, $entity->getGenericField());
292
293
        $entity = $this->hydratorByReference->hydrate($data, $entity);
294
295
        $this->assertInstanceOf('DateTime', $entity->getGenericField());
296
        $this->assertEquals($now, $entity->getGenericField());
297
298
299
        $entity = new Asset\SimpleEntityWithGenericField();
300
        $now = new DateTime();
301
        $data = ['genericField' => clone $now];
302
303
        $entity = $this->hydratorByValue->hydrate($data, $entity);
304
305
        $this->assertInstanceOf('DateTime', $entity->getGenericField());
306
        $this->assertEquals($now, $entity->getGenericField());
307
308
        $entity = $this->hydratorByReference->hydrate($data, $entity);
309
310
        $this->assertInstanceOf('DateTime', $entity->getGenericField());
311
        $this->assertEquals($now, $entity->getGenericField());
312
    }
313
314
    public function testHandleTypeConversionsTime()
315
    {
316
        // When using hydration by value, it will use the public API of the entity to set values (setters)
317
        $this->configureObjectManagerForSimpleEntityWithGenericField('time');
318
319
        $entity = new Asset\SimpleEntityWithGenericField();
320
        $now = new DateTime();
321
        $now->setTimestamp(1522353676);
322
        $data = ['genericField' => 1522353676];
323
324
        $entity = $this->hydratorByValue->hydrate($data, $entity);
325
326
        $this->assertInstanceOf('DateTime', $entity->getGenericField());
327
        $this->assertEquals($now, $entity->getGenericField());
328
329
        $entity = $this->hydratorByReference->hydrate($data, $entity);
330
331
        $this->assertInstanceOf('DateTime', $entity->getGenericField());
332
        $this->assertEquals($now, $entity->getGenericField());
333
334
335
        $entity = new Asset\SimpleEntityWithGenericField();
336
        $now = new DateTime();
337
        $data = ['genericField' => $now->format('Y-m-d\TH:i:s\.u')];
338
339
        $entity = $this->hydratorByValue->hydrate($data, $entity);
340
341
        $this->assertInstanceOf('DateTime', $entity->getGenericField());
342
        $this->assertEquals($now, $entity->getGenericField());
343
344
        $entity = $this->hydratorByReference->hydrate($data, $entity);
345
346
        $this->assertInstanceOf('DateTime', $entity->getGenericField());
347
        $this->assertEquals($now, $entity->getGenericField());
348
349
350
        $entity = new Asset\SimpleEntityWithGenericField();
351
        $now = new DateTime();
352
        $data = ['genericField' => clone $now];
353
354
        $entity = $this->hydratorByValue->hydrate($data, $entity);
355
356
        $this->assertInstanceOf('DateTime', $entity->getGenericField());
357
        $this->assertEquals($now, $entity->getGenericField());
358
359
        $entity = $this->hydratorByReference->hydrate($data, $entity);
360
361
        $this->assertInstanceOf('DateTime', $entity->getGenericField());
362
        $this->assertEquals($now, $entity->getGenericField());
363
    }
364
365
    public function testHandleTypeConversionsDate()
366
    {
367
        // When using hydration by value, it will use the public API of the entity to set values (setters)
368
        $this->configureObjectManagerForSimpleEntityWithGenericField('date');
369
370
        $entity = new Asset\SimpleEntityWithGenericField();
371
        $now = new DateTime();
372
        $now->setTimestamp(1522353676);
373
        $data = ['genericField' => 1522353676];
374
375
        $entity = $this->hydratorByValue->hydrate($data, $entity);
376
377
        $this->assertInstanceOf('DateTime', $entity->getGenericField());
378
        $this->assertEquals($now, $entity->getGenericField());
379
380
        $entity = $this->hydratorByReference->hydrate($data, $entity);
381
382
        $this->assertInstanceOf('DateTime', $entity->getGenericField());
383
        $this->assertEquals($now, $entity->getGenericField());
384
385
386
        $entity = new Asset\SimpleEntityWithGenericField();
387
        $now = new DateTime();
388
        $data = ['genericField' => $now->format('Y-m-d\TH:i:s\.u')];
389
390
        $entity = $this->hydratorByValue->hydrate($data, $entity);
391
392
        $this->assertInstanceOf('DateTime', $entity->getGenericField());
393
        $this->assertEquals($now, $entity->getGenericField());
394
395
        $entity = $this->hydratorByReference->hydrate($data, $entity);
396
397
        $this->assertInstanceOf('DateTime', $entity->getGenericField());
398
        $this->assertEquals($now, $entity->getGenericField());
399
400
401
        $entity = new Asset\SimpleEntityWithGenericField();
402
        $now = new DateTime();
403
        $data = ['genericField' => clone $now];
404
405
        $entity = $this->hydratorByValue->hydrate($data, $entity);
406
407
        $this->assertInstanceOf('DateTime', $entity->getGenericField());
408
        $this->assertEquals($now, $entity->getGenericField());
409
410
        $entity = $this->hydratorByReference->hydrate($data, $entity);
411
412
        $this->assertInstanceOf('DateTime', $entity->getGenericField());
413
        $this->assertEquals($now, $entity->getGenericField());
414
    }
415
416
    public function testHandleTypeConversionsInteger()
417
    {
418
        // When using hydration by value, it will use the public API of the entity to set values (setters)
419
        $this->configureObjectManagerForSimpleEntityWithGenericField('integer');
420
421
        $entity = new Asset\SimpleEntityWithGenericField();
422
        $value = 123465;
423
        $data = ['genericField' => '123465'];
424
425
        $entity = $this->hydratorByValue->hydrate($data, $entity);
426
427
        $this->assertTrue(is_integer($entity->getGenericField()));
428
        $this->assertEquals($value, $entity->getGenericField());
429
430
        $entity = new Asset\SimpleEntityWithGenericField();
431
        $value = 123465;
432
        $data = ['genericField' => '123465'];
433
434
        $entity = $this->hydratorByReference->hydrate($data, $entity);
435
436
        $this->assertTrue(is_integer($entity->getGenericField()));
437
        $this->assertEquals($value, $entity->getGenericField());
438
    }
439
440
    public function testHandleTypeConversionsSmallint()
441
    {
442
        // When using hydration by value, it will use the public API of the entity to set values (setters)
443
        $this->configureObjectManagerForSimpleEntityWithGenericField('smallint');
444
445
        $entity = new Asset\SimpleEntityWithGenericField();
446
        $value = 123465;
447
        $data = ['genericField' => '123465'];
448
449
        $entity = $this->hydratorByValue->hydrate($data, $entity);
450
451
        $this->assertTrue(is_integer($entity->getGenericField()));
452
        $this->assertEquals($value, $entity->getGenericField());
453
454
        $entity = new Asset\SimpleEntityWithGenericField();
455
        $value = 123465;
456
        $data = ['genericField' => '123465'];
457
458
        $entity = $this->hydratorByReference->hydrate($data, $entity);
459
460
        $this->assertTrue(is_integer($entity->getGenericField()));
461
        $this->assertEquals($value, $entity->getGenericField());
462
    }
463
464
    public function testHandleTypeConversionsFloat()
465
    {
466
        // When using hydration by value, it will use the public API of the entity to set values (setters)
467
        $this->configureObjectManagerForSimpleEntityWithGenericField('float');
468
469
        $entity = new Asset\SimpleEntityWithGenericField();
470
        $value = 123.465;
471
        $data = ['genericField' => '123.465'];
472
473
        $entity = $this->hydratorByValue->hydrate($data, $entity);
474
475
        $this->assertTrue(is_float($entity->getGenericField()));
476
        $this->assertEquals($value, $entity->getGenericField());
477
478
        $entity = new Asset\SimpleEntityWithGenericField();
479
        $value = 123.465;
480
        $data = ['genericField' => '123.465'];
481
482
        $entity = $this->hydratorByReference->hydrate($data, $entity);
483
484
        $this->assertTrue(is_float($entity->getGenericField()));
485
        $this->assertEquals($value, $entity->getGenericField());
486
    }
487
488
    public function testHandleTypeConversionsBoolean()
489
    {
490
        // When using hydration by value, it will use the public API of the entity to set values (setters)
491
        $this->configureObjectManagerForSimpleEntityWithGenericField('boolean');
492
493
        $entity = new Asset\SimpleEntityWithGenericField();
494
        $data = ['genericField' => true];
495
496
        $entity = $this->hydratorByValue->hydrate($data, $entity);
497
498
        $this->assertTrue(is_bool($entity->getGenericField()));
499
        $this->assertEquals(true, $entity->getGenericField());
500
501
        $entity = new Asset\SimpleEntityWithGenericField();
502
        $data = ['genericField' => true];
503
504
        $entity = $this->hydratorByReference->hydrate($data, $entity);
505
506
        $this->assertTrue(is_bool($entity->getGenericField()));
507
        $this->assertEquals(true, $entity->getGenericField());
508
509
510
        $entity = new Asset\SimpleEntityWithGenericField();
511
        $data = ['genericField' => 1];
512
513
        $entity = $this->hydratorByValue->hydrate($data, $entity);
514
515
        $this->assertTrue(is_bool($entity->getGenericField()));
516
        $this->assertEquals(true, $entity->getGenericField());
517
518
        $entity = new Asset\SimpleEntityWithGenericField();
519
        $data = ['genericField' => 1];
520
521
        $entity = $this->hydratorByReference->hydrate($data, $entity);
522
523
        $this->assertTrue(is_bool($entity->getGenericField()));
524
        $this->assertEquals(true, $entity->getGenericField());
525
    }
526
527
    public function testHandleTypeConversionsString()
528
    {
529
        // When using hydration by value, it will use the public API of the entity to set values (setters)
530
        $this->configureObjectManagerForSimpleEntityWithGenericField('string');
531
532
        $entity = new Asset\SimpleEntityWithGenericField();
533
        $data = ['genericField' => 'stringvalue'];
534
535
        $entity = $this->hydratorByValue->hydrate($data, $entity);
536
537
        $this->assertTrue(is_string($entity->getGenericField()));
538
        $this->assertEquals('stringvalue', $entity->getGenericField());
539
540
        $entity = new Asset\SimpleEntityWithGenericField();
541
        $data = ['genericField' => 'stringvalue'];
542
543
        $entity = $this->hydratorByReference->hydrate($data, $entity);
544
545
        $this->assertTrue(is_string($entity->getGenericField()));
546
        $this->assertEquals('stringvalue', $entity->getGenericField());
547
548
549
        $entity = new Asset\SimpleEntityWithGenericField();
550
        $data = ['genericField' => 'stringvalue'];
551
552
        $entity = $this->hydratorByValue->hydrate($data, $entity);
553
554
        $this->assertTrue(is_string($entity->getGenericField()));
555
        $this->assertEquals('stringvalue', $entity->getGenericField());
556
557
        $entity = new Asset\SimpleEntityWithGenericField();
558
        $data = ['genericField' => 'stringvalue'];
559
560
        $entity = $this->hydratorByReference->hydrate($data, $entity);
561
562
        $this->assertTrue(is_string($entity->getGenericField()));
563
        $this->assertEquals('stringvalue', $entity->getGenericField());
564
565
        $entity = new Asset\SimpleEntityWithGenericField();
566
        $data = ['genericField' => 12345];
567
568
        $entity = $this->hydratorByReference->hydrate($data, $entity);
569
570
        $this->assertTrue(is_string($entity->getGenericField()));
571
        $this->assertEquals('12345', $entity->getGenericField());
572
    }
573
574
    public function testHandleTypeConversionsText()
575
    {
576
        // When using hydration by value, it will use the public API of the entity to set values (setters)
577
        $this->configureObjectManagerForSimpleEntityWithGenericField('text');
578
579
        $entity = new Asset\SimpleEntityWithGenericField();
580
        $data = ['genericField' => 'stringvalue'];
581
582
        $entity = $this->hydratorByValue->hydrate($data, $entity);
583
584
        $this->assertTrue(is_string($entity->getGenericField()));
585
        $this->assertEquals('stringvalue', $entity->getGenericField());
586
587
        $entity = new Asset\SimpleEntityWithGenericField();
588
        $data = ['genericField' => 'stringvalue'];
589
590
        $entity = $this->hydratorByReference->hydrate($data, $entity);
591
592
        $this->assertTrue(is_string($entity->getGenericField()));
593
        $this->assertEquals('stringvalue', $entity->getGenericField());
594
595
596
        $entity = new Asset\SimpleEntityWithGenericField();
597
        $data = ['genericField' => 'stringvalue'];
598
599
        $entity = $this->hydratorByValue->hydrate($data, $entity);
600
601
        $this->assertTrue(is_string($entity->getGenericField()));
602
        $this->assertEquals('stringvalue', $entity->getGenericField());
603
604
        $entity = new Asset\SimpleEntityWithGenericField();
605
        $data = ['genericField' => 'stringvalue'];
606
607
        $entity = $this->hydratorByReference->hydrate($data, $entity);
608
609
        $this->assertTrue(is_string($entity->getGenericField()));
610
        $this->assertEquals('stringvalue', $entity->getGenericField());
611
612
        $entity = new Asset\SimpleEntityWithGenericField();
613
        $data = ['genericField' => 12345];
614
615
        $entity = $this->hydratorByReference->hydrate($data, $entity);
616
617
        $this->assertTrue(is_string($entity->getGenericField()));
618
        $this->assertEquals('12345', $entity->getGenericField());
619
    }
620
621
    public function testHandleTypeConversionsBigint()
622
    {
623
        // When using hydration by value, it will use the public API of the entity to set values (setters)
624
        $this->configureObjectManagerForSimpleEntityWithGenericField('bigint');
625
626
        $entity = new Asset\SimpleEntityWithGenericField();
627
        $data = ['genericField' => 'stringvalue'];
628
629
        $entity = $this->hydratorByValue->hydrate($data, $entity);
630
631
        $this->assertTrue(is_string($entity->getGenericField()));
632
        $this->assertEquals('stringvalue', $entity->getGenericField());
633
634
        $entity = new Asset\SimpleEntityWithGenericField();
635
        $data = ['genericField' => 'stringvalue'];
636
637
        $entity = $this->hydratorByReference->hydrate($data, $entity);
638
639
        $this->assertTrue(is_string($entity->getGenericField()));
640
        $this->assertEquals('stringvalue', $entity->getGenericField());
641
642
643
        $entity = new Asset\SimpleEntityWithGenericField();
644
        $data = ['genericField' => 'stringvalue'];
645
646
        $entity = $this->hydratorByValue->hydrate($data, $entity);
647
648
        $this->assertTrue(is_string($entity->getGenericField()));
649
        $this->assertEquals('stringvalue', $entity->getGenericField());
650
651
        $entity = new Asset\SimpleEntityWithGenericField();
652
        $data = ['genericField' => 'stringvalue'];
653
654
        $entity = $this->hydratorByReference->hydrate($data, $entity);
655
656
        $this->assertTrue(is_string($entity->getGenericField()));
657
        $this->assertEquals('stringvalue', $entity->getGenericField());
658
659
        $entity = new Asset\SimpleEntityWithGenericField();
660
        $data = ['genericField' => 12345];
661
662
        $entity = $this->hydratorByReference->hydrate($data, $entity);
663
664
        $this->assertTrue(is_string($entity->getGenericField()));
665
        $this->assertEquals('12345', $entity->getGenericField());
666
    }
667
668
    public function testHandleTypeConversionsDecimal()
669
    {
670
        // When using hydration by value, it will use the public API of the entity to set values (setters)
671
        $this->configureObjectManagerForSimpleEntityWithGenericField('decimal');
672
673
        $entity = new Asset\SimpleEntityWithGenericField();
674
        $data = ['genericField' => '123.45'];
675
676
        $entity = $this->hydratorByValue->hydrate($data, $entity);
677
678
        $this->assertTrue(is_string($entity->getGenericField()));
679
        $this->assertEquals('123.45', $entity->getGenericField());
680
681
        $entity = new Asset\SimpleEntityWithGenericField();
682
        $data = ['genericField' => '123.45'];
683
684
        $entity = $this->hydratorByReference->hydrate($data, $entity);
685
686
        $this->assertTrue(is_string($entity->getGenericField()));
687
        $this->assertEquals('123.45', $entity->getGenericField());
688
689
690
        $entity = new Asset\SimpleEntityWithGenericField();
691
        $data = ['genericField' => '123.45'];
692
693
        $entity = $this->hydratorByValue->hydrate($data, $entity);
694
695
        $this->assertTrue(is_string($entity->getGenericField()));
696
        $this->assertEquals('123.45', $entity->getGenericField());
697
698
        $entity = new Asset\SimpleEntityWithGenericField();
699
        $data = ['genericField' => '123.45'];
700
701
        $entity = $this->hydratorByReference->hydrate($data, $entity);
702
703
        $this->assertTrue(is_string($entity->getGenericField()));
704
        $this->assertEquals('123.45', $entity->getGenericField());
705
706
        $entity = new Asset\SimpleEntityWithGenericField();
707
        $data = ['genericField' => 12345];
708
709
        $entity = $this->hydratorByReference->hydrate($data, $entity);
710
711
        $this->assertTrue(is_string($entity->getGenericField()));
712
        $this->assertEquals('12345', $entity->getGenericField());
713
    }
714
715
    public function testHandleTypeConversionsNullable()
716
    {
717
        // When using hydration by value, it will use the public API of the entity to set values (setters)
718
        $this->configureObjectManagerForSimpleEntityWithGenericField(null);
719
720
        $entity = new Asset\SimpleEntityWithGenericField();
721
        $data = ['genericField' => null];
722
723
        $entity = $this->hydratorByValue->hydrate($data, $entity);
724
725
        $this->assertNull($entity->getGenericField());
726
727
        $entity = new Asset\SimpleEntityWithGenericField();
728
        $data = ['genericField' => null];
729
730
        $entity = $this->hydratorByReference->hydrate($data, $entity);
731
732
        $this->assertNull($entity->getGenericField());
733
    }
734
735
    public function testHandleTypeConversionsNullableForAssociatedFields()
736
    {
737
        $this->configureObjectManagerForOneToOneEntity();
738
739
        $entity = new Asset\OneToOneEntity();
740
        $data = ['toOne' => null];
741
742
        $entity = $this->hydratorByReference->hydrate($data, $entity);
743
744
        $this->assertNull($entity->getToOne(false));
745
    }
746
}
747