Completed
Push — master ( 38c765...4253e9 )
by Gianluca
02:58
created

testHydrateOneToManyAssociationByValueUsingIdentifiersArrayForRelations()   B

Complexity

Conditions 4
Paths 2

Size

Total Lines 62
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 62
rs 8.9167
c 0
b 0
f 0
cc 4
eloc 39
nc 2
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 DoctrineModuleTest\Stdlib\Hydrator\Asset\ContextStrategy;
6
use PHPUnit_Framework_TestCase as BaseTestCase;
7
use ReflectionClass;
8
use Doctrine\Common\Collections\ArrayCollection;
9
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineObjectHydrator;
10
use DoctrineModule\Stdlib\Hydrator\Strategy;
11
use DoctrineModule\Stdlib\Hydrator\Filter;
12
use DoctrineModuleTest\Stdlib\Hydrator\Asset\NamingStrategyEntity;
13
use Zend\Hydrator\NamingStrategy\UnderscoreNamingStrategy;
14
15
class DoctrineObjectTest extends BaseTestCase
16
{
17
    /**
18
     * @var DoctrineObjectHydrator
19
     */
20
    protected $hydratorByValue;
21
22
    /**
23
     * @var DoctrineObjectHydrator
24
     */
25
    protected $hydratorByReference;
26
27
    /**
28
     * @var \Doctrine\Common\Persistence\Mapping\ClassMetadata|\PHPUnit_Framework_MockObject_MockObject
29
     */
30
    protected $metadata;
31
32
    /**
33
     * @var \Doctrine\Common\Persistence\ObjectManager|\PHPUnit_Framework_MockObject_MockObject
34
     */
35
    protected $objectManager;
36
37
    /**
38
     * setUp
39
     */
40
    public function setUp()
41
    {
42
        parent::setUp();
43
44
        $this->metadata      = $this->getMock('Doctrine\Common\Persistence\Mapping\ClassMetadata');
45
        $this->objectManager = $this->getMock('Doctrine\Common\Persistence\ObjectManager');
46
47
        $this->objectManager->expects($this->any())
48
                            ->method('getClassMetadata')
49
                            ->will($this->returnValue($this->metadata));
50
    }
51
52
    public function configureObjectManagerForSimpleEntity()
53
    {
54
        $refl = new ReflectionClass('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity');
55
56
        $this
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...
57
            ->metadata
58
            ->expects($this->any())
59
            ->method('getName')
60
            ->will($this->returnValue('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity'));
61
        $this
62
            ->metadata
63
            ->expects($this->any())
64
            ->method('getAssociationNames')
65
            ->will($this->returnValue(array()));
66
67
        $this
68
            ->metadata
69
            ->expects($this->any())
70
            ->method('getFieldNames')
71
            ->will($this->returnValue(array('id', 'field')));
72
73
        $this
74
            ->metadata
75
            ->expects($this->any())
76
            ->method('getTypeOfField')
77
            ->with($this->logicalOr($this->equalTo('id'), $this->equalTo('field')))
78
            ->will(
79
                $this->returnCallback(
80
                    function ($arg) {
81
                        if ('id' === $arg) {
82
                            return 'integer';
83
                        } elseif ('field' === $arg) {
84
                            return 'string';
85
                        }
86
87
                        throw new \InvalidArgumentException();
88
                    }
89
                )
90
            );
91
92
        $this
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...
93
            ->metadata
94
            ->expects($this->any())
95
            ->method('hasAssociation')
96
            ->will($this->returnValue(false));
97
98
        $this
99
            ->metadata
100
            ->expects($this->any())
101
            ->method('getIdentifierFieldNames')
102
            ->will($this->returnValue(array('id')));
103
104
        $this
105
            ->metadata
106
            ->expects($this->any())
107
            ->method('getReflectionClass')
108
            ->will($this->returnValue($refl));
109
110
        $this->hydratorByValue     = new DoctrineObjectHydrator(
111
            $this->objectManager,
112
            true
113
        );
114
        $this->hydratorByReference = new DoctrineObjectHydrator(
115
            $this->objectManager,
116
            false
117
        );
118
    }
119
120
    public function configureObjectManagerForNamingStrategyEntity()
121
    {
122
        $refl = new ReflectionClass('DoctrineModuleTest\Stdlib\Hydrator\Asset\NamingStrategyEntity');
123
124
        $this
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...
125
            ->metadata
126
            ->expects($this->any())
127
            ->method('getName')
128
            ->will($this->returnValue('DoctrineModuleTest\Stdlib\Hydrator\Asset\NamingStrategyEntity'));
129
        $this
130
            ->metadata
131
            ->expects($this->any())
132
            ->method('getAssociationNames')
133
            ->will($this->returnValue(array()));
134
135
        $this
136
            ->metadata
137
            ->expects($this->any())
138
            ->method('getFieldNames')
139
            ->will($this->returnValue(array('camelCase')));
140
141
        $this
142
            ->metadata
143
            ->expects($this->any())
144
            ->method('getTypeOfField')
145
            ->with($this->equalTo('camelCase'))
146
            ->will($this->returnValue('string'));
147
148
        $this
149
            ->metadata
150
            ->expects($this->any())
151
            ->method('hasAssociation')
152
            ->will($this->returnValue(false));
153
154
        $this
155
            ->metadata
156
            ->expects($this->any())
157
            ->method('getIdentifierFieldNames')
158
            ->will($this->returnValue(array('camelCase')));
159
160
        $this
161
            ->metadata
162
            ->expects($this->any())
163
            ->method('getReflectionClass')
164
            ->will($this->returnValue($refl));
165
166
        $this->hydratorByValue     = new DoctrineObjectHydrator(
167
            $this->objectManager,
168
            true
169
        );
170
        $this->hydratorByReference = new DoctrineObjectHydrator(
171
            $this->objectManager,
172
            false
173
        );
174
    }
175
176
    public function configureObjectManagerForSimpleIsEntity()
177
    {
178
        $refl = new ReflectionClass('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleIsEntity');
179
180
        $this
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...
181
            ->metadata
182
            ->expects($this->any())
183
            ->method('getName')
184
            ->will($this->returnValue('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleIsEntity'));
185
        $this
186
            ->metadata
187
            ->expects($this->any())
188
            ->method('getAssociationNames')
189
            ->will($this->returnValue(array()));
190
191
        $this
192
            ->metadata
193
            ->expects($this->any())
194
            ->method('getFieldNames')
195
            ->will($this->returnValue(array('id', 'done')));
196
197
        $this
198
            ->metadata
199
            ->expects($this->any())
200
            ->method('getTypeOfField')
201
            ->with($this->logicalOr($this->equalTo('id'), $this->equalTo('done')))
202
            ->will(
203
                $this->returnCallback(
204
                    function ($arg) {
205
                        if ('id' === $arg) {
206
                            return 'integer';
207
                        } elseif ('done' === $arg) {
208
                            return 'boolean';
209
                        }
210
211
                        throw new \InvalidArgumentException();
212
                    }
213
                )
214
            );
215
216
        $this
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...
217
            ->metadata
218
            ->expects($this->any())
219
            ->method('hasAssociation')
220
            ->will($this->returnValue(false));
221
222
        $this
223
            ->metadata
224
            ->expects($this->any())
225
            ->method('getIdentifierFieldNames')
226
            ->will($this->returnValue(array('id')));
227
228
        $this
229
            ->metadata
230
            ->expects($this->any())
231
            ->method('getReflectionClass')
232
            ->will($this->returnValue($refl));
233
234
        $this->hydratorByValue     = new DoctrineObjectHydrator(
235
            $this->objectManager,
236
            true
237
        );
238
        $this->hydratorByReference = new DoctrineObjectHydrator(
239
            $this->objectManager,
240
            false
241
        );
242
    }
243
244
    public function configureObjectManagerForSimpleEntityWithIsBoolean()
245
    {
246
        $refl = new ReflectionClass('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntityWithIsBoolean');
247
248
        $this
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...
249
            ->metadata
250
            ->expects($this->any())
251
            ->method('getName')
252
            ->will($this->returnValue('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntityWithIsBoolean'));
253
        $this
254
            ->metadata
255
            ->expects($this->any())
256
            ->method('getAssociationNames')
257
            ->will($this->returnValue(array()));
258
259
        $this
260
            ->metadata
261
            ->expects($this->any())
262
            ->method('getFieldNames')
263
            ->will($this->returnValue(array('id', 'isActive')));
264
265
        $this
266
            ->metadata
267
            ->expects($this->any())
268
            ->method('getTypeOfField')
269
            ->with($this->logicalOr($this->equalTo('id'), $this->equalTo('isActive')))
270
            ->will(
271
                $this->returnCallback(
272
                    function ($arg) {
273
                        if ('id' === $arg) {
274
                            return 'integer';
275
                        } elseif ('isActive' === $arg) {
276
                            return 'boolean';
277
                        }
278
279
                        throw new \InvalidArgumentException();
280
                    }
281
                )
282
            );
283
284
        $this
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...
285
            ->metadata
286
            ->expects($this->any())
287
            ->method('hasAssociation')
288
            ->will($this->returnValue(false));
289
290
        $this
291
            ->metadata
292
            ->expects($this->any())
293
            ->method('getIdentifierFieldNames')
294
            ->will($this->returnValue(array('id')));
295
296
        $this
297
            ->metadata
298
            ->expects($this->any())
299
            ->method('getReflectionClass')
300
            ->will($this->returnValue($refl));
301
302
        $this->hydratorByValue     = new DoctrineObjectHydrator(
303
            $this->objectManager,
304
            true
305
        );
306
        $this->hydratorByReference = new DoctrineObjectHydrator(
307
            $this->objectManager,
308
            false
309
        );
310
    }
311
312
    public function configureObjectManagerForSimpleEntityWithStringId()
313
    {
314
        $refl = new ReflectionClass('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity');
315
316
        $this
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...
317
            ->metadata
318
            ->expects($this->any())
319
            ->method('getName')
320
            ->will($this->returnValue('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity'));
321
        $this
322
            ->metadata
323
            ->expects($this->any())
324
            ->method('getAssociationNames')
325
            ->will($this->returnValue(array()));
326
327
        $this
328
            ->metadata
329
            ->expects($this->any())
330
            ->method('getFieldNames')
331
            ->will($this->returnValue(array('id', 'field')));
332
333
        $this
334
            ->metadata
335
            ->expects($this->any())
336
            ->method('getTypeOfField')
337
            ->with($this->logicalOr($this->equalTo('id'), $this->equalTo('field')))
338
            ->will($this->returnValue('string'));
339
340
        $this
341
            ->metadata
342
            ->expects($this->any())
343
            ->method('hasAssociation')
344
            ->will($this->returnValue(false));
345
346
        $this
347
            ->metadata
348
            ->expects($this->any())
349
            ->method('getIdentifierFieldNames')
350
            ->will($this->returnValue(array('id')));
351
352
        $this
353
            ->metadata
354
            ->expects($this->any())
355
            ->method('getReflectionClass')
356
            ->will($this->returnValue($refl));
357
358
        $this->hydratorByValue     = new DoctrineObjectHydrator(
359
            $this->objectManager,
360
            true
361
        );
362
        $this->hydratorByReference = new DoctrineObjectHydrator(
363
            $this->objectManager,
364
            false
365
        );
366
    }
367
368
    public function configureObjectManagerForSimpleEntityWithDateTime()
369
    {
370
        $refl = new ReflectionClass('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntityWithDateTime');
371
372
        $this
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...
373
            ->metadata
374
            ->expects($this->any())
375
            ->method('getAssociationNames')
376
            ->will($this->returnValue(array()));
377
378
        $this
379
            ->metadata
380
            ->expects($this->any())
381
            ->method('getFieldNames')
382
            ->will($this->returnValue(array('id', 'date')));
383
384
        $this
385
            ->metadata
386
            ->expects($this->any())
387
            ->method('getTypeOfField')
388
            ->with($this->logicalOr($this->equalTo('id'), $this->equalTo('date')))
389
            ->will(
390
                $this->returnCallback(
391
                    function ($arg) {
392
                        if ($arg === 'id') {
393
                            return 'integer';
394
                        } elseif ($arg === 'date') {
395
                            return 'datetime';
396
                        }
397
398
                        throw new \InvalidArgumentException();
399
                    }
400
                )
401
            );
402
403
        $this
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...
404
            ->metadata
405
            ->expects($this->any())
406
            ->method('hasAssociation')
407
            ->will($this->returnValue(false));
408
409
        $this
410
            ->metadata
411
            ->expects($this->any())
412
            ->method('getIdentifierFieldNames')
413
            ->will($this->returnValue(array('id')));
414
415
        $this
416
            ->metadata
417
            ->expects($this->any())
418
            ->method('getReflectionClass')
419
            ->will($this->returnValue($refl));
420
421
        $this->hydratorByValue     = new DoctrineObjectHydrator(
422
            $this->objectManager,
423
            true
424
        );
425
        $this->hydratorByReference = new DoctrineObjectHydrator(
426
            $this->objectManager,
427
            false
428
        );
429
    }
430
431
    public function configureObjectManagerForOneToOneEntity()
432
    {
433
        $refl = new ReflectionClass('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToOneEntity');
434
435
        $this
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...
436
            ->metadata
437
            ->expects($this->any())
438
            ->method('getFieldNames')
439
            ->will($this->returnValue(array('id')));
440
441
        $this
442
            ->metadata
443
            ->expects($this->any())
444
            ->method('getAssociationNames')
445
            ->will($this->returnValue(array('toOne')));
446
447
        $this
448
            ->metadata
449
            ->expects($this->any())
450
            ->method('getTypeOfField')
451
            ->with($this->logicalOr($this->equalTo('id'), $this->equalTo('toOne')))
452
            ->will(
453
                $this->returnCallback(
454
                    function ($arg) {
455
                        if ($arg === 'id') {
456
                            return 'integer';
457
                        } elseif ($arg === 'toOne') {
458
                            return 'DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity';
459
                        }
460
461
                        throw new \InvalidArgumentException();
462
                    }
463
                )
464
            );
465
466
        $this
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...
467
            ->metadata
468
            ->expects($this->any())
469
            ->method('hasAssociation')
470
            ->with($this->logicalOr($this->equalTo('id'), $this->equalTo('toOne')))
471
            ->will(
472
                $this->returnCallback(
473
                    function ($arg) {
474
                        if ($arg === 'id') {
475
                            return false;
476
                        } elseif ($arg === 'toOne') {
477
                            return true;
478
                        }
479
480
                        throw new \InvalidArgumentException();
481
                    }
482
                )
483
            );
484
485
        $this
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...
486
            ->metadata
487
            ->expects($this->any())
488
            ->method('isSingleValuedAssociation')
489
            ->with('toOne')
490
            ->will($this->returnValue(true));
491
492
        $this
493
            ->metadata
494
            ->expects($this->any())
495
            ->method('getAssociationTargetClass')
496
            ->with('toOne')
497
            ->will($this->returnValue('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity'));
498
499
        $this
500
            ->metadata
501
            ->expects($this->any())
502
            ->method('getReflectionClass')
503
            ->will($this->returnValue($refl));
504
505
        $this
506
            ->metadata
507
            ->expects($this->any())
508
            ->method('getIdentifier')
509
            ->will($this->returnValue(array("id")));
510
511
        $this->hydratorByValue     = new DoctrineObjectHydrator(
512
            $this->objectManager,
513
            true
514
        );
515
        $this->hydratorByReference = new DoctrineObjectHydrator(
516
            $this->objectManager,
517
            false
518
        );
519
    }
520
521
    public function configureObjectManagerForOneToOneEntityNotNullable()
522
    {
523
        $refl = new ReflectionClass('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToOneEntityNotNullable');
524
525
        $this
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...
526
            ->metadata
527
            ->expects($this->any())
528
            ->method('getFieldNames')
529
            ->will($this->returnValue(array('id')));
530
531
        $this
532
            ->metadata
533
            ->expects($this->any())
534
            ->method('getAssociationNames')
535
            ->will($this->returnValue(array('toOne')));
536
537
        $this
538
            ->metadata
539
            ->expects($this->any())
540
            ->method('getTypeOfField')
541
            ->with(
542
                $this->logicalOr(
543
                    $this->equalTo('id'),
544
                    $this->equalTo('toOne'),
545
                    $this->equalTo('field')
546
                )
547
            )
548
            ->will(
549
                $this->returnCallback(
550
                    function ($arg) {
551
                        if ($arg === 'id') {
552
                            return 'integer';
553
                        } elseif ($arg === 'toOne') {
554
                            return 'DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity';
555
                        } elseif ($arg === 'field') {
556
                            return 'string';
557
                        }
558
559
                        throw new \InvalidArgumentException();
560
                    }
561
                )
562
            );
563
564
        $this
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...
565
            ->metadata
566
            ->expects($this->any())
567
            ->method('hasAssociation')
568
            ->with(
569
                $this->logicalOr(
570
                    $this->equalTo('id'),
571
                    $this->equalTo('toOne'),
572
                    $this->equalTo('field')
573
                )
574
            )
575
            ->will(
576
                $this->returnCallback(
577
                    function ($arg) {
578
                        if ($arg === 'id' || $arg === 'field') {
579
                            return false;
580
                        } elseif ($arg === 'toOne') {
581
                            return true;
582
                        }
583
584
                        throw new \InvalidArgumentException();
585
                    }
586
                )
587
            );
588
589
        $this
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...
590
            ->metadata
591
            ->expects($this->any())
592
            ->method('isSingleValuedAssociation')
593
            ->with('toOne')
594
            ->will($this->returnValue(true));
595
596
        $this
597
            ->metadata
598
            ->expects($this->any())
599
            ->method('getAssociationTargetClass')
600
            ->with('toOne')
601
            ->will($this->returnValue('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity'));
602
603
        $this
604
            ->metadata
605
            ->expects($this->any())
606
            ->method('getReflectionClass')
607
            ->will($this->returnValue($refl));
608
609
        $this
610
            ->metadata
611
            ->expects($this->any())
612
            ->method('getIdentifier')
613
            ->will($this->returnValue(array("id")));
614
615
        $this->hydratorByValue     = new DoctrineObjectHydrator(
616
            $this->objectManager,
617
            true
618
        );
619
        $this->hydratorByReference = new DoctrineObjectHydrator(
620
            $this->objectManager,
621
            false
622
        );
623
    }
624
625
    public function configureObjectManagerForOneToManyEntity()
626
    {
627
        $refl = new ReflectionClass('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntity');
628
629
        $this
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...
630
            ->metadata
631
            ->expects($this->any())
632
            ->method('getFieldNames')
633
            ->will($this->returnValue(array('id')));
634
635
        $this
636
            ->metadata
637
            ->expects($this->any())
638
            ->method('getAssociationNames')
639
            ->will($this->returnValue(array('entities')));
640
641
        $this
642
            ->metadata
643
            ->expects($this->any())
644
            ->method('getTypeOfField')
645
            ->with(
646
                $this->logicalOr(
647
                    $this->equalTo('id'),
648
                    $this->equalTo('entities'),
649
                    $this->equalTo('field')
650
                )
651
            )
652
            ->will(
653
                $this->returnCallback(
654
                    function ($arg) {
655
                        if ($arg === 'id') {
656
                            return 'integer';
657
                        } elseif ($arg === 'field') {
658
                            return 'string';
659
                        } elseif ($arg === 'entities') {
660
                            return 'Doctrine\Common\Collections\ArrayCollection';
661
                        }
662
663
                        throw new \InvalidArgumentException();
664
                    }
665
                )
666
            );
667
668
        $this
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...
669
            ->metadata
670
            ->expects($this->any())
671
            ->method('hasAssociation')
672
            ->with($this->logicalOr($this->equalTo('id'), $this->equalTo('entities'), $this->equalTo('field')))
673
            ->will(
674
                $this->returnCallback(
675
                    function ($arg) {
676
                        if ($arg === 'id') {
677
                            return false;
678
                        } elseif ($arg === 'field') {
679
                            return false;
680
                        } elseif ($arg === 'entities') {
681
                            return true;
682
                        }
683
684
                        throw new \InvalidArgumentException();
685
                    }
686
                )
687
            );
688
689
        $this
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...
690
            ->metadata
691
            ->expects($this->any())
692
            ->method('isSingleValuedAssociation')
693
            ->with('entities')
694
            ->will($this->returnValue(false));
695
696
        $this
697
            ->metadata
698
            ->expects($this->any())
699
            ->method('isCollectionValuedAssociation')
700
            ->with('entities')
701
            ->will($this->returnValue(true));
702
703
        $this
704
            ->metadata
705
            ->expects($this->any())
706
            ->method('getAssociationTargetClass')
707
            ->with('entities')
708
            ->will($this->returnValue('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity'));
709
710
        $this
711
            ->metadata
712
            ->expects($this->any())
713
            ->method('getReflectionClass')
714
            ->will($this->returnValue($refl));
715
716
        $this->metadata
717
            ->expects($this->any())
718
            ->method('getIdentifier')
719
            ->will($this->returnValue(array("id")));
720
721
        $this->hydratorByValue     = new DoctrineObjectHydrator(
722
            $this->objectManager,
723
            true
724
        );
725
        $this->hydratorByReference = new DoctrineObjectHydrator(
726
            $this->objectManager,
727
            false
728
        );
729
    }
730
731
    public function configureObjectManagerForOneToManyArrayEntity()
732
    {
733
        $refl = new ReflectionClass('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyArrayEntity');
734
735
        $this
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...
736
            ->metadata
737
            ->expects($this->any())
738
            ->method('getFieldNames')
739
            ->will($this->returnValue(array('id')));
740
741
        $this
742
            ->metadata
743
            ->expects($this->any())
744
            ->method('getAssociationNames')
745
            ->will($this->returnValue(array('entities')));
746
747
        $this
748
            ->metadata
749
            ->expects($this->any())
750
            ->method('getTypeOfField')
751
            ->with(
752
                $this->logicalOr(
753
                    $this->equalTo('id'),
754
                    $this->equalTo('entities'),
755
                    $this->equalTo('field')
756
                )
757
            )
758
            ->will(
759
                $this->returnCallback(
760
                    function ($arg) {
761
                        if ($arg === 'id') {
762
                            return 'integer';
763
                        } elseif ($arg === 'field') {
764
                            return 'string';
765
                        } elseif ($arg === 'entities') {
766
                            return 'Doctrine\Common\Collections\ArrayCollection';
767
                        }
768
769
                        throw new \InvalidArgumentException();
770
                    }
771
                )
772
            );
773
774
        $this
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...
775
            ->metadata
776
            ->expects($this->any())
777
            ->method('hasAssociation')
778
            ->with($this->logicalOr($this->equalTo('id'), $this->equalTo('entities')))
779
            ->will(
780
                $this->returnCallback(
781
                    function ($arg) {
782
                        if ($arg === 'id') {
783
                            return false;
784
                        } elseif ($arg === 'field') {
785
                            return 'string';
786
                        } elseif ($arg === 'entities') {
787
                            return true;
788
                        }
789
790
                        throw new \InvalidArgumentException();
791
                    }
792
                )
793
            );
794
795
        $this
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...
796
            ->metadata
797
            ->expects($this->any())
798
            ->method('isSingleValuedAssociation')
799
            ->with('entities')
800
            ->will($this->returnValue(false));
801
802
        $this
803
            ->metadata
804
            ->expects($this->any())
805
            ->method('isCollectionValuedAssociation')
806
            ->with('entities')
807
            ->will($this->returnValue(true));
808
809
        $this
810
            ->metadata
811
            ->expects($this->any())
812
            ->method('getAssociationTargetClass')
813
            ->with('entities')
814
            ->will($this->returnValue('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity'));
815
816
        $this
817
            ->metadata
818
            ->expects($this->any())
819
            ->method('getReflectionClass')
820
            ->will($this->returnValue($refl));
821
822
        $this->metadata
823
            ->expects($this->any())
824
            ->method('getIdentifier')
825
            ->will($this->returnValue(array("id")));
826
827
        $this->hydratorByValue     = new DoctrineObjectHydrator(
828
            $this->objectManager,
829
            true
830
        );
831
        $this->hydratorByReference = new DoctrineObjectHydrator(
832
            $this->objectManager,
833
            false
834
        );
835
    }
836
837
    public function testObjectIsPassedForContextToStrategies()
838
    {
839
        $entity = new Asset\ContextEntity();
840
        $entity->setId(2);
841
        $entity->setField('foo', false);
0 ignored issues
show
Unused Code introduced by
The call to ContextEntity::setField() has too many arguments starting with false.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
842
843
        $this->configureObjectManagerForSimpleEntity();
844
845
        $hydrator = $this->hydratorByValue;
846
        $entity   = $hydrator->hydrate(array('id' => 3, 'field' => 'bar'), $entity);
847
        $this->assertEquals(array('id' => 3, 'field' => 'bar'), $hydrator->extract($entity));
848
849
        $hydrator->addStrategy('id', new ContextStrategy());
850
        $entity = $hydrator->hydrate(array('id' => 3, 'field' => 'bar'), $entity);
851
        $this->assertEquals(array('id' => '3barbar', 'field' => 'bar'), $hydrator->extract($entity));
852
    }
853
854
    public function testCanExtractSimpleEntityByValue()
855
    {
856
        // When using extraction by value, it will use the public API of the entity to retrieve values (getters)
857
        $entity = new Asset\SimpleEntity();
858
        $entity->setId(2);
859
        $entity->setField('foo', false);
860
861
        $this->configureObjectManagerForSimpleEntity();
862
863
        $data = $this->hydratorByValue->extract($entity);
864
        $this->assertEquals(array('id' => 2, 'field' => 'From getter: foo'), $data);
865
    }
866
867
    public function testCanExtractSimpleEntityByReference()
868
    {
869
        // When using extraction by reference, it won't use the public API of entity (getters won't be called)
870
        $entity = new Asset\SimpleEntity();
871
        $entity->setId(2);
872
        $entity->setField('foo', false);
873
874
        $this->configureObjectManagerForSimpleEntity();
875
876
        $data = $this->hydratorByReference->extract($entity);
877
        $this->assertEquals(array('id' => 2, 'field' => 'foo'), $data);
878
    }
879
880
    public function testCanHydrateSimpleEntityByValue()
881
    {
882
        // When using hydration by value, it will use the public API of the entity to set values (setters)
883
        $entity = new Asset\SimpleEntity();
884
        $this->configureObjectManagerForSimpleEntity();
885
        $data = array('field' => 'foo');
886
887
        $entity = $this->hydratorByValue->hydrate($data, $entity);
888
889
        $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $entity);
890
        $this->assertEquals('From setter: foo', $entity->getField(false));
891
    }
