Failed Conditions
Push — 2.7 ( c036c0...266f0d )
by Jonathan
57:23 queued 50:07
created

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

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