File: DDC117Test.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 (449 lines) | stat: -rw-r--r-- 17,933 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\ORM\UnitOfWork;
use Doctrine\Tests\Models\DDC117\DDC117ApproveChanges;
use Doctrine\Tests\Models\DDC117\DDC117Article;
use Doctrine\Tests\Models\DDC117\DDC117ArticleDetails;
use Doctrine\Tests\Models\DDC117\DDC117Editor;
use Doctrine\Tests\Models\DDC117\DDC117Link;
use Doctrine\Tests\Models\DDC117\DDC117Reference;
use Doctrine\Tests\Models\DDC117\DDC117Translation;
use Doctrine\Tests\OrmFunctionalTestCase;
use Exception;
use PHPUnit\Framework\Attributes\Group;

use function assert;
use function count;

#[Group('DDC-117')]
class DDC117Test extends OrmFunctionalTestCase
{
    private DDC117Article|null $article1;

    private DDC117Article|null $article2;

    private DDC117Reference $reference;

    private DDC117Translation|null $translation;

    private DDC117ArticleDetails $articleDetails;

    protected function setUp(): void
    {
        $this->useModelSet('ddc117');

        parent::setUp();

        $this->article1 = new DDC117Article('Foo');
        $this->article2 = new DDC117Article('Bar');

        $this->_em->persist($this->article1);
        $this->_em->persist($this->article2);
        $this->_em->flush();

        $link = new DDC117Link($this->article1, $this->article2, 'Link-Description');
        $this->_em->persist($link);

        $this->reference = new DDC117Reference($this->article1, $this->article2, 'Test-Description');
        $this->_em->persist($this->reference);

        $this->translation = new DDC117Translation($this->article1, 'en', 'Bar');
        $this->_em->persist($this->translation);

        $this->articleDetails = new DDC117ArticleDetails($this->article1, 'Very long text');
        $this->_em->persist($this->articleDetails);
        $this->_em->flush();

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

    #[Group('DDC-117')]
    public function testAssociationOnlyCompositeKey(): void
    {
        $idCriteria = ['source' => $this->article1->id(), 'target' => $this->article2->id()];

        $mapRef = $this->_em->find(DDC117Reference::class, $idCriteria);
        self::assertInstanceOf(DDC117Reference::class, $mapRef);
        self::assertInstanceOf(DDC117Article::class, $mapRef->target());
        self::assertInstanceOf(DDC117Article::class, $mapRef->source());
        self::assertSame($mapRef, $this->_em->find(DDC117Reference::class, $idCriteria));

        $this->_em->clear();

        $dql    = 'SELECT r, s FROM ' . DDC117Reference::class . ' r JOIN r.source s WHERE r.source = ?1';
        $dqlRef = $this->_em->createQuery($dql)->setParameter(1, 1)->getSingleResult();

        self::assertInstanceOf(DDC117Reference::class, $mapRef);
        self::assertInstanceOf(DDC117Article::class, $mapRef->target());
        self::assertInstanceOf(DDC117Article::class, $mapRef->source());
        self::assertSame($dqlRef, $this->_em->find(DDC117Reference::class, $idCriteria));

        $this->_em->clear();

        $dql    = 'SELECT r, s FROM ' . DDC117Reference::class . ' r JOIN r.source s WHERE s.title = ?1';
        $dqlRef = $this->_em->createQuery($dql)->setParameter(1, 'Foo')->getSingleResult();

        self::assertInstanceOf(DDC117Reference::class, $dqlRef);
        self::assertInstanceOf(DDC117Article::class, $dqlRef->target());
        self::assertInstanceOf(DDC117Article::class, $dqlRef->source());
        self::assertSame($dqlRef, $this->_em->find(DDC117Reference::class, $idCriteria));

        $dql    = 'SELECT r, s FROM ' . DDC117Reference::class . ' r JOIN r.source s WHERE s.title = ?1';
        $dqlRef = $this->_em->createQuery($dql)->setParameter(1, 'Foo')->getSingleResult();

        $this->_em->contains($dqlRef);
    }

    #[Group('DDC-117')]
    public function testUpdateAssociationEntity(): void
    {
        $idCriteria = ['source' => $this->article1->id(), 'target' => $this->article2->id()];

        $mapRef = $this->_em->find(DDC117Reference::class, $idCriteria);
        self::assertNotNull($mapRef);
        $mapRef->setDescription('New Description!!');
        $this->_em->flush();
        $this->_em->clear();

        $mapRef = $this->_em->find(DDC117Reference::class, $idCriteria);

        self::assertEquals('New Description!!', $mapRef->getDescription());
    }

    #[Group('DDC-117')]
    public function testFetchDql(): void
    {
        $dql  = 'SELECT r, s FROM Doctrine\Tests\Models\DDC117\DDC117Reference r JOIN r.source s WHERE s.title = ?1';
        $refs = $this->_em->createQuery($dql)->setParameter(1, 'Foo')->getResult();

        self::assertGreaterThan(0, $refs, 'Has to contain at least one Reference.');

        foreach ($refs as $ref) {
            self::assertInstanceOf(DDC117Reference::class, $ref, 'Contains only Reference instances.');
            self::assertTrue($this->_em->contains($ref), 'Contains Reference in the IdentityMap.');
        }
    }

    #[Group('DDC-117')]
    public function testRemoveCompositeElement(): void
    {
        $idCriteria = ['source' => $this->article1->id(), 'target' => $this->article2->id()];

        $refRep = $this->_em->find(DDC117Reference::class, $idCriteria);

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

        self::assertNull($this->_em->find(DDC117Reference::class, $idCriteria));
    }

    #[Group('DDC-117')]
    #[Group('non-cacheable')]
    public function testDqlRemoveCompositeElement(): void
    {
        $idCriteria = ['source' => $this->article1->id(), 'target' => $this->article2->id()];

        $dql = 'DELETE Doctrine\Tests\Models\DDC117\DDC117Reference r WHERE r.source = ?1 AND r.target = ?2';
        $this->_em->createQuery($dql)
                  ->setParameter(1, $this->article1->id())
                  ->setParameter(2, $this->article2->id())
                  ->execute();

        self::assertNull($this->_em->find(DDC117Reference::class, $idCriteria));
    }

    #[Group('DDC-117')]
    public function testInverseSideAccess(): void
    {
        $this->article1 = $this->_em->find(DDC117Article::class, $this->article1->id());

        self::assertCount(1, $this->article1->references());

        foreach ($this->article1->references() as $this->reference) {
            self::assertInstanceOf(DDC117Reference::class, $this->reference);
            self::assertSame($this->article1, $this->reference->source());
        }

        $this->_em->clear();

        $dql        = 'SELECT a, r FROM Doctrine\Tests\Models\DDC117\DDC117Article a INNER JOIN a.references r WHERE a.id = ?1';
        $articleDql = $this->_em->createQuery($dql)
                                ->setParameter(1, $this->article1->id())
                                ->getSingleResult();

        self::assertCount(1, $this->article1->references());

        foreach ($this->article1->references() as $this->reference) {
            self::assertInstanceOf(DDC117Reference::class, $this->reference);
            self::assertSame($this->article1, $this->reference->source());
        }
    }

    #[Group('DDC-117')]
    public function testMixedCompositeKey(): void
    {
        $idCriteria = ['article' => $this->article1->id(), 'language' => 'en'];

        $this->translation = $this->_em->find(DDC117Translation::class, $idCriteria);
        self::assertInstanceOf(DDC117Translation::class, $this->translation);

        self::assertSame($this->translation, $this->_em->find(DDC117Translation::class, $idCriteria));

        $this->_em->clear();

        $dql      = 'SELECT t, a FROM Doctrine\Tests\Models\DDC117\DDC117Translation t JOIN t.article a WHERE t.article = ?1 AND t.language = ?2';
        $dqlTrans = $this->_em->createQuery($dql)
                              ->setParameter(1, $this->article1->id())
                              ->setParameter(2, 'en')
                              ->getSingleResult();

        self::assertInstanceOf(DDC117Translation::class, $this->translation);
    }

    #[Group('DDC-117')]
    public function testMixedCompositeKeyViolateUniqueness(): void
    {
        $this->article1 = $this->_em->find(DDC117Article::class, $this->article1->id());
        $this->article1->addTranslation('en', 'Bar');
        $this->article1->addTranslation('en', 'Baz');

        // exception depending on the underlying Database Driver
        $this->expectException(Exception::class);
        $this->_em->flush();
    }

    #[Group('DDC-117')]
    public function testOneToOneForeignObjectId(): void
    {
        $this->article1 = new DDC117Article('Foo');
        $this->_em->persist($this->article1);
        $this->_em->flush();

        $this->articleDetails = new DDC117ArticleDetails($this->article1, 'Very long text');
        $this->_em->persist($this->articleDetails);
        $this->_em->flush();

        $this->articleDetails->update('not so very long text!');
        $this->_em->flush();
        $this->_em->clear();

        $article = $this->_em->find($this->article1::class, $this->article1->id());
        assert($article instanceof DDC117Article);
        self::assertEquals('not so very long text!', $article->getText());
    }

    #[Group('DDC-117')]
    public function testOneToOneCascadeRemove(): void
    {
        $article = $this->_em->find($this->article1::class, $this->article1->id());
        $this->_em->remove($article);
        $this->_em->flush();

        self::assertFalse($this->_em->contains($article->getDetails()));
    }

    #[Group('DDC-117')]
    public function testOneToOneCascadePersist(): void
    {
        $this->article1       = new DDC117Article('Foo');
        $this->articleDetails = new DDC117ArticleDetails($this->article1, 'Very long text');

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

        self::assertSame($this->articleDetails, $this->_em->find(DDC117ArticleDetails::class, $this->article1));
    }

    #[Group('DDC-117')]
    public function testReferencesToForeignKeyEntities(): void
    {
        $idCriteria = ['source' => $this->article1->id(), 'target' => $this->article2->id()];
        $reference  = $this->_em->find(DDC117Reference::class, $idCriteria);

        $idCriteria  = ['article' => $this->article1->id(), 'language' => 'en'];
        $translation = $this->_em->find(DDC117Translation::class, $idCriteria);

        $approveChanges = new DDC117ApproveChanges($reference->source()->getDetails(), $reference, $translation);
        $this->_em->persist($approveChanges);
        $this->_em->flush();
        $this->_em->clear();

        $approveChanges = $this->_em->find(DDC117ApproveChanges::class, $approveChanges->getId());

        self::assertInstanceOf(DDC117ArticleDetails::class, $approveChanges->getArticleDetails());
        self::assertInstanceOf(DDC117Reference::class, $approveChanges->getReference());
        self::assertInstanceOf(DDC117Translation::class, $approveChanges->getTranslation());
    }

    #[Group('DDC-117')]
    public function testLoadOneToManyCollectionOfForeignKeyEntities(): void
    {
        $article = $this->_em->find($this->article1::class, $this->article1->id());
        assert($article instanceof DDC117Article);

        $translations = $article->getTranslations();
        self::assertFalse($translations->isInitialized());
        self::assertContainsOnlyInstancesOf(DDC117Translation::class, $translations);
        self::assertTrue($translations->isInitialized());
    }

    #[Group('DDC-117')]
    public function testLoadManyToManyCollectionOfForeignKeyEntities(): void
    {
        $editor = $this->loadEditorFixture();

        self::assertFalse($editor->reviewingTranslations->isInitialized());
        self::assertContainsOnlyInstancesOf(DDC117Translation::class, $editor->reviewingTranslations);
        self::assertTrue($editor->reviewingTranslations->isInitialized());

        $this->_em->clear();

        $dql    = 'SELECT e, t FROM Doctrine\Tests\Models\DDC117\DDC117Editor e JOIN e.reviewingTranslations t WHERE e.id = ?1';
        $editor = $this->_em->createQuery($dql)->setParameter(1, $editor->id)->getSingleResult();
        self::assertTrue($editor->reviewingTranslations->isInitialized());
        self::assertContainsOnlyInstancesOf(DDC117Translation::class, $editor->reviewingTranslations);
    }

    #[Group('DDC-117')]
    public function testClearManyToManyCollectionOfForeignKeyEntities(): void
    {
        $editor = $this->loadEditorFixture();
        self::assertCount(3, $editor->reviewingTranslations);

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

        $editor = $this->_em->find($editor::class, $editor->id);
        self::assertCount(0, $editor->reviewingTranslations);
    }

    #[Group('DDC-117')]
    public function testLoadInverseManyToManyCollection(): void
    {
        $editor = $this->loadEditorFixture();

        self::assertInstanceOf(DDC117Translation::class, $editor->reviewingTranslations[0]);

        $reviewedBy = $editor->reviewingTranslations[0]->getReviewedByEditors();
        self::assertCount(1, $reviewedBy);
        self::assertSame($editor, $reviewedBy[0]);

        $this->_em->clear();

        $dql   = 'SELECT t, e FROM Doctrine\Tests\Models\DDC117\DDC117Translation t ' .
               'JOIN t.reviewedByEditors e WHERE t.article = ?1 AND t.language = ?2';
        $trans = $this->_em->createQuery($dql)
                           ->setParameter(1, $this->translation->getArticleId())
                           ->setParameter(2, $this->translation->getLanguage())
                           ->getSingleResult();

        self::assertInstanceOf(DDC117Translation::class, $trans);
        self::assertContainsOnlyInstancesOf(DDC117Editor::class, $trans->reviewedByEditors);
        self::assertCount(1, $trans->reviewedByEditors);
    }

    #[Group('DDC-117')]
    public function testLoadOneToManyOfSourceEntityWithAssociationIdentifier(): void
    {
        $editor = $this->loadEditorFixture();

        $editor->addLastTranslation($editor->reviewingTranslations[0]);
        $this->_em->flush();
        $this->_em->clear();

        $editor           = $this->_em->find($editor::class, $editor->id);
        $lastTranslatedBy = $editor->reviewingTranslations[0]->getLastTranslatedBy();
        $lastTranslatedBy->count();

        self::assertCount(1, $lastTranslatedBy);
    }

    private function loadEditorFixture(): DDC117Editor
    {
        $editor = new DDC117Editor('beberlei');

        $article1 = $this->_em->find($this->article1::class, $this->article1->id());
        assert($article1 instanceof DDC117Article);
        foreach ($article1->getTranslations() as $translation) {
            $editor->reviewingTranslations[] = $translation;
        }

        $article2 = $this->_em->find($this->article2::class, $this->article2->id());
        assert($article2 instanceof DDC117Article);
        $article2->addTranslation('de', 'Vanille-Krapferl'); // omnomnom
        $article2->addTranslation('fr', "Sorry can't speak french!");

        foreach ($article2->getTranslations() as $translation) {
            $this->_em->persist($translation); // otherwise persisting the editor won't work, reachability!
            $editor->reviewingTranslations[] = $translation;
        }

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

        return $this->_em->find($editor::class, $editor->id);
    }

    #[Group('DDC-1652')]
    public function testArrayHydrationWithCompositeKey(): void
    {
        $dql    = 'SELECT r,s,t FROM Doctrine\Tests\Models\DDC117\DDC117Reference r INNER JOIN r.source s INNER JOIN r.target t';
        $before = count($this->_em->createQuery($dql)->getResult());

        $this->article1 = $this->_em->find(DDC117Article::class, $this->article1->id());
        $this->article2 = $this->_em->find(DDC117Article::class, $this->article2->id());

        $this->reference = new DDC117Reference($this->article2, $this->article1, 'Test-Description');
        $this->_em->persist($this->reference);

        $this->reference = new DDC117Reference($this->article1, $this->article1, 'Test-Description');
        $this->_em->persist($this->reference);

        $this->reference = new DDC117Reference($this->article2, $this->article2, 'Test-Description');
        $this->_em->persist($this->reference);

        $this->_em->flush();

        $dql  = 'SELECT r,s,t FROM Doctrine\Tests\Models\DDC117\DDC117Reference r INNER JOIN r.source s INNER JOIN r.target t';
        $data = $this->_em->createQuery($dql)->getArrayResult();

        self::assertCount($before + 3, $data);
    }

    #[Group('DDC-2246')]
    public function testGetEntityState(): void
    {
        if ($this->isSecondLevelCacheEnabled) {
            self::markTestIncomplete('Second level cache - not supported yet');
        }

        $this->article1 = $this->_em->find(DDC117Article::class, $this->article1->id());
        $this->article2 = $this->_em->find(DDC117Article::class, $this->article2->id());

        $this->reference = new DDC117Reference($this->article2, $this->article1, 'Test-Description');

        self::assertEquals(UnitOfWork::STATE_NEW, $this->_em->getUnitOfWork()->getEntityState($this->reference));

        $idCriteria = ['source' => $this->article1->id(), 'target' => $this->article2->id()];
        $reference  = $this->_em->find(DDC117Reference::class, $idCriteria);

        self::assertEquals(UnitOfWork::STATE_MANAGED, $this->_em->getUnitOfWork()->getEntityState($reference));
    }

    #[Group('DDC-117')]
    public function testIndexByOnCompositeKeyField(): void
    {
        $article = $this->_em->find(DDC117Article::class, $this->article1->id());

        self::assertInstanceOf(DDC117Article::class, $article);
        self::assertCount(1, $article->getLinks());
        self::assertTrue($article->getLinks()->offsetExists($this->article2->id()));
    }
}