Issues (1543)

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

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\ORM\Functional\Ticket;
6
7
use Doctrine\ORM\Annotation as ORM;
8
use Doctrine\Tests\OrmFunctionalTestCase;
9
use Exception;
10
use ProxyManager\Proxy\GhostObjectInterface;
11
use function get_class;
12
13
/**
14
 * Tests that join columns (foreign keys) can be named the same as the association
15
 * fields they're used on without causing issues.
16
 */
17
class DDC522Test extends OrmFunctionalTestCase
18
{
19
    protected function setUp() : void
20
    {
21
        parent::setUp();
22
23
        try {
24
            $this->schemaTool->createSchema(
25
                [
26
                    $this->em->getClassMetadata(DDC522Customer::class),
27
                    $this->em->getClassMetadata(DDC522Cart::class),
28
                    $this->em->getClassMetadata(DDC522ForeignKeyTest::class),
29
                ]
30
            );
31
        } catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
32
        }
33
    }
34
35
    /**
36
     * @group DDC-522
37
     */
38
    public function testJoinColumnWithSameNameAsAssociationField() : void
39
    {
40
        $cust       = new DDC522Customer();
41
        $cust->name = 'name';
42
43
        $cart           = new DDC522Cart();
44
        $cart->total    = 0;
45
        $cust->cart     = $cart;
46
        $cart->customer = $cust;
47
48
        $this->em->persist($cust);
49
        $this->em->persist($cart);
50
        $this->em->flush();
51
        $this->em->clear();
52
53
        $cart = $this->em
54
            ->createQuery('select ca, c from ' . DDC522Cart::class . ' ca join ca.customer c')
55
            ->getSingleResult();
56
57
        self::assertInstanceOf(DDC522Cart::class, $cart);
58
        self::assertInstanceOf(DDC522Customer::class, $cart->customer);
59
        self::assertNotInstanceOf(GhostObjectInterface::class, $cart->customer);
60
        self::assertEquals('name', $cart->customer->name);
61
62
        $cartId = $cart->id;
63
64
        $fkt       = new DDC522ForeignKeyTest();
65
        $fkt->cart = $cart; // must be set properly
66
67
        $this->em->persist($fkt);
68
        $this->em->flush();
69
        $this->em->clear();
70
71
        $fkt2 = $this->em->find(get_class($fkt), $fkt->id);
72
73
        self::assertEquals($fkt->cart->id, $cartId);
74
        self::assertInstanceOf(GhostObjectInterface::class, $fkt2->cart);
75
        self::assertFalse($fkt2->cart->isProxyInitialized());
76
    }
77
78
    /**
79
     * @group DDC-522
80
     * @group DDC-762
81
     */
82
    public function testJoinColumnWithNullSameNameAssociationField() : void
83
    {
84
        $fkCust       = new DDC522ForeignKeyTest();
85
        $fkCust->name = 'name';
86
        $fkCust->cart = null;
87
88
        $this->em->persist($fkCust);
89
        $this->em->flush();
90
        $this->em->clear();
91
92
        $expected = clone $fkCust;
93
94
        // removing dynamic field (which is not persisted)
95
        unset($expected->name);
96
97
        self::assertEquals($expected, $this->em->find(DDC522ForeignKeyTest::class, $fkCust->id));
98
    }
99
}
100
101
/**
102
 * @ORM\Entity
103
 */
104
class DDC522Customer
105
{
106
    /**
107
     * @ORM\Id
108
     * @ORM\Column(type="integer")
109
     * @ORM\GeneratedValue
110
     */
111
    public $id;
112
113
    /** @ORM\Column */
114
    public $name;
115
116
    /** @ORM\OneToOne(targetEntity=DDC522Cart::class, mappedBy="customer") */
117
    public $cart;
118
}
119
120
/**
121
 * @ORM\Entity
122
 */
123
class DDC522Cart
124
{
125
    /**
126
     * @ORM\Id
127
     * @ORM\Column(type="integer")
128
     * @ORM\GeneratedValue
129
     */
130
    public $id;
131
132
    /** @ORM\Column(type="integer") */
133
    public $total;
134
135
    /**
136
     * @ORM\OneToOne(targetEntity=DDC522Customer::class, inversedBy="cart")
137
     * @ORM\JoinColumn(name="customer", referencedColumnName="id")
138
     */
139
    public $customer;
140
}
141
142
/**
143
 * @ORM\Entity
144
 */
145
class DDC522ForeignKeyTest
146
{
147
    /**
148
     * @ORM\Id
149
     * @ORM\Column(type="integer")
150
     * @ORM\GeneratedValue
151
     */
152
    public $id;
153
154
    /**
155
     * @ORM\OneToOne(targetEntity=DDC522Cart::class)
156
     * @ORM\JoinColumn(name="cart_id", referencedColumnName="id")
157
     */
158
    public $cart;
159
}
160