ManyToManyAssociationTest   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 186
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 8
dl 0
loc 186
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 35 1
A testSaveManyToMany() 0 18 1
A testInverseManyToManyLazyLoad() 0 14 1
A testInverseManyToManyIdentityMap() 0 13 1
A testInverseManyToManySeveralEntries() 0 7 1
A testUpdateInverseSideIsIgnored() 0 15 1
A testFlushingOwningSideWithAssocationChangesTwiceOnlySavesOnce() 0 19 1
A testNoTargetDocument() 0 31 3
A testPersistKeys() 0 19 3
1
<?php
2
3
namespace Doctrine\Tests\ODM\CouchDB\Functional;
4
5
use Doctrine\Tests\Models\CMS\CmsArticle;
6
use Doctrine\Tests\Models\CMS\CmsNode;
7
8
class ManyToManyAssociationTest extends \Doctrine\Tests\ODM\CouchDB\CouchDBFunctionalTestCase
9
{
10
    private $userId;
11
    private $groupIds = array();
12
    private $dm;
13
14
    public function setUp()
15
    {
16
        $this->dm = $this->createDocumentManager();
17
18
        $user1 = new \Doctrine\Tests\Models\CMS\CmsUser();
19
        $user1->username = "beberlei";
20
        $user1->status = "active";
21
        $user1->name = "Benjamin";
22
23
        $user2 = new \Doctrine\Tests\Models\CMS\CmsUser();
24
        $user2->username = "lsmith";
25
        $user2->status = "active";
26
        $user2->name = "Lukas";
27
28
        $group1 = new \Doctrine\Tests\Models\CMS\CmsGroup();
29
        $group1->name = "Admin";
30
31
        $group2 = new \Doctrine\Tests\Models\CMS\CmsGroup();
32
        $group2->name = "User";
33
34
        $user1->addGroup($group1);
35
        $user1->addGroup($group2);
36
37
        $user2->addGroup($group2);
38
39
        $this->dm->persist($user1);
40
        $this->dm->persist($user2);
41
        $this->dm->persist($group1);
42
        $this->dm->persist($group2);
43
        $this->dm->flush();
44
        $this->dm->clear();
45
46
        $this->userId = $user1->id;
47
        $this->groupIds = array($group1->id, $group2->id);
48
    }
49
50
    public function testSaveManyToMany()
51
    {
52
        $user = $this->dm->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
53
        $this->assertInstanceOf('Doctrine\ODM\CouchDB\PersistentCollection', $user->groups);
54
        $this->assertFalse($user->groups->isInitialized);
55
        $this->assertCount(2, $user->groups);
56
        $this->assertTrue($user->groups->isInitialized);
57
58
        $group3 = new \Doctrine\Tests\Models\CMS\CmsGroup();
59
        $group3->name = "User";
60
        $user->addGroup($group3);
61
        $this->dm->persist($group3);
62
        $this->dm->flush();
63
        $this->dm->clear();
64
65
        $user = $this->dm->find('Doctrine\Tests\Models\CMS\CmsUser', $user->id);
66
        $this->assertCount(3, $user->groups);
67
    }
68
69
    public function testInverseManyToManyLazyLoad()
70
    {
71
        $group = $this->dm->find('Doctrine\Tests\Models\CMS\CmsGroup', $this->groupIds[0]);
72
        $this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsGroup', $group);
73
74
        $this->assertInstanceOf('Doctrine\ODM\CouchDB\PersistentCollection', $group->users);
75
        $this->assertFalse($group->users->isInitialized);
76
        $this->assertCount(1, $group->users);
77
        $this->assertTrue($group->users->isInitialized);
78
79
        $this->assertEquals('beberlei', $group->users[0]->getUsername());
80
        $user = $this->dm->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
81
        $this->assertSame($user, $group->users[0]);
82
    }
83
84
    public function testInverseManyToManyIdentityMap()
85
    {
86
        $user = $this->dm->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
87
        $group = $this->dm->find('Doctrine\Tests\Models\CMS\CmsGroup', $this->groupIds[0]);
88
        $this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsGroup', $group);
89
90
        $this->assertInstanceOf('Doctrine\ODM\CouchDB\PersistentCollection', $group->users);
91
        $this->assertFalse($group->users->isInitialized);
92
        $this->assertCount(1, $group->users);
93
        $this->assertTrue($group->users->isInitialized);
94
95
        $this->assertSame($user, $group->users[0]);
96
    }
97
98
    public function testInverseManyToManySeveralEntries()
99
    {
100
        $group = $this->dm->find('Doctrine\Tests\Models\CMS\CmsGroup', $this->groupIds[1]);
101
102
        $this->assertCount(2, $group->users);
103
        $this->assertTrue($group->users->isInitialized);
104
    }
105
106
    public function testUpdateInverseSideIsIgnored()
107
    {
108
        $user = $this->dm->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
109
110
        $group3 = new \Doctrine\Tests\Models\CMS\CmsGroup();
111
        $group3->name = "User";
112
        $group3->users[] = $user;
113
114
        $this->dm->persist($group3);
115
        $this->dm->flush();
116
        $this->dm->clear();
117
118
        $user = $this->dm->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
119
        $this->assertCount(2, $user->groups);
120
    }
121
122
    public function testFlushingOwningSideWithAssocationChangesTwiceOnlySavesOnce()
123
    {
124
        $listener = new CountScheduledUpdatesListener();
125
        $this->dm->getEventManager()->addEventListener(array('preUpdate'), $listener);
126
        $this->dm->clear(); // new unit of work has new event listener
127
128
        $user = $this->dm->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
129
        $group3 = new \Doctrine\Tests\Models\CMS\CmsGroup();
130
        $group3->name = "User";
131
132
        $user->addGroup($group3);
133
        $this->dm->persist($group3);
134
135
        $this->dm->flush();
136
        $this->assertEquals(2, $listener->preUpdates);
137
        
138
        $this->dm->flush();
139
        $this->assertEquals(2, $listener->preUpdates);
140
    }
141
142
    public function testNoTargetDocument()
143
    {
144
        $article = new CmsArticle();
145
        $article->text = "Foo";
146
        $article->headline = "Bar";
0 ignored issues
show
Bug introduced by
The property headline does not seem to exist in Doctrine\Tests\Models\CMS\CmsArticle.

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...
147
        $node = new CmsNode();
148
        $node->references[] = $article;
149
        foreach ($this->groupIds AS $groupId) {
150
            $node->references[] = $this->dm->find('Doctrine\Tests\Models\CMS\CmsGroup', $groupId);
151
        }
152
        $node->references[] = $this->dm->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
153
154
        $this->dm->persist($article);
155
        $this->dm->persist($node);
156
157
        $this->dm->flush();
158
        $this->dm->clear();
159
160
        $node = $this->dm->find('Doctrine\Tests\Models\CMS\CmsNode', $node->id);
161
        $this->assertCount(4, $node->references);
162
        $classes = array();
163
        foreach ($node->references AS $reference) {
164
            $classes[] = get_class($reference);
165
        }
166
        $this->assertEquals(array(
167
          'Doctrine\\Tests\\Models\\CMS\\CmsArticle',
168
          'Doctrine\\Tests\\Models\\CMS\\CmsGroup',
169
          'Doctrine\\Tests\\Models\\CMS\\CmsGroup',
170
          'Doctrine\\Tests\\Models\\CMS\\CmsUser',
171
        ), $classes);
172
    }
173
174
    public function testPersistKeys()
175
    {
176
        $node = new CmsNode();
177
        foreach ($this->groupIds AS $groupId) {
178
            $node->references[$groupId] = $this->dm->find('Doctrine\Tests\Models\CMS\CmsGroup', $groupId);
179
        }
180
181
        $this->dm->persist($node);
182
        $this->dm->flush();
183
        $this->dm->clear();
184
185
        $node = $this->dm->find('Doctrine\Tests\Models\CMS\CmsNode', $node->id);
186
        $this->assertCount(2, $node->references);
187
188
        foreach ($this->groupIds AS $groupId) {
189
            $this->assertArrayHasKey($groupId, $node->references, "References array should be indexed by group id, but key does not exist");
190
            $this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsGroup', $node->references[$groupId]);
191
        }
192
    }
193
}
194
195
class CountScheduledUpdatesListener
196
{
197
    public $preUpdates = 0;
198
199
    public function preUpdate($args)
200
    {
201
        $uow = $args->getDocumentManager()->getUnitOfWork();
0 ignored issues
show
Unused Code introduced by
$uow 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...
202
        $this->preUpdates++;
203
    }
204
}