892
893
    /**
894
     * When using hydration by value, it will use the public API of the entity to set values (setters)
895
     *
896
     * @covers \DoctrineModule\Stdlib\Hydrator\DoctrineObject::hydrateByValue
897
     */
898
    public function testCanHydrateSimpleEntityWithStringIdByValue()
899
    {
900
        $entity = new Asset\SimpleEntity();
901
        $data   = array('id' => 'bar', 'field' => 'foo');
902
903
        $this->configureObjectManagerForSimpleEntityWithStringId();
904
905
        $entity = $this->hydratorByValue->hydrate($data, $entity);
906
907
        $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $entity);
908
        $this->assertEquals('From setter: foo', $entity->getField(false));
909
    }
910
911
    public function testCanHydrateSimpleEntityByReference()
912
    {
913
        // When using hydration by reference, it won't use the public API of the entity to set values (setters)
914
        $entity = new Asset\SimpleEntity();
915
        $this->configureObjectManagerForSimpleEntity();
916
        $data = array('field' => 'foo');
917
918
        $entity = $this->hydratorByReference->hydrate($data, $entity);
919
920
        $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $entity);
921
        $this->assertEquals('foo', $entity->getField(false));
922
    }
923
924
    /**
925
     * When using hydration by reference, it won't use the public API of the entity to set values (getters)
926
     *
927
     * @covers \DoctrineModule\Stdlib\Hydrator\DoctrineObject::hydrateByReference
928
     */
