Failed Conditions
Push — develop ( a7d1bd...7cdf30 )
by Guilherme
174:02 queued 109:01
created

BasicFunctionalTest::testOneToOneMergeSetNull()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 17
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\ORM\Functional;
6
7
use Doctrine\DBAL\Logging\DebugStack;
8
use Doctrine\ORM\EntityNotFoundException;
9
use Doctrine\ORM\Mapping\ClassMetadata;
10
use Doctrine\ORM\Mapping\FetchMode;
11
use Doctrine\ORM\ORMInvalidArgumentException;
12
use Doctrine\ORM\PersistentCollection;
13
use Doctrine\ORM\Proxy\Proxy;
14
use Doctrine\ORM\Query;
15
use Doctrine\ORM\UnitOfWork;
16
use Doctrine\Tests\Models\CMS\CmsAddress;
17
use Doctrine\Tests\Models\CMS\CmsArticle;
18
use Doctrine\Tests\Models\CMS\CmsComment;
19
use Doctrine\Tests\Models\CMS\CmsPhonenumber;
20
use Doctrine\Tests\Models\CMS\CmsUser;
21
use Doctrine\Tests\OrmFunctionalTestCase;
22
23
class BasicFunctionalTest extends OrmFunctionalTestCase
24
{
25
    protected function setUp()
26
    {
27
        $this->useModelSet('cms');
28
        parent::setUp();
29
    }
30
31
    public function testBasicUnitsOfWorkWithOneToManyAssociation()
32
    {
33
        // Create
34
        $user = new CmsUser;
35
        $user->name = 'Roman';
36
        $user->username = 'romanb';
37
        $user->status = 'developer';
38
        $this->em->persist($user);
39
40
        $this->em->flush();
41
42
        self::assertTrue(is_numeric($user->id));
43
        self::assertTrue($this->em->contains($user));
44
45
        // Read
46
        $user2 = $this->em->find(CmsUser::class, $user->id);
47
        self::assertTrue($user === $user2);
48
49
        // Add a phonenumber
50
        $ph = new CmsPhonenumber;
51
        $ph->phonenumber = "12345";
52
        $user->addPhonenumber($ph);
53
        $this->em->flush();
54
        self::assertTrue($this->em->contains($ph));
55
        self::assertTrue($this->em->contains($user));
56
57
        // Update name
58
        $user->name = 'guilherme';
59
        $this->em->flush();
60
        self::assertEquals('guilherme', $user->name);
61
62
        // Add another phonenumber
63
        $ph2 = new CmsPhonenumber;
64
        $ph2->phonenumber = "6789";
65
        $user->addPhonenumber($ph2);
66
        $this->em->flush();
67
        self::assertTrue($this->em->contains($ph2));
68
69
        // Delete
70
        $this->em->remove($user);
71
        self::assertTrue($this->em->getUnitOfWork()->isScheduledForDelete($user));
72
        self::assertTrue($this->em->getUnitOfWork()->isScheduledForDelete($ph));
73
        self::assertTrue($this->em->getUnitOfWork()->isScheduledForDelete($ph2));
74
        $this->em->flush();
75
        self::assertFalse($this->em->getUnitOfWork()->isScheduledForDelete($user));
76
        self::assertFalse($this->em->getUnitOfWork()->isScheduledForDelete($ph));
77
        self::assertFalse($this->em->getUnitOfWork()->isScheduledForDelete($ph2));
78
        self::assertEquals(\Doctrine\ORM\UnitOfWork::STATE_NEW, $this->em->getUnitOfWork()->getEntityState($user));
79
        self::assertEquals(\Doctrine\ORM\UnitOfWork::STATE_NEW, $this->em->getUnitOfWork()->getEntityState($ph));
80
        self::assertEquals(\Doctrine\ORM\UnitOfWork::STATE_NEW, $this->em->getUnitOfWork()->getEntityState($ph2));
81
    }
82
83
    public function testOneToManyAssociationModification()
84
    {
85
        $user = new CmsUser;
86
        $user->name = 'Roman';
87
        $user->username = 'romanb';
88
        $user->status = 'developer';
89
90
        $ph1 = new CmsPhonenumber;
91
        $ph1->phonenumber = "0301234";
92
        $ph2 = new CmsPhonenumber;
93
        $ph2->phonenumber = "987654321";
94
95
        $user->addPhonenumber($ph1);
96
        $user->addPhonenumber($ph2);
97
98
        $this->em->persist($user);
99
        $this->em->flush();
100
101
        // Remove the first element from the collection
102
        unset($user->phonenumbers[0]);
103
        $ph1->user = null; // owning side!
104
105
        $this->em->flush();
106
107
        self::assertEquals(1, count($user->phonenumbers));
108
        self::assertNull($ph1->user);
109
    }
110
111
    public function testBasicOneToOne()
112
    {
113
        //$this->em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
114
        $user = new CmsUser;
115
        $user->name = 'Roman';
116
        $user->username = 'romanb';
117
        $user->status = 'developer';
118
119
        $address = new CmsAddress;
120
        $address->country = 'Germany';
121
        $address->city = 'Berlin';
122
        $address->zip = '12345';
123
124
        $user->address = $address; // inverse side
125
        $address->user = $user; // owning side!
126
127
        $this->em->persist($user);
128
        $this->em->flush();
129
130
        // Check that the foreign key has been set
131
        $userId = $this->em->getConnection()->executeQuery(
132
            "SELECT user_id FROM cms_addresses WHERE id=?", [$address->id]
133
        )->fetchColumn();
134
        self::assertTrue(is_numeric($userId));
135
136
        $this->em->clear();
137
138
        $user2 = $this->em->createQuery('select u from \Doctrine\Tests\Models\CMS\CmsUser u where u.id=?1')
139
                ->setParameter(1, $userId)
140
                ->getSingleResult();
141
142
        // Address has been eager-loaded because it cant be lazy
143
        self::assertInstanceOf(CmsAddress::class, $user2->address);
144
        self::assertNotInstanceOf(Proxy::class, $user2->address);
145
    }
146
147
    /**
148
     * @group DDC-1230
149
     */
150
    public function testRemove()
151
    {
152
        $user = new CmsUser;
153
        $user->name = 'Guilherme';
154
        $user->username = 'gblanco';
155
        $user->status = 'developer';
156
157
        self::assertEquals(UnitOfWork::STATE_NEW, $this->em->getUnitOfWork()->getEntityState($user), "State should be UnitOfWork::STATE_NEW");
158
159
        $this->em->persist($user);
160
161
        self::assertEquals(UnitOfWork::STATE_MANAGED, $this->em->getUnitOfWork()->getEntityState($user), "State should be UnitOfWork::STATE_MANAGED");
162
163
        $this->em->remove($user);
164
165
        self::assertEquals(UnitOfWork::STATE_NEW, $this->em->getUnitOfWork()->getEntityState($user), "State should be UnitOfWork::STATE_NEW");
166
167
        $this->em->persist($user);
168
        $this->em->flush();
169
        $id = $user->getId();
170
171
        $this->em->remove($user);
172
173
        self::assertEquals(UnitOfWork::STATE_REMOVED, $this->em->getUnitOfWork()->getEntityState($user), "State should be UnitOfWork::STATE_REMOVED");
174
        $this->em->flush();
175
176
        self::assertEquals(UnitOfWork::STATE_NEW, $this->em->getUnitOfWork()->getEntityState($user), "State should be UnitOfWork::STATE_NEW");
177
178
        self::assertNull($this->em->find(CmsUser::class, $id));
179
    }
180
181
    public function testOneToManyOrphanRemoval()
182
    {
183
        $user = new CmsUser;
184
        $user->name = 'Guilherme';
185
        $user->username = 'gblanco';
186
        $user->status = 'developer';
187
188 View Code Duplication
        for ($i=0; $i<3; ++$i) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
189
            $phone = new CmsPhonenumber;
190
            $phone->phonenumber = 100 + $i;
191
            $user->addPhonenumber($phone);
192
        }
193
194
        $this->em->persist($user);
195
196
        $this->em->flush();
197
198
        $user->getPhonenumbers()->remove(0);
199
        self::assertEquals(2, count($user->getPhonenumbers()));
200
201
        $this->em->flush();
202
203
        // Check that there are just 2 phonenumbers left
204
        $count = $this->em->getConnection()->fetchColumn("SELECT COUNT(*) FROM cms_phonenumbers");
205
        self::assertEquals(2, $count); // only 2 remaining
206
207
        // check that clear() removes the others via orphan removal
208
        $user->getPhonenumbers()->clear();
209
        $this->em->flush();
210
        self::assertEquals(0, $this->em->getConnection()->fetchColumn("select count(*) from cms_phonenumbers"));
211
    }
