File: GH10348Test.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 (89 lines) | stat: -rw-r--r-- 2,300 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
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Tests\OrmFunctionalTestCase;

final class GH10348Test extends OrmFunctionalTestCase
{
    public function setUp(): void
    {
        parent::setUp();

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

    public function testTheORMRemovesReferencedEmployeeBeforeReferencingEmployee(): void
    {
        $person1         = new GH10348Person();
        $person2         = new GH10348Person();
        $person2->mentor = $person1;

        $company = new GH10348Company();
        $company->addEmployee($person1)->addEmployee($person2);

        $this->_em->persist($company);
        $this->_em->flush();

        $company = $this->_em->find(GH10348Company::class, $company->id);

        $this->_em->remove($company);
        $this->_em->flush();

        self::assertEmpty($this->_em->createQuery('SELECT c FROM ' . GH10348Company::class . ' c')->getResult());
        self::assertEmpty($this->_em->createQuery('SELECT p FROM ' . GH10348Person::class . ' p')->getResult());
    }
}

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

    /** @var ?GH10348Company */
    #[ORM\ManyToOne(targetEntity: GH10348Company::class, inversedBy: 'employees')]
    public $employer = null;

    /** @var ?GH10348Person */
    #[ORM\ManyToOne(targetEntity: self::class, cascade: ['remove'])]
    public $mentor = null;
}

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

    /** @var Collection */
    #[ORM\OneToMany(mappedBy: 'employer', targetEntity: GH10348Person::class, cascade: ['persist', 'remove'])]
    private $employees;

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

    public function addEmployee(GH10348Person $person): self
    {
        $person->employer = $this;
        $this->employees->add($person);

        return $this;
    }
}