929
    public function testCanHydrateSimpleEntityWithStringIdByReference()
930
    {
931
        $entity = new Asset\SimpleEntity();
932
        $data   = array('id' => 'bar', 'field' => 'foo');
933
934
        $this->configureObjectManagerForSimpleEntityWithStringId();
935
936
        $entity = $this->hydratorByReference->hydrate($data, $entity);
937
938
        $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $entity);
939
        $this->assertEquals('foo', $entity->getField(false));
940
    }
941
942
    public function testReuseExistingEntityIfDataArrayContainsIdentifier()
943
    {
944
        // When using hydration by reference, it won't use the public API of the entity to set values (setters)
945
        $entity = new Asset\SimpleEntity();
946
947
        $this->configureObjectManagerForSimpleEntity();
948
        $data = array('id' => 1);
949
950
        $entityInDatabaseWithIdOfOne = new Asset\SimpleEntity();
951
        $entityInDatabaseWithIdOfOne->setId(1);
952
        $entityInDatabaseWithIdOfOne->setField('bar', false);
953
954
        $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\ObjectManager.

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...
955
            ->objectManager
956
            ->expects($this->once())
957
            ->method('find')
958
            ->with('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', array('id' => 1))
959
            ->will($this->returnValue($entityInDatabaseWithIdOfOne));
960
961
        $entity = $this->hydratorByValue->hydrate($data, $entity);
962
963
        $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $entity);
964
        $this->assertEquals('bar', $entity->getField(false));
965
    }
966
967
    /**
968
     * Test for https://github.com/doctrine/DoctrineModule/issues/456
969
     */
970
    public function testReuseExistingEntityIfDataArrayContainsIdentifierWithZeroIdentifier()
971
    {
972
        // When using hydration by reference, it won't use the public API of the entity to set values (setters)
973
        $entity = new Asset\SimpleEntity();
974
975
        $this->configureObjectManagerForSimpleEntity();
976
        $data = array('id' => 0);
977
978
        $entityInDatabaseWithIdOfOne = new Asset\SimpleEntity();
979
        $entityInDatabaseWithIdOfOne->setId(0);
980
        $entityInDatabaseWithIdOfOne->setField('bar', false);
981
982
        $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\ObjectManager.

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...
983
            ->objectManager
984
            ->expects($this->once())
985
            ->method('find')
986
            ->with('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', array('id' => 0))
987
            ->will($this->returnValue($entityInDatabaseWithIdOfOne));
988
989
        $entity = $this->hydratorByValue->hydrate($data, $entity);
990
991
        $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $entity);
992
        $this->assertEquals('bar', $entity->getField(false));
993
    }
994
995
    public function testExtractOneToOneAssociationByValue()
996
    {
997
        // When using extraction by value, it will use the public API of the entity to retrieve values (getters)
998
        $toOne = new Asset\SimpleEntity();
999
        $toOne->setId(2);
1000
        $toOne->setField('foo', false);
1001
1002
        $entity = new Asset\OneToOneEntity();
1003
        $entity->setId(2);
1004
        $entity->setToOne($toOne);
1005
1006
        $this->configureObjectManagerForOneToOneEntity();
1007
1008
        $data = $this->hydratorByValue->extract($entity);
1009
1010
        $this->assertEquals(2, $data['id']);
1011
        $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $data['toOne']);
1012
        $this->assertEquals('Modified from getToOne getter', $data['toOne']->getField(false));
1013
        $this->assertSame($toOne, $data['toOne']);
1014
    }
1015
1016
    public function testExtractOneToOneAssociationByReference()
1017
    {
1018
        // When using extraction by value, it will use the public API of the entity to retrieve values (getters)
1019
        $toOne = new Asset\SimpleEntity();
1020
        $toOne->setId(2);
1021
        $toOne->setField('foo', false);
1022
1023
        $entity = new Asset\OneToOneEntity();
1024
        $entity->setId(2);
1025
        $entity->setToOne($toOne, false);
1026
1027
        $this->configureObjectManagerForOneToOneEntity();
1028
1029
        $data = $this->hydratorByReference->extract($entity);
1030
1031
        $this->assertEquals(2, $data['id']);
1032
        $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $data['toOne']);
1033
        $this->assertEquals('foo', $data['toOne']->getField(false));
1034
        $this->assertSame($toOne, $data['toOne']);
1035
    }
1036
1037
    public function testHydrateOneToOneAssociationByValue()
1038
    {
1039
        // When using hydration by value, it will use the public API of the entity to set values (setters)
1040
        $toOne = new Asset\SimpleEntity();
1041
        $toOne->setId(2);
1042
        $toOne->setField('foo', false);
1043
1044
        $entity = new Asset\OneToOneEntity();
1045
        $this->configureObjectManagerForOneToOneEntity();
1046
1047
        $data = array('toOne' => $toOne);
1048
1049
        $entity = $this->hydratorByValue->hydrate($data, $entity);
1050
1051
        $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToOneEntity', $entity);
1052
        $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $entity->getToOne(false));
1053
        $this->assertEquals('Modified from setToOne setter', $entity->getToOne(false)->getField(false));
1054
    }
1055
1056
    public function testHydrateOneToOneAssociationByReference()
1057
    {
1058
        // When using hydration by value, it will use the public API of the entity to set values (setters)
1059
        $toOne = new Asset\SimpleEntity();
1060
        $toOne->setId(2);
1061
        $toOne->setField('foo', false);
1062
1063
        $entity = new Asset\OneToOneEntity();
1064
        $this->configureObjectManagerForOneToOneEntity();
1065
1066
        $data = array('toOne' => $toOne);
1067
1068
        $entity = $this->hydratorByReference->hydrate($data, $entity);
1069
1070
        $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToOneEntity', $entity);
1071
        $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $entity->getToOne(false));
1072
        $this->assertEquals('foo', $entity->getToOne(false)->getField(false));
1073
    }
1074
1075
    public function testHydrateOneToOneAssociationByValueUsingIdentifierForRelation()
1076
    {
1077
        // When using hydration by value, it will use the public API of the entity to set values (setters)
1078
        $entity = new Asset\OneToOneEntity();
1079
        $this->configureObjectManagerForOneToOneEntity();
1080
1081
        // Use entity of id 1 as relation
1082
        $data = array('toOne' => 1);
1083
1084
        $entityInDatabaseWithIdOfOne = new Asset\SimpleEntity();
1085
        $entityInDatabaseWithIdOfOne->setId(1);
1086
        $entityInDatabaseWithIdOfOne->setField('bar', false);
1087
1088
        $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\ObjectManager.

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...
1089
            ->objectManager
1090
            ->expects($this->once())
1091
            ->method('find')
1092
            ->with('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', 1)
1093
            ->will($this->returnValue($entityInDatabaseWithIdOfOne));
1094
1095
        $entity = $this->hydratorByValue->hydrate($data, $entity);
1096
1097
        $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToOneEntity', $entity);
1098
        $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $entity->getToOne(false));
1099
        $this->assertSame($entityInDatabaseWithIdOfOne, $entity->getToOne(false));
1100
    }
1101
1102
    public function testHydrateOneToOneAssociationByReferenceUsingIdentifierForRelation()