212
213
    public function testBasicQuery()
214
    {
215
        $user = new CmsUser;
216
        $user->name = 'Guilherme';
217
        $user->username = 'gblanco';
218
        $user->status = 'developer';
219
        $this->em->persist($user);
220
        $this->em->flush();
221
222
        $query = $this->em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u");
223
224
        $users = $query->getResult();
225
226
        self::assertEquals(1, count($users));
227
        self::assertEquals('Guilherme', $users[0]->name);
228
        self::assertEquals('gblanco', $users[0]->username);
229
        self::assertEquals('developer', $users[0]->status);
230
        //self::assertNull($users[0]->phonenumbers);
0 ignored issues
show
Unused Code Comprehensibility introduced by
84% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
231
        //self::assertNull($users[0]->articles);
0 ignored issues
show
Unused Code Comprehensibility introduced by
84% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
232
233
        $usersArray = $query->getArrayResult();
234
235
        self::assertTrue(is_array($usersArray));
236
        self::assertEquals(1, count($usersArray));
237
        self::assertEquals('Guilherme', $usersArray[0]['name']);
238
        self::assertEquals('gblanco', $usersArray[0]['username']);
239
        self::assertEquals('developer', $usersArray[0]['status']);
240
241
        $usersScalar = $query->getScalarResult();
242
243
        self::assertTrue(is_array($usersScalar));
244
        self::assertEquals(1, count($usersScalar));
245
        self::assertEquals('Guilherme', $usersScalar[0]['u_name']);
246
        self::assertEquals('gblanco', $usersScalar[0]['u_username']);
247
        self::assertEquals('developer', $usersScalar[0]['u_status']);
248
    }
