Failed Conditions
Push — master ( a0c0d3...57a950 )
by Marco
22:07
created

AbstractHydratorTest::setUp()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 24
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\ORM\Functional\Ticket;
4
5
use Doctrine\Common\EventManager;
6
use Doctrine\DBAL\Connection;
7
use Doctrine\DBAL\Driver\Statement;
8
use Doctrine\ORM\EntityManagerInterface;
9
use Doctrine\ORM\Events;
10
use Doctrine\ORM\Internal\Hydration\AbstractHydrator;
11
use Doctrine\ORM\Query\ResultSetMapping;
12
use Doctrine\Tests\OrmFunctionalTestCase;
13
14
/**
15
 * @covers \Doctrine\ORM\Internal\Hydration\AbstractHydrator
16
 */
17
class AbstractHydratorTest extends OrmFunctionalTestCase
18
{
19
    /**
20
     * @var EventManager|\PHPUnit_Framework_MockObject_MockObject
21
     */
22
    private $mockEventManager;
23
24
    /**
25
     * @var Statement|\PHPUnit_Framework_MockObject_MockObject
26
     */
27
    private $mockStatement;
28
29
    /**
30
     * @var ResultSetMapping|\PHPUnit_Framework_MockObject_MockObject
31
     */
32
    private $mockResultMapping;
33
34
    /**
35
     * @var AbstractHydrator
36
     */
37
    private $hydrator;
38
39
    protected function setUp() : void
40
    {
41
        parent::setUp();
42
43
        $mockConnection             = $this->createMock(Connection::class);
44
        $mockEntityManagerInterface = $this->createMock(EntityManagerInterface::class);
45
        $this->mockEventManager     = $this->createMock(EventManager::class);
46
        $this->mockStatement        = $this->createMock(Statement::class);
47
        $this->mockResultMapping    = $this->getMockBuilder(ResultSetMapping::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockBuilder(\D...esultSetMapping::class) of type object<PHPUnit_Framework_MockObject_MockBuilder> is incompatible with the declared type object<Doctrine\ORM\Quer..._MockObject_MockObject> of property $mockResultMapping.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
48
49
        $mockEntityManagerInterface
50
            ->expects(self::any())
51
            ->method('getEventManager')
52
            ->willReturn($this->mockEventManager);
53
        $mockEntityManagerInterface
54
            ->expects(self::any())
55
            ->method('getConnection')
56
            ->willReturn($mockConnection);
57
        $this->mockStatement
58
            ->expects(self::any())
59
            ->method('fetch')
60
            ->willReturn(false);
61
62
        $this->hydrator = $this
63
            ->getMockBuilder(AbstractHydrator::class)
64
            ->setConstructorArgs([$mockEntityManagerInterface])
65
            ->setMethods(['hydrateAllData'])
66
            ->getMock();
67
    }
68
69
    /**
70
     * @group DDC-3146
71
     * @group #1515
72
     *
73
     * Verify that the number of added events to the event listener from the abstract hydrator class is equal to the
74
     * number of removed events
75
     */
76 View Code Duplication
    public function testOnClearEventListenerIsDetachedOnCleanup() : void
77
    {
78
        $this
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\Common\EventManager.

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...
79
            ->mockEventManager
80
            ->expects(self::at(0))
81
            ->method('addEventListener')
82
            ->with([Events::onClear], $this->hydrator);
83
84
        $this
85
            ->mockEventManager
86
            ->expects(self::at(1))
87
            ->method('removeEventListener')
88
            ->with([Events::onClear], $this->hydrator);
89
90
        iterator_to_array($this->hydrator->iterate($this->mockStatement, $this->mockResultMapping));
91
    }
92
93
    /**
94
     * @group #6623
95
     */
96 View Code Duplication
    public function testHydrateAllRegistersAndClearsAllAttachedListeners() : void
97
    {
98
        $this
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\Common\EventManager.

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...
99
            ->mockEventManager
100
            ->expects(self::at(0))
101
            ->method('addEventListener')
102
            ->with([Events::onClear], $this->hydrator);
103
104
        $this
105
            ->mockEventManager
106
            ->expects(self::at(1))
107
            ->method('removeEventListener')
108
            ->with([Events::onClear], $this->hydrator);
109
110
        $this->hydrator->hydrateAll($this->mockStatement, $this->mockResultMapping);
111
    }
112
}
113