Failed Conditions
Pull Request — 2.7 (#7940)
by Benjamin
06:29
created

GH7864Test   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
eloc 21
dl 0
loc 36
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A testExtraLazyRemoveElement() 0 27 1
1
<?php
2
3
namespace Doctrine\Tests\ORM\Functional\Ticket;
4
5
use Doctrine\Tests\Models\Tweet\User;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Doctrine\Tests\ORM\Functional\Ticket\User. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
use Doctrine\Tests\Models\Tweet\Tweet;
7
8
class GH7864Test extends \Doctrine\Tests\OrmFunctionalTestCase
9
{
10
    protected function setUp() : void
11
    {
12
        $this->useModelSet('tweet');
13
14
        parent::setup();
15
    }
16
17
    public function testExtraLazyRemoveElement()
18
    {
19
        $user = new User();
20
        $user->name = "test";
21
22
        $tweet1 = new Tweet();
23
        $tweet1->content = "Hello World!";
24
        $user->addTweet($tweet1);
25
26
        $tweet2 = new Tweet();
27
        $tweet2->content = "Goodbye, and thanks for all the fish";
28
        $user->addTweet($tweet2);
29
30
        $this->_em->persist($user);
31
        $this->_em->persist($tweet1);
32
        $this->_em->persist($tweet2);
33
        $this->_em->flush();
34
        $this->_em->clear();
35
36
        $user = $this->_em->find(User::class, $user->id);
37
        $tweet = $this->_em->find(Tweet::class, $tweet1->id);
38
39
        $user->tweets->removeElement($tweet);
40
41
        $tweets = $user->tweets->map(function (Tweet $tweet) { return $tweet->content; });
42
43
        $this->assertEquals(['Goodbye, and thanks for all the fish'], array_values($tweets->toArray()));
44
    }
45
}
46