249
250
    public function testBasicOneToManyInnerJoin()
251
    {
252
        $user = new CmsUser;
253
        $user->name = 'Guilherme';
254
        $user->username = 'gblanco';
255
        $user->status = 'developer';
256
        $this->em->persist($user);
257
        $this->em->flush();
258
259
        $query = $this->em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u join u.phonenumbers p");
260
261
        $users = $query->getResult();
262
263
        self::assertEquals(0, count($users));
264
    }
265
266
    public function testBasicOneToManyLeftJoin()
267
    {
268
        $user = new CmsUser;
269
        $user->name = 'Guilherme';
270
        $user->username = 'gblanco';
271
        $user->status = 'developer';
272
        $this->em->persist($user);
273
        $this->em->flush();
274
275
        $query = $this->em->createQuery("select u,p from Doctrine\Tests\Models\CMS\CmsUser u left join u.phonenumbers p");
276
277
        $users = $query->getResult();
278
279
        self::assertEquals(1, count($users));
280
        self::assertEquals('Guilherme', $users[0]->name);
281
        self::assertEquals('gblanco', $users[0]->username);
282
        self::assertEquals('developer', $users[0]->status);
283
        self::assertInstanceOf(PersistentCollection::class, $users[0]->phonenumbers);
284
        self::assertTrue($users[0]->phonenumbers->isInitialized());
285
        self::assertEquals(0, $users[0]->phonenumbers->count());
286
    }
287
288
    public function testBasicRefresh()
289
    {
290
        $user = new CmsUser;
291
        $user->name = 'Guilherme';
292
        $user->username = 'gblanco';
293
        $user->status = 'developer';
294
295
        $this->em->persist($user);
296
        $this->em->flush();
297
298
        $user->status = 'mascot';
299
300
        self::assertEquals('mascot', $user->status);
301
        $this->em->refresh($user);
302
        self::assertEquals('developer', $user->status);
303
    }
304
305
    /**
306
     * @group DDC-833
307
     */
308
    public function testRefreshResetsCollection()
309
    {
310
        $user = new CmsUser;
311
        $user->name = 'Guilherme';
312
        $user->username = 'gblanco';
313
        $user->status = 'developer';
314
315
        // Add a phonenumber
316
        $ph1 = new CmsPhonenumber;
317
        $ph1->phonenumber = "12345";
318
        $user->addPhonenumber($ph1);
319
320
        // Add a phonenumber
321
        $ph2 = new CmsPhonenumber;
322
        $ph2->phonenumber = "54321";
323
324
        $this->em->persist($user);
325
        $this->em->persist($ph1);
326
        $this->em->persist($ph2);
327
        $this->em->flush();
328
329
        $user->addPhonenumber($ph2);
330
331
        self::assertEquals(2, count($user->phonenumbers));
332
        $this->em->refresh($user);
333
334
        self::assertEquals(1, count($user->phonenumbers));
335
    }
