RepositoryTest   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 185
Duplicated Lines 22.7 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 5
dl 42
loc 185
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A testFindMany() 0 38 1
A testFindAll() 8 24 3
A testLoadManyWithMissingIds() 0 18 1
A testFindBy() 8 31 3
A testFindByManyConstraints() 8 16 3
A testFindByIdUsesIdentityMap() 18 18 1
A testFindByReusesIdentities() 0 18 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Doctrine\Tests\ODM\CouchDB\Functional;
4
5
class RepositoryTest extends \Doctrine\Tests\ODM\CouchDB\CouchDBFunctionalTestCase
6
{
7
    /**
8
     * @var DocumentManager
9
     */
10
    private $dm;
11
12
    private $type;
13
14
    public function setUp()
15
    {
16
        $this->type = 'Doctrine\Tests\Models\CMS\CmsUser';
17
        $this->dm = $this->createDocumentManager();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createDocumentManager() of type object<Doctrine\ODM\CouchDB\DocumentManager> is incompatible with the declared type object<Doctrine\Tests\OD...tional\DocumentManager> of property $dm.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
18
    }
19
20
    public function testFindMany()
21
    {
22
        $this->dm = $this->createDocumentManager();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createDocumentManager() of type object<Doctrine\ODM\CouchDB\DocumentManager> is incompatible with the declared type object<Doctrine\Tests\OD...tional\DocumentManager> of property $dm.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
23
24
        $user1 = new \Doctrine\Tests\Models\CMS\CmsUser();
25
        $user1->username = "beberlei";
26
        $user1->status = "active";
27
        $user1->name = "Benjamin";
28
29
        $user2 = new \Doctrine\Tests\Models\CMS\CmsUser();
30
        $user2->username = "lsmith";
31
        $user2->status = "active";
32
        $user2->name = "Lukas";
33
34
        $this->dm = $this->createDocumentManager();
35
        $this->dm->persist($user1);
36
        $this->dm->persist($user2);
37
        $this->dm->flush();
38
39
        $users = $this->dm->getRepository('Doctrine\Tests\Models\CMS\CmsUser')->findMany(array($user1->id, $user2->id));
40
        $this->assertCount(2, $users);
41
        $this->assertSame($user1, $users[0]);
42
        $this->assertSame($user2, $users[1]);
43
44
        $users = $this->dm->getRepository('Doctrine\Tests\Models\CMS\CmsUser')->findMany(array($user1->id, $user2->id), 1, 0);
45
        $this->assertCount(1, $users);
46
        $this->assertSame($user1, $users[0]);
47
48
        $users = $this->dm->getRepository('Doctrine\Tests\Models\CMS\CmsUser')->findMany(array($user1->id, $user2->id), 1, 1);
49
        $this->assertCount(1, $users);
50
        $this->assertSame($user2, $users[1]);
51
52
        $this->dm->clear();
53
54
        $users = $this->dm->getRepository('Doctrine\Tests\Models\CMS\CmsUser')->findMany(array($user1->id, $user2->id));
55
        $this->assertEquals($user1->id, $users[0]->id);
56
        $this->assertEquals($user2->id, $users[1]->id);
57
    }
58
59
    public function testFindAll()
60
    {
61 View Code Duplication
        for ($i = 0; $i < 10; $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...
62
            $user = new \Doctrine\Tests\Models\CMS\CmsUser();
63
            $user->username = "beberlei" . $i;
64
            $user->status = "active";
65
            $user->name = "Benjamin" . $i;
66
67
            $this->dm->persist($user);
68
        }
69
        for ($i = 0; $i < 10; $i++) {
70
            $group = new \Doctrine\Tests\Models\CMS\CmsGroup();
71
            $group->name = "Group" . $i;
72
            $this->dm->persist($group);
73
        }
74
        $this->dm->flush();
75
76
        $users = $this->dm->getRepository('Doctrine\Tests\Models\CMS\CmsUser')->findAll();
77
        $this->assertCount(10, $users);
78
        $this->assertContainsOnly('Doctrine\Tests\Models\CMS\CmsUser', $users);
79
80
        $groups = $this->dm->getRepository('Doctrine\Tests\Models\CMS\CmsGroup')->findAll();
81
        $this->assertCount(0, $groups, "No results, group is not indexed!");
82
    }
83
84
    public function testLoadManyWithMissingIds()
85
    {
86
        $this->dm = $this->createDocumentManager();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createDocumentManager() of type object<Doctrine\ODM\CouchDB\DocumentManager> is incompatible with the declared type object<Doctrine\Tests\OD...tional\DocumentManager> of property $dm.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
87
        $users = $this->dm->getRepository('Doctrine\Tests\Models\CMS\CmsUser')->findMany(array('missing-id-1', 'missing-id-2'));
88
        $this->assertEmpty($users);
89
90
        $user1 = new \Doctrine\Tests\Models\CMS\CmsUser();
91
        $user1->username = "beberlei";
92
        $user1->status = "active";
93
        $user1->name = "Benjamin";
94
        $this->dm = $this->createDocumentManager();
95
        $this->dm->persist($user1);
96
        $this->dm->flush();
97
        $this->dm->clear();
98
99
        $users = $this->dm->getRepository('Doctrine\Tests\Models\CMS\CmsUser')->findMany(array($user1->id, 'missing-id-2'));
100
        $this->assertCount(1, $users);
101
    }
102
103
    public function testFindBy()
104
    {
105 View Code Duplication
        for ($i = 0; $i < 10; $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...
106
            $user = new \Doctrine\Tests\Models\CMS\CmsUser();
107
            $user->username = "beberlei" . $i;
108
            $user->status = ($i % 2 == 0) ? "active" : "inactive";
109
            $user->name = "Benjamin";
110
111
            $this->dm->persist($user);
112
        }
113
        $this->dm->flush();
114
115
        $users = $this->dm->getRepository('Doctrine\Tests\Models\CMS\CmsUser')->findBy(array('status' => 'active'));
116
117
        $this->assertCount(5, $users);
118
        $this->assertContainsOnly('Doctrine\Tests\Models\CMS\CmsUser', $users);
119
120
        $users = $this->dm->getRepository('Doctrine\Tests\Models\CMS\CmsUser')->findBy(array('status' => 'active'), null, 2);
121
        $this->assertCount(2, $users);
122
123
        $users = $this->dm->getRepository('Doctrine\Tests\Models\CMS\CmsUser')->findBy(array('status' => 'inactive'));
124
125
        $this->assertCount(5, $users);
126
        $this->assertContainsOnly('Doctrine\Tests\Models\CMS\CmsUser', $users);
127
128
        $users = $this->dm->getRepository('Doctrine\Tests\Models\CMS\CmsUser')->findBy(array('status' => 'active', 'username' => 'beberlei0'));
129
        $this->assertCount(1, $users);
130
131
        $users = $this->dm->getRepository('Doctrine\Tests\Models\CMS\CmsUser')->findBy(array('status' => 'active', 'name' => 'Benjamin'), null, 2);
132
        $this->assertCount(2, $users);
133
    }
134
135
    public function testFindByManyConstraints()
136
    {
137 View Code Duplication
        for ($i = 0; $i < 10; $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...
138
            $user = new \Doctrine\Tests\Models\CMS\CmsUser();
139
            $user->username = "beberlei" . $i;
140
            $user->status = ($i % 2 == 0) ? "active" : "inactive";
141
            $user->name = "Benjamin" . $i;
142
143
            $this->dm->persist($user);
144
        }
145
        $this->dm->flush();
146
147
        $users = $this->dm->getRepository('Doctrine\Tests\Models\CMS\CmsUser')->findBy(array('username' => 'beberlei0'));
148
        $this->assertCount(1, $users);
149
        $this->assertEquals('beberlei0', $users[0]->username);
150
    }
151
152 View Code Duplication
    public function testFindByIdUsesIdentityMap()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
153
    {
154
        $this->dm = $this->createDocumentManager();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createDocumentManager() of type object<Doctrine\ODM\CouchDB\DocumentManager> is incompatible with the declared type object<Doctrine\Tests\OD...tional\DocumentManager> of property $dm.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
155
156
        $user = new \Doctrine\Tests\Models\CMS\CmsUser();
157
        $user->username = "beberlei";
158
        $user->status = "active";
159
        $user->name = "Benjamin";
160
161
        $this->dm->persist($user);
162
        $this->dm->flush();
163
        $this->dm->clear();
164
165
        $user1 = $this->dm->getRepository('Doctrine\Tests\Models\CMS\CmsUser')->find($user->id);
166
        $user2 = $this->dm->getRepository('Doctrine\Tests\Models\CMS\CmsUser')->find($user->id);
167
168
        $this->assertSame($user1, $user2);
169
    }
170
171
    public function testFindByReusesIdentities()
172
    {
173
        $this->dm = $this->createDocumentManager();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createDocumentManager() of type object<Doctrine\ODM\CouchDB\DocumentManager> is incompatible with the declared type object<Doctrine\Tests\OD...tional\DocumentManager> of property $dm.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
174
175
        $user = new \Doctrine\Tests\Models\CMS\CmsUser();
176
        $user->username = "beberlei";
177
        $user->status = "active";
178
        $user->name = "Benjamin";
179
180
        $this->dm->persist($user);
181
        $this->dm->flush();
182
        $this->dm->clear();
183
184
        $user1 = $this->dm->getRepository('Doctrine\Tests\Models\CMS\CmsUser')->findOneBy(array('username' => 'beberlei'));
185
        $user2 = $this->dm->getRepository('Doctrine\Tests\Models\CMS\CmsUser')->findOneBy(array('username' => 'beberlei'));
186
187
        $this->assertSame($user1, $user2);
188
    }
189
}
190