Completed
Push — master ( 61404e...7a0385 )
by Marco
19:03 queued 10:27
created

DDC1885Test::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 12
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\ORM\Functional\Ticket;
4
5
use Doctrine\Tests\Models\Quote\Group;
6
use Doctrine\Tests\Models\Quote\User;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Doctrine\Tests\ORM\Functional\Ticket\User.

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...
7
8
/**
9
 * @group DDC-1845
10
 * @group DDC-1885
11
 */
12
class DDC1885Test extends \Doctrine\Tests\OrmFunctionalTestCase
13
{
14
15
    /**
16
     * @var \Doctrine\Tests\Models\Quote\User
17
     */
18
    private $user;
19
20
    protected function setUp() : void
21
    {
22
        $this->useModelSet('quote');
23
24
        parent::setUp();
25
26
        $user           = new User();
27
        $user->name     = "FabioBatSilva";
28
        $user->email    = "[email protected]";
0 ignored issues
show
Bug introduced by
The property email does not seem to exist in Doctrine\Tests\Models\Quote\User.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
29
        $user->groups[] = new Group('G 1');
30
        $user->groups[] = new Group('G 2');
31
        $this->user     = $user;
32
33
        // Create
34
        $this->_em->persist($user);
35
        $this->_em->flush();
36
        $this->_em->clear();
37
38
    }
39
40
    public function testCreateRetrieveUpdateDelete()
41
    {
42
        $user   = $this->user;
43
        $g1     = $user->getGroups()->get(0);
44
        $g2     = $user->getGroups()->get(1);
45
46
        $u1Id   = $user->id;
47
        $g1Id   = $g1->id;
48
        $g2Id   = $g2->id;
49
50
        // Retrieve
51
        $user = $this->_em->find(User::class, $u1Id);
52
53
        $this->assertInstanceOf(User::class, $user);
54
        $this->assertEquals('FabioBatSilva', $user->name);
55
        $this->assertEquals($u1Id, $user->id);
56
57
        $this->assertCount(2, $user->groups);
58
59
        $g1 = $user->getGroups()->get(0);
60
        $g2 = $user->getGroups()->get(1);
61
62
        $this->assertInstanceOf(Group::class, $g1);
63
        $this->assertInstanceOf(Group::class, $g2);
64
65
        $g1->name = 'Bar 11';
66
        $g2->name = 'Foo 22';
67
68
        // Update
69
        $this->_em->persist($user);
70
        $this->_em->flush();
71
        $this->_em->clear();
72
73
        $user = $this->_em->find(User::class, $u1Id);
74
75
        $this->assertInstanceOf(User::class, $user);
76
        $this->assertEquals('FabioBatSilva', $user->name);
77
        $this->assertEquals($u1Id, $user->id);
78
79
        // Delete
80
        $this->_em->remove($user);
81
82
        $this->_em->flush();
83
        $this->_em->clear();
84
85
        $this->assertNull($this->_em->find(User::class, $u1Id));
86
        $this->assertNull($this->_em->find(Group::class, $g1Id));
87
        $this->assertNull($this->_em->find(Group::class, $g2Id));
88
    }
89
90 View Code Duplication
    public function testRemoveItem()
91
    {
92
        $user   = $this->user;
93
        $u1Id   = $user->id;
94
        $user   = $this->_em->find(User::class, $u1Id);
95
96
        $this->assertInstanceOf(User::class, $user);
97
        $this->assertEquals('FabioBatSilva', $user->name);
98
        $this->assertEquals($u1Id, $user->id);
99
100
        $this->assertCount(2, $user->groups);
101
        $this->assertInstanceOf(Group::class, $user->getGroups()->get(0));
102
        $this->assertInstanceOf(Group::class, $user->getGroups()->get(1));
103
104
        $user->getGroups()->remove(0);
105
106
        // Update
107
        $this->_em->persist($user);
108
        $this->_em->flush();
109
        $this->_em->clear();
110
111
        $user = $this->_em->find(User::class, $u1Id);
112
113
        $this->assertInstanceOf(User::class, $user);
114
        $this->assertEquals('FabioBatSilva', $user->name);
115
        $this->assertEquals($u1Id, $user->id);
116
117
        $this->assertCount(1, $user->getGroups());
118
    }
119
120 View Code Duplication
    public function testClearAll()
121
    {
122
        $user   = $this->user;
123
        $u1Id   = $user->id;
124
        $user   = $this->_em->find(User::class, $u1Id);
125
126
        $this->assertInstanceOf(User::class, $user);
127
        $this->assertEquals('FabioBatSilva', $user->name);
128
        $this->assertEquals($u1Id, $user->id);
129
130
        $this->assertCount(2, $user->groups);
131
        $this->assertInstanceOf(Group::class, $user->getGroups()->get(0));
132
        $this->assertInstanceOf(Group::class, $user->getGroups()->get(1));
133
134
        $user->getGroups()->clear();
135
136
        // Update
137
        $this->_em->persist($user);
138
        $this->_em->flush();
139
        $this->_em->clear();
140
141
        $user = $this->_em->find(User::class, $u1Id);
142
143
        $this->assertInstanceOf(User::class, $user);
144
        $this->assertEquals('FabioBatSilva', $user->name);
145
        $this->assertEquals($u1Id, $user->id);
146
147
        $this->assertCount(0, $user->getGroups());
148
    }
149
150
    public function testCountExtraLazy()
151
    {
152
        $user   = $this->user;
153
        $u1Id   = $user->id;
154
        $user   = $this->_em->find(User::class, $u1Id);
155
156
        $this->assertInstanceOf(User::class, $user);
157
        $this->assertEquals('FabioBatSilva', $user->name);
158
        $this->assertEquals($u1Id, $user->id);
159
160
        $this->assertCount(2, $user->groups);
161
        $this->assertInstanceOf(Group::class, $user->getGroups()->get(0));
162
        $this->assertInstanceOf(Group::class, $user->getGroups()->get(1));
163
    }
164
}
165