File: GH10913Test.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 (153 lines) | stat: -rw-r--r-- 3,527 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
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\ORM\Internal\TopologicalSort\CycleDetectedException;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Tests\OrmFunctionalTestCase;

use function array_filter;
use function array_values;
use function strpos;

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

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

    public function testExample1(): void
    {
        [$a, $b, $c] = $this->createEntities(3);

        $c->ref = $b;
        $b->odc = $a;

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

        $this->_em->remove($a);
        $this->_em->remove($b);
        $this->_em->remove($c);

        $this->flushAndAssertNumberOfDeleteQueries(3);
    }

    public function testExample2(): void
    {
        [$a, $b, $c] = $this->createEntities(3);

        $a->odc = $b;
        $b->odc = $a;
        $c->ref = $b;

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

        $this->_em->remove($a);
        $this->_em->remove($b);
        $this->_em->remove($c);

        $this->flushAndAssertNumberOfDeleteQueries(3);
    }

    public function testExample3(): void
    {
        [$a, $b, $c] = $this->createEntities(3);

        $a->odc = $b;
        $a->ref = $c;
        $c->ref = $b;
        $b->odc = $a;

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

        $this->_em->remove($a);
        $this->_em->remove($b);
        $this->_em->remove($c);

        self::expectException(CycleDetectedException::class);

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

    public function testExample4(): void
    {
        [$a, $b, $c, $d] = $this->createEntities(4);

        $a->ref = $b;
        $b->odc = $c;
        $c->odc = $b;
        $d->ref = $c;

        $this->_em->persist($a);
        $this->_em->persist($b);
        $this->_em->persist($c);
        $this->_em->persist($d);
        $this->_em->flush();

        $this->_em->remove($b);
        $this->_em->remove($c);
        $this->_em->remove($d);
        $this->_em->remove($a);

        $this->flushAndAssertNumberOfDeleteQueries(4);
    }

    private function flushAndAssertNumberOfDeleteQueries(int $expectedCount): void
    {
        $queryLog = $this->getQueryLog();
        $queryLog->reset()->enable();

        $this->_em->flush();

        $queries = array_values(array_filter($queryLog->queries, static function (array $entry): bool {
            return strpos($entry['sql'], 'DELETE') === 0;
        }));

        self::assertCount($expectedCount, $queries);
    }

    /** @return list<GH10913Entity> */
    private function createEntities(int $count = 1): array
    {
        $result = [];

        for ($i = 0; $i < $count; $i++) {
            $result[] = new GH10913Entity();
        }

        return $result;
    }
}

#[ORM\Entity]
class GH10913Entity
{
    #[ORM\Id]
    #[ORM\Column(type: 'integer')]
    #[ORM\GeneratedValue]
    public int $id;

    #[ORM\ManyToOne(targetEntity: self::class)]
    #[ORM\JoinColumn(nullable: true, onDelete: 'CASCADE')]
    public GH10913Entity $odc;

    #[ORM\ManyToOne(targetEntity: self::class)]
    #[ORM\JoinColumn(nullable: true)]
    public GH10913Entity $ref;
}