File: GH5998Test.php

package info (click to toggle)
doctrine 3.3.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 11,236 kB
  • sloc: php: 105,633; xml: 1,312; makefile: 35; sh: 14
file content (201 lines) | stat: -rw-r--r-- 5,114 bytes parent folder | download | duplicates (3)
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\DBAL\LockMode;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Tests\OrmFunctionalTestCase;
use PHPUnit\Framework\Attributes\Group;

#[Group('GH-5998')]
class GH5998Test extends OrmFunctionalTestCase
{
    protected function setUp(): void
    {
        parent::setUp();

        $this->_schemaTool->createSchema([
            $this->_em->getClassMetadata(GH5998JTI::class),
            $this->_em->getClassMetadata(GH5998JTIChild::class),
            $this->_em->getClassMetadata(GH5998STI::class),
            $this->_em->getClassMetadata(GH5998Basic::class),
            $this->_em->getClassMetadata(GH5998Related::class),
        ]);
    }

    /**
     * Verifies that MappedSuperclasses work within an inheritance hierarchy.
     */
    public function testIssue(): void
    {
        // Test JTI
        $this->classTests(GH5998JTIChild::class);
        // Test STI
        $this->classTests(GH5998STIChild::class);
        // Test Basic
        $this->classTests(GH5998Basic::class);
    }

    private function classTests($className): void
    {
        // Test insert
        $child      = new $className('Sam', 0, 1);
        $child->rel = new GH5998Related();
        $this->_em->persist($child);
        $this->_em->persist($child->rel);
        $this->_em->flush();
        $this->_em->clear();
        $id = $child->id;

        // Test find by rel
        $child = $this->_em->getRepository($className)->findOneBy(['rel' => $child->rel]);
        self::assertNotNull($child);
        $this->_em->clear();

        // Test query by id with fetch join
        $child = $this->_em->createQuery('SELECT t, r FROM ' . $className . ' t JOIN t.rel r WHERE t.id = ?0')->setParameter(0, $id)->getOneOrNullResult();
        self::assertNotNull($child);

        // Test lock and update
        $this->_em->wrapInTransaction(static function ($em) use ($child): void {
            $em->lock($child, LockMode::NONE);
            $child->firstName = 'Bob';
            $child->status    = 0;
        });
        $this->_em->clear();
        $child = $this->_em->getRepository($className)->find($id);
        self::assertNotNull($child);
        self::assertEquals($child->firstName, 'Bob');
        self::assertEquals($child->status, 0);

        // Test delete
        $this->_em->remove($child);
        $this->_em->flush();
        $child = $this->_em->getRepository($className)->find($id);
        self::assertNull($child);
    }
}

#[ORM\MappedSuperclass]
class GH5998Common
{
    /** @var int */
    #[ORM\Id]
    #[ORM\Column(type: 'integer')]
    #[ORM\GeneratedValue]
    public $id;

    /** @var GH5998Related */
    #[ORM\ManyToOne(targetEntity: GH5998Related::class)]
    #[ORM\JoinColumn(name: 'related_id', referencedColumnName: 'id')]
    public $rel;

    /** @var int */
    #[ORM\Version]
    #[ORM\Column(type: 'integer')]
    public $version;

    /** @var mixed */
    public $other;
}

#[ORM\Entity]
#[ORM\InheritanceType('JOINED')]
#[ORM\DiscriminatorMap(['child' => GH5998JTIChild::class])]
abstract class GH5998JTI extends GH5998Common
{
    /** @var string */
    #[ORM\Column(type: 'string', length: 255)]
    public $firstName;
}

#[ORM\MappedSuperclass]
class GH5998JTICommon extends GH5998JTI
{
    /** @var int */
    #[ORM\Column(type: 'integer')]
    public $status;
}

#[ORM\Entity]
class GH5998JTIChild extends GH5998JTICommon
{
    /** @var int */
    #[ORM\Column(type: 'integer')]
    public $type;

    public function __construct(string $firstName, int $type, int $status)
    {
        $this->firstName = $firstName;
        $this->type      = $type;
        $this->status    = $status;
    }
}

#[ORM\Entity]
#[ORM\InheritanceType('SINGLE_TABLE')]
#[ORM\DiscriminatorMap(['child' => GH5998STIChild::class])]
abstract class GH5998STI extends GH5998Common
{
    /** @var string */
    #[ORM\Column(type: 'string', length: 255)]
    public $firstName;
}

#[ORM\MappedSuperclass]
class GH5998STICommon extends GH5998STI
{
    /** @var int */
    #[ORM\Column(type: 'integer')]
    public $status;
}

#[ORM\Entity]
class GH5998STIChild extends GH5998STICommon
{
    /** @var int */
    #[ORM\Column(type: 'integer')]
    public $type;

    public function __construct(string $firstName, int $type, int $status)
    {
        $this->firstName = $firstName;
        $this->type      = $type;
        $this->status    = $status;
    }
}

#[ORM\Entity]
class GH5998Basic extends GH5998Common
{
    /** @var string */
    #[ORM\Column(type: 'string', length: 255)]
    public $firstName;

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

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

    public function __construct(string $firstName, int $type, int $status)
    {
        $this->firstName = $firstName;
        $this->type      = $type;
        $this->status    = $status;
    }
}

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