Failed Conditions
Push — master ( 2b8acb...aa13e4 )
by Luís
14s
created

Tests/ORM/Functional/Ticket/DDC1690Test.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Doctrine\Tests\ORM\Functional\Ticket;
4
5
use Doctrine\Common\NotifyPropertyChanged;
6
use Doctrine\Common\PropertyChangedListener;
7
use Doctrine\ORM\Proxy\Proxy;
8
9
class DDC1690Test extends \Doctrine\Tests\OrmFunctionalTestCase
10
{
11
    protected function setUp() {
12
        parent::setUp();
13
        try {
14
            $this->_schemaTool->createSchema(
15
                [
16
                $this->_em->getClassMetadata(DDC1690Parent::class),
17
                $this->_em->getClassMetadata(DDC1690Child::class)
18
                ]
19
            );
20
        } catch (\Exception $e) {
21
            // Swallow all exceptions. We do not test the schema tool here.
22
        }
23
    }
24
25
    public function testChangeTracking()
26
    {
27
        $parent = new DDC1690Parent();
28
        $child = new DDC1690Child();
29
        $parent->setName('parent');
30
        $child->setName('child');
31
32
        $parent->setChild($child);
33
        $child->setParent($parent);
34
35
        $this->_em->persist($parent);
36
        $this->_em->persist($child);
37
38
        $this->assertEquals(1, count($parent->listeners));
39
        $this->assertEquals(1, count($child->listeners));
40
41
        $this->_em->flush();
42
        $this->_em->clear();
43
44
        $this->assertEquals(1, count($parent->listeners));
45
        $this->assertEquals(1, count($child->listeners));
46
47
        $parentId = $parent->getId();
48
        $childId = $child->getId();
49
        unset($parent, $child);
50
51
        $parent = $this->_em->find(DDC1690Parent::class, $parentId);
52
        $child = $this->_em->find(DDC1690Child::class, $childId);
53
54
        $this->assertEquals(1, count($parent->listeners));
55
        $this->assertInstanceOf(Proxy::class, $child, 'Verifying that $child is a proxy before using proxy API');
56
        $this->assertCount(0, $child->listeners);
57
        $child->__load();
58
        $this->assertCount(1, $child->listeners);
59
        unset($parent, $child);
60
61
        $parent = $this->_em->find(DDC1690Parent::class, $parentId);
62
        $child = $parent->getChild();
63
64
        $this->assertEquals(1, count($parent->listeners));
65
        $this->assertEquals(1, count($child->listeners));
66
        unset($parent, $child);
67
68
        $child = $this->_em->find(DDC1690Child::class, $childId);
69
        $parent = $child->getParent();
70
71
        $this->assertEquals(1, count($parent->listeners));
72
        $this->assertEquals(1, count($child->listeners));
73
    }
74
}
75
76 View Code Duplication
class NotifyBaseEntity implements NotifyPropertyChanged {
77
    public $listeners = [];
78
79
    public function addPropertyChangedListener(PropertyChangedListener $listener) {
80
        if (!in_array($listener, $this->listeners)) {
81
            $this->listeners[] = $listener;
82
        }
83
    }
84
85
    protected function onPropertyChanged($propName, $oldValue, $newValue) {
86
        if ($this->listeners) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->listeners of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
87
            foreach ($this->listeners as $listener) {
88
                $listener->propertyChanged($this, $propName, $oldValue, $newValue);
89
            }
90
        }
91
    }
92
}
93
94
/** @Entity @ChangeTrackingPolicy("NOTIFY") */
95
class DDC1690Parent extends NotifyBaseEntity {
96
    /** @Id @Column(type="integer") @GeneratedValue */
97
    private $id;
98
99
    /** @Column */
100
    private $name;
101
102
    /** @OneToOne(targetEntity="DDC1690Child") */
103
    private $child;
104
105
    function getId() {
106
        return $this->id;
107
    }
108
109
    function getName() {
110
        return $this->name;
111
    }
112
113
    function setName($name) {
114
        $this->onPropertyChanged('name', $this->name, $name);
115
        $this->name = $name;
116
    }
117
118
    function setChild($child) {
119
        $this->child = $child;
120
    }
121
122
    function getChild() {
123
        return $this->child;
124
    }
125
}
126
127
/** @Entity */
128
class DDC1690Child extends NotifyBaseEntity {
129
    /** @Id @Column(type="integer") @GeneratedValue */
130
    private $id;
131
132
    /** @Column */
133
    private $name;
134
135
    /** @OneToOne(targetEntity="DDC1690Parent", mappedBy="child") */
136
    private $parent;
137
138
    function getId() {
139
        return $this->id;
140
    }
141
142
    function getName() {
143
        return $this->name;
144
    }
145
146
    function setName($name) {
147
        $this->onPropertyChanged('name', $this->name, $name);
148
        $this->name = $name;
149
    }
150
151
    function setParent($parent) {
152
        $this->parent = $parent;
153
    }
154
155
    function getParent() {
156
        return $this->parent;
157
    }
158
}
159