1103
    {
1104
        // When using hydration by reference, it won't use the public API of the entity to set values (setters)
1105
        $entity = new Asset\OneToOneEntity();
1106
        $this->configureObjectManagerForOneToOneEntity();
1107
1108
        // Use entity of id 1 as relation
1109
        $data = array('toOne' => 1);
1110
1111
        $entityInDatabaseWithIdOfOne = new Asset\SimpleEntity();
1112
        $entityInDatabaseWithIdOfOne->setId(1);
1113
        $entityInDatabaseWithIdOfOne->setField('bar', false);
1114
1115
        $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\ObjectManager.

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...
1116
            ->objectManager
1117
            ->expects($this->once())
1118
            ->method('find')
1119
            ->with('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', 1)
1120
            ->will($this->returnValue($entityInDatabaseWithIdOfOne));
1121
1122
        $entity = $this->hydratorByReference->hydrate($data, $entity);
1123
1124
        $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToOneEntity', $entity);
1125
        $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $entity->getToOne(false));
1126
        $this->assertSame($entityInDatabaseWithIdOfOne, $entity->getToOne(false));
1127
    }
1128
1129
    public function testHydrateOneToOneAssociationByValueUsingIdentifierArrayForRelation()
1130
    {
1131
        // When using hydration by value, it will use the public API of the entity to set values (setters)
1132
        $entity = new Asset\OneToOneEntity();
1133
        $this->configureObjectManagerForOneToOneEntity();
1134
1135
        // Use entity of id 1 as relation
1136
        $data = array('toOne' => array('id' => 1));
1137
1138
        $entityInDatabaseWithIdOfOne = new Asset\SimpleEntity();
1139
        $entityInDatabaseWithIdOfOne->setId(1);
1140
        $entityInDatabaseWithIdOfOne->setField('bar', false);
1141
1142
        $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\ObjectManager.

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...
1143
            ->objectManager
1144
            ->expects($this->once())
1145
            ->method('find')
1146
            ->with('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', array('id' => 1))
1147
            ->will($this->returnValue($entityInDatabaseWithIdOfOne));
1148
1149
        $entity = $this->hydratorByValue->hydrate($data, $entity);
1150
1151
        $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToOneEntity', $entity);
1152
        $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $entity->getToOne(false));
1153
        $this->assertSame($entityInDatabaseWithIdOfOne, $entity->getToOne(false));
1154
    }
1155
1156
    public function testHydrateOneToOneAssociationByValueUsingFullArrayForRelation()
1157
    {
1158
        $entity = new Asset\OneToOneEntityNotNullable;
1159
        $this->configureObjectManagerForOneToOneEntityNotNullable();
1160
1161
        // Use entity of id 1 as relation
1162
        $data = array('toOne' => array('id' => 1, 'field' => 'foo'));
1163
1164
        $entityInDatabaseWithIdOfOne = new Asset\SimpleEntity();
1165
        $entityInDatabaseWithIdOfOne->setId(1);
1166
        $entityInDatabaseWithIdOfOne->setField('bar', false);
1167
1168
        $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\ObjectManager.

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...
1169
            ->objectManager
1170
            ->expects($this->once())
1171
            ->method('find')
1172
            ->with(
1173
                'DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity',
1174
                array('id' => 1)
1175
            )
1176
            ->will($this->returnValue($entityInDatabaseWithIdOfOne));
1177
1178
        $entity = $this->hydratorByValue->hydrate($data, $entity);
1179
1180
        $this->assertInstanceOf(
1181
            'DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToOneEntityNotNullable',
1182
            $entity
1183
        );
1184
        $this->assertInstanceOf(
1185
            'DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity',
1186
            $entity->getToOne(false)
1187
        );
1188
        $this->assertSame($entityInDatabaseWithIdOfOne, $entity->getToOne(false));
1189
        $this->assertEquals(
1190
            'From getter: Modified from setToOne setter',
1191
            $entityInDatabaseWithIdOfOne->getField()
1192
        );
1193
    }
1194
1195
    public function testHydrateOneToOneAssociationByReferenceUsingIdentifierArrayForRelation()
1196
    {
1197
        // When using hydration by reference, it won't use the public API of the entity to set values (setters)
1198
        $entity = new Asset\OneToOneEntity();
1199
        $this->configureObjectManagerForOneToOneEntity();
1200
1201
        // Use entity of id 1 as relation
1202
        $data = array('toOne' => array('id' => 1));
1203
1204
        $entityInDatabaseWithIdOfOne = new Asset\SimpleEntity();
1205
        $entityInDatabaseWithIdOfOne->setId(1);
1206
        $entityInDatabaseWithIdOfOne->setField('bar', false);
1207
1208
        $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\ObjectManager.

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...
1209
            ->objectManager
1210
            ->expects($this->once())
1211
            ->method('find')
1212
            ->with('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', array('id' => 1))
1213
            ->will($this->returnValue($entityInDatabaseWithIdOfOne));
1214
1215
        $entity = $this->hydratorByReference->hydrate($data, $entity);
1216
1217
        $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToOneEntity', $entity);
1218
        $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $entity->getToOne(false));
1219
        $this->assertSame($entityInDatabaseWithIdOfOne, $entity->getToOne(false));
1220
    }
1221
1222
    public function testCanHydrateOneToOneAssociationByValueWithNullableRelation()
1223
    {
1224
        // When using hydration by value, it will use the public API of the entity to retrieve values (setters)
1225
        $entity = new Asset\OneToOneEntity();
1226
        $this->configureObjectManagerForOneToOneEntity();
1227
1228
        $data = array('toOne' => null);
1229
1230
        $this->metadata->expects($this->once())
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...
1231
                       ->method('hasAssociation');
1232
1233
        $object = $this->hydratorByValue->hydrate($data, $entity);
1234
        $this->assertNull($object->getToOne(false));
1235
    }
1236
1237
    public function testCanHydrateOneToOneAssociationByReferenceWithNullableRelation()
1238
    {
1239
        // When using hydration by reference, it won't use the public API of the entity to retrieve values (setters)
1240
        $entity = new Asset\OneToOneEntity();
1241
1242
        $this->configureObjectManagerForOneToOneEntity();
1243
        $this->objectManager->expects($this->never())->method('find');
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\Common\Persistence\ObjectManager.

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...
1244
        $this->metadata->expects($this->once())->method('hasAssociation');
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...
1245
1246
        $data = array('toOne' => null);
1247
1248
        $object = $this->hydratorByReference->hydrate($data, $entity);
1249
        $this->assertNull($object->getToOne(false));
1250
    }
1251
1252
    public function testExtractOneToManyAssociationByValue()
1253
    {
1254
        // When using extraction by value, it will use the public API of the entity to retrieve values (getters)
1255
        $toMany1 = new Asset\SimpleEntity();
1256
        $toMany1->setId(2);
1257
        $toMany1->setField('foo', false);
1258
1259
        $toMany2 = new Asset\SimpleEntity();
1260
        $toMany2->setId(3);
1261
        $toMany2->setField('bar', false);
1262
1263
        $collection = new ArrayCollection(array($toMany1, $toMany2));
1264
1265
        $entity = new Asset\OneToManyEntity();
1266
        $entity->setId(4);
1267
        $entity->addEntities($collection);
1268
1269
        $this->configureObjectManagerForOneToManyEntity();
1270
1271
        $data = $this->hydratorByValue->extract($entity);
1272
1273
        $this->assertEquals(4, $data['id']);
1274
        $this->assertInstanceOf('Doctrine\Common\Collections\Collection', $data['entities']);
1275
1276
        $this->assertEquals($toMany1->getId(), $data['entities'][0]->getId());
1277
        $this->assertSame($toMany1, $data['entities'][0]);
1278
        $this->assertEquals($toMany2->getId(), $data['entities'][1]->getId());
1279
        $this->assertSame($toMany2, $data['entities'][1]);
1280
    }
1281
1282
    /**
1283
     * @depends testExtractOneToManyAssociationByValue
1284
     */
1285
    public function testExtractOneToManyByValueWithArray()
1286
    {
1287
        // When using extraction by value, it will use the public API of the entity to retrieve values (getters)
1288
        $toMany1 = new Asset\SimpleEntity();
1289
        $toMany1->setId(2);
1290
        $toMany1->setField('foo', false);
1291
1292
        $toMany2 = new Asset\SimpleEntity();
1293
        $toMany2->setId(3);
1294
        $toMany2->setField('bar', false);
1295
1296
        $collection = new ArrayCollection(array($toMany1, $toMany2));
1297
1298
        $entity = new Asset\OneToManyArrayEntity();
1299
        $entity->setId(4);
1300
        $entity->addEntities($collection);
1301
1302
        $this->configureObjectManagerForOneToManyArrayEntity();
1303
1304
        $data = $this->hydratorByValue->extract($entity);
1305
1306
        $this->assertEquals(4, $data['id']);
1307
        $this->assertInternalType('array', $data['entities']);
1308
1309
        $this->assertEquals($toMany1->getId(), $data['entities'][0]->getId());
1310
        $this->assertSame($toMany1, $data['entities'][0]);
1311
        $this->assertEquals($toMany2->getId(), $data['entities'][1]->getId());
1312
        $this->assertSame($toMany2, $data['entities'][1]);
1313
    }
1314
1315
    public function testExtractOneToManyAssociationByReference()
1316
    {
1317
        // When using extraction by reference, it won't use the public API of the entity to retrieve values (getters)
1318
        $toMany1 = new Asset\SimpleEntity();
1319
        $toMany1->setId(2);
1320
        $toMany1->setField('foo', false);
1321
1322
        $toMany2 = new Asset\SimpleEntity();
1323
        $toMany2->setId(3);
1324
        $toMany2->setField('bar', false);
1325
1326
        $collection = new ArrayCollection(array($toMany1, $toMany2));
1327
1328
        $entity = new Asset\OneToManyEntity();
1329
        $entity->setId(4);
1330
        $entity->addEntities($collection);
1331
1332
        $this->configureObjectManagerForOneToManyEntity();
1333
1334
        $data = $this->hydratorByReference->extract($entity);
1335
1336
        $this->assertEquals(4, $data['id']);
1337
        $this->assertInstanceOf('Doctrine\Common\Collections\Collection', $data['entities']);
1338
1339
        $this->assertEquals($toMany1->getId(), $data['entities'][0]->getId());
1340
        $this->assertSame($toMany1, $data['entities'][0]);
1341
        $this->assertEquals($toMany2->getId(), $data['entities'][1]->getId());
1342
        $this->assertSame($toMany2, $data['entities'][1]);
1343
    }
1344
1345
    /**
1346
     * @depends testExtractOneToManyAssociationByReference
1347
     */
1348
    public function testExtractOneToManyArrayByReference()
1349
    {
1350
        // When using extraction by reference, it won't use the public API of the entity to retrieve values (getters)
1351
        $toMany1 = new Asset\SimpleEntity();
1352
        $toMany1->setId(2);
1353
        $toMany1->setField('foo', false);
1354
1355
        $toMany2 = new Asset\SimpleEntity();
1356
        $toMany2->setId(3);
1357
        $toMany2->setField('bar', false);
1358
1359
        $collection = new ArrayCollection(array($toMany1, $toMany2));
1360
1361
        $entity = new Asset\OneToManyArrayEntity();
1362
        $entity->setId(4);
1363
        $entity->addEntities($collection);
1364
1365
        $this->configureObjectManagerForOneToManyArrayEntity();
1366
1367
        $data = $this->hydratorByReference->extract($entity);
1368
1369
        $this->assertEquals(4, $data['id']);
1370
        $this->assertInstanceOf('Doctrine\Common\Collections\Collection', $data['entities']);
1371
1372
        $this->assertEquals($toMany1->getId(), $data['entities'][0]->getId());
1373
        $this->assertSame($toMany1, $data['entities'][0]);
1374
        $this->assertEquals($toMany2->getId(), $data['entities'][1]->getId());
1375
        $this->assertSame($toMany2, $data['entities'][1]);
1376
    }
1377
1378
    public function testHydrateOneToManyAssociationByValue()