336
337
    /**
338
     * @group DDC-833
339
     */
340
    public function testDqlRefreshResetsCollection()
341
    {
342
        $user = new CmsUser;
343
        $user->name = 'Guilherme';
344
        $user->username = 'gblanco';
345
        $user->status = 'developer';
346
347
        // Add a phonenumber
348
        $ph1 = new CmsPhonenumber;
349
        $ph1->phonenumber = "12345";
350
        $user->addPhonenumber($ph1);
351
352
        // Add a phonenumber
353
        $ph2 = new CmsPhonenumber;
354
        $ph2->phonenumber = "54321";
355
356
        $this->em->persist($user);
357
        $this->em->persist($ph1);
358
        $this->em->persist($ph2);
359
        $this->em->flush();
360
361
        $user->addPhonenumber($ph2);
362
363
        self::assertEquals(2, count($user->phonenumbers));
364
        $dql = "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = ?1";
365
        $user = $this->em->createQuery($dql)
366
                          ->setParameter(1, $user->id)
367
                          ->setHint(Query::HINT_REFRESH, true)
368
                          ->getSingleResult();
369
370
        self::assertEquals(1, count($user->phonenumbers));
371
    }
372
373
    /**
374
     * @group DDC-833
375
     */
376
    public function testCreateEntityOfProxy()
377
    {
378
        $user = new CmsUser;
379
        $user->name = 'Guilherme';
380
        $user->username = 'gblanco';
381
        $user->status = 'developer';
382
383
        // Add a phonenumber
384
        $ph1 = new CmsPhonenumber;
385
        $ph1->phonenumber = "12345";
386
        $user->addPhonenumber($ph1);
387
388
        // Add a phonenumber
389
        $ph2 = new CmsPhonenumber;
390
        $ph2->phonenumber = "54321";
391
392
        $this->em->persist($user);
393
        $this->em->persist($ph1);
394
        $this->em->persist($ph2);
395
        $this->em->flush();
396
        $this->em->clear();
397
398
        $userId = $user->id;
399
        $user = $this->em->getReference(CmsUser::class, $user->id);
0 ignored issues
show
Unused Code introduced by
$user is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
400
401
        $dql = "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = ?1";
402
        $user = $this->em->createQuery($dql)
403
                          ->setParameter(1, $userId)
404
                          ->getSingleResult();
405
406
        self::assertEquals(1, count($user->phonenumbers));
407
    }
408
409
    public function testAddToCollectionDoesNotInitialize()
410
    {
411
        $user = new CmsUser;
412
        $user->name = 'Guilherme';
413
        $user->username = 'gblanco';
414
        $user->status = 'developer';
415
416 View Code Duplication
        for ($i=0; $i<3; ++$i) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
417
            $phone = new CmsPhonenumber;
418
            $phone->phonenumber = 100 + $i;
419
            $user->addPhonenumber($phone);
420
        }
421
422
        $this->em->persist($user);
423
        $this->em->flush();
424
        $this->em->clear();
425
426
        self::assertEquals(3, $user->getPhonenumbers()->count());
427
428
        $query = $this->em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username='gblanco'");
429
430
        $gblanco = $query->getSingleResult();
431
432
        self::assertFalse($gblanco->getPhonenumbers()->isInitialized());
433
434
        $newPhone = new CmsPhonenumber;
435
        $newPhone->phonenumber = 555;
436
        $gblanco->addPhonenumber($newPhone);
437
438
        self::assertFalse($gblanco->getPhonenumbers()->isInitialized());
439
        $this->em->persist($gblanco);
440
441
        $this->em->flush();
442
        $this->em->clear();
443
444
        $query = $this->em->createQuery("select u, p from Doctrine\Tests\Models\CMS\CmsUser u join u.phonenumbers p where u.username='gblanco'");
445
        $gblanco2 = $query->getSingleResult();
446
        self::assertEquals(4, $gblanco2->getPhonenumbers()->count());
447
    }
448
449
    public function testInitializeCollectionWithNewObjectsRetainsNewObjects()
