NotifyCommandTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 186
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 5
c 2
b 0
f 1
lcom 1
cbo 9
dl 0
loc 186
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
B setUp() 0 35 1
B testGetConfig() 0 37 1
B testGetGauges() 0 29 1
A testGetStatsd() 0 53 1
A getInputInterface() 0 10 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Petrica
5
 * Date: 3/30/2016
6
 * Time: 23:17
7
 */
8
namespace Petrica\StatsdSystem\Tests\Command;
9
10
use Domnikl\Statsd\Client;
11
use Petrica\StatsdSystem\Statsd\Connection\UdpSocket;
12
use org\bovigo\vfs\vfsStream;
13
use org\bovigo\vfs\vfsStreamDirectory;
14
use org\bovigo\vfs\vfsStreamFile;
15
use org\bovigo\vfs\vfsStreamWrapper;
16
use Petrica\StatsdSystem\Command\NotifyCommand;
17
use Petrica\StatsdSystem\Gauge\CpuAverageGauge;
18
use Petrica\StatsdSystem\Gauge\MemoryGauge;
19
20
class NotifyCommandTest extends \PHPUnit_Framework_TestCase
21
{
22
    /**
23
     * @var vfsStreamFile
24
     */
25
    private $validFile;
26
27
    /**
28
     * @var vfsStreamFile
29
     */
30
    private $invalidFile;
31
32
    public function setUp()
33
    {
34
        parent::setUp();
35
36
        vfsStreamWrapper::register();
37
        $root = vfsStreamWrapper::setRoot(new vfsStreamDirectory('root'));
38
39
        // Create configuration yml
40
        $yml = <<<'DOC'
41
gauges:
42
    cpu:
43
        class: Petrica\StatsdSystem\Gauge\CpuAverageGauge
44
        arguments: ~
45
46
    memory:
47
        class: Petrica\StatsdSystem\Gauge\MemoryGauge
48
DOC;
49
50
        $this->validFile = vfsStream::newFile('gauges.yml')
51
            ->withContent($yml)
52
            ->at($root);
53
54
55
        $yml = <<<'DOC'
56
gauges:
57
    cpu:
58
        class: Petrica\StatsdSystem\Gauge\CpuAverageGauge
59
        arguments: ~
60
61
    memory:
62
DOC;
63
        $this->invalidFile = vfsStream::newFile('wrong_gauges.yml')
64
            ->withContent($yml)
65
            ->at($root);
66
    }
67
68
    public function testGetConfig()
69
    {
70
        /** @var NotifyCommand $command */
71
        $command = $this->getMockBuilder('Petrica\StatsdSystem\Command\NotifyCommand')
72
            ->disableOriginalConstructor()
73
            ->getMock();
74
75
        $class = new \ReflectionClass('Petrica\StatsdSystem\Command\NotifyCommand');
76
        $getConfig = $class->getMethod('getConfiguration');
77
        $getConfig->setAccessible(true);
78
79
        $input = $this->getInputInterface();
80
81
        $input->method('getArgument')
82
            ->willReturn($this->validFile->url());
83
84
        $loader = $getConfig->invokeArgs($command, array($input));
85
86
        $this->assertEquals(array(
87
            'cpu' => array(
88
                'class' => 'Petrica\StatsdSystem\Gauge\CpuAverageGauge',
89
                'arguments' => null
90
            ),
91
            'memory' => array(
92
                'class' => 'Petrica\StatsdSystem\Gauge\MemoryGauge'
93
            )
94
        ), $loader);
95
96
        $input = $this->getInputInterface();
97
98
        $input->method('getArgument')
99
            ->willReturn($this->invalidFile->url());
100
101
        // Test wrong configuration exception
102
        $this->setExpectedException('Symfony\Component\Config\Definition\Exception\InvalidConfigurationException');
103
        $getConfig->invokeArgs($command, array($input));
104
    }
105
106
    public function testGetGauges()
107
    {
108
        $command = $this->getMockBuilder('Petrica\StatsdSystem\Command\NotifyCommand')
109
            ->disableOriginalConstructor()
110
            ->getMock();
111
112
        $config = array(
113
            'cpu' => array(
114
                'class' => 'Petrica\StatsdSystem\Gauge\CpuAverageGauge',
115
                'arguments' => array('test' => 1)
116
            ),
117
            'memory' => array(
118
                'class' => 'Petrica\StatsdSystem\Gauge\MemoryGauge'
119
            )
120
        );
121
122
        $class = new \ReflectionClass('Petrica\StatsdSystem\Command\NotifyCommand');
123
        $getGauges = $class->getMethod('getGauges');
124
        $getGauges->setAccessible(true);
125
126
        $gauges = $getGauges->invokeArgs($command, array($config));
127
128
        $expects = array(
129
            'cpu' => new CpuAverageGauge(),
130
            'memory' => new MemoryGauge()
131
        );
132
133
        $this->assertEquals($expects, $gauges);
134
    }
135
136
    public function testGetStatsd()
137
    {
138
        $command = $this->getMockBuilder('Petrica\StatsdSystem\Command\NotifyCommand')
139
            ->disableOriginalConstructor()
140
            ->getMock();
141
142
        $input = $this->getInputInterface();
143
144
        $map = array(
145
            [ 'statsd-host', 'localhost' ],
146
            [ 'statsd-port', '8125' ],
147
            [ 'statsd-namespace', 'test' ]
148
        );
149
150
        $input->expects($this->atLeastOnce())
151
            ->method('getOption')
152
            ->will($this->returnValueMap($map));
153
154
        $class = new \ReflectionClass('Petrica\StatsdSystem\Command\NotifyCommand');
155
        $getStatsd = $class->getMethod('getStatsd');
156
        $getStatsd->setAccessible(true);
157
158
        $statsd = $getStatsd->invokeArgs($command, array($input));
159
160
        $connection = new UdpSocket('localhost', 8125);
161
        $expects = new Client($connection, 'test');
162
163
        $this->assertEquals($expects, $statsd);
164
165
        // Not equal host
166
        $connection = new UdpSocket('localhost1', 8125);
167
        $expects = new Client($connection, 'test');
168
169
        $statsd = $getStatsd->invokeArgs($command, array($input));
170
171
        $this->assertNotEquals($expects, $statsd);
172
173
        // Not equal port
174
        $connection = new UdpSocket('localhost', 8126);
175
        $expects = new Client($connection, 'test');
176
177
        $statsd = $getStatsd->invokeArgs($command, array($input));
178
179
        $this->assertNotEquals($expects, $statsd);
180
181
        // Not equal namespace
182
        $connection = new UdpSocket('localhost', 8125);
183
        $expects = new Client($connection, 'test1');
184
185
        $statsd = $getStatsd->invokeArgs($command, array($input));
186
187
        $this->assertNotEquals($expects, $statsd);
188
    }
189
190
    /**
191
     * Return a mock object of the inputinterface
192
     *
193
     * @return \PHPUnit_Framework_MockObject_MockObject
194
     */
195
    protected function getInputInterface()
196
    {
197
        return $this->getMockBuilder('Symfony\Component\Console\Input\ArrayInput')
198
            ->disableOriginalConstructor()
199
            ->setMethods(array(
200
                'getArgument',
201
                'getOption'
202
            ))
203
            ->getMock();
204
    }
205
}