1379
    {
1380
        // When using hydration by value, it will use the public API of the entity to set values (setters)
1381
        $toMany1 = new Asset\SimpleEntity();
1382
        $toMany1->setId(2);
1383
        $toMany1->setField('foo', false);
1384
1385
        $toMany2 = new Asset\SimpleEntity();
1386
        $toMany2->setId(3);
1387
        $toMany2->setField('bar', false);
1388
1389
        $entity = new Asset\OneToManyEntity();
1390
        $this->configureObjectManagerForOneToManyEntity();
1391
1392
        $data = array(
1393
            'entities' => array(
1394
                $toMany1, $toMany2
1395
            )
1396
        );
1397
1398
        $entity = $this->hydratorByValue->hydrate($data, $entity);
1399
1400
        $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntity', $entity);
1401
1402
        $entities = $entity->getEntities(false);
1403
1404
        foreach ($entities as $en) {
1405
            $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $en);
1406
            $this->assertInternalType('integer', $en->getId());
1407
            $this->assertContains('Modified from addEntities adder', $en->getField(false));
1408
        }
1409
1410
        $this->assertEquals(2, $entities[0]->getId());
1411
        $this->assertSame($toMany1, $entities[0]);
1412
1413
        $this->assertEquals(3, $entities[1]->getId());
1414
        $this->assertSame($toMany2, $entities[1]);
1415
    }
1416
1417
    /**
1418
     * @depends testHydrateOneToManyAssociationByValue
1419
     */
1420
    public function testHydrateOneToManyArrayByValue()
1421
    {
1422
        // When using hydration by value, it will use the public API of the entity to set values (setters)
1423
        $toMany1 = new Asset\SimpleEntity();
1424
        $toMany1->setId(2);
1425
        $toMany1->setField('foo', false);
1426
1427
        $toMany2 = new Asset\SimpleEntity();
1428
        $toMany2->setId(3);
1429
        $toMany2->setField('bar', false);
1430
1431
        $entity = new Asset\OneToManyArrayEntity();
1432
        $this->configureObjectManagerForOneToManyArrayEntity();
1433
1434
        $data = array(
1435
            'entities' => array(
1436
                $toMany1, $toMany2
1437
            )
1438
        );
1439
1440
        $entity = $this->hydratorByValue->hydrate($data, $entity);
1441
1442
        $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyArrayEntity', $entity);
1443
1444
        $entities = $entity->getEntities(false);
1445
1446
        foreach ($entities as $en) {
1447
            $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $en);
1448
            $this->assertInternalType('integer', $en->getId());
1449
            $this->assertContains('Modified from addEntities adder', $en->getField(false));
1450
        }
1451
1452
        $this->assertEquals(2, $entities[0]->getId());
1453
        $this->assertSame($toMany1, $entities[0]);
1454
1455
        $this->assertEquals(3, $entities[1]->getId());
1456
        $this->assertSame($toMany2, $entities[1]);
1457
    }
1458
1459
    public function testHydrateOneToManyAssociationByReference()
1460
    {
1461
        // When using hydration by value, it will use the public API of the entity to set values (setters)
1462
        $toMany1 = new Asset\SimpleEntity();
1463
        $toMany1->setId(2);
1464
        $toMany1->setField('foo', false);
1465
1466
        $toMany2 = new Asset\SimpleEntity();
1467
        $toMany2->setId(3);
1468
        $toMany2->setField('bar', false);
1469
1470
        $entity = new Asset\OneToManyEntity();
1471
        $this->configureObjectManagerForOneToManyEntity();
1472
1473
        $data = array(
1474
            'entities' => array(
1475
                $toMany1, $toMany2
1476
            )
1477
        );
1478
1479
        $entity = $this->hydratorByReference->hydrate($data, $entity);
1480
1481
        $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntity', $entity);
1482
1483
        $entities = $entity->getEntities(false);
1484
1485
        foreach ($entities as $en) {
1486
            $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $en);
1487
            $this->assertInternalType('integer', $en->getId());
1488
            $this->assertNotContains('Modified from addEntities adder', $en->getField(false));
1489
        }
1490
1491
        $this->assertEquals(2, $entities[0]->getId());
1492
        $this->assertSame($toMany1, $entities[0]);
1493
1494
        $this->assertEquals(3, $entities[1]->getId());
1495
        $this->assertSame($toMany2, $entities[1]);
1496
    }
1497
1498
    /**
1499
     * @depends testHydrateOneToManyAssociationByReference
1500
     */
1501
    public function testHydrateOneToManyArrayByReference()
1502
    {
1503
        // When using hydration by value, it will use the public API of the entity to set values (setters)
1504
        $toMany1 = new Asset\SimpleEntity();
1505
        $toMany1->setId(2);
1506
        $toMany1->setField('foo', false);
1507
1508
        $toMany2 = new Asset\SimpleEntity();
1509
        $toMany2->setId(3);
1510
        $toMany2->setField('bar', false);
1511
1512
        $entity = new Asset\OneToManyArrayEntity();
1513
        $this->configureObjectManagerForOneToManyArrayEntity();
1514
1515
        $data = array(
1516
            'entities' => array(
1517
                $toMany1, $toMany2
1518
            )
1519
        );
1520
1521
        $entity = $this->hydratorByReference->hydrate($data, $entity);
1522
1523
        $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyArrayEntity', $entity);
1524
1525
        $entities = $entity->getEntities(false);
1526
1527
        foreach ($entities as $en) {
1528
            $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $en);
1529
            $this->assertInternalType('integer', $en->getId());
1530
            $this->assertNotContains('Modified from addEntities adder', $en->getField(false));
1531
        }
1532
1533
        $this->assertEquals(2, $entities[0]->getId());
1534
        $this->assertSame($toMany1, $entities[0]);
1535
1536
        $this->assertEquals(3, $entities[1]->getId());
1537
        $this->assertSame($toMany2, $entities[1]);
1538
    }
1539
1540
    public function testHydrateOneToManyAssociationByValueUsingIdentifiersForRelations()
1541
    {
1542
        // When using hydration by value, it will use the public API of the entity to set values (setters)
1543
        $entity = new Asset\OneToManyEntity();
1544
        $this->configureObjectManagerForOneToManyEntity();
1545
1546
        $data = array(
1547
            'entities' => array(
1548
                2, 3
1549
            )
1550
        );
1551
1552
        $entityInDatabaseWithIdOfTwo = new Asset\SimpleEntity();
1553
        $entityInDatabaseWithIdOfTwo->setId(2);
1554
        $entityInDatabaseWithIdOfTwo->setField('foo', false);
1555
1556
        $entityInDatabaseWithIdOfThree = new Asset\SimpleEntity();
1557
        $entityInDatabaseWithIdOfThree->setId(3);
1558
        $entityInDatabaseWithIdOfThree->setField('bar', false);
1559
1560
        $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\ObjectManager.

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...
1561
            ->objectManager
1562
            ->expects($this->exactly(2))
1563
            ->method('find')
1564
            ->with(
1565
                'DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity',
1566
                $this->logicalOr($this->equalTo(array('id' => 2)), $this->equalTo(array('id' => 3)))
1567
            )
1568
            ->will(
1569
                $this->returnCallback(
1570
                    function ($target, $arg) use ($entityInDatabaseWithIdOfTwo, $entityInDatabaseWithIdOfThree) {
1571
                        if ($arg['id'] === 2) {
1572
                            return $entityInDatabaseWithIdOfTwo;
1573
                        } elseif ($arg['id'] === 3) {
1574
                            return $entityInDatabaseWithIdOfThree;
1575
                        }
1576
1577
                        throw new \InvalidArgumentException();
1578
                    }
1579
                )
1580
            );
1581
1582
        $entity = $this->hydratorByValue->hydrate($data, $entity);
1583
1584
        $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntity', $entity);
1585
1586
        /* @var $entity \DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntity */
1587
        $entities = $entity->getEntities(false);
1588
1589
        foreach ($entities as $en) {
1590
            $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $en);
1591
            $this->assertInternalType('integer', $en->getId());
1592
            $this->assertContains('Modified from addEntities adder', $en->getField(false));
1593
        }
1594
1595
        $this->assertEquals(2, $entities[0]->getId());
1596
        $this->assertSame($entityInDatabaseWithIdOfTwo, $entities[0]);
1597
1598
        $this->assertEquals(3, $entities[1]->getId());
1599
        $this->assertSame($entityInDatabaseWithIdOfThree, $entities[1]);
1600
    }
1601
1602
    public function testHydrateOneToManyAssociationByValueUsingIdentifiersArrayForRelations()
1603
    {
1604
        // When using hydration by value, it will use the public API of the entity to set values (setters)
1605
        $entity = new Asset\OneToManyEntity();
1606
        $this->configureObjectManagerForOneToManyEntity();
1607
1608
        $data = array(
1609
            'entities' => array(
1610
                array('id' => 2),
1611
                array('id' => 3)
1612
            )
1613
        );
1614
1615
        $entityInDatabaseWithIdOfTwo = new Asset\SimpleEntity();
1616
        $entityInDatabaseWithIdOfTwo->setId(2);
1617
        $entityInDatabaseWithIdOfTwo->setField('foo', false);
1618
1619
        $entityInDatabaseWithIdOfThree = new Asset\SimpleEntity();
1620
        $entityInDatabaseWithIdOfThree->setId(3);
1621
        $entityInDatabaseWithIdOfThree->setField('bar', false);
1622
1623
        $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\ObjectManager.

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...
1624
            ->objectManager
1625
            ->expects($this->exactly(2))
1626
            ->method('find')
1627
            ->with(
1628
                'DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity',
1629
                $this->logicalOr($this->equalTo(array('id' => 2)), $this->equalTo(array('id' => 3)))
1630
            )
1631
            ->will(
1632
                $this->returnCallback(
1633
                    function ($target, $arg) use ($entityInDatabaseWithIdOfTwo, $entityInDatabaseWithIdOfThree) {
1634
                        if ($arg['id'] === 2) {
1635
                            return $entityInDatabaseWithIdOfTwo;
1636
                        } elseif ($arg['id'] === 3) {
1637
                            return $entityInDatabaseWithIdOfThree;
1638
                        }
1639
1640
                        throw new \InvalidArgumentException();
1641
                    }
1642
                )
1643
            );
1644
1645
        $entity = $this->hydratorByValue->hydrate($data, $entity);
1646
1647
        $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntity', $entity);
1648
1649
        /* @var $entity \DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntity */
1650
        $entities = $entity->getEntities(false);
1651
1652
        foreach ($entities as $en) {
1653
            $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $en);
1654
            $this->assertInternalType('integer', $en->getId());
1655
            $this->assertContains('Modified from addEntities adder', $en->getField(false));
1656
        }
1657
1658
        $this->assertEquals(2, $entities[0]->getId());
1659
        $this->assertSame($entityInDatabaseWithIdOfTwo, $entities[0]);
1660
1661
        $this->assertEquals(3, $entities[1]->getId());
1662
        $this->assertSame($entityInDatabaseWithIdOfThree, $entities[1]);
1663
    }
1664
1665
    public function testHydrateOneToManyAssociationByReferenceUsingIdentifiersArrayForRelations()
