File: GH10532Test.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 (167 lines) | stat: -rw-r--r-- 4,545 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Tests\OrmFunctionalTestCase;

class GH10532Test extends OrmFunctionalTestCase
{
    protected function setUp(): void
    {
        parent::setUp();

        $this->createSchemaForModels(
            GH10532A::class,
            GH10532B::class,
            GH10532C::class,
            GH10532X::class,
        );
    }

    public function tearDown(): void
    {
        $conn = static::$sharedConn;
        $conn->executeStatement('DELETE FROM gh10532_c');
        $conn->executeStatement('DELETE FROM gh10532_b');
        $conn->executeStatement('DELETE FROM gh10532_a');
        $conn->executeStatement('DELETE FROM gh10532_x');
    }

    public function testInserts(): void
    {
        // Dependencies are $a1 -> $b -> $a2 -> $c

        $a1 = new GH10532A();
        $b  = new GH10532B();
        $a2 = new GH10532A();
        $c  = new GH10532C();

        $a1->x = $b;
        $b->a  = $a2;
        $a2->x = $c;

        /*
         * The following would force a working commit order, but that's not what
         * we want (the ORM shall sort this out internally).
         *
         * $this->_em->persist($c);
         * $this->_em->persist($a2);
         * $this->_em->flush();
         * $this->_em->persist($b);
         * $this->_em->persist($a1);
         * $this->_em->flush();
         */

        $this->_em->persist($a1);
        $this->_em->persist($a2);
        $this->_em->persist($b);
        $this->_em->persist($c);
        $this->_em->flush();

        self::assertNotNull($a1->id);
        self::assertNotNull($b->id);
        self::assertNotNull($a2->id);
        self::assertNotNull($c->id);
    }

    public function testDeletes(): void
    {
        // Dependencies are $a1 -> $b -> $a2 -> $c

        $this->expectNotToPerformAssertions();
        $con = $this->_em->getConnection();

        // The "c" entity
        $con->insert('gh10532_x', ['id' => 1, 'discr' => 'C']);
        $con->insert('gh10532_c', ['id' => 1]);
        $c = $this->_em->find(GH10532C::class, 1);

        // The "a2" entity
        $con->insert('gh10532_a', ['id' => 2, 'gh10532x_id' => 1]);
        $a2 = $this->_em->find(GH10532A::class, 2);

        // The "b" entity
        $con->insert('gh10532_x', ['id' => 3, 'discr' => 'B']);
        $con->insert('gh10532_b', ['id' => 3, 'gh10532a_id' => 2]);
        $b = $this->_em->find(GH10532B::class, 3);

        // The "a1" entity
        $con->insert('gh10532_a', ['id' => 4, 'gh10532x_id' => 3]);
        $a1 = $this->_em->find(GH10532A::class, 4);

        /*
         * The following would make the deletions happen in an order
         * where the not-nullable foreign key constraints would not be
         * violated. But, we want the ORM to be able to sort this out
         * internally.
         *
         * $this->_em->remove($a1);
         * $this->_em->flush();
         * $this->_em->remove($b);
         * $this->_em->flush();
         * $this->_em->remove($a2);
         * $this->_em->remove($c);
         * $this->_em->flush();
         */

        $this->_em->remove($a1);
        $this->_em->remove($a2);
        $this->_em->remove($b);
        $this->_em->remove($c);

        $this->_em->flush();
    }
}

/**
 * We are using JTI here, since STI would relax the not-nullable constraint for the "parent"
 * column. Causes another error, but not the constraint violation I'd like to point out.
 */
#[ORM\Entity]
#[ORM\Table(name: 'gh10532_x')]
#[ORM\DiscriminatorColumn(name: 'discr', type: 'string')]
#[ORM\DiscriminatorMap(['B' => GH10532B::class, 'C' => GH10532C::class])]
#[ORM\InheritanceType('JOINED')]
abstract class GH10532X
{
    /** @var int */
    #[ORM\Id]
    #[ORM\GeneratedValue(strategy: 'AUTO')]
    #[ORM\Column(type: 'integer')]
    public $id;
}

#[ORM\Entity]
#[ORM\Table(name: 'gh10532_b')]
class GH10532B extends GH10532X
{
    /** @var GH10532A */
    #[ORM\ManyToOne(targetEntity: GH10532A::class)]
    #[ORM\JoinColumn(nullable: false, name: 'gh10532a_id')]
    public $a;
}

#[ORM\Entity]
#[ORM\Table(name: 'gh10532_c')]
class GH10532C extends GH10532X
{
}

#[ORM\Entity]
#[ORM\Table(name: 'gh10532_a')]
class GH10532A
{
    /** @var int */
    #[ORM\Id]
    #[ORM\GeneratedValue(strategy: 'AUTO')]
    #[ORM\Column(type: 'integer')]
    public $id;

    /** @var GH10532X */
    #[ORM\ManyToOne(targetEntity: GH10532X::class)]
    #[ORM\JoinColumn(nullable: false, name: 'gh10532x_id')]
    public $x;
}