ProcessesGaugeTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 123
Duplicated Lines 40.65 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 3
c 2
b 1
f 1
lcom 1
cbo 7
dl 50
loc 123
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetGauge() 0 62 1
B testPersistCollection() 25 25 1
B testPersistRemoteCollection() 25 25 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
 * Created by PhpStorm.
4
 * User: Petrica
5
 * Date: 5/29/2016
6
 * Time: 13:10
7
 */
8
namespace Petrica\StatsdSystem\Tests\Gauge;
9
10
use Petrica\StatsdSystem\Collection\ValuesCollection;
11
use Petrica\StatsdSystem\Gauge\ProcessesGauge;
12
use Petrica\StatsdSystem\Gauge\RemoteProcessesGauge;
13
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
14
15
class ProcessesGaugeTest extends \PHPUnit_Framework_TestCase
16
{
17
    public function testGetGauge()
18
    {
19
        $gauge = $this->getMockBuilder('Petrica\StatsdSystem\Gauge\ProcessesGauge')
20
            ->setMethods(array(
21
                'getCommand'
22
            ))
23
            ->getMock();
24
25
        $command = $this->getMockBuilder('Petrica\StatsdSystem\Model\TopCommand')
26
            ->setMethods(array(
27
                'run'
28
            ))
29
            ->getMock();
30
31
        $command->method('run')
32
            ->willReturn(array(
33
                array(),
34
                array(),
35
                array(),
36
                array(),
37
                array(),
38
                array(),
39
                array(),
40
                array(
41
                    '1', 'root', '0', '0', '0', '0', '0', '0', '10.5', '5.5', '0:0', 'process_name'
42
                ),
43
                array(
44
                    '2', 'root', '0', '0', '0', '0', '0', '0', '3.5', '1.5', '0:0', 'process_name'
45
                ),
46
                array(
47
                    '3', 'root', '0', '0', '0', '0', '0', '0', '2', '0.5', '0:0', 'process_name_exclude'
48
                ),
49
                array(
50
                    '4', 'root', '0', '0', '0', '0', '0', '0', '5', '2.5', '0:0', 'other_process'
51
                )
52
            ));
53
54
        $gauge->method('getCommand')
55
            ->willReturn($command);
56
57
        $cache = new FilesystemAdapter('statsd.localhost');
58
        $item = $cache->getItem('collection');
59
60
        $cacheCollection = new ValuesCollection();
61
        $cacheCollection->add('test_cache', 5);
62
63
        $item->set($cacheCollection);
64
        $cache->save($item);
65
66
        $collection = $gauge->getCollection();
67
68
        $expected = new ValuesCollection();
69
        $expected->add('process_name.cpu.value', 14);
70
        $expected->add('other_process.cpu.value', 5);
71
        $expected->add('process_name.memory.value', 7);
72
        $expected->add('other_process.memory.value', 2.5);
73
        $expected->add('test_cache', 0);
74
75
        $this->assertEquals($expected, $collection);
76
77
        $cache->clear();
78
    }
79
80
    /**
81
     * Test cache system
82
     */
83 View Code Duplication
    public function testPersistCollection()
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...
84
    {
85
        $gauge = new ProcessesGauge();
86
87
        $class = new \ReflectionClass('Petrica\StatsdSystem\Gauge\ProcessesGauge');
88
        $persistMethod = $class->getMethod('persistCollection');
89
        $persistMethod->setAccessible(true);
90
91
        $class = new \ReflectionClass('Petrica\StatsdSystem\Gauge\ProcessesGauge');
92
        $retrieveCollection = $class->getMethod('retrieveCollection');
93
        $retrieveCollection->setAccessible(true);
94
95
        $collection = new ValuesCollection();
96
        $collection->add('test_1', 1);
97
        $collection->add('test_2', 2);
98
99
        $persistMethod->invokeArgs($gauge, array($collection));
100
101
        $retreive = $retrieveCollection->invoke($gauge);
102
103
        $cache = new FilesystemAdapter('statsd.localhost');
104
        $cache->clear();
105
106
        $this->assertEquals($collection, $retreive);
107
    }
108
109
    /**
110
     * Test remote cache system
111
     */
112 View Code Duplication
    public function testPersistRemoteCollection()
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...
113
    {
114
        $gauge = new RemoteProcessesGauge('[email protected]', 22);
115
116
        $class = new \ReflectionClass('Petrica\StatsdSystem\Gauge\ProcessesGauge');
117
        $persistMethod = $class->getMethod('persistCollection');
118
        $persistMethod->setAccessible(true);
119
120
        $class = new \ReflectionClass('Petrica\StatsdSystem\Gauge\ProcessesGauge');
121
        $retrieveCollection = $class->getMethod('retrieveCollection');
122
        $retrieveCollection->setAccessible(true);
123
124
        $collection = new ValuesCollection();
125
        $collection->add('test_1', 1);
126
        $collection->add('test_2', 2);
127
128
        $persistMethod->invokeArgs($gauge, array($collection));
129
130
        $retreive = $retrieveCollection->invoke($gauge);
131
132
        $cache = new FilesystemAdapter('statsd.127.0.0.1.22');
133
        $cache->clear();
134
135
        $this->assertEquals($collection, $retreive);
136
    }
137
}
138