File: GH10752Test.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 (99 lines) | stat: -rw-r--r-- 2,635 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
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Tests\OrmFunctionalTestCase;
use PHPUnit\Framework\Attributes\Group;

#[Group('GH10752')]
class GH10752Test extends OrmFunctionalTestCase
{
    protected function setUp(): void
    {
        parent::setUp();

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

    public function testThrowExceptionWhenRemovingPromotionThatIsInUse(): void
    {
        $order     = new GH10752Order();
        $promotion = new GH10752Promotion();

        $order->addPromotion($promotion);

        $this->_em->persist($order);
        $this->_em->persist($promotion);
        $this->_em->flush();

        $this->_em->remove($promotion);

        $this->expectException(ForeignKeyConstraintViolationException::class);
        $this->_em->flush();
    }

    public function testThrowExceptionWhenRemovingPromotionThatIsInUseAndOrderIsNotInMemory(): void
    {
        $order     = new GH10752Order();
        $promotion = new GH10752Promotion();

        $order->addPromotion($promotion);

        $this->_em->persist($order);
        $this->_em->persist($promotion);
        $this->_em->flush();

        $this->_em->clear();

        $promotion = $this->_em->find(GH10752Promotion::class, $promotion->id);
        $this->_em->remove($promotion);

        $this->expectException(ForeignKeyConstraintViolationException::class);
        $this->_em->flush();
    }
}

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

    #[ORM\ManyToMany(targetEntity: GH10752Promotion::class, cascade: ['persist'])]
    #[ORM\JoinTable(name: 'order_promotion')]
    #[ORM\JoinColumn(name: 'order_id', referencedColumnName: 'id', onDelete: 'CASCADE')]
    #[ORM\InverseJoinColumn(name: 'promotion_id', referencedColumnName: 'id')]
    private Collection $promotions;

    public function __construct()
    {
        $this->promotions = new ArrayCollection();
    }

    public function addPromotion(GH10752Promotion $promotion): void
    {
        if (! $this->promotions->contains($promotion)) {
            $this->promotions->add($promotion);
        }
    }
}

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