450
    {
451
        $user = new CmsUser;
452
        $user->name = 'Guilherme';
453
        $user->username = 'gblanco';
454
        $user->status = 'developer';
455
456 View Code Duplication
        for ($i=0; $i<3; ++$i) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
457
            $phone = new CmsPhonenumber;
458
            $phone->phonenumber = 100 + $i;
459
            $user->addPhonenumber($phone);
460
        }
461
462
        $this->em->persist($user);
463
        $this->em->flush();
464
        $this->em->clear();
465
466
        self::assertEquals(3, $user->getPhonenumbers()->count());
467
468
        $query = $this->em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username='gblanco'");
469
470
        $gblanco = $query->getSingleResult();
471
472
        self::assertFalse($gblanco->getPhonenumbers()->isInitialized());
473
474
        $newPhone = new CmsPhonenumber;
475
        $newPhone->phonenumber = 555;
476
        $gblanco->addPhonenumber($newPhone);
477
478
        self::assertFalse($gblanco->getPhonenumbers()->isInitialized());
479
        self::assertEquals(4, $gblanco->getPhonenumbers()->count());
480
        self::assertTrue($gblanco->getPhonenumbers()->isInitialized());
481
482
        $this->em->flush();
483
        $this->em->clear();
484
485
        $query = $this->em->createQuery("select u, p from Doctrine\Tests\Models\CMS\CmsUser u join u.phonenumbers p where u.username='gblanco'");
486
        $gblanco2 = $query->getSingleResult();
487
        self::assertEquals(4, $gblanco2->getPhonenumbers()->count());
488
    }
489
490
    public function testOneToManyCascadeRemove()
491
    {
492
        $user = new CmsUser;
493
        $user->name = 'Guilherme';
494
        $user->username = 'gblanco';
495
        $user->status = 'developer';
496
497 View Code Duplication
        for ($i=0; $i<3; ++$i) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
498
            $phone = new CmsPhonenumber;
499
            $phone->phonenumber = 100 + $i;
500
            $user->addPhonenumber($phone);
501
        }
502
503
        $this->em->persist($user);
504
        $this->em->flush();
505
        $this->em->clear();
506
507
        $query = $this->em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username='gblanco'");
508
        $gblanco = $query->getSingleResult();
509
510
        $this->em->remove($gblanco);
511
        $this->em->flush();
512
513
        $this->em->clear();
514
515
        self::assertEquals(0, $this->em->createQuery(
516
                "select count(p.phonenumber) from Doctrine\Tests\Models\CMS\CmsPhonenumber p")
517
                ->getSingleScalarResult());
518
519
        self::assertEquals(0, $this->em->createQuery(
520
                "select count(u.id) from Doctrine\Tests\Models\CMS\CmsUser u")
521
                ->getSingleScalarResult());
522
    }
523
524
    public function testTextColumnSaveAndRetrieve()
525
    {
526
        $user = new CmsUser;
527
        $user->name = 'Guilherme';
528
        $user->username = 'gblanco';
529
        $user->status = 'developer';
530
531
        $this->em->persist($user);
532
533
        $article = new CmsArticle();
534
        $article->text = "Lorem ipsum dolor sunt.";
535
        $article->topic = "A Test Article!";
536
        $article->setAuthor($user);
537
538
        $this->em->persist($article);
539
        $this->em->flush();
540
        $articleId = $article->id;
541
542
        $this->em->clear();
543
544
        // test find() with leading backslash at the same time
545
        $articleNew = $this->em->find('\Doctrine\Tests\Models\CMS\CmsArticle', $articleId);
546
        self::assertTrue($this->em->contains($articleNew));
547
        self::assertEquals("Lorem ipsum dolor sunt.", $articleNew->text);
548
549
        self::assertNotSame($article, $articleNew);
550
551
        $articleNew->text = "Lorem ipsum dolor sunt. And stuff!";
552
553
        $this->em->flush();
554
        $this->em->clear();
555
556
        $articleNew = $this->em->find(CmsArticle::class, $articleId);
557
        self::assertEquals("Lorem ipsum dolor sunt. And stuff!", $articleNew->text);
558
        self::assertTrue($this->em->contains($articleNew));
559
    }
560
561
    public function testFlushDoesNotIssueUnnecessaryUpdates()
