Failed Conditions
Push — master ( 6744b4...2b8acb )
by Marco
60:45 queued 60:36
created

Tests/ORM/Functional/Ticket/DDC117Test.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Doctrine\Tests\ORM\Functional\Ticket;
4
5
use Doctrine\Tests\Models\DDC117\DDC117ArticleDetails;
6
use Doctrine\Tests\Models\DDC117\DDC117Article;
7
use Doctrine\Tests\Models\DDC117\DDC117Reference;
8
use Doctrine\Tests\Models\DDC117\DDC117Translation;
9
use Doctrine\Tests\Models\DDC117\DDC117ApproveChanges;
10
use Doctrine\Tests\Models\DDC117\DDC117Editor;
11
use Doctrine\Tests\Models\DDC117\DDC117Link;
12
13
/**
14
 * @group DDC-117
15
 */
16
class DDC117Test extends \Doctrine\Tests\OrmFunctionalTestCase
17
{
18
    private $article1;
19
    private $article2;
20
    private $reference;
21
    private $translation;
22
    private $articleDetails;
23
24
    protected function setUp()
25
    {
26
        $this->useModelSet('ddc117');
27
        parent::setUp();
28
29
        $this->article1 = new DDC117Article("Foo");
30
        $this->article2 = new DDC117Article("Bar");
31
32
        $this->_em->persist($this->article1);
33
        $this->_em->persist($this->article2);
34
        $this->_em->flush();
35
36
        $link = new DDC117Link($this->article1, $this->article2, "Link-Description");
37
        $this->_em->persist($link);
38
39
        $this->reference = new DDC117Reference($this->article1, $this->article2, "Test-Description");
40
        $this->_em->persist($this->reference);
41
42
        $this->translation = new DDC117Translation($this->article1, "en", "Bar");
43
        $this->_em->persist($this->translation);
44
45
        $this->articleDetails = new DDC117ArticleDetails($this->article1, "Very long text");
46
        $this->_em->persist($this->articleDetails);
47
        $this->_em->flush();
48
49
        $this->_em->clear();
50
    }
51
52
    /**
53
     * @group DDC-117
54
     */
55
    public function testAssociationOnlyCompositeKey()
56
    {
57
        $idCriteria = ['source' => $this->article1->id(), 'target' => $this->article2->id()];
58
59
        $mapRef = $this->_em->find(DDC117Reference::class, $idCriteria);
60
        $this->assertInstanceOf(DDC117Reference::class, $mapRef);
61
        $this->assertInstanceOf(DDC117Article::class, $mapRef->target());
62
        $this->assertInstanceOf(DDC117Article::class, $mapRef->source());
63
        $this->assertSame($mapRef, $this->_em->find(DDC117Reference::class, $idCriteria));
64
65
        $this->_em->clear();
66
67
        $dql = 'SELECT r, s FROM ' . DDC117Reference::class . ' r JOIN r.source s WHERE r.source = ?1';
68
        $dqlRef = $this->_em->createQuery($dql)->setParameter(1, 1)->getSingleResult();
69
70
        $this->assertInstanceOf(DDC117Reference::class, $mapRef);
71
        $this->assertInstanceOf(DDC117Article::class, $mapRef->target());
72
        $this->assertInstanceOf(DDC117Article::class, $mapRef->source());
73
        $this->assertSame($dqlRef, $this->_em->find(DDC117Reference::class, $idCriteria));
74
75
        $this->_em->clear();
76
77
        $dql = 'SELECT r, s FROM ' . DDC117Reference::class . ' r JOIN r.source s WHERE s.title = ?1';
78
        $dqlRef = $this->_em->createQuery($dql)->setParameter(1, 'Foo')->getSingleResult();
79
80
        $this->assertInstanceOf(DDC117Reference::class, $dqlRef);
81
        $this->assertInstanceOf(DDC117Article::class, $dqlRef->target());
82
        $this->assertInstanceOf(DDC117Article::class, $dqlRef->source());
83
        $this->assertSame($dqlRef, $this->_em->find(DDC117Reference::class, $idCriteria));
84
85
        $dql = 'SELECT r, s FROM ' . DDC117Reference::class . ' r JOIN r.source s WHERE s.title = ?1';
86
        $dqlRef = $this->_em->createQuery($dql)->setParameter(1, 'Foo')->getSingleResult();
87
88
        $this->_em->contains($dqlRef);
89
    }
90
91
    /**
92
     * @group DDC-117
93
     */
94
    public function testUpdateAssociationEntity()
95
    {
96
        $idCriteria = ['source' => $this->article1->id(), 'target' => $this->article2->id()];
97
98
        $mapRef = $this->_em->find(DDC117Reference::class, $idCriteria);
99
        $this->assertNotNull($mapRef);
100
        $mapRef->setDescription("New Description!!");
101
        $this->_em->flush();
102
        $this->_em->clear();
103
104
        $mapRef = $this->_em->find(DDC117Reference::class, $idCriteria);
105
106
        $this->assertEquals('New Description!!', $mapRef->getDescription());
107
    }
108
109
    /**
110
     * @group DDC-117
111
     */
112
    public function testFetchDql()
113
    {
114
        $dql = "SELECT r, s FROM Doctrine\Tests\Models\DDC117\DDC117Reference r JOIN r.source s WHERE s.title = ?1";
115
        $refs = $this->_em->createQuery($dql)->setParameter(1, 'Foo')->getResult();
116
117
        $this->assertTrue(count($refs) > 0, "Has to contain at least one Reference.");
118
119
        foreach ($refs AS $ref) {
120
            $this->assertInstanceOf(DDC117Reference::class, $ref, "Contains only Reference instances.");
121
            $this->assertTrue($this->_em->contains($ref), "Contains Reference in the IdentityMap.");
122
        }
123
    }
124
125
    /**
126
     * @group DDC-117
127
     */
128
    public function testRemoveCompositeElement()
129
    {
130
        $idCriteria = ['source' => $this->article1->id(), 'target' => $this->article2->id()];
131
132
        $refRep = $this->_em->find(DDC117Reference::class, $idCriteria);
133
134
        $this->_em->remove($refRep);
135
        $this->_em->flush();
136
        $this->_em->clear();
137
138
        $this->assertNull($this->_em->find(DDC117Reference::class, $idCriteria));
139
    }
140
141
    /**
142
     * @group DDC-117
143
     * @group non-cacheable
144
     */
145
    public function testDqlRemoveCompositeElement()
146
    {
147
        $idCriteria = ['source' => $this->article1->id(), 'target' => $this->article2->id()];
148
149
        $dql = "DELETE Doctrine\Tests\Models\DDC117\DDC117Reference r WHERE r.source = ?1 AND r.target = ?2";
150
        $this->_em->createQuery($dql)
151
                  ->setParameter(1, $this->article1->id())
152
                  ->setParameter(2, $this->article2->id())
153
                  ->execute();
154
155
        $this->assertNull($this->_em->find(DDC117Reference::class, $idCriteria));
156
    }
157
158
    /**
159
     * @group DDC-117
160
     */
161
    public function testInverseSideAccess()
162
    {
163
        $this->article1 = $this->_em->find(DDC117Article::class, $this->article1->id());
164
165
        $this->assertEquals(1, count($this->article1->references()));
166
167 View Code Duplication
        foreach ($this->article1->references() AS $this->reference) {
168
            $this->assertInstanceOf(DDC117Reference::class, $this->reference);
169
            $this->assertSame($this->article1, $this->reference->source());
170
        }
171
172
        $this->_em->clear();
173
174
        $dql = 'SELECT a, r FROM Doctrine\Tests\Models\DDC117\DDC117Article a INNER JOIN a.references r WHERE a.id = ?1';
175
        $articleDql = $this->_em->createQuery($dql)
176
                                ->setParameter(1, $this->article1->id())
177
                                ->getSingleResult();
178
179
        $this->assertEquals(1, count($this->article1->references()));
180
181 View Code Duplication
        foreach ($this->article1->references() AS $this->reference) {
182
            $this->assertInstanceOf(DDC117Reference::class, $this->reference);
183
            $this->assertSame($this->article1, $this->reference->source());
184
        }
185
    }
186
187
    /**
188
     * @group DDC-117
189
     */
190
    public function testMixedCompositeKey()
191
    {
192
        $idCriteria = ['article' => $this->article1->id(), 'language' => 'en'];
193
194
        $this->translation = $this->_em->find(DDC117Translation::class, $idCriteria);
195
        $this->assertInstanceOf(DDC117Translation::class, $this->translation);
196
197
        $this->assertSame($this->translation, $this->_em->find(DDC117Translation::class, $idCriteria));
198
199
        $this->_em->clear();
200
201
        $dql = 'SELECT t, a FROM Doctrine\Tests\Models\DDC117\DDC117Translation t JOIN t.article a WHERE t.article = ?1 AND t.language = ?2';
202
        $dqlTrans = $this->_em->createQuery($dql)
203
                              ->setParameter(1, $this->article1->id())
204
                              ->setParameter(2, 'en')
205
                              ->getSingleResult();
206
207
        $this->assertInstanceOf(DDC117Translation::class, $this->translation);
208
    }
209
210
    /**
211
     * @group DDC-117
212
     */
213
    public function testMixedCompositeKeyViolateUniqueness()
214
    {
215
        $this->article1 = $this->_em->find(DDC117Article::class, $this->article1->id());
216
        $this->article1->addTranslation('en', 'Bar');
217
        $this->article1->addTranslation('en', 'Baz');
218
219
        $exceptionThrown = false;
220
        try {
221
            // exception depending on the underlying Database Driver
222
            $this->_em->flush();
223
        } catch(\Exception $e) {
224
            $exceptionThrown = true;
225
        }
226
227
        $this->assertTrue($exceptionThrown, "The underlying database driver throws an exception.");
228
    }
229
230
    /**
231
     * @group DDC-117
232
     */
233
    public function testOneToOneForeignObjectId()
234
    {
235
        $this->article1 = new DDC117Article("Foo");
236
        $this->_em->persist($this->article1);
237
        $this->_em->flush();
238
239
        $this->articleDetails = new DDC117ArticleDetails($this->article1, "Very long text");
240
        $this->_em->persist($this->articleDetails);
241
        $this->_em->flush();
242
243
        $this->articleDetails->update("not so very long text!");
244
        $this->_em->flush();
245
        $this->_em->clear();
246
247
        /* @var $article DDC117Article */
248
        $article = $this->_em->find(get_class($this->article1), $this->article1->id());
249
        $this->assertEquals('not so very long text!', $article->getText());
250
    }
251
252
    /**
253
     * @group DDC-117
254
     */
255
    public function testOneToOneCascadeRemove()
256
    {
257
        $article = $this->_em->find(get_class($this->article1), $this->article1->id());
258
        $this->_em->remove($article);
259
        $this->_em->flush();
260
261
        $this->assertFalse($this->_em->contains($article->getDetails()));
262
    }
263
264
    /**
265
     * @group DDC-117
266
     */
267
    public function testOneToOneCascadePersist()
268
    {
269
        if ( ! $this->_em->getConnection()->getDatabasePlatform()->prefersSequences()) {
270
            $this->markTestSkipped('Test only works with databases that prefer sequences as ID strategy.');
271
        }
272
273
        $this->article1 = new DDC117Article("Foo");
274
        $this->articleDetails = new DDC117ArticleDetails($this->article1, "Very long text");
275
276
        $this->_em->persist($this->article1);
277
        $this->_em->flush();
278
279
        self::assertSame($this->articleDetails, $this->_em->find(DDC117ArticleDetails::class, $this->article1));
280
    }
281
282
    /**
283
     * @group DDC-117
284
     */
285
    public function testReferencesToForeignKeyEntities()
286
    {
287
        $idCriteria = ['source' => $this->article1->id(), 'target' => $this->article2->id()];
288
        $reference = $this->_em->find(DDC117Reference::class, $idCriteria);
289
290
        $idCriteria = ['article' => $this->article1->id(), 'language' => 'en'];
291
        $translation = $this->_em->find(DDC117Translation::class, $idCriteria);
292
293
        $approveChanges = new DDC117ApproveChanges($reference->source()->getDetails(), $reference, $translation);
294
        $this->_em->persist($approveChanges);
295
        $this->_em->flush();
296
        $this->_em->clear();
297
298
        $approveChanges = $this->_em->find(DDC117ApproveChanges::class, $approveChanges->getId());
299
300
        $this->assertInstanceOf(DDC117ArticleDetails::class, $approveChanges->getArticleDetails());
301
        $this->assertInstanceOf(DDC117Reference::class, $approveChanges->getReference());
302
        $this->assertInstanceOf(DDC117Translation::class, $approveChanges->getTranslation());
303
    }
304
305
    /**
306
     * @group DDC-117
307
     */
308
    public function testLoadOneToManyCollectionOfForeignKeyEntities()
309
    {
310
        /* @var $article DDC117Article */
311
        $article = $this->_em->find(get_class($this->article1), $this->article1->id());
312
313
        $translations = $article->getTranslations();
314
        $this->assertFalse($translations->isInitialized());
315
        $this->assertContainsOnly(DDC117Translation::class, $translations);
316
        $this->assertTrue($translations->isInitialized());
317
    }
318
319
    /**
320
     * @group DDC-117
321
     */
322
    public function testLoadManyToManyCollectionOfForeignKeyEntities()
323
    {
324
        $editor = $this->loadEditorFixture();
325
326
        $this->assertFalse($editor->reviewingTranslations->isInitialized());
327
        $this->assertContainsOnly(DDC117Translation::class, $editor->reviewingTranslations);
328
        $this->assertTrue($editor->reviewingTranslations->isInitialized());
0 ignored issues
show
The method isInitialized() does not seem to exist on object<Doctrine\Common\C...ctions\ArrayCollection>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
329
330
        $this->_em->clear();
331
332
        $dql = "SELECT e, t FROM Doctrine\Tests\Models\DDC117\DDC117Editor e JOIN e.reviewingTranslations t WHERE e.id = ?1";
333
        $editor = $this->_em->createQuery($dql)->setParameter(1, $editor->id)->getSingleResult();
334
        $this->assertTrue($editor->reviewingTranslations->isInitialized());
335
        $this->assertContainsOnly(DDC117Translation::class, $editor->reviewingTranslations);
336
    }
337
338
    /**
339
     * @group DDC-117
340
     */
341
    public function testClearManyToManyCollectionOfForeignKeyEntities()
342
    {
343
        $editor = $this->loadEditorFixture();
344
        $this->assertEquals(3, count($editor->reviewingTranslations));
345
346
        $editor->reviewingTranslations->clear();
347
        $this->_em->flush();
348
        $this->_em->clear();
349
350
        $editor = $this->_em->find(get_class($editor), $editor->id);
351
        $this->assertEquals(0, count($editor->reviewingTranslations));
352
    }
353
354
    /**
355
     * @group DDC-117
356
     */
357
    public function testLoadInverseManyToManyCollection()
358
    {
359
        $editor = $this->loadEditorFixture();
360
361
        $this->assertInstanceOf(DDC117Translation::class, $editor->reviewingTranslations[0]);
362
363
        $reviewedBy = $editor->reviewingTranslations[0]->getReviewedByEditors();
364
        $this->assertEquals(1, count($reviewedBy));
365
        $this->assertSame($editor, $reviewedBy[0]);
366
367
        $this->_em->clear();
368
369
        $dql = "SELECT t, e FROM Doctrine\Tests\Models\DDC117\DDC117Translation t ".
370
               "JOIN t.reviewedByEditors e WHERE t.article = ?1 AND t.language = ?2";
371
        $trans = $this->_em->createQuery($dql)
372
                           ->setParameter(1, $this->translation->getArticleId())
373
                           ->setParameter(2, $this->translation->getLanguage())
374
                           ->getSingleResult();
375
376
        $this->assertInstanceOf(DDC117Translation::class, $trans);
377
        $this->assertContainsOnly(DDC117Editor::class, $trans->reviewedByEditors);
378
        $this->assertEquals(1, count($trans->reviewedByEditors));
379
    }
380
381
    /**
382
     * @group DDC-117
383
     */
384
    public function testLoadOneToManyOfSourceEntityWithAssociationIdentifier()
385
    {
386
        $editor = $this->loadEditorFixture();
387
388
        $editor->addLastTranslation($editor->reviewingTranslations[0]);
389
        $this->_em->flush();
390
        $this->_em->clear();
391
392
        $editor = $this->_em->find(get_class($editor), $editor->id);
393
        $lastTranslatedBy = $editor->reviewingTranslations[0]->getLastTranslatedBy();
394
        $lastTranslatedBy->count();
395
396
        $this->assertEquals(1, count($lastTranslatedBy));
397
    }
398
399
    /**
400
     * @return DDC117Editor
401
     */
402
    private function loadEditorFixture()
403
    {
404
        $editor = new DDC117Editor("beberlei");
405
406
        /* @var $article1 DDC117Article */
407
        $article1 = $this->_em->find(get_class($this->article1), $this->article1->id());
408
        foreach ($article1->getTranslations() AS $translation) {
409
            $editor->reviewingTranslations[] = $translation;
410
        }
411
412
        /* @var $article2 DDC117Article */
413
        $article2 = $this->_em->find(get_class($this->article2), $this->article2->id());
414
        $article2->addTranslation("de", "Vanille-Krapferl"); // omnomnom
415
        $article2->addTranslation("fr", "Sorry can't speak french!");
416
417
        foreach ($article2->getTranslations() AS $translation) {
418
            $this->_em->persist($translation); // otherwise persisting the editor won't work, reachability!
419
            $editor->reviewingTranslations[] = $translation;
420
        }
421
422
        $this->_em->persist($editor);
423
        $this->_em->flush();
424
        $this->_em->clear();
425
426
        return $this->_em->find(get_class($editor), $editor->id);
427
    }
428
429
    /**
430
     * @group DDC-1519
431
     */
432
    public function testMergeForeignKeyIdentifierEntity()
433
    {
434
        $idCriteria = ['source' => $this->article1->id(), 'target' => $this->article2->id()];
435
436
        $refRep = $this->_em->find(DDC117Reference::class, $idCriteria);
437
438
        $this->_em->detach($refRep);
439
        $refRep = $this->_em->merge($refRep);
440
441
        $this->assertEquals($this->article1->id(), $refRep->source()->id());
442
        $this->assertEquals($this->article2->id(), $refRep->target()->id());
443
    }
444
445
    /**
446
     * @group DDC-1652
447
     */
448
    public function testArrayHydrationWithCompositeKey()
449
    {
450
        $dql = "SELECT r,s,t FROM Doctrine\Tests\Models\DDC117\DDC117Reference r INNER JOIN r.source s INNER JOIN r.target t";
451
        $before = count($this->_em->createQuery($dql)->getResult());
452
453
        $this->article1 = $this->_em->find(DDC117Article::class, $this->article1->id());
454
        $this->article2 = $this->_em->find(DDC117Article::class, $this->article2->id());
455
456
        $this->reference = new DDC117Reference($this->article2, $this->article1, "Test-Description");
457
        $this->_em->persist($this->reference);
458
459
        $this->reference = new DDC117Reference($this->article1, $this->article1, "Test-Description");
460
        $this->_em->persist($this->reference);
461
462
        $this->reference = new DDC117Reference($this->article2, $this->article2, "Test-Description");
463
        $this->_em->persist($this->reference);
464
465
        $this->_em->flush();
466
467
        $dql = "SELECT r,s,t FROM Doctrine\Tests\Models\DDC117\DDC117Reference r INNER JOIN r.source s INNER JOIN r.target t";
468
        $data = $this->_em->createQuery($dql)->getArrayResult();
469
470
        $this->assertEquals($before + 3, count($data));
471
    }
472
473
    /**
474
     * @group DDC-2246
475
     */
476
    public function testGetEntityState()
477
    {
478
        if ($this->isSecondLevelCacheEnabled) {
479
            $this->markTestIncomplete('Second level cache - not supported yet');
480
        }
481
482
        $this->article1 = $this->_em->find(DDC117Article::class, $this->article1->id());
483
        $this->article2 = $this->_em->find(DDC117Article::class, $this->article2->id());
484
485
        $this->reference = new DDC117Reference($this->article2, $this->article1, "Test-Description");
486
487
        $this->assertEquals(\Doctrine\ORM\UnitOfWork::STATE_NEW, $this->_em->getUnitOfWork()->getEntityState($this->reference));
488
489
        $idCriteria = ['source' => $this->article1->id(), 'target' => $this->article2->id()];
490
        $reference = $this->_em->find(DDC117Reference::class, $idCriteria);
491
492
        $this->assertEquals(\Doctrine\ORM\UnitOfWork::STATE_MANAGED, $this->_em->getUnitOfWork()->getEntityState($reference));
493
    }
494
    /**
495
     * @group DDC-117
496
     */
497
    public function testIndexByOnCompositeKeyField()
498
    {
499
        $article = $this->_em->find(DDC117Article::class, $this->article1->id());
500
501
        $this->assertInstanceOf(DDC117Article::class, $article);
502
        $this->assertEquals(1, count($article->getLinks()));
503
        $this->assertTrue($article->getLinks()->offsetExists($this->article2->id()));
504
    }
505
}
506