/
DDC117Test.php
503 lines (393 loc) · 19.5 KB
/
DDC117Test.php
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
<?php
namespace Doctrine\Tests\ORM\Functional\Ticket;
use Doctrine\Tests\Models\DDC117\DDC117ArticleDetails;
use Doctrine\Tests\Models\DDC117\DDC117Article;
use Doctrine\Tests\Models\DDC117\DDC117Reference;
use Doctrine\Tests\Models\DDC117\DDC117Translation;
use Doctrine\Tests\Models\DDC117\DDC117ApproveChanges;
use Doctrine\Tests\Models\DDC117\DDC117Editor;
use Doctrine\Tests\Models\DDC117\DDC117Link;
/**
* @group DDC-117
*/
class DDC117Test extends \Doctrine\Tests\OrmFunctionalTestCase
{
private $article1;
private $article2;
private $reference;
private $translation;
private $articleDetails;
protected function setUp() {
$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()
{
$idCriteria = array('source' => $this->article1->id(), 'target' => $this->article2->id());
$mapRef = $this->_em->find("Doctrine\Tests\Models\DDC117\DDC117Reference", $idCriteria);
$this->assertInstanceOf("Doctrine\Tests\Models\DDC117\DDC117Reference", $mapRef);
$this->assertInstanceOf("Doctrine\Tests\Models\DDC117\DDC117Article", $mapRef->target());
$this->assertInstanceOf("Doctrine\Tests\Models\DDC117\DDC117Article", $mapRef->source());
$this->assertSame($mapRef, $this->_em->find("Doctrine\Tests\Models\DDC117\DDC117Reference", $idCriteria));
$this->_em->clear();
$dql = "SELECT r, s FROM "."Doctrine\Tests\Models\DDC117\DDC117Reference r JOIN r.source s WHERE r.source = ?1";
$dqlRef = $this->_em->createQuery($dql)->setParameter(1, 1)->getSingleResult();
$this->assertInstanceOf("Doctrine\Tests\Models\DDC117\DDC117Reference", $mapRef);
$this->assertInstanceOf("Doctrine\Tests\Models\DDC117\DDC117Article", $mapRef->target());
$this->assertInstanceOf("Doctrine\Tests\Models\DDC117\DDC117Article", $mapRef->source());
$this->assertSame($dqlRef, $this->_em->find("Doctrine\Tests\Models\DDC117\DDC117Reference", $idCriteria));
$this->_em->clear();
$dql = "SELECT r, s FROM "."Doctrine\Tests\Models\DDC117\DDC117Reference r JOIN r.source s WHERE s.title = ?1";
$dqlRef = $this->_em->createQuery($dql)->setParameter(1, 'Foo')->getSingleResult();
$this->assertInstanceOf("Doctrine\Tests\Models\DDC117\DDC117Reference", $dqlRef);
$this->assertInstanceOf("Doctrine\Tests\Models\DDC117\DDC117Article", $dqlRef->target());
$this->assertInstanceOf("Doctrine\Tests\Models\DDC117\DDC117Article", $dqlRef->source());
$this->assertSame($dqlRef, $this->_em->find("Doctrine\Tests\Models\DDC117\DDC117Reference", $idCriteria));
$dql = "SELECT r, s FROM "."Doctrine\Tests\Models\DDC117\DDC117Reference 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()
{
$idCriteria = array('source' => $this->article1->id(), 'target' => $this->article2->id());
$mapRef = $this->_em->find("Doctrine\Tests\Models\DDC117\DDC117Reference", $idCriteria);
$this->assertNotNull($mapRef);
$mapRef->setDescription("New Description!!");
$this->_em->flush();
$this->_em->clear();
$mapRef = $this->_em->find("Doctrine\Tests\Models\DDC117\DDC117Reference", $idCriteria);
$this->assertEquals('New Description!!', $mapRef->getDescription());
}
/**
* @group DDC-117
*/
public function testFetchDql()
{
$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();
$this->assertTrue(count($refs) > 0, "Has to contain at least one Reference.");
foreach ($refs AS $ref) {
$this->assertInstanceOf("Doctrine\Tests\Models\DDC117\DDC117Reference", $ref, "Contains only Reference instances.");
$this->assertTrue($this->_em->contains($ref), "Contains Reference in the IdentityMap.");
}
}
/**
* @group DDC-117
*/
public function testRemoveCompositeElement()
{
$idCriteria = array('source' => $this->article1->id(), 'target' => $this->article2->id());
$refRep = $this->_em->find("Doctrine\Tests\Models\DDC117\DDC117Reference", $idCriteria);
$this->_em->remove($refRep);
$this->_em->flush();
$this->_em->clear();
$this->assertNull($this->_em->find("Doctrine\Tests\Models\DDC117\DDC117Reference", $idCriteria));
}
/**
* @group DDC-117
* @group non-cacheable
*/
public function testDqlRemoveCompositeElement()
{
$idCriteria = array('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();
$this->assertNull($this->_em->find("Doctrine\Tests\Models\DDC117\DDC117Reference", $idCriteria));
}
/**
* @group DDC-117
*/
public function testInverseSideAccess()
{
$this->article1 = $this->_em->find("Doctrine\Tests\Models\DDC117\DDC117Article", $this->article1->id());
$this->assertEquals(1, count($this->article1->references()));
foreach ($this->article1->references() AS $this->reference) {
$this->assertInstanceOf("Doctrine\Tests\Models\DDC117\DDC117Reference", $this->reference);
$this->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();
$this->assertEquals(1, count($this->article1->references()));
foreach ($this->article1->references() AS $this->reference) {
$this->assertInstanceOf("Doctrine\Tests\Models\DDC117\DDC117Reference", $this->reference);
$this->assertSame($this->article1, $this->reference->source());
}
}
/**
* @group DDC-117
*/
public function testMixedCompositeKey()
{
$idCriteria = array('article' => $this->article1->id(), 'language' => 'en');
$this->translation = $this->_em->find('Doctrine\Tests\Models\DDC117\DDC117Translation', $idCriteria);
$this->assertInstanceOf('Doctrine\Tests\Models\DDC117\DDC117Translation', $this->translation);
$this->assertSame($this->translation, $this->_em->find('Doctrine\Tests\Models\DDC117\DDC117Translation', $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();
$this->assertInstanceOf('Doctrine\Tests\Models\DDC117\DDC117Translation', $this->translation);
}
/**
* @group DDC-117
*/
public function testMixedCompositeKeyViolateUniqueness()
{
$this->article1 = $this->_em->find('Doctrine\Tests\Models\DDC117\DDC117Article', $this->article1->id());
$this->article1->addTranslation('en', 'Bar');
$this->article1->addTranslation('en', 'Baz');
$exceptionThrown = false;
try {
// exception depending on the underlying Database Driver
$this->_em->flush();
} catch(\Exception $e) {
$exceptionThrown = true;
}
$this->assertTrue($exceptionThrown, "The underlying database driver throws an exception.");
}
/**
* @group DDC-117
*/
public function testOneToOneForeignObjectId()
{
$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();
/* @var $article DDC117Article */
$article = $this->_em->find(get_class($this->article1), $this->article1->id());
$this->assertEquals('not so very long text!', $article->getText());
}
/**
* @group DDC-117
*/
public function testOneToOneCascadeRemove()
{
$article = $this->_em->find(get_class($this->article1), $this->article1->id());
$this->_em->remove($article);
$this->_em->flush();
$this->assertFalse($this->_em->contains($article->getDetails()));
}
/**
* @group DDC-117
*/
public function testOneToOneCascadePersist()
{
if (!$this->_em->getConnection()->getDatabasePlatform()->prefersSequences()) {
$this->markTestSkipped('Test only works with databases that prefer sequences as ID strategy.');
}
$this->article1 = new DDC117Article("Foo");
$this->articleDetails = new DDC117ArticleDetails($this->article1, "Very long text");
$this->_em->persist($this->article1);
$this->_em->flush();
}
/**
* @group DDC-117
*/
public function testReferencesToForeignKeyEntities()
{
$idCriteria = array('source' => $this->article1->id(), 'target' => $this->article2->id());
$reference = $this->_em->find("Doctrine\Tests\Models\DDC117\DDC117Reference", $idCriteria);
$idCriteria = array('article' => $this->article1->id(), 'language' => 'en');
$translation = $this->_em->find('Doctrine\Tests\Models\DDC117\DDC117Translation', $idCriteria);
$approveChanges = new DDC117ApproveChanges($reference->source()->getDetails(), $reference, $translation);
$this->_em->persist($approveChanges);
$this->_em->flush();
$this->_em->clear();
$approveChanges = $this->_em->find("Doctrine\Tests\Models\DDC117\DDC117ApproveChanges", $approveChanges->getId());
$this->assertInstanceOf('Doctrine\Tests\Models\DDC117\DDC117ArticleDetails', $approveChanges->getArticleDetails());
$this->assertInstanceOf('Doctrine\Tests\Models\DDC117\DDC117Reference', $approveChanges->getReference());
$this->assertInstanceOf('Doctrine\Tests\Models\DDC117\DDC117Translation', $approveChanges->getTranslation());
}
/**
* @group DDC-117
*/
public function testLoadOneToManyCollectionOfForeignKeyEntities()
{
/* @var $article DDC117Article */
$article = $this->_em->find(get_class($this->article1), $this->article1->id());
$translations = $article->getTranslations();
$this->assertFalse($translations->isInitialized());
$this->assertContainsOnly('Doctrine\Tests\Models\DDC117\DDC117Translation', $translations);
$this->assertTrue($translations->isInitialized());
}
/**
* @group DDC-117
*/
public function testLoadManyToManyCollectionOfForeignKeyEntities()
{
$editor = $this->loadEditorFixture();
$this->assertFalse($editor->reviewingTranslations->isInitialized());
$this->assertContainsOnly("Doctrine\Tests\Models\DDC117\DDC117Translation", $editor->reviewingTranslations);
$this->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();
$this->assertTrue($editor->reviewingTranslations->isInitialized());
$this->assertContainsOnly("Doctrine\Tests\Models\DDC117\DDC117Translation", $editor->reviewingTranslations);
}
/**
* @group DDC-117
*/
public function testClearManyToManyCollectionOfForeignKeyEntities()
{
$editor = $this->loadEditorFixture();
$this->assertEquals(3, count($editor->reviewingTranslations));
$editor->reviewingTranslations->clear();
$this->_em->flush();
$this->_em->clear();
$editor = $this->_em->find(get_class($editor), $editor->id);
$this->assertEquals(0, count($editor->reviewingTranslations));
}
/**
* @group DDC-117
*/
public function testLoadInverseManyToManyCollection()
{
$editor = $this->loadEditorFixture();
$this->assertInstanceOf('Doctrine\Tests\Models\DDC117\DDC117Translation', $editor->reviewingTranslations[0]);
$reviewedBy = $editor->reviewingTranslations[0]->getReviewedByEditors();
$this->assertEquals(1, count($reviewedBy));
$this->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();
$this->assertInstanceOf('Doctrine\Tests\Models\DDC117\DDC117Translation', $trans);
$this->assertContainsOnly('Doctrine\Tests\Models\DDC117\DDC117Editor', $trans->reviewedByEditors);
$this->assertEquals(1, count($trans->reviewedByEditors));
}
/**
* @group DDC-117
*/
public function testLoadOneToManyOfSourceEntityWithAssociationIdentifier()
{
$editor = $this->loadEditorFixture();
$editor->addLastTranslation($editor->reviewingTranslations[0]);
$this->_em->flush();
$this->_em->clear();
$editor = $this->_em->find(get_class($editor), $editor->id);
$lastTranslatedBy = $editor->reviewingTranslations[0]->getLastTranslatedBy();
$lastTranslatedBy->count();
$this->assertEquals(1, count($lastTranslatedBy));
}
/**
* @return DDC117Editor
*/
private function loadEditorFixture()
{
$editor = new DDC117Editor("beberlei");
/* @var $article1 DDC117Article */
$article1 = $this->_em->find(get_class($this->article1), $this->article1->id());
foreach ($article1->getTranslations() AS $translation) {
$editor->reviewingTranslations[] = $translation;
}
/* @var $article2 DDC117Article */
$article2 = $this->_em->find(get_class($this->article2), $this->article2->id());
$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(get_class($editor), $editor->id);
}
/**
* @group DDC-1519
*/
public function testMergeForeignKeyIdentifierEntity()
{
$idCriteria = array('source' => $this->article1->id(), 'target' => $this->article2->id());
$refRep = $this->_em->find("Doctrine\Tests\Models\DDC117\DDC117Reference", $idCriteria);
$this->_em->detach($refRep);
$refRep = $this->_em->merge($refRep);
$this->assertEquals($this->article1->id(), $refRep->source()->id());
$this->assertEquals($this->article2->id(), $refRep->target()->id());
}
/**
* @group DDC-1652
*/
public function testArrayHydrationWithCompositeKey()
{
$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("Doctrine\Tests\Models\DDC117\DDC117Article", $this->article1->id());
$this->article2 = $this->_em->find("Doctrine\Tests\Models\DDC117\DDC117Article", $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();
$this->assertEquals($before + 3, count($data));
}
/**
* @group DDC-2246
*/
public function testGetEntityState()
{
if ($this->isSecondLevelCacheEnabled) {
$this->markTestIncomplete('Second level cache - not supported yet');
}
$this->article1 = $this->_em->find("Doctrine\Tests\Models\DDC117\DDC117Article", $this->article1->id());
$this->article2 = $this->_em->find("Doctrine\Tests\Models\DDC117\DDC117Article", $this->article2->id());
$this->reference = new DDC117Reference($this->article2, $this->article1, "Test-Description");
$this->assertEquals(\Doctrine\ORM\UnitOfWork::STATE_NEW, $this->_em->getUnitOfWork()->getEntityState($this->reference));
$idCriteria = array('source' => $this->article1->id(), 'target' => $this->article2->id());
$reference = $this->_em->find("Doctrine\Tests\Models\DDC117\DDC117Reference", $idCriteria);
$this->assertEquals(\Doctrine\ORM\UnitOfWork::STATE_MANAGED, $this->_em->getUnitOfWork()->getEntityState($reference));
}
/**
* @group DDC-117
*/
public function testIndexByOnCompositeKeyField()
{
$article = $this->_em->find("Doctrine\Tests\Models\DDC117\DDC117Article", $this->article1->id());
$this->assertInstanceOf('Doctrine\Tests\Models\DDC117\DDC117Article', $article);
$this->assertEquals(1, count($article->getLinks()));
$this->assertTrue($article->getLinks()->offsetExists($this->article2->id()));
}
}