File: GH10747Test.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 (156 lines) | stat: -rw-r--r-- 4,116 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
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type as DBALType;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\Table;
use Doctrine\Tests\DbalTypes\CustomIdObject;
use Doctrine\Tests\OrmFunctionalTestCase;
use PHPUnit\Framework\Attributes\Group;

use function str_replace;

/**
 * Functional tests for asserting that orphaned children in a OneToMany relationship get removed with a custom identifier
 */
#[Group('GH10747')]
final class GH10747Test extends OrmFunctionalTestCase
{
    protected function setUp(): void
    {
        parent::setUp();

        if (! DBALType::hasType(GH10747CustomIdObjectHashType::class)) {
            DBALType::addType(GH10747CustomIdObjectHashType::class, GH10747CustomIdObjectHashType::class);
        }

        $this->setUpEntitySchema([GH10747Article::class, GH10747Credit::class]);
    }

    public function testOrphanedOneToManyDeletesCollection(): void
    {
        $object = new GH10747Article(
            new CustomIdObject('article'),
        );

        $creditOne = new GH10747Credit(
            $object,
            'credit1',
        );

        $creditTwo = new GH10747Credit(
            $object,
            'credit2',
        );

        $object->setCredits(new ArrayCollection([$creditOne, $creditTwo]));

        $this->_em->persist($object);
        $this->_em->persist($creditOne);
        $this->_em->persist($creditTwo);
        $this->_em->flush();

        $id = $object->id;

        $object2 = $this->_em->find(GH10747Article::class, $id);

        $creditThree = new GH10747Credit(
            $object2,
            'credit3',
        );

        $object2->setCredits(new ArrayCollection([$creditThree]));

        $this->_em->persist($object2);
        $this->_em->persist($creditThree);
        $this->_em->flush();

        $currentDatabaseCredits = $this->_em->createQueryBuilder()
            ->select('c.id')
            ->from(GH10747Credit::class, 'c')
            ->getQuery()
            ->execute();

        self::assertCount(1, $currentDatabaseCredits);
    }
}

#[Entity]
#[Table]
class GH10747Article
{
    #[Id]
    #[Column(type: GH10747CustomIdObjectHashType::class, length: 24)] // strlen(PHP_INT_MAX . '_test')
    public CustomIdObject $id;

    /** @var Collection<int, GH10747Credit> */
    #[ORM\OneToMany(mappedBy: 'article', targetEntity: GH10747Credit::class, orphanRemoval: true)]
    public Collection $credits;

    public function __construct(CustomIdObject $id)
    {
        $this->id      = $id;
        $this->credits = new ArrayCollection();
    }

    public function setCredits(Collection $credits): void
    {
        $this->credits = $credits;
    }

    /** @return Collection<int, GH10747Credit> */
    public function getCredits(): Collection
    {
        return $this->credits;
    }
}

#[Entity]
#[Table]
class GH10747Credit
{
    #[ORM\Column(type: 'integer')]
    #[ORM\GeneratedValue]
    #[Id]
    public int|null $id = null;

    #[ORM\ManyToOne(inversedBy: 'credits', targetEntity: GH10747Article::class)]
    public GH10747Article $article;

    public function __construct(GH10747Article $article, public string $name)
    {
        $this->article = $article;
    }
}

class GH10747CustomIdObjectHashType extends DBALType
{
    public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform): string
    {
        return $value->id . '_test';
    }

    public function convertToPHPValue(mixed $value, AbstractPlatform $platform): CustomIdObject
    {
        return new CustomIdObject(str_replace('_test', '', $value));
    }

    public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
    {
        return $platform->getStringTypeDeclarationSQL($column);
    }

    public function getName(): string
    {
        return self::class;
    }
}