Failed Conditions
Push — 2.7 ( c036c0...266f0d )
by Jonathan
57:23 queued 50:07
created

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

Labels
Severity
1
<?php
2
3
namespace Doctrine\Tests\ORM\Functional\Ticket;
4
5
use Doctrine\Tests\Models\Quote\Address;
6
use Doctrine\Tests\Models\Quote\City;
7
use Doctrine\Tests\Models\Quote\FullAddress;
8
use Doctrine\Tests\Models\Quote\User;
0 ignored issues
show
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...
9
use Doctrine\Tests\OrmFunctionalTestCase;
10
11
/**
12
 * @group 6402
13
 */
14
class GH6402Test extends OrmFunctionalTestCase
15
{
16
    protected function setUp()
17
    {
18
        $this->useModelSet('quote');
19
20
        parent::setUp();
21
    }
22
23
    public function testFind()
24
    {
25
        $id = $this->createAddress();
26
27
        $address = $this->_em->find(Address::class, $id);
28
        self::assertNotNull($address->user);
29
    }
30
31
    public function testQuery()
32
    {
33
        $id = $this->createAddress();
34
35
        $addresses = $this->_em->createQuery('SELECT a FROM ' . Address::class . ' a WHERE a.id = :id')
36
            ->setParameter('id', $id)
37
            ->getResult();
38
39
        self::assertCount(1, $addresses);
40
        self::assertNotNull($addresses[0]->user);
41
    }
42
43
    public function testFindWithSubClass()
44
    {
45
        $id = $this->createFullAddress();
46
47
        $address = $this->_em->find(FullAddress::class, $id);
48
        self::assertNotNull($address->user);
49
    }
50
51
    public function testQueryWithSubClass()
52
    {
53
        $id = $this->createFullAddress();
54
55
        $addresses = $this->_em->createQuery('SELECT a FROM ' . FullAddress::class . ' a WHERE a.id = :id')
56
            ->setParameter('id', $id)
57
            ->getResult();
58
59
        self::assertCount(1, $addresses);
60
        self::assertNotNull($addresses[0]->user);
61
    }
62
63
    private function createAddress()
64
    {
65
        $address = new Address();
66
        $address->zip = 'bar';
67
68
        $this->persistAddress($address);
69
70
        return $address->id;
71
    }
72
73
    private function createFullAddress()
74
    {
75
        $address = new FullAddress();
76
        $address->zip = 'bar';
77
        $address->city = new City('London');
78
79
        $this->persistAddress($address);
80
81
        return $address->id;
82
    }
83
84
    private function persistAddress(Address $address)
85
    {
86
        $user = new User();
87
        $user->name = "foo";
88
        $user->setAddress($address);
89
90
        $this->_em->persist($user);
91
        $this->_em->flush();
92
        $this->_em->clear();
93
    }
94
}
95