562
    {
563
        $user = new CmsUser;
564
        $user->name = 'Guilherme';
565
        $user->username = 'gblanco';
566
        $user->status = 'developer';
567
568
        $address = new CmsAddress;
569
        $address->country = 'Germany';
570
        $address->city = 'Berlin';
571
        $address->zip = '12345';
572
573
        $address->user = $user;
574
        $user->address = $address;
575
576
        $article = new CmsArticle();
577
        $article->text = "Lorem ipsum dolor sunt.";
578
        $article->topic = "A Test Article!";
579
        $article->setAuthor($user);
580
581
        $this->em->persist($article);
582
        $this->em->persist($user);
583
584
        //$this->em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
585
586
        $this->em->flush();
587
        $this->em->clear();
588
589
        $query = $this->em->createQuery('select u,a,ad from Doctrine\Tests\Models\CMS\CmsUser u join u.articles a join u.address ad');
590
        $user2 = $query->getSingleResult();
591
592
        self::assertEquals(1, count($user2->articles));
593
        self::assertInstanceOf(CmsAddress::class, $user2->address);
594
595
        $oldLogger = $this->em->getConnection()->getConfiguration()->getSQLLogger();
596
        $debugStack = new DebugStack();
597
        $this->em->getConnection()->getConfiguration()->setSQLLogger($debugStack);
598
599
        $this->em->flush();
600
        self::assertEquals(0, count($debugStack->queries));
601
602
        $this->em->getConnection()->getConfiguration()->setSQLLogger($oldLogger);
603
    }
604
605
    public function testRemoveEntityByReference()
606
    {
607
        $user = new CmsUser;
608
        $user->name = 'Guilherme';
609
        $user->username = 'gblanco';
610
        $user->status = 'developer';
611
612
        //$this->em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
613
614
        $this->em->persist($user);
615
        $this->em->flush();
616
        $this->em->clear();
617
618
        $userRef = $this->em->getReference(CmsUser::class, $user->getId());
619
        $this->em->remove($userRef);
620
        $this->em->flush();
621
        $this->em->clear();
622
623
        self::assertEquals(0, $this->em->getConnection()->fetchColumn("select count(*) from cms_users"));
624
625
        //$this->em->getConnection()->getConfiguration()->setSQLLogger(null);
0 ignored issues
show
Unused Code Comprehensibility introduced by
77% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
626
    }
627
628
    public function testQueryEntityByReference()
629
    {
630
        $user = new CmsUser;
631
        $user->name = 'Guilherme';
632
        $user->username = 'gblanco';
633
        $user->status = 'developer';
634
635
        $address = new CmsAddress;
636
        $address->country = 'Germany';
637
        $address->city = 'Berlin';
638
        $address->zip = '12345';
639
640
        $user->setAddress($address);
641
642
        $this->em->transactional(function($em) use($user) {
643
            $em->persist($user);
644
        });
645
        $this->em->clear();
646
647
        //$this->em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
648
649
        $userRef = $this->em->getReference(CmsUser::class, $user->getId());
650
        $address2 = $this->em->createQuery('select a from Doctrine\Tests\Models\CMS\CmsAddress a where a.user = :user')
651
                ->setParameter('user', $userRef)
652
                ->getSingleResult();
653
654
        self::assertInstanceOf(Proxy::class, $address2->getUser());
655
        self::assertTrue($userRef === $address2->getUser());
656
        self::assertFalse($userRef->__isInitialized());
657
        self::assertEquals('Germany', $address2->country);
658
        self::assertEquals('Berlin', $address2->city);
659
        self::assertEquals('12345', $address2->zip);
660
    }
661
662
    public function testOneToOneNullUpdate()
663
    {
664
        $user = new CmsUser();
665
        $user->username = "beberlei";
666
        $user->name = "Benjamin E.";
667
        $user->status = 'active';
668
669
        $address = new CmsAddress();
670
        $address->city = "Bonn";
671
        $address->zip = "12354";
672
        $address->country = "Germany";
673
        $address->street = "somestreet";
674
        $address->user = $user;
675
676
        $this->em->persist($address);
677
        $this->em->persist($user);
678
        $this->em->flush();
679
680
        self::assertEquals(1, $this->em->getConnection()->fetchColumn("select 1 from cms_addresses where user_id = ".$user->id));
681
682
        $address->user = null;
683
        $this->em->flush();
684
685
        self::assertNotEquals(1, $this->em->getConnection()->fetchColumn("select 1 from cms_addresses where user_id = ".$user->id));
686
    }
687
688
    /**
689
     * @group DDC-600
690
     * @group DDC-455
691
     */
