File: DDC1400Test.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 (121 lines) | stat: -rw-r--r-- 3,260 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
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\ManyToOne;
use Doctrine\ORM\Mapping\OneToMany;
use Doctrine\Tests\OrmFunctionalTestCase;
use PHPUnit\Framework\Attributes\Group;

#[Group('DDC-1400')]
class DDC1400Test extends OrmFunctionalTestCase
{
    protected function setUp(): void
    {
        parent::setUp();

        $this->createSchemaForModels(
            DDC1400Article::class,
            DDC1400User::class,
            DDC1400UserState::class,
        );
    }

    public function testFailingCase(): void
    {
        $article = new DDC1400Article();
        $user1   = new DDC1400User();
        $user2   = new DDC1400User();

        $this->_em->persist($article);
        $this->_em->persist($user1);
        $this->_em->persist($user2);
        $this->_em->flush();

        $userState1            = new DDC1400UserState();
        $userState1->article   = $article;
        $userState1->articleId = $article->id;
        $userState1->user      = $user1;
        $userState1->userId    = $user1->id;

        $userState2            = new DDC1400UserState();
        $userState2->article   = $article;
        $userState2->articleId = $article->id;
        $userState2->user      = $user2;
        $userState2->userId    = $user2->id;

        $this->_em->persist($userState1);
        $this->_em->persist($userState2);
        $this->_em->flush();
        $this->_em->clear();

        $user1 = $this->_em->getReference(DDC1400User::class, $user1->id);

        $this->_em->createQuery('SELECT a, s FROM ' . DDC1400Article::class . ' a JOIN a.userStates s WITH s.user = :activeUser')
                  ->setParameter('activeUser', $user1)
                  ->getResult();

        $this->getQueryLog()->reset()->enable();

        $this->_em->flush();

        $this->assertQueryCount(0, 'No query should be executed during flush in this case');
    }
}

#[Entity]
class DDC1400Article
{
    /** @var int */
    #[Id]
    #[Column(type: 'integer')]
    #[GeneratedValue]
    public $id;

    /** @phpstan-var Collection<int, DDC1400UserState> */
    #[OneToMany(targetEntity: 'DDC1400UserState', mappedBy: 'article', indexBy: 'userId', fetch: 'EXTRA_LAZY')]
    public $userStates;
}

#[Entity]
class DDC1400User
{
    /** @var int */
    #[Id]
    #[Column(type: 'integer')]
    #[GeneratedValue]
    public $id;

    /** @phpstan-var Collection<int, DDC1400UserState> */
    #[OneToMany(targetEntity: 'DDC1400UserState', mappedBy: 'user', indexBy: 'articleId', fetch: 'EXTRA_LAZY')]
    public $userStates;
}

#[Entity]
class DDC1400UserState
{
    /** @var DDC1400Article */
    #[Id]
    #[ManyToOne(targetEntity: 'DDC1400Article', inversedBy: 'userStates')]
    public $article;

    /** @var DDC1400User */
    #[Id]
    #[ManyToOne(targetEntity: 'DDC1400User', inversedBy: 'userStates')]
    public $user;

    /** @var int */
    #[Column(name: 'user_id', type: 'integer')]
    public $userId;

    /** @var int */
    #[Column(name: 'article_id', type: 'integer')]
    public $articleId;
}