File: GH8443Test.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 (128 lines) | stat: -rw-r--r-- 3,372 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
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
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\DiscriminatorColumn;
use Doctrine\ORM\Mapping\DiscriminatorMap;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\InheritanceType;
use Doctrine\ORM\Mapping\JoinColumn;
use Doctrine\ORM\Mapping\OneToOne;
use Doctrine\ORM\Mapping\Table;
use Doctrine\ORM\Query;
use Doctrine\Tests\Models\Company\CompanyManager;
use Doctrine\Tests\Models\Company\CompanyPerson;
use Doctrine\Tests\OrmFunctionalTestCase;
use PHPUnit\Framework\Attributes\Group;

use function assert;

final class GH8443Test extends OrmFunctionalTestCase
{
    protected function setUp(): void
    {
        $this->useModelSet('company');

        parent::setUp();

        $this->createSchemaForModels(GH8443Foo::class);
    }

    #[Group('GH-8443')]
    public function testJoinRootEntityWithForcePartialLoad(): void
    {
        $person = new CompanyPerson();
        $person->setName('John');

        $manager = new CompanyManager();
        $manager->setName('Adam');
        $manager->setSalary(1000);
        $manager->setDepartment('IT');
        $manager->setTitle('manager');

        $manager->setSpouse($person);

        $this->_em->persist($person);
        $this->_em->persist($manager);
        $this->_em->flush();
        $this->_em->clear();

        $manager = $this->_em->createQuery(
            "SELECT m from Doctrine\Tests\Models\Company\CompanyManager m
               JOIN m.spouse s
               WITH s.name = 'John'",
        )->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true)->getSingleResult();
        $this->_em->refresh($manager);

        $this->assertEquals('John', $manager->getSpouse()->getName());
    }

    #[Group('GH-8443')]
    public function testJoinRootEntityWithOnlyOneEntityInHierarchy(): void
    {
        $bar = new GH8443Foo('bar');

        $foo = new GH8443Foo('foo');
        $foo->setBar($bar);

        $this->_em->persist($bar);
        $this->_em->persist($foo);
        $this->_em->flush();
        $this->_em->clear();

        $foo = $this->_em->createQuery(
            'SELECT f from ' . GH8443Foo::class . " f JOIN f.bar b WITH b.name = 'bar'",
        )->getSingleResult();
        assert($foo instanceof GH8443Foo);

        $bar = $foo->getBar();
        assert($bar !== null);
        $this->assertEquals('bar', $bar->getName());
    }
}
#[Table(name: 'GH2947_foo')]
#[Entity]
#[InheritanceType('JOINED')]
#[DiscriminatorColumn(name: 'discr', type: 'string')]
#[DiscriminatorMap(['foo' => 'GH8443Foo'])]
class GH8443Foo
{
    #[Id]
    #[Column(type: 'integer')]
    #[GeneratedValue]
    private int|null $id = null;

    /** @var GH8443Foo|null */
    #[OneToOne(targetEntity: 'GH8443Foo')]
    #[JoinColumn(name: 'bar_id', referencedColumnName: 'id')]
    private $bar;

    public function __construct(
        #[Column]
        private string $name,
    ) {
    }

    public function getName(): string|null
    {
        return $this->name;
    }

    public function setBar(GH8443Foo $bar): void
    {
        if ($bar !== $this->bar) {
            $this->bar      = $bar;
            $this->bar->bar = $this;
        }
    }

    public function getBar(): GH8443Foo|null
    {
        return $this->bar;
    }
}