1666
    {
1667
1668
        // When using hydration by value, it will use the public API of the entity to set values (setters)
1669
        $entity = new Asset\OneToManyEntity();
1670
        $this->configureObjectManagerForOneToManyEntity();
1671
1672
        $data = array(
1673
            'entities' => array(
1674
                array('id' => 2),
1675
                array('id' => 3)
1676
            )
1677
        );
1678
1679
        $entityInDatabaseWithIdOfTwo = new Asset\SimpleEntity();
1680
        $entityInDatabaseWithIdOfTwo->setId(2);
1681
        $entityInDatabaseWithIdOfTwo->setField('foo', false);
1682
1683
        $entityInDatabaseWithIdOfThree = new Asset\SimpleEntity();
1684
        $entityInDatabaseWithIdOfThree->setId(3);
1685
        $entityInDatabaseWithIdOfThree->setField('bar', false);
1686
1687
        $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\ObjectManager.

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...
1688
            ->objectManager
1689
            ->expects($this->exactly(2))
1690
            ->method('find')
1691
            ->with(
1692
                'DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity',
1693
                $this->logicalOr($this->equalTo(array('id' => 2)), $this->equalTo(array('id' => 3)))
1694
            )
1695
            ->will(
1696
                $this->returnCallback(
1697
                    function ($target, $arg) use ($entityInDatabaseWithIdOfTwo, $entityInDatabaseWithIdOfThree) {
1698
                        if ($arg['id'] === 2) {
1699
                            return $entityInDatabaseWithIdOfTwo;
1700
                        } elseif ($arg['id'] === 3) {
1701
                            return $entityInDatabaseWithIdOfThree;
1702
                        }
1703
1704
                        throw new \InvalidArgumentException();
1705
                    }
1706
                )
1707
            );
1708
1709
        $entity = $this->hydratorByReference->hydrate($data, $entity);
1710
1711
        $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntity', $entity);
1712
1713
        $entities = $entity->getEntities(false);
1714
1715
        foreach ($entities as $en) {
1716
            $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $en);
1717
            $this->assertInternalType('integer', $en->getId());
1718
            $this->assertNotContains('Modified from addEntities adder', $en->getField(false));
1719
        }
1720
1721
        $this->assertEquals(2, $entities[0]->getId());
1722
        $this->assertSame($entityInDatabaseWithIdOfTwo, $entities[0]);
1723
1724
        $this->assertEquals(3, $entities[1]->getId());
1725
        $this->assertSame($entityInDatabaseWithIdOfThree, $entities[1]);
1726
    }
1727
1728
    public function testHydrateOneToManyAssociationByReferenceUsingIdentifiersForRelations()
1729
    {
1730
        // When using hydration by reference, it won't use the public API of the entity to set values (setters)
1731
        $entity = new Asset\OneToManyEntity();
1732
        $this->configureObjectManagerForOneToManyEntity();
1733
1734
        $data = array(
1735
            'entities' => array(
1736
                2, 3
1737
            )
1738
        );
1739
1740
        $entityInDatabaseWithIdOfTwo = new Asset\SimpleEntity();
1741
        $entityInDatabaseWithIdOfTwo->setId(2);
1742
        $entityInDatabaseWithIdOfTwo->setField('foo', false);
1743
1744
        $entityInDatabaseWithIdOfThree = new Asset\SimpleEntity();
1745
        $entityInDatabaseWithIdOfThree->setId(3);
1746
        $entityInDatabaseWithIdOfThree->setField('bar', false);
1747
1748
        $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\ObjectManager.

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...
1749
            ->objectManager
1750
            ->expects($this->any())
1751
            ->method('find')
1752
            ->with(
1753
                'DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity',
1754
                $this->logicalOr($this->equalTo(array('id' => 2)), $this->equalTo(array('id' => 3)))
1755
            )
1756
            ->will(
1757
                $this->returnCallback(
1758
                    function ($target, $arg) use ($entityInDatabaseWithIdOfTwo, $entityInDatabaseWithIdOfThree) {
1759
                        if ($arg['id'] === 2) {
1760
                            return $entityInDatabaseWithIdOfTwo;
1761
                        } elseif ($arg['id'] === 3) {
1762
                            return $entityInDatabaseWithIdOfThree;
1763
                        }
1764
1765
                        throw new \InvalidArgumentException();
1766
                    }
1767
                )
1768
            );
1769
1770
        $entity = $this->hydratorByReference->hydrate($data, $entity);
1771
1772
        $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntity', $entity);
1773
1774
        $entities = $entity->getEntities(false);
1775
1776
        foreach ($entities as $en) {
1777
            $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $en);
1778
            $this->assertInternalType('integer', $en->getId());
1779
            $this->assertNotContains('Modified from addEntities adder', $en->getField(false));
1780
        }
1781
1782
        $this->assertEquals(2, $entities[0]->getId());
1783
        $this->assertSame($entityInDatabaseWithIdOfTwo, $entities[0]);
1784
1785
        $this->assertEquals(3, $entities[1]->getId());
1786
        $this->assertSame($entityInDatabaseWithIdOfThree, $entities[1]);
1787
    }
1788
1789
    public function testHydrateOneToManyAssociationByValueUsingDisallowRemoveStrategy()
1790
    {
1791
        // When using hydration by value, it will use the public API of the entity to set values (setters)
1792
        $toMany1 = new Asset\SimpleEntity();
1793
        $toMany1->setId(2);
1794
        $toMany1->setField('foo', false);
1795
1796
        $toMany2 = new Asset\SimpleEntity();
1797
        $toMany2->setId(3);
1798
        $toMany2->setField('bar', false);
1799
1800
        $toMany3 = new Asset\SimpleEntity();
1801
        $toMany3->setId(8);
1802
        $toMany3->setField('baz', false);
1803
1804
        $entity = new Asset\OneToManyEntity();
1805
        $this->configureObjectManagerForOneToManyEntity();
1806
1807
        // Initially add two elements
1808
        $entity->addEntities(new ArrayCollection(array($toMany1, $toMany2)));
1809
1810
        // The hydrated collection contains two other elements, one of them is new, and one of them is missing
1811
        // in the new strategy
1812
        $data = array(
1813
            'entities' => array(
1814
                $toMany2, $toMany3
1815
            )
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->assertEquals(3, count($entities));
1826
1827
        foreach ($entities as $en) {
1828
            $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $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\SimpleEntity();
1846
        $toMany1->setId(2);
1847
        $toMany1->setField('foo', false);
1848
1849
        $toMany2 = new Asset\SimpleEntity();
1850
        $toMany2->setId(3);
1851
        $toMany2->setField('bar', false);
1852
1853
        $toMany3 = new Asset\SimpleEntity();
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(array($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 = array(
1866
            'entities' => array(
1867
                $toMany2, $toMany3
1868
            )
1869
        );
1870
1871
        // Use a DisallowRemove strategy
1872
        $this->hydratorByReference->addStrategy('entities', new Strategy\DisallowRemoveByReference());
1873
        $entity = $this->hydratorByReference->hydrate($data, $entity);
1874
1875
        $entities = $entity->getEntities(false);
1876
1877
        // DisallowStrategy should not remove existing entities in Collection even if it's not in the new collection
1878
        $this->assertEquals(3, count($entities));
1879
1880
        foreach ($entities as $en) {
1881
            $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $en);
1882
            $this->assertInternalType('integer', $en->getId());
1883
1884
            // Only the third element is new so the adder has not been called on it
1885
            if ($en === $toMany3) {
1886
                $this->assertNotContains('Modified from addEntities adder', $en->getField(false));
1887
            }
1888
        }
1889
1890
        $this->assertEquals(2, $entities[0]->getId());
1891
        $this->assertSame($toMany1, $entities[0]);
1892
1893
        $this->assertEquals(3, $entities[1]->getId());
1894
        $this->assertSame($toMany2, $entities[1]);
1895
1896
        $this->assertEquals(8, $entities[2]->getId());
1897
        $this->assertSame($toMany3, $entities[2]);
1898
    }
1899
1900
    public function testHydrateOneToManyAssociationByValueWithArrayCausingDataModifications()
1901
    {
1902
        // When using hydration by value, it will use the public API of the entity to set values (setters)
1903
        $data = array(
1904
            'entities' => array(
1905
                array('id' => 2, 'field' => 'Modified By Hydrate'),
1906
                array('id' => 3, 'field' => 'Modified By Hydrate')
1907
            )
1908
        );
1909
1910
        $entityInDatabaseWithIdOfTwo = new Asset\SimpleEntity();
1911
        $entityInDatabaseWithIdOfTwo->setId(2);
1912
        $entityInDatabaseWithIdOfTwo->setField('foo', false);
1913
1914
        $entityInDatabaseWithIdOfThree = new Asset\SimpleEntity();
1915
        $entityInDatabaseWithIdOfThree->setId(3);
1916
        $entityInDatabaseWithIdOfThree->setField('bar', false);
1917
1918
        $entity = new Asset\OneToManyEntityWithEntities(
1919
            new ArrayCollection(array(
1920
                $entityInDatabaseWithIdOfTwo,
1921
                $entityInDatabaseWithIdOfThree
1922
            ))
1923
        );
1924
        $this->configureObjectManagerForOneToManyEntity();
1925
1926
        $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\ObjectManager.

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...
1927
            ->objectManager
1928
            ->expects($this->exactly(2))
1929
            ->method('find')
1930
            ->with(
1931
                'DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity',
1932
                $this->logicalOr($this->equalTo(array('id' => 2)), $this->equalTo(array('id' => 3)))
1933
            )
1934
            ->will(
1935
                $this->returnCallback(
1936
                    function ($target, $arg) use ($entityInDatabaseWithIdOfTwo, $entityInDatabaseWithIdOfThree) {
1937
                        if ($arg['id'] === 2) {
1938
                            return $entityInDatabaseWithIdOfTwo;
1939
                        } elseif ($arg['id'] === 3) {
1940
                            return $entityInDatabaseWithIdOfThree;
1941
                        }
1942
1943
                        throw new \InvalidArgumentException();
1944
                    }
1945
                )
1946
            );
1947
1948
        $entity = $this->hydratorByValue->hydrate($data, $entity);
1949
1950
        $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntityWithEntities', $entity);
1951
1952
        /* @var $entity \DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntity */
1953
        $entities = $entity->getEntities(false);
1954
1955
        foreach ($entities as $en) {
1956
            $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $en);
1957
            $this->assertInternalType('integer', $en->getId());
1958
            $this->assertInternalType('string', $en->getField());
1959
            $this->assertContains('Modified By Hydrate', $en->getField(false));
1960
        }
1961
1962
        $this->assertEquals(2, $entities[0]->getId());
1963
        $this->assertSame($entityInDatabaseWithIdOfTwo, $entities[0]);
1964
1965
        $this->assertEquals(3, $entities[1]->getId());
1966
        $this->assertSame($entityInDatabaseWithIdOfThree, $entities[1]);
1967
    }
1968
1969
1970
    public function testHydrateOneToManyAssociationByValueWithTraversableCausingDataModifications()
1971
    {
1972
        // When using hydration by value, it will use the public API of the entity to set values (setters)
1973
        $data = array(
1974
            'entities' => new ArrayCollection(
1975
                array(
1976
                    array('id' => 2, 'field' => 'Modified By Hydrate'),
1977
                    array('id' => 3, 'field' => 'Modified By Hydrate')
1978
                )
1979
            )
1980
        );
1981
1982
        $entityInDatabaseWithIdOfTwo = new Asset\SimpleEntity();
1983
        $entityInDatabaseWithIdOfTwo->setId(2);
1984
        $entityInDatabaseWithIdOfTwo->setField('foo', false);
1985
1986
        $entityInDatabaseWithIdOfThree = new Asset\SimpleEntity();
1987
        $entityInDatabaseWithIdOfThree->setId(3);
1988
        $entityInDatabaseWithIdOfThree->setField('bar', false);
1989
1990
        $entity = new Asset\OneToManyEntityWithEntities(
1991
            new ArrayCollection(array(
1992
                $entityInDatabaseWithIdOfTwo,
1993
                $entityInDatabaseWithIdOfThree
1994
            ))
1995
        );
1996
        $this->configureObjectManagerForOneToManyEntity();
1997
1998
        $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\ObjectManager.

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...
1999
            ->objectManager
2000
            ->expects($this->exactly(2))
2001
            ->method('find')
2002
            ->with(
2003
                'DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity',
2004
                $this->logicalOr($this->equalTo(array('id' => 2)), $this->equalTo(array('id' => 3)))
2005
            )
2006
            ->will(
2007
                $this->returnCallback(
2008
                    function ($target, $arg) use ($entityInDatabaseWithIdOfTwo, $entityInDatabaseWithIdOfThree) {
2009
                        if ($arg['id'] === 2) {
2010
                            return $entityInDatabaseWithIdOfTwo;
2011
                        } elseif ($arg['id'] === 3) {
2012
                            return $entityInDatabaseWithIdOfThree;
2013
                        }
2014
2015
                        throw new \InvalidArgumentException();
2016
                    }
2017
                )
2018
            );
2019
2020
        $entity = $this->hydratorByValue->hydrate($data, $entity);
2021
2022
        $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntityWithEntities', $entity);
2023
2024
        /* @var $entity \DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntity */
2025
        $entities = $entity->getEntities(false);
2026
2027
        foreach ($entities as $en) {
2028
            $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $en);
2029
            $this->assertInternalType('integer', $en->getId());
2030
            $this->assertInternalType('string', $en->getField());
2031
            $this->assertContains('Modified By Hydrate', $en->getField(false));
2032
        }
2033
2034
        $this->assertEquals(2, $entities[0]->getId());
2035
        $this->assertSame($entityInDatabaseWithIdOfTwo, $entities[0]);
2036
2037
        $this->assertEquals(3, $entities[1]->getId());
2038
        $this->assertSame($entityInDatabaseWithIdOfThree, $entities[1]);
2039
    }
2040
2041
    public function testHydrateOneToManyAssociationByValueWithStdClass()
2042
    {
2043
        // When using hydration by value, it will use the public API of the entity to set values (setters)
2044
        $stdClass1     = new \StdClass();
2045
        $stdClass1->id = 2;
2046
2047
        $stdClass2     = new \StdClass();
2048
        $stdClass2->id = 3;
2049
2050
        $data = array('entities' => array($stdClass1, $stdClass2));
2051
2052
        $entityInDatabaseWithIdOfTwo = new Asset\SimpleEntity();
2053
        $entityInDatabaseWithIdOfTwo->setId(2);
2054
        $entityInDatabaseWithIdOfTwo->setField('foo', false);
2055
2056
        $entityInDatabaseWithIdOfThree = new Asset\SimpleEntity();
2057
        $entityInDatabaseWithIdOfThree->setId(3);
2058
        $entityInDatabaseWithIdOfThree->setField('bar', false);
2059
2060
        $entity = new Asset\OneToManyEntityWithEntities(
2061
            new ArrayCollection(array(
2062
                $entityInDatabaseWithIdOfTwo,
2063
                $entityInDatabaseWithIdOfThree
2064
            ))
2065
        );
2066
        $this->configureObjectManagerForOneToManyEntity();
2067
2068
        $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\ObjectManager.

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...
2069
            ->objectManager
2070
            ->expects($this->exactly(2))
2071
            ->method('find')
2072
            ->with(
2073
                'DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity',
2074
                $this->logicalOr($this->equalTo(array('id' => 2)), $this->equalTo(array('id' => 3)))
2075
            )
2076
            ->will(
2077
                $this->returnCallback(
2078
                    function ($target, $arg) use ($entityInDatabaseWithIdOfTwo, $entityInDatabaseWithIdOfThree) {
2079
                        if ($arg['id'] === 2) {
2080
                            return $entityInDatabaseWithIdOfTwo;
2081
                        } elseif ($arg['id'] === 3) {
2082
                            return $entityInDatabaseWithIdOfThree;
2083
                        }
2084
2085
                        throw new \InvalidArgumentException();
2086
                    }
2087
                )
2088
            );
2089
2090
        $entity = $this->hydratorByValue->hydrate($data, $entity);
2091
2092
        $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntityWithEntities', $entity);
2093
2094
        /* @var $entity \DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntity */
2095
        $entities = $entity->getEntities(false);
2096
2097
        foreach ($entities as $en) {
2098
            $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $en);
2099
            $this->assertInternalType('integer', $en->getId());
2100
        }
2101
2102
        $this->assertEquals(2, $entities[0]->getId());
2103
        $this->assertSame($entityInDatabaseWithIdOfTwo, $entities[0]);
2104
2105
        $this->assertEquals(3, $entities[1]->getId());
2106
        $this->assertSame($entityInDatabaseWithIdOfThree, $entities[1]);
2107
    }