692
    public function testNewAssociatedEntityDuringFlushThrowsException()
693
    {
694
        $this->expectException(\InvalidArgumentException::class);
695
696
        $user = new CmsUser();
697
        $user->username = "beberlei";
698
        $user->name = "Benjamin E.";
699
        $user->status = 'active';
700
701
        $address = new CmsAddress();
702
        $address->city = "Bonn";
703
        $address->zip = "12354";
704
        $address->country = "Germany";
705
        $address->street = "somestreet";
706
        $address->user = $user;
707
708
        $this->em->persist($address);
709
710
        // flushing without persisting $user should raise an exception
711
        $this->em->flush();
712
    }
713
714
    /**
715
     * @group DDC-600
716
     * @group DDC-455
717
     */
718
    public function testNewAssociatedEntityDuringFlushThrowsException2()
719
    {
720
        $this->expectException(\InvalidArgumentException::class);
721
722
        $user = new CmsUser();
723
        $user->username = "beberlei";
724
        $user->name = "Benjamin E.";
725
        $user->status = 'active';
726
727
        $address = new CmsAddress();
728
        $address->city = "Bonn";
729
        $address->zip = "12354";
730
        $address->country = "Germany";
731
        $address->street = "somestreet";
732
        $address->user = $user;
733
734
        $this->em->persist($address);
735
        $this->em->persist($user);
736
        $this->em->flush();
737
738
        $u2 = new CmsUser;
739
        $u2->username = "beberlei";
740
        $u2->name = "Benjamin E.";
741
        $u2->status = 'inactive';
742
        $address->user = $u2;
743
744
        // flushing without persisting $u2 should raise an exception
745
        $this->em->flush();
746
    }
747
748
    /**
749
     * @group DDC-600
750
     * @group DDC-455
751
     */
752
    public function testNewAssociatedEntityDuringFlushThrowsException3()
753
    {
754
        $this->expectException(\InvalidArgumentException::class);
755
756
        $art = new CmsArticle();
757
        $art->topic = 'topic';
758
        $art->text = 'the text';
759
760
        $com = new CmsComment();
761
        $com->topic = 'Good';
762
        $com->text = 'Really good!';
763
        $art->addComment($com);
764
765
        $this->em->persist($art);
766
767
        // flushing without persisting $com should raise an exception
768
        $this->em->flush();
769
    }
770
771
    public function testOneToOneOrphanRemoval()
772
    {
773
        $user = new CmsUser();
774
        $user->username = "beberlei";
775
        $user->name = "Benjamin E.";
776
        $user->status = 'active';
777
778
        $address = new CmsAddress();
779
        $address->city = "Bonn";
780
        $address->zip = "12354";
781
        $address->country = "Germany";
782
        $address->street = "somestreet";
783
        $address->user = $user;
784
        $user->address = $address;
785
786
        $this->em->persist($address);
787
        $this->em->persist($user);
788
        $this->em->flush();
789
        $addressId = $address->getId();
0 ignored issues
show
Unused Code introduced by
$addressId is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
790
791
        $user->address = null;
792
793
        $this->em->flush();
794
795
        self::assertEquals(0, $this->em->getConnection()->fetchColumn("select count(*) from cms_addresses"));
796
797
        // check orphan removal through replacement
798
        $user->address = $address;
799
        $address->user = $user;
800
801
        $this->em->flush();
802
        self::assertEquals(1, $this->em->getConnection()->fetchColumn("select count(*) from cms_addresses"));
803
804
        // remove $address to free up unique key id
805
        $this->em->remove($address);
806
        $this->em->flush();
807
808
        $newAddress = new CmsAddress();
809
        $newAddress->city = "NewBonn";
810
        $newAddress->zip = "12354";
811
        $newAddress->country = "NewGermany";
812
        $newAddress->street = "somenewstreet";
813
        $newAddress->user = $user;
814
        $user->address = $newAddress;
815
816
        $this->em->flush();
817
        self::assertEquals(1, $this->em->getConnection()->fetchColumn("select count(*) from cms_addresses"));
818
    }
819
820
    public function testGetPartialReferenceToUpdateObjectWithoutLoadingIt()
