File: DDC522Test.php

package info (click to toggle)
doctrine 3.5.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,556 kB
  • sloc: php: 108,620; xml: 1,340; makefile: 35; sh: 14
file content (141 lines) | stat: -rw-r--r-- 3,753 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\JoinColumn;
use Doctrine\ORM\Mapping\OneToOne;
use Doctrine\Tests\OrmFunctionalTestCase;
use PHPUnit\Framework\Attributes\Group;

/**
 * Tests that join columns (foreign keys) can be named the same as the association
 * fields they're used on without causing issues.
 */
class DDC522Test extends OrmFunctionalTestCase
{
    protected function setUp(): void
    {
        parent::setUp();

        $this->createSchemaForModels(
            DDC522Customer::class,
            DDC522Cart::class,
            DDC522ForeignKeyTest::class,
        );
    }

    #[Group('DDC-522')]
    public function testJoinColumnWithSameNameAsAssociationField(): void
    {
        $cust           = new DDC522Customer();
        $cust->name     = 'name';
        $cart           = new DDC522Cart();
        $cart->total    = 0;
        $cust->cart     = $cart;
        $cart->customer = $cust;
        $this->_em->persist($cust);
        $this->_em->persist($cart);
        $this->_em->flush();

        $this->_em->clear();

        $r = $this->_em->createQuery('select ca,c from ' . DDC522Cart::class . ' ca join ca.customer c')
                       ->getResult();

        self::assertInstanceOf(DDC522Cart::class, $r[0]);
        self::assertInstanceOf(DDC522Customer::class, $r[0]->customer);
        self::assertFalse($this->isUninitializedObject($r[0]->customer));
        self::assertEquals('name', $r[0]->customer->name);

        $fkt         = new DDC522ForeignKeyTest();
        $fkt->cartId = $r[0]->id; // ignored for persistence
        $fkt->cart   = $r[0]; // must be set properly
        $this->_em->persist($fkt);
        $this->_em->flush();
        $this->_em->clear();

        $fkt2 = $this->_em->find($fkt::class, $fkt->id);
        self::assertEquals($fkt->cart->id, $fkt2->cartId);
        self::assertTrue($this->isUninitializedObject($fkt2->cart));
    }

    #[Group('DDC-522')]
    #[Group('DDC-762')]
    public function testJoinColumnWithNullSameNameAssociationField(): void
    {
        $fkCust       = new DDC522ForeignKeyTest();
        $fkCust->cart = null;

        $this->_em->persist($fkCust);
        $this->_em->flush();
        $this->_em->clear();

        $expected = clone $fkCust;
        // removing dynamic field (which is not persisted)
        unset($expected->name);

        self::assertEquals($expected, $this->_em->find(DDC522ForeignKeyTest::class, $fkCust->id));
    }
}

#[Entity]
class DDC522Customer
{
    /** @var int */
    #[Id]
    #[Column(type: 'integer')]
    #[GeneratedValue]
    public $id;

    /** @var mixed */
    #[Column]
    public $name;

    /** @var DDC522Cart */
    #[OneToOne(targetEntity: 'DDC522Cart', mappedBy: 'customer')]
    public $cart;
}

#[Entity]
class DDC522Cart
{
    /** @var int */
    #[Id]
    #[Column(type: 'integer')]
    #[GeneratedValue]
    public $id;

    /** @var int */
    #[Column(type: 'integer')]
    public $total;

    /** @var DDC522Customer */
    #[OneToOne(targetEntity: 'DDC522Customer', inversedBy: 'cart')]
    #[JoinColumn(name: 'customer', referencedColumnName: 'id')]
    public $customer;
}

#[Entity]
class DDC522ForeignKeyTest
{
    /** @var int */
    #[Id]
    #[Column(type: 'integer')]
    #[GeneratedValue]
    public $id;

    /** @var int|null */
    #[Column(type: 'integer', name: 'cart_id', nullable: true)]
    public $cartId;

    /** @var DDC522Cart|null */
    #[OneToOne(targetEntity: 'DDC522Cart')]
    #[JoinColumn(name: 'cart_id', referencedColumnName: 'id')]
    public $cart;
}