2108
2109
    public function testHydrateOneToManyAssociationByReferenceWithArrayCausingDataModifications()
2110
    {
2111
        // When using hydration by value, it will use the public API of the entity to set values (setters)
2112
        $data = array(
2113
            'entities' => array(
2114
                array('id' => 2, 'field' => 'Modified By Hydrate'),
2115
                array('id' => 3, 'field' => 'Modified By Hydrate')
2116
            )
2117
        );
2118
2119
        $entityInDatabaseWithIdOfTwo = new Asset\SimpleEntity();
2120
        $entityInDatabaseWithIdOfTwo->setId(2);
2121
        $entityInDatabaseWithIdOfTwo->setField('Unmodified Value', false);
2122
2123
        $entityInDatabaseWithIdOfThree = new Asset\SimpleEntity();
2124
        $entityInDatabaseWithIdOfThree->setId(3);
2125
        $entityInDatabaseWithIdOfThree->setField('Unmodified Value', false);
2126
2127
        $entity = new Asset\OneToManyEntityWithEntities(
2128
            new ArrayCollection(array(
2129
                $entityInDatabaseWithIdOfTwo,
2130
                $entityInDatabaseWithIdOfThree
2131
            ))
2132
        );
2133
2134
        $reflSteps = array(
2135
            new ReflectionClass('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntityWithEntities'),
2136
            new ReflectionClass('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity'),
2137
            new ReflectionClass('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity'),
2138
            new ReflectionClass('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntityWithEntities'),
2139
        );
2140
        $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...
2141
            ->metadata
2142
            ->expects($this->any())
2143
            ->method('getReflectionClass')
2144
            ->will($this->returnCallback(
2145
                function () use (&$reflSteps) {
2146
                    $refl = array_shift($reflSteps);
2147
                    return $refl;
2148
                }
2149
            ));
2150
2151
        $this->configureObjectManagerForOneToManyEntity();
2152
2153
        $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\ObjectManager.

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...
2154
            ->objectManager
2155
            ->expects($this->exactly(2))
2156
            ->method('find')
2157
            ->with(
2158
                'DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity',
2159
                $this->logicalOr($this->equalTo(array('id' => 2)), $this->equalTo(array('id' => 3)))
2160
            )
2161
            ->will(
2162
                $this->returnCallback(
2163
                    function ($target, $arg) use ($entityInDatabaseWithIdOfTwo, $entityInDatabaseWithIdOfThree) {
2164
                        if ($arg['id'] === 2) {
2165
                            return $entityInDatabaseWithIdOfTwo;
2166
                        } elseif ($arg['id'] === 3) {
2167
                            return $entityInDatabaseWithIdOfThree;
2168
                        }
2169
2170
                        throw new \InvalidArgumentException();
2171
                    }
2172
                )
2173
            );
2174
2175
        $entity = $this->hydratorByReference->hydrate($data, $entity);
2176
2177
        $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntityWithEntities', $entity);
2178
2179
        /* @var $entity \DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntity */
2180
        $entities = $entity->getEntities(false);
2181
2182
        foreach ($entities as $en) {
2183
            $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $en);
2184
            $this->assertInternalType('integer', $en->getId());
2185
            $this->assertInternalType('string', $en->getField());
2186
            $this->assertContains('Modified By Hydrate', $en->getField(false));
2187
        }
2188
2189
        $this->assertEquals(2, $entities[0]->getId());
2190
        $this->assertSame($entityInDatabaseWithIdOfTwo, $entities[0]);
2191
2192
        $this->assertEquals(3, $entities[1]->getId());
2193
        $this->assertSame($entityInDatabaseWithIdOfThree, $entities[1]);
2194
    }
2195
2196
    public function testAssertCollectionsAreNotSwappedDuringHydration()
2197
    {
2198
        // When using hydration by value, it will use the public API of the entity to set values (setters)
2199
        $entity = new Asset\OneToManyEntity();
2200
        $this->configureObjectManagerForOneToManyEntity();
2201
2202
        $toMany1 = new Asset\SimpleEntity();
2203
        $toMany1->setId(2);
2204
        $toMany1->setField('foo', false);
2205
2206
        $toMany2 = new Asset\SimpleEntity();
2207
        $toMany2->setId(3);
2208
        $toMany2->setField('bar', false);
2209
2210
        $data = array(
2211
            'entities' => array(
2212
                $toMany1, $toMany2
2213
            )
2214
        );
2215
2216
        // Set the initial collection
2217
        $entity->addEntities(new ArrayCollection(array($toMany1, $toMany2)));
2218
        $initialCollection = $entity->getEntities(false);
2219
2220
        $entity = $this->hydratorByValue->hydrate($data, $entity);
2221
2222
        $modifiedCollection = $entity->getEntities(false);
2223
        $this->assertSame($initialCollection, $modifiedCollection);
2224
    }
2225
2226
    public function testAssertCollectionsAreNotSwappedDuringHydrationUsingIdentifiersForRelations()
2227
    {
2228
        // When using hydration by value, it will use the public API of the entity to set values (setters)
2229
        $entity = new Asset\OneToManyEntity();
2230
        $this->configureObjectManagerForOneToManyEntity();
2231
2232
        $data = array(
2233
            'entities' => array(
2234
                2, 3
2235
            )
2236
        );
2237
2238
        $entityInDatabaseWithIdOfTwo = new Asset\SimpleEntity();
2239
        $entityInDatabaseWithIdOfTwo->setId(2);
2240
        $entityInDatabaseWithIdOfTwo->setField('foo', false);
2241
2242
        $entityInDatabaseWithIdOfThree = new Asset\SimpleEntity();
2243
        $entityInDatabaseWithIdOfThree->setId(3);
2244
        $entityInDatabaseWithIdOfThree->setField('bar', false);
2245
2246
        // Set the initial collection
2247
        $entity->addEntities(new ArrayCollection(array($entityInDatabaseWithIdOfTwo, $entityInDatabaseWithIdOfThree)));
2248
        $initialCollection = $entity->getEntities(false);
2249
2250
        $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\ObjectManager.

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...
2251
            ->objectManager
2252
            ->expects($this->any())
2253
            ->method('find')
2254
            ->with(
2255
                'DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity',
2256
                $this->logicalOr($this->equalTo(array('id' =>2)), $this->equalTo(array('id' => 3)))
2257
            )
2258
            ->will(
2259
                $this->returnCallback(
2260
                    function ($target, $arg) use ($entityInDatabaseWithIdOfTwo, $entityInDatabaseWithIdOfThree) {
2261
                        if ($arg['id'] === 2) {
2262
                            return $entityInDatabaseWithIdOfTwo;
2263
                        }
2264
2265
                        return $entityInDatabaseWithIdOfThree;
2266
                    }
2267
                )
2268
            );
2269
2270
        $entity = $this->hydratorByValue->hydrate($data, $entity);
2271
2272
        $modifiedCollection = $entity->getEntities(false);
2273
        $this->assertSame($initialCollection, $modifiedCollection);
2274
    }
2275
2276
    public function testCanLookupsForEmptyIdentifiers()
2277
    {
2278
        // When using hydration by reference, it won't use the public API of the entity to set values (setters)
2279
        $entity = new Asset\OneToManyEntity();
2280
        $this->configureObjectManagerForOneToManyEntity();
2281
2282
        $data = array(
2283
            'entities' => array(
2284
                ''
2285
            )
2286
        );
2287
2288
        $entityInDatabaseWithEmptyId = new Asset\SimpleEntity();
2289
        $entityInDatabaseWithEmptyId->setId('');
2290
        $entityInDatabaseWithEmptyId->setField('baz', false);
2291
2292
        $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\ObjectManager.

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...
2293
            ->objectManager
2294
            ->expects($this->any())
2295
            ->method('find')
2296
            ->with('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', '')
2297
            ->will($this->returnValue($entityInDatabaseWithEmptyId));
2298
2299
        $entity = $this->hydratorByValue->hydrate($data, $entity);
2300
2301
        $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToManyEntity', $entity);
2302
2303
        $entities = $entity->getEntities(false);
2304
        $entity   = $entities[0];
2305
2306
        $this->assertEquals(1, count($entities));
2307
2308
        $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $entity);
