MongoStorageTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 39
Duplicated Lines 20.51 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 3
c 1
b 0
f 1
lcom 1
cbo 4
dl 8
loc 39
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A clearCollection() 0 4 1
A testStoreMessage() 0 4 1
A testGetHistory() 8 8 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
namespace tests\codeception\unit;
3
4
use Yii;
5
use yii\codeception\TestCase;
6
use jones\wschat\components\AbstractStorage;
7
use jones\wschat\collections\History;
8
9
class MongoStorageTest extends TestCase
10
{
11
    protected $params = [
12
        'chat_id' => 1,
13
        'chat_title' => 'MongoDb chat room',
14
        'user_id' => 1,
15
        'username' => 'MondoDB user',
16
        'avatar_16' => null,
17
        'avatar_32' => null,
18
        'message' => 'Simple text message',
19
        'timestamp' => 0
20
    ];
21
22
    protected function clearCollection()
23
    {
24
        Yii::$app->mongodb->getCollection(History::collectionName())->remove();
25
    }
26
27
    /**
28
     * @covers \jones\wschat\collections\History::storeMessage
29
     */
30
    public function testStoreMessage()
31
    {
32
        AbstractStorage::factory('mongodb')->storeMessage($this->params);
33
    }
34
35
    /**
36
     * @depends testStoreMessage
37
     * @covers \jones\wschat\collections\History::getHistory
38
     */
39 View Code Duplication
    public function testGetHistory()
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...
40
    {
41
        $data = AbstractStorage::factory('mongodb')->getHistory($this->params['chat_id']);
42
        $this->assertNotEmpty($data);
43
        $this->assertEquals(1, sizeof($data), 'Only one message should be in history collection');
44
        $this->assertEquals($this->params['message'], $data[0]['message']);
45
        $this->clearCollection();
46
    }
47
}