Failed Conditions
Pull Request — master (#6143)
by Luís
10:34
created

GH5887Customer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 55
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getId() 0 4 1
A setId() 0 4 1
A getCart() 0 4 1
A setCart() 0 7 2
1
<?php
2
3
namespace Doctrine\Tests\ORM\Functional\Ticket;
4
5
use Doctrine\DBAL\Types\StringType;
6
use Doctrine\DBAL\Types\Type;
7
use Doctrine\DBAL\Platforms\AbstractPlatform;
8
use Doctrine\Tests\OrmFunctionalTestCase;
9
10
/**
11
 * @group 5887
12
 */
13
class GH5887Test extends OrmFunctionalTestCase
14
{
15 View Code Duplication
    public function setUp()
16
    {
17
        parent::setUp();
18
19
        Type::addType(GH5887CustomIdObjectType::NAME, GH5887CustomIdObjectType::class);
20
21
        $this->_schemaTool->createSchema(
22
            [
23
                $this->_em->getClassMetadata(GH5887Cart::class),
24
                $this->_em->getClassMetadata(GH5887Customer::class),
25
            ]
26
        );
27
    }
28
29
    public function testLazyLoadsForeignEntitiesInOneToOneRelationWhileHavingCustomIdObject()
30
    {
31
        $customerId = new GH5887CustomIdObject(1);
32
        $customer = new GH5887Customer();
33
        $customer->setId($customerId);
34
35
        $cartId = 2;
36
        $cart = new GH5887Cart();
37
        $cart->setId($cartId);
38
        $cart->setCustomer($customer);
39
40
        $this->_em->persist($customer);
41
        $this->_em->persist($cart);
42
        $this->_em->flush();
43
44
        // Clearing cached entities
45
        $this->_em->clear();
46
47
        $customerRepository = $this->_em->getRepository(GH5887Customer::class);
48
        /** @var GH5887Customer $customer */
49
        $customer = $customerRepository->createQueryBuilder('c')
50
            ->where('c.id = :id')
51
            ->setParameter('id', $customerId->getId())
52
            ->getQuery()
53
            ->getOneOrNullResult();
54
55
        $this->assertInstanceOf(GH5887Cart::class, $customer->getCart());
56
    }
57
}
58
59
/**
60
 * @Entity
61
 */
62
class GH5887Cart
63
{
64
    /**
65
     * @var int
66
     *
67
     * @Id
68
     * @Column(type="integer")
69
     * @GeneratedValue(strategy="NONE")
70
     */
71
    private $id;
72
73
    /**
74
     * One Cart has One Customer.
75
     *
76
     * @var GH5887Customer
77
     *
78
     * @OneToOne(targetEntity="GH5887Customer", inversedBy="cart")
79
     * @JoinColumn(name="customer_id", referencedColumnName="id")
80
     */
81
    private $customer;
82
83
    /**
84
     * @return int
85
     */
86
    public function getId()
87
    {
88
        return $this->id;
89
    }
90
91
    /**
92
     * @param int $id
93
     */
94
    public function setId($id)
95
    {
96
        $this->id = $id;
97
    }
98
99
    /**
100
     * @return GH5887Customer
101
     */
102
    public function getCustomer()
103
    {
104
        return $this->customer;
105
    }
106
107
    /**
108
     * @param GH5887Customer $customer
109
     */
110
    public function setCustomer(GH5887Customer $customer)
111
    {
112
        if ($this->customer !== $customer) {
113
            $this->customer = $customer;
114
            $customer->setCart($this);
115
        }
116
    }
117
}
118
119
/**
120
 * @Entity
121
 */
122
class GH5887Customer
123
{
124
    /**
125
     * @var GH5887CustomIdObject
126
     *
127
     * @Id
128
     * @Column(type="GH5887CustomIdObject")
129
     * @GeneratedValue(strategy="NONE")
130
     */
131
    private $id;
132
133
    /**
134
     * One Customer has One Cart.
135
     *
136
     * @var GH5887Cart
137
     *
138
     * @OneToOne(targetEntity="GH5887Cart", mappedBy="customer")
139
     */
140
    private $cart;
141
142
    /**
143
     * @return GH5887CustomIdObject
144
     */
145
    public function getId()
146
    {
147
        return $this->id;
148
    }
149
150
    /**
151
     * @param GH5887CustomIdObject $id
152
     */
153
    public function setId(GH5887CustomIdObject $id)
154
    {
155
        $this->id = $id;
156
    }
157
158
    /**
159
     * @return GH5887Cart
160
     */
161
    public function getCart(): GH5887Cart
162
    {
163
        return $this->cart;
164
    }
165
166
    /**
167
     * @param GH5887Cart $cart
168
     */
169
    public function setCart(GH5887Cart $cart)
170
    {
171
        if ($this->cart !== $cart) {
172
            $this->cart = $cart;
173
            $cart->setCustomer($this);
174
        }
175
    }
176
}
177
178
class GH5887CustomIdObject
179
{
180
    /**
181
     * @var int
182
     */
183
    private $id;
184
185
    /**
186
     * @param int $id
187
     */
188
    public function __construct($id)
189
    {
190
        $this->id = $id;
191
    }
192
193
    /**
194
     * @return int
195
     */
196
    public function getId(): int
197
    {
198
        return $this->id;
199
    }
200
201
    public function __toString()
202
    {
203
        return 'non existing id';
204
    }
205
}
206
207
class GH5887CustomIdObjectType extends StringType
208
{
209
    const NAME = 'GH5887CustomIdObject';
210
211
    /**
212
     * {@inheritdoc}
213
     */
214
    public function convertToDatabaseValue($value, AbstractPlatform $platform)
215
    {
216
        return $value->getId();
217
    }
218
219
    /**
220
     * {@inheritdoc}
221
     */
222
    public function convertToPHPValue($value, AbstractPlatform $platform)
223
    {
224
        return new GH5887CustomIdObject($value);
225
    }
226
227
    /**
228
     * {@inheritdoc}
229
     */
230
    public function getName()
231
    {
232
        return self::NAME;
233
    }
234
}
235