2309
        $this->assertSame($entityInDatabaseWithEmptyId, $entity);
2310
    }
2311
2312
    public function testHandleDateTimeConversionUsingByValue()
2313
    {
2314
        // When using hydration by value, it will use the public API of the entity to set values (setters)
2315
        $entity = new Asset\SimpleEntityWithDateTime();
2316
        $this->configureObjectManagerForSimpleEntityWithDateTime();
2317
2318
        $now  = time();
2319
        $data = array('date' => $now);
2320
2321
        $entity = $this->hydratorByValue->hydrate($data, $entity);
2322
2323
        $this->assertInstanceOf('DateTime', $entity->getDate());
2324
        $this->assertEquals($now, $entity->getDate()->getTimestamp());
2325
    }
2326
2327
    public function testEmptyStringIsNotConvertedToDateTime()
2328
    {
2329
        $entity = new Asset\SimpleEntityWithDateTime();
2330
        $this->configureObjectManagerForSimpleEntityWithDateTime();
2331
2332
        $data = array('date' => '');
2333
2334
        $entity = $this->hydratorByValue->hydrate($data, $entity);
2335
2336
        $this->assertNull($entity->getDate());
2337
    }
2338
2339
    public function testAssertNullValueHydratedForOneToOneWithOptionalMethodSignature()
2340
    {
2341
        $entity = new Asset\OneToOneEntity();
2342
2343
        $this->configureObjectManagerForOneToOneEntity();
2344
        $this->objectManager->expects($this->never())->method('find');
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\Common\Persistence\ObjectManager.

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...
2345
2346
        $data = array('toOne' => null);
2347
2348
2349
        $object = $this->hydratorByValue->hydrate($data, $entity);
2350
        $this->assertNull($object->getToOne(false));
2351
    }
2352
2353
    public function testAssertNullValueNotUsedAsIdentifierForOneToOneWithNonOptionalMethodSignature()
2354
    {
2355
        $entity = new Asset\OneToOneEntityNotNullable();
2356
2357
        $entity->setToOne(new Asset\SimpleEntity());
2358
        $this->configureObjectManagerForOneToOneEntityNotNullable();
2359
        $this->objectManager->expects($this->never())->method('find');
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\Common\Persistence\ObjectManager.

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...
2360
2361
        $data = array('toOne' => null);
2362
2363
        $object = $this->hydratorByValue->hydrate($data, $entity);
2364
        $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $object->getToOne(false));
2365
    }
2366
2367
    public function testUsesStrategyOnSimpleFieldsWhenHydratingByValue()
2368
    {
2369
        // When using hydration by value, it will use the public API of the entity to set values (setters)
2370
        $entity = new Asset\SimpleEntity();
2371
        $this->configureObjectManagerForSimpleEntity();
2372
        $data = array('field' => 'foo');
2373
2374
        $this->hydratorByValue->addStrategy('field', new Asset\SimpleStrategy());
2375
        $entity = $this->hydratorByValue->hydrate($data, $entity);
2376
2377
        $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $entity);
2378
        $this->assertEquals('From setter: modified while hydrating', $entity->getField(false));
2379
    }
2380
2381
    public function testUsesStrategyOnSimpleFieldsWhenHydratingByReference()
2382
    {
2383
        // When using hydration by value, it will use the public API of the entity to set values (setters)
2384
        $entity = new Asset\SimpleEntity();
2385
        $this->configureObjectManagerForSimpleEntity();
2386
        $data = array('field' => 'foo');
2387
2388
        $this->hydratorByReference->addStrategy('field', new Asset\SimpleStrategy());
2389
        $entity = $this->hydratorByReference->hydrate($data, $entity);
2390
2391
        $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $entity);
2392
        $this->assertEquals('modified while hydrating', $entity->getField(false));
2393
    }
2394
2395
    public function testUsesStrategyOnSimpleFieldsWhenExtractingByValue()
2396
    {
2397
        $entity = new Asset\SimpleEntity();
2398
        $entity->setId(2);
2399
        $entity->setField('foo', false);
2400
2401
        $this->configureObjectManagerForSimpleEntity();
2402
2403
        $this->hydratorByValue->addStrategy('field', new Asset\SimpleStrategy());
2404
        $data = $this->hydratorByValue->extract($entity);
2405
        $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $entity);
2406
        $this->assertEquals(array('id' => 2, 'field' => 'modified while extracting'), $data);
2407
    }
2408
2409
    public function testUsesStrategyOnSimpleFieldsWhenExtractingByReference()
2410
    {
2411
        $entity = new Asset\SimpleEntity();
2412
        $entity->setId(2);
2413
        $entity->setField('foo', false);
2414
2415
        $this->configureObjectManagerForSimpleEntity();
2416
2417
        $this->hydratorByReference->addStrategy('field', new Asset\SimpleStrategy());
2418
        $data = $this->hydratorByReference->extract($entity);
2419
        $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity', $entity);
2420
        $this->assertEquals(array('id' => 2, 'field' => 'modified while extracting'), $data);
2421
    }
2422
2423
    public function testCanExtractIsserByValue()
2424
    {
2425
        $entity = new Asset\SimpleIsEntity();
2426
        $entity->setId(2);
2427
        $entity->setDone(true);
2428
2429
        $this->configureObjectManagerForSimpleIsEntity();
2430
2431
        $data = $this->hydratorByValue->extract($entity);
2432
        $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleIsEntity', $entity);
2433
        $this->assertEquals(array('id' => 2, 'done' => true), $data);
2434
    }
2435
2436
    public function testCanExtractIsserThatStartsWithIsByValue()
2437
    {
2438
        $entity = new Asset\SimpleEntityWithIsBoolean();
2439
        $entity->setId(2);
2440
        $entity->setIsActive(true);
2441
2442
        $this->configureObjectManagerForSimpleEntityWithIsBoolean();
2443
2444
        $data = $this->hydratorByValue->extract($entity);
2445
        $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntityWithIsBoolean', $entity);
2446
        $this->assertEquals(array('id' => 2, 'isActive' => true), $data);
2447
    }
2448
2449
    public function testExtractWithPropertyNameFilterByValue()
2450
    {
2451
        $entity = new Asset\SimpleEntity();
2452
        $entity->setId(2);
2453
        $entity->setField('foo', false);
2454
2455
        $filter = new Filter\PropertyName(array('id'), false);
2456
2457
        $this->configureObjectManagerForSimpleEntity();
2458
2459
        $this->hydratorByValue->addFilter('propertyname', $filter);
0 ignored issues
show
Documentation introduced by
$filter is of type object<DoctrineModule\St...or\Filter\PropertyName>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
2460
        $data = $this->hydratorByValue->extract($entity);
2461
2462
        $this->assertEquals(2, $data['id']);
2463
        $this->assertEquals(array('id'), array_keys($data), 'Only the "id" field should have been extracted.');
2464
    }
2465
2466
    public function testExtractWithPropertyNameFilterByReference()
2467
    {
2468
        $entity = new Asset\SimpleEntity();
2469
        $entity->setId(2);
2470
        $entity->setField('foo', false);
2471
2472
        $filter = new Filter\PropertyName(array('id'), false);
2473
2474
        $this->configureObjectManagerForSimpleEntity();
2475
2476
        $this->hydratorByReference->addFilter('propertyname', $filter);
0 ignored issues
show
Documentation introduced by
$filter is of type object<DoctrineModule\St...or\Filter\PropertyName>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
2477
        $data = $this->hydratorByReference->extract($entity);
2478
2479
        $this->assertEquals(2, $data['id']);
2480
        $this->assertEquals(array('id'), array_keys($data), 'Only the "id" field should have been extracted.');
2481
    }
2482
2483
    public function testExtractByReferenceUsesNamingStrategy()
2484
    {
2485
        $this->configureObjectManagerForNamingStrategyEntity();
2486
        $name = 'Foo';
2487
        $this->hydratorByReference->setNamingStrategy(new UnderscoreNamingStrategy());
2488
        $data = $this->hydratorByReference->extract(new NamingStrategyEntity($name));
2489
        $this->assertEquals($name, $data['camel_case']);
2490
    }
2491
2492
    public function testExtractByValueUsesNamingStrategy()
2493
    {
2494
        $this->configureObjectManagerForNamingStrategyEntity();
2495
        $name = 'Bar';
2496
        $this->hydratorByValue->setNamingStrategy(new UnderscoreNamingStrategy());
2497
        $data = $this->hydratorByValue->extract(new NamingStrategyEntity($name));
2498
        $this->assertEquals($name, $data['camel_case']);
2499
    }
2500
2501
    public function testHydrateByReferenceUsesNamingStrategy()
2502
    {
2503
        $this->configureObjectManagerForNamingStrategyEntity();
2504
        $name = 'Baz';
2505
        $this->hydratorByReference->setNamingStrategy(new UnderscoreNamingStrategy());
2506
        $entity = $this->hydratorByReference->hydrate(array('camel_case' => $name), new NamingStrategyEntity());
2507
        $this->assertEquals($name, $entity->getCamelCase());
2508
    }
2509
2510
    public function testHydrateByValueUsesNamingStrategy()
2511
    {
2512
        $this->configureObjectManagerForNamingStrategyEntity();
2513
        $name = 'Qux';
2514
        $this->hydratorByValue->setNamingStrategy(new UnderscoreNamingStrategy());
2515
        $entity = $this->hydratorByValue->hydrate(array('camel_case' => $name), new NamingStrategyEntity());
2516
        $this->assertEquals($name, $entity->getCamelCase());
2517
    }
2518
2519
    public function configureObjectManagerForSimplePrivateEntity()
2520
    {
2521
        $refl = new ReflectionClass('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimplePrivateEntity');
2522
2523
        $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...
2524
            ->metadata
2525
            ->expects($this->any())
2526
            ->method('getName')
2527
            ->will($this->returnValue('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimplePrivateEntity'));
2528
        $this
2529
            ->metadata
2530
            ->expects($this->any())
2531
            ->method('getAssociationNames')
2532
            ->will($this->returnValue(array()));
2533
2534
        $this
2535
            ->metadata
2536
            ->expects($this->any())
2537
            ->method('getFieldNames')
2538
            ->will($this->returnValue(array('private', 'protected')));
2539
2540
        $this
2541
            ->metadata
2542
            ->expects($this->any())
2543
            ->method('getTypeOfField')
2544
            ->with($this->logicalOr($this->equalTo('private'), $this->equalTo('protected')))
2545
            ->will($this->returnValue('integer'));
2546
2547
        $this
2548
            ->metadata
2549
            ->expects($this->any())
2550
            ->method('hasAssociation')
2551
            ->will($this->returnValue(false));
2552
2553
        $this
2554
            ->metadata
2555
            ->expects($this->any())
2556
            ->method('getIdentifierFieldNames')
2557
            ->will($this->returnValue(array('private')));
2558
2559
        $this
2560
            ->metadata
2561
            ->expects($this->any())
2562
            ->method('getReflectionClass')
2563
            ->will($this->returnValue($refl));
2564
2565
        $this->hydratorByValue     = new DoctrineObjectHydrator(
2566
            $this->objectManager,
2567
            true
2568
        );
2569
        $this->hydratorByReference = new DoctrineObjectHydrator(
2570
            $this->objectManager,
2571
            false
2572
        );
2573
    }
2574
2575
    public function testCannotHydratePrivateByValue()
2576
    {
2577
        $entity = new Asset\SimplePrivateEntity();
2578
        $this->configureObjectManagerForSimplePrivateEntity();
2579
        $data = array('private' => 123, 'protected' => 456);
2580
2581
        $this->hydratorByValue->hydrate($data, $entity);
2582
2583
        $this->assertInstanceOf('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimplePrivateEntity', $entity);
2584
    }
2585
}
2586