821
    {
822
        $user = new CmsUser();
823
        $user->username = "beberlei";
824
        $user->name = "Benjamin E.";
825
        $user->status = 'active';
826
        $this->em->persist($user);
827
        $this->em->flush();
828
        $userId = $user->id;
829
        $this->em->clear();
830
831
        $user = $this->em->getPartialReference(CmsUser::class, $userId);
832
        self::assertTrue($this->em->contains($user));
833
        self::assertNull($user->getName());
834
        self::assertEquals($userId, $user->id);
835
836
        $user->name = 'Stephan';
837
        $this->em->flush();
838
        $this->em->clear();
839
840
        self::assertEquals('Benjamin E.', $this->em->find(get_class($user), $userId)->name);
841
    }
842
843
    /**
844
     * @group DDC-952
845
     */
846
    public function testManyToOneFetchModeQuery()
847
    {
848
        $user = new CmsUser();
849
        $user->username = "beberlei";
850
        $user->name = "Benjamin E.";
851
        $user->status = 'active';
852
853
        $article = new CmsArticle();
854
        $article->topic = "foo";
855
        $article->text = "bar";
856
        $article->user = $user;
857
858
        $this->em->persist($article);
859
        $this->em->persist($user);
860
        $this->em->flush();
861
        $this->em->clear();
862
863
        $qc = $this->getCurrentQueryCount();
864
        $dql = "SELECT a FROM Doctrine\Tests\Models\CMS\CmsArticle a WHERE a.id = ?1";
865
        $article = $this->em
866
            ->createQuery($dql)
867
            ->setParameter(1, $article->id)
868
            ->setFetchMode(CmsArticle::class, 'user', FetchMode::EAGER)
869
            ->getSingleResult();
870
871
        self::assertInstanceOf(Proxy::class, $article->user, "It IS a proxy, ...");
872
        self::assertTrue($article->user->__isInitialized(), "...but its initialized!");
0 ignored issues
show
Bug introduced by
The method __isInitialized() does not seem to exist on object<Doctrine\Tests\Models\CMS\CmsUser>.

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...
873
        self::assertEquals($qc+2, $this->getCurrentQueryCount());
874
    }
875
876
    /**
877
     * @group DDC-1278
878
     */
879
    public function testClear()
880
    {
881
        $user = new CmsUser;
882
        $user->name = 'Dominik';
883
        $user->username = 'domnikl';
884
        $user->status = 'developer';
885
886
        $address = new CmsAddress();
887
        $address->city = "Springfield";
888
        $address->zip = "12354";
889
        $address->country = "Germany";
890
        $address->street = "Foo Street";
891
        $address->user = $user;
892
        $user->address = $address;
893
894
        $article1 = new CmsArticle();
895
        $article1->topic = 'Foo';
896
        $article1->text = 'Foo Text';
897
898
        $article2 = new CmsArticle();
899
        $article2->topic = 'Bar';
900
        $article2->text = 'Bar Text';
901
902
        $user->addArticle($article1);
903
        $user->addArticle($article2);
904
905
        $this->em->persist($article1);
906
        $this->em->persist($article2);
907
        $this->em->persist($address);
908
        $this->em->persist($user);
909
        $this->em->flush();
910
911
        $unitOfWork = $this->em->getUnitOfWork();
912
913
        $this->em->clear();
914
915
        self::assertEquals(UnitOfWork::STATE_DETACHED, $unitOfWork->getEntityState($user));
916
        self::assertEquals(UnitOfWork::STATE_DETACHED, $unitOfWork->getEntityState($article1));
917
        self::assertEquals(UnitOfWork::STATE_DETACHED, $unitOfWork->getEntityState($article2));
918
        self::assertEquals(UnitOfWork::STATE_DETACHED, $unitOfWork->getEntityState($address));
919
    }
920
921
    /**
922
     * @group DDC-1585
923
     */
924
    public function testWrongAssociationInstance()
925
    {
926
        $user = new CmsUser;
927
        $user->name = 'Dominik';
928
        $user->username = 'domnikl';
929
        $user->status = 'developer';
930
        $user->address = $user;
931
932
        $this->expectException(ORMInvalidArgumentException::class);
933
        $this->expectExceptionMessage(
934
            'Expected value of type "Doctrine\Tests\Models\CMS\CmsAddress" for association field ' .
935
            '"Doctrine\Tests\Models\CMS\CmsUser#$address", got "Doctrine\Tests\Models\CMS\CmsUser" instead.'
936
        );
937
938
        $this->em->persist($user);
939
        $this->em->flush();
940
    }
941
}
942