RepositoryTest   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 290
Duplicated Lines 51.38 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 14
c 3
b 0
f 0
lcom 1
cbo 6
dl 149
loc 290
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 8 8 1
A tearDown() 0 4 1
A testGetRepository() 0 10 1
B testInsertMany() 0 25 1
A testUpdate() 0 17 1
B testUpdateMany() 24 24 1
A testReplaceOne() 23 23 1
A testDelete() 0 18 1
A testDeleteOne() 23 23 1
A testDeleteMany() 23 23 1
B testAggregate() 0 38 2
B testMin() 24 24 1
B testMax() 24 24 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 Pouzor\MongoDBBundle\Tests\Unit\DocumentManager;
4
5
use Pouzor\MongoDBBundle\DocumentManager\DocumentManager;
6
use Psr\Log\NullLogger;
7
8
class RepositoryTest extends \PHPUnit_Framework_TestCase
9
{
10
    private $manager = null;
11
    private $repository = null;
12
13
    private $config = [
14
        "db" => "mongodbbundle",
15
        "host" => "localhost",
16
        "port" => "27017",
17
        "username" => null,
18
        "password" => null,
19
        "schema" => ['Foo' => ['indexes' => ["bar" => -1]]],
20
        "options" => []
21
    ];
22
23 View Code Duplication
    protected function setUp()
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...
24
    {
25
        $logger = new NullLogger();
26
        $this->manager = new DocumentManager($this->config, $logger);
27
        $this->repository = $this->manager->getRepository("Foo");
28
        $this->manager->removeAll("Foo");
29
30
    }
31
32
    protected function tearDown()
33
    {
34
        $this->manager = null;
35
    }
36
37
    public function testGetRepository()
38
    {
39
40
        $this->assertEquals(get_class($this->repository), 'Pouzor\MongoDBBundle\Repository\Repository');
41
        $this->assertEquals("Foo", $this->repository->getName());
42
43
        $indexes = $this->repository->getIndexes();
44
        $this->assertArrayHasKey("bar", $indexes);
45
        $this->assertEquals(-1, $indexes['bar']);
46
    }
47
48
    public function testInsertMany()
49
    {
50
        $datas = [
51
            [
52
                "name" => "foo",
53
                "value" => 1
54
            ],
55
            [
56
                "name" => "bar",
57
                "value" => 2
58
            ]
59
        ];
60
61
        $this->assertEquals(0, $this->repository->count());
62
        $this->repository->insertMany($datas);
63
64
        $this->assertEquals(2, $this->repository->count());
65
66
        $foo = $this->repository->findOneBy(["name" => "foo"]);
67
        $this->assertEquals(1, $foo['value']);
68
69
        $bar = $this->repository->findOneBy(["name" => "bar"]);
70
        $this->assertEquals(2, $bar['value']);
71
72
    }
73
74
    public function testUpdate()
75
    {
76
        $data = [
77
            "name" => "foo",
78
            "value" => 1
79
        ];
80
81
        $test = $this->repository->insertOne($data);
82
83
        $this->assertEquals(1, $this->repository->count());
84
        $this->repository->update($test->getInsertedId(), ['$set' => ["value" => 2]]);
85
86
        $newValue = $this->repository->find($test->getInsertedId());
87
88
        $this->assertEquals(2, $newValue["value"]);
89
        $this->assertEquals(1, $this->repository->count());
90
    }
91
92 View Code Duplication
    public function testUpdateMany()
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...
93
    {
94
        $datas = [
95
            [
96
                "name" => "foo",
97
                "value" => 1
98
            ],
99
            [
100
                "name" => "bar",
101
                "value" => 2
102
            ],
103
            [
104
                "name" => "foo",
105
                "value" => 3
106
            ]
107
        ];
108
109
        $this->repository->insertMany($datas);
110
        $this->assertEquals(1, $this->repository->count(['name' => 'foo', 'value' => 3]));
111
112
        $this->repository->updateMany(['name' => 'foo'], ['$set' => ['value' => 3]]);
113
        $this->assertEquals(2, $this->repository->count(['name' => 'foo', 'value' => 3]));
114
115
    }
116
117 View Code Duplication
    public function testReplaceOne()
118
    {
119
        $datas = [
120
            [
121
                "name" => "foo",
122
                "value" => 1
123
            ],
124
            [
125
                "name" => "bar",
126
                "value" => 2
127
            ],
128
            [
129
                "name" => "foo",
130
                "value" => 3
131
            ]
132
        ];
133
134
        $this->repository->insertMany($datas);
135
        $this->assertEquals(1, $this->repository->count(['name' => 'foo', 'value' => 3]));
136
        $this->repository->replaceOne(['name' => 'foo'], ["name" => "foo", "value" => 5]);
137
138
        $this->assertEquals(1, $this->repository->count(["name" => "foo", "value" => 5]));
139
    }
140
141
    public function testDelete()
142
    {
143
        $data = [
144
            "name" => "foo",
145
            "value" => 1
146
        ];
147
148
        $test = $this->repository->insertOne($data);
149
        $this->assertEquals(1, $this->repository->count());
150
151
        $return = $this->repository->delete($test->getInsertedId());
152
        $this->assertEquals(0, $this->repository->count());
153
        $this->assertEquals(1, $return->getDeletedCount());
154
        $return = $this->repository->delete($test->getInsertedId());
155
156
        $this->assertEquals(0, $return->getDeletedCount());
157
158
    }
159
160 View Code Duplication
    public function testDeleteOne()
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...
161
    {
162
        $datas = [
163
            [
164
                "name" => "foo",
165
                "value" => 1
166
            ],
167
            [
168
                "name" => "bar",
169
                "value" => 2
170
            ],
171
            [
172
                "name" => "foo",
173
                "value" => 3
174
            ]
175
        ];
176
177
        $this->repository->insertMany($datas);
178
        $this->assertEquals(2, $this->repository->count(['name' => 'foo']));
179
180
        $this->repository->deleteOne(['name' => 'foo']);
181
        $this->assertEquals(1, $this->repository->count(['name' => 'foo']));
182
    }
183
184 View Code Duplication
    public function testDeleteMany()
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...
185
    {
186
        $datas = [
187
            [
188
                "name" => "foo",
189
                "value" => 1
190
            ],
191
            [
192
                "name" => "bar",
193
                "value" => 2
194
            ],
195
            [
196
                "name" => "foo",
197
                "value" => 3
198
            ]
199
        ];
200
201
        $this->repository->insertMany($datas);
202
        $this->assertEquals(2, $this->repository->count(['name' => 'foo']));
203
204
        $this->repository->deletemany(['name' => 'foo']);
205
        $this->assertEquals(0, $this->repository->count(['name' => 'foo']));
206
    }
207
208
    public function testAggregate()
209
    {
210
        $datas = [
211
            [
212
                "name" => "foo",
213
                "value" => 1
214
            ],
215
            [
216
                "name" => "bar",
217
                "value" => 2
218
            ],
219
            [
220
                "name" => "foo",
221
                "value" => 3
222
            ]
223
        ];
224
225
        $this->repository->insertMany($datas);
226
227
        $result = $this->repository->aggregate(
228
            [
229
                [
230
                    '$match' => [
231
                        "name" => "bar"
232
                    ]
233
                ]
234
            ]
235
        );
236
237
        $i = 0;
238
        //In specific version of mongodb driver, $result->toArray() doesn't work, nor count
239
        foreach ($result as $data) {
240
            $i++;
241
        }
242
243
        $this->assertEquals(1, $i);
244
245
    }
246
247 View Code Duplication
    public function testMin() {
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...
248
        $datas = [
249
            [
250
                "name" => "foo",
251
                "value" => 10
252
            ],
253
            [
254
                "name" => "bar",
255
                "value" => 5
256
            ],
257
            [
258
                "name" => "foo",
259
                "value" => 30
260
            ]
261
        ];
262
263
        $this->repository->insertMany($datas);
264
265
        $result = $this->repository->min("value");
266
        $this->assertEquals(5, $result);
267
268
        $result = $this->repository->min("value", ["name" => "foo"]);
269
        $this->assertEquals(10, $result);
270
    }
271
272 View Code Duplication
    public function testMax() {
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...
273
        $datas = [
274
            [
275
                "name" => "foo",
276
                "value" => 10
277
            ],
278
            [
279
                "name" => "bar",
280
                "value" => 5
281
            ],
282
            [
283
                "name" => "foo",
284
                "value" => 30
285
            ]
286
        ];
287
288
        $this->repository->insertMany($datas);
289
290
        $result = $this->repository->max("value");
291
        $this->assertEquals(30, $result);
292
293
        $result = $this->repository->max("value", ["name" => "bar"]);
294
        $this->assertEquals(5, $result);
295
    }
296
297
}