Failed Conditions
Pull Request — master (#6591)
by Luís
14:08
created

GH6589Test::testMoveTo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
namespace Doctrine\Tests\ORM\Functional\Ticket;
3
4
use Doctrine\Common\Collections\ArrayCollection;
5
use Doctrine\Tests\OrmFunctionalTestCase;
6
7
final class GH6589Test extends OrmFunctionalTestCase
8
{
9
    protected function setUp()
10
    {
11
        parent::setUp();
12
13
        $this->_schemaTool->createSchema(
14
            [
15
                $this->_em->getClassMetadata(GH6589Room::class),
16
                $this->_em->getClassMetadata(GH6589RoomType::class),
17
            ]
18
        );
19
20
        $roomType1 = new GH6589RoomType(1);
21
        $roomType2 = new GH6589RoomType(2);
22
        $room = new GH6589Room(1, $roomType1);
23
        $this->_em->persist($roomType1);
24
        $this->_em->persist($roomType2);
25
        $this->_em->persist($room);
26
        $this->_em->flush();
27
        $this->_em->clear();
28
    }
29
30
    public function testMoveTo()
31
    {
32
        /** @var GH6589RoomType $roomType2 */
33
        $roomType2 = $this->_em->find(GH6589RoomType::class, 2);
34
35
        /** @var GH6589Room $room */
36
        $room = $this->_em->find(GH6589Room::class, 1);
37
        $room->moveTo($roomType2);
38
39
        self::assertEquals(2, $room->roomType->id);
40
    }
41
}
42
43
/**
44
 * @Entity
45
 */
46
class GH6589Room
47
{
48
    /**
49
     * @var integer
50
     *
51
     * @Id
52
     * @Column(type="integer")
53
     */
54
    public $id;
55
56
    /**
57
     * @var GH6589RoomType
58
     *
59
     * @ManyToOne(targetEntity=GH6589RoomType::class, inversedBy="rooms")
60
     */
61
    public $roomType;
62
63
    public function __construct($id, GH6589RoomType $roomType)
64
    {
65
        $this->id = $id;
66
        $this->roomType = $roomType;
67
        $this->roomType->addRoom($this);
68
    }
69
70
    public function moveTo(GH6589RoomType $roomType)
71
    {
72
        $oldRoomType = $this->roomType;
73
        $this->roomType = $roomType;
74
        $roomType->addRoom($this);
75
        $oldRoomType->removeRoom($this);
76
    }
77
}
78
79
/**
80
 * @Entity
81
 */
82
class GH6589RoomType
83
{
84
    /**
85
     * @var integer
86
     *
87
     * @Id
88
     * @Column(type="integer")
89
     */
90
    public $id;
91
92
    /**
93
     * @var GH6589Room[]
94
     *
95
     * @OneToMany(targetEntity=GH6589Room::class, mappedBy="roomType")
96
     */
97
    public $rooms;
98
99
    public function __construct($id)
100
    {
101
        $this->id = $id;
102
        $this->rooms = new ArrayCollection();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Doctrine\Common\Collections\ArrayCollection() of type object<Doctrine\Common\C...ctions\ArrayCollection> is incompatible with the declared type array<integer,object<Doc...nal\Ticket\GH6589Room>> of property $rooms.

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...
103
    }
104
105
    public function addRoom(GH6589Room $room)
106
    {
107
        $this->rooms->add($room);
0 ignored issues
show
Bug introduced by
The method add cannot be called on $this->rooms (of type array<integer,object<Doc...nal\Ticket\GH6589Room>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
108
    }
109
110
    public function removeRoom(GH6589Room $room)
111
    {
112
        $this->rooms->removeElement($room);
0 ignored issues
show
Bug introduced by
The method removeElement cannot be called on $this->rooms (of type array<integer,object<Doc...nal\Ticket\GH6589Room>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
113
    }
114
}
115