Completed
Pull Request — master (#6473)
by Vadim
63:15
created

DDC6470Driver::getUserProfile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\ORM\Functional\Ticket;
4
5
use Doctrine\ORM\Tools\ToolsException;
6
7
/**
8
 */
9
class DDC6470Test extends \Doctrine\Tests\OrmFunctionalTestCase
10
{
11
    public function setUp()
12
    {
13
        $this->enableSecondLevelCache();
14
        parent::setUp();
15
16
        try {
17
            $this->_schemaTool->createSchema(
18
                [
19
                    $this->_em->getClassMetadata(DDC6470User::class),
20
                    $this->_em->getClassMetadata(DDC6470Driver::class),
21
                ]
22
            );
23
        } catch (ToolsException $exc) {
24
        }
25
    }
26
27
    public function testIssue()
28
    {
29
        $driver1 = new DDC6470Driver('Bar');
30
31
        $this->_em->persist($driver1);
32
        $this->_em->flush();
33
        $this->_em->clear();
34
35
36
        $this->assertTrue($this->_em->getCache()->containsEntity(DDC6470User::class, ['id' => $driver1->getUserProfile()->getId()]));
37
        $queryCount = $this->getCurrentQueryCount();
38
        $driver3 = $this->_em->createQueryBuilder()
39
            ->from(DDC6470Driver::class, 'd')
40
            ->select('d')
41
            ->where('d.id = :id')
42
            ->setParameter('id', $driver1->getId())
43
            ->getQuery()
44
            ->getOneOrNullResult();
45
        $this->assertEquals($queryCount+1, $this->getCurrentQueryCount());
46
        $this->assertInstanceOf(DDC6470Driver::class, $driver3);
47
        $this->assertInstanceOf(DDC6470User::class, $driver3->getUserProfile());
48
        $this->assertEquals($queryCount, $this->getCurrentQueryCount());
49
        $this->assertEquals('Franta', $driver3->getName());
50
        $this->assertEquals('Foo', $driver3->getUserProfile()->getName());
51
    }
52
}
53
54
/**
55
 * @Entity
56
 * @Table(name="ddc6470_drivers")
57
 */
58
class DDC6470Driver
59
{
60
    /**
61
     * @Id
62
     * @GeneratedValue
63
     * @Column(type="integer")
64
     */
65
    protected $id;
66
67
    /**
68
     * @Column(type="string")
69
     * @var string
70
     */
71
    protected $name;
72
73
    /**
74
     * @Cache("NONSTRICT_READ_WRITE")
75
     * @OneToOne(targetEntity="DDC6470User", mappedBy="user", cascade={"persist"})
76
     */
77
    protected $userProfile;
78
79
    public function __construct($name, $userProfile = null)
80
    {
81
        $this->name = $name;
82
        $this->userProfile = new DDC6470User('Foo', $this);
83
    }
84
85
    /**
86
     * @return integer
87
     */
88
    public function getId()
89
    {
90
        return $this->id;
91
    }
92
93
    /**
94
     * @param string $name
95
     */
96
    public function setName($name)
97
    {
98
        $this->name = $name;
99
    }
100
101
    /**
102
     * @return string
103
     */
104
    public function getName()
105
    {
106
        return $this->name;
107
    }
108
109
    /**
110
     * @param \Entities\User $userProfile
111
     */
112
    public function setUserProfile($userProfile)
113
    {
114
        $this->userProfile = $userProfile;
115
    }
116
117
    /**
118
     * @return \Entities\User
119
     */
120
    public function getUserProfile()
121
    {
122
        return $this->userProfile;
123
    }
124
125
}
126
127
/**
128
 * @Entity
129
 * @Table(name="ddc6470_users")
130
 * @Cache("NONSTRICT_READ_WRITE")
131
 */
132
class DDC6470User
133
{
134
    /**
135
     * @Id
136
     * @GeneratedValue
137
     * @Column(type="integer")
138
     */
139
    protected $id;
140
141
    /**
142
     * @Column(type="string")
143
     * @var string
144
     */
145
    protected $name;
146
    /**
147
     *
148
     * @OneToOne(targetEntity="DDC6470Driver", inversedBy="userProfile")
149
     * @JoinColumn(name="user_id", referencedColumnName="id", onDelete="cascade")
150
     * @Cache(usage="READ_ONLY")
151
     */
152
    protected $user;
153
154
    public function __construct($name, DDC6470Driver $user)
155
    {
156
        $this->name = $name;
157
        $this->user = $user;
158
    }
159
160
    /**
161
     * @return integer
162
     */
163
    public function getId()
164
    {
165
        return $this->id;
166
    }
167
168
    /**
169
     * @param string $name
170
     */
171
    public function setName($name)
172
    {
173
        $this->name = $name;
174
    }
175
176
    /**
177
     * @return string
178
     */
179
    public function getName()
180
    {
181
        return $this->name;
182
    }
183
184
}
185