File: CompositePrimaryKeyTest.php

package info (click to toggle)
doctrine 3.5.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 11,552 kB
  • sloc: php: 108,302; xml: 1,340; makefile: 35; sh: 14
file content (190 lines) | stat: -rw-r--r-- 6,345 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional;

use Doctrine\ORM\Exception\MissingIdentifierField;
use Doctrine\ORM\Exception\UnrecognizedIdentifierFields;
use Doctrine\ORM\Query\QueryException;
use Doctrine\Tests\IterableTester;
use Doctrine\Tests\Models\Navigation\NavCountry;
use Doctrine\Tests\Models\Navigation\NavPhotos;
use Doctrine\Tests\Models\Navigation\NavPointOfInterest;
use Doctrine\Tests\Models\Navigation\NavTour;
use Doctrine\Tests\Models\Navigation\NavUser;
use Doctrine\Tests\OrmFunctionalTestCase;
use PHPUnit\Framework\Attributes\Group;

class CompositePrimaryKeyTest extends OrmFunctionalTestCase
{
    protected function setUp(): void
    {
        $this->useModelSet('navigation');

        parent::setUp();
    }

    public function putGermanysBrandenburderTor(): void
    {
        $country = new NavCountry('Germany');
        $this->_em->persist($country);
        $poi = new NavPointOfInterest(100, 200, 'Brandenburger Tor', $country);
        $this->_em->persist($poi);
        $this->_em->flush();
        $this->_em->clear();
    }

    public function putTripAroundEurope(): NavTour
    {
        $poi = $this->_em->find(NavPointOfInterest::class, ['lat' => 100, 'long' => 200]);

        $tour = new NavTour('Trip around Europe');
        $tour->addPointOfInterest($poi);

        $this->_em->persist($tour);
        $this->_em->flush();
        $this->_em->clear();

        return $tour;
    }

    public function testPersistCompositePkEntity(): void
    {
        $this->putGermanysBrandenburderTor();

        $poi = $this->_em->find(NavPointOfInterest::class, ['lat' => 100, 'long' => 200]);

        self::assertInstanceOf(NavPointOfInterest::class, $poi);
        self::assertEquals(100, $poi->getLat());
        self::assertEquals(200, $poi->getLong());
        self::assertEquals('Brandenburger Tor', $poi->getName());
    }

    #[Group('DDC-1651')]
    public function testSetParameterCompositeKeyObject(): void
    {
        $this->putGermanysBrandenburderTor();

        $poi   = $this->_em->find(NavPointOfInterest::class, ['lat' => 100, 'long' => 200]);
        $photo = new NavPhotos($poi, 'asdf');
        $this->_em->persist($photo);
        $this->_em->flush();
        $this->_em->clear();

        $dql = 'SELECT t FROM Doctrine\Tests\Models\Navigation\NavPhotos t WHERE t.poi = ?1';

        $this->expectException(QueryException::class);
        $this->expectExceptionMessage('A single-valued association path expression to an entity with a composite primary key is not supported.');

        $sql = $this->_em->createQuery($dql)->getSQL();
    }

    public function testIdentityFunctionWithCompositePrimaryKey(): void
    {
        $this->putGermanysBrandenburderTor();

        $poi   = $this->_em->find(NavPointOfInterest::class, ['lat' => 100, 'long' => 200]);
        $photo = new NavPhotos($poi, 'asdf');
        $this->_em->persist($photo);
        $this->_em->flush();
        $this->_em->clear();

        $dql    = "SELECT IDENTITY(p.poi, 'long') AS long, IDENTITY(p.poi, 'lat') AS lat FROM Doctrine\Tests\Models\Navigation\NavPhotos p";
        $query  = $this->_em->createQuery($dql);
        $result = $query->getResult();

        self::assertCount(1, $result);
        self::assertEquals(200, $result[0]['long']);
        self::assertEquals(100, $result[0]['lat']);

        $this->_em->clear();

        IterableTester::assertResultsAreTheSame($query);
    }

    public function testManyToManyCompositeRelation(): void
    {
        $this->putGermanysBrandenburderTor();
        $tour = $this->putTripAroundEurope();

        $tour = $this->_em->find(NavTour::class, $tour->getId());

        self::assertCount(1, $tour->getPointOfInterests());
    }

    public function testCompositeDqlEagerFetching(): void
    {
        $this->putGermanysBrandenburderTor();
        $this->putTripAroundEurope();

        $dql   = 'SELECT t, p, c FROM Doctrine\Tests\Models\Navigation\NavTour t ' .
               'INNER JOIN t.pois p INNER JOIN p.country c';
        $tours = $this->_em->createQuery($dql)->getResult();

        $query = $this->_em->createQuery($dql);
        $tours = $query->getResult();

        self::assertCount(1, $tours);

        $pois = $tours[0]->getPointOfInterests();

        self::assertCount(1, $pois);
        self::assertEquals('Brandenburger Tor', $pois[0]->getName());
    }

    public function testCompositeCollectionMemberExpression(): void
    {
        self::markTestSkipped('How to test this?');

        $this->putGermanysBrandenburderTor();
        $this->putTripAroundEurope();

        $dql   = 'SELECT t FROM Doctrine\Tests\Models\Navigation\NavTour t, Doctrine\Tests\Models\Navigation\NavPointOfInterest p ' .
               'WHERE p MEMBER OF t.pois';
        $tours = $this->_em->createQuery($dql)
                           ->getResult();

        self::assertCount(1, $tours);

        $this->_em->clear();

        IterableTester::assertResultsAreTheSame($query);
    }

    public function testSpecifyUnknownIdentifierPrimaryKeyFails(): void
    {
        $this->expectException(MissingIdentifierField::class);
        $this->expectExceptionMessage('The identifier long is missing for a query of Doctrine\Tests\Models\Navigation\NavPointOfInterest');

        $poi = $this->_em->find(NavPointOfInterest::class, ['key1' => 100]);
    }

    public function testUnrecognizedIdentifierFieldsOnGetReference(): void
    {
        $this->expectException(UnrecognizedIdentifierFields::class);
        $this->expectExceptionMessage('Unrecognized identifier fields: "key1"');

        $poi = $this->_em->getReference(NavPointOfInterest::class, ['lat' => 10, 'long' => 20, 'key1' => 100]);
    }

    #[Group('DDC-1939')]
    public function testDeleteCompositePersistentCollection(): void
    {
        $this->putGermanysBrandenburderTor();

        $poi = $this->_em->find(NavPointOfInterest::class, ['lat' => 100, 'long' => 200]);
        $poi->addVisitor(new NavUser('test1'));
        $poi->addVisitor(new NavUser('test2'));

        $this->_em->flush();

        $poi->getVisitors()->clear();

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

        $poi = $this->_em->find(NavPointOfInterest::class, ['lat' => 100, 'long' => 200]);
        self::assertCount(0, $poi->getVisitors());
    }
}