Passed
Push — master ( 2fe392...1f483c )
by Neil
11:34
created

FpingTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 64
c 1
b 0
f 0
dl 0
loc 103
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testPartialDownPing() 0 19 1
A mockFpingProcess() 0 12 1
A testDuplicatePing() 0 24 1
A testUpPing() 0 19 1
A testDownPing() 0 19 1
1
<?php
2
/**
3
 * FpingTest.php
4
 *
5
 * -Description-
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 * @package    LibreNMS
21
 * @link       http://librenms.org
22
 * @copyright  2019 Tony Murray
23
 * @author     Tony Murray <[email protected]>
24
 */
25
26
namespace LibreNMS\Tests;
27
28
use Symfony\Component\Process\Process;
29
30
class FpingTest extends TestCase
31
{
32
    public function testUpPing()
33
    {
34
        $output = "192.168.1.3 : xmt/rcv/%loss = 3/3/0%, min/avg/max = 0.62/0.71/0.93\n";
35
        $this->mockFpingProcess($output, 0);
36
37
        $expected = [
38
            "xmt" => 3,
39
            "rcv" => 3,
40
            "loss" => 0,
41
            "min" => 0.62,
42
            "max" => 0.93,
43
            "avg" => 0.71,
44
            "dup" => 0,
45
            "exitcode" => 0,
46
        ];
47
48
        $actual = fping('192.168.1.3');
49
50
        $this->assertSame($expected, $actual);
51
    }
52
53
    public function testPartialDownPing()
54
    {
55
        $output = "192.168.1.7 : xmt/rcv/%loss = 5/3/40%, min/avg/max = 0.13/0.23/0.32\n";
56
        $this->mockFpingProcess($output, 0);
57
58
        $expected = [
59
            "xmt" => 5,
60
            "rcv" => 3,
61
            "loss" => 40,
62
            "min" => 0.13,
63
            "max" => 0.32,
64
            "avg" => 0.23,
65
            "dup" => 0,
66
            "exitcode" => 0,
67
        ];
68
69
        $actual = fping('192.168.1.7');
70
71
        $this->assertSame($expected, $actual);
72
    }
73
74
    public function testDownPing()
75
    {
76
        $output = "192.168.53.1 : xmt/rcv/%loss = 3/0/100%\n";
77
        $this->mockFpingProcess($output, 1);
78
79
        $expected = [
80
            "xmt" => 3,
81
            "rcv" => 0,
82
            "loss" => 100,
83
            "min" => 0.0,
84
            "max" => 0.0,
85
            "avg" => 0.0,
86
            "dup" => 0,
87
            "exitcode" => 1,
88
        ];
89
90
        $actual = fping('192.168.53.1');
91
92
        $this->assertSame($expected, $actual);
93
    }
94
95
    public function testDuplicatePing()
96
    {
97
        $output = <<<'OUT'
98
192.168.1.2 : duplicate for [0], 84 bytes, 0.91 ms
99
192.168.1.2 : duplicate for [0], 84 bytes, 0.95 ms
100
192.168.1.2 : xmt/rcv/%loss = 3/3/0%, min/avg/max = 0.68/0.79/0.91
101
OUT;
102
103
        $this->mockFpingProcess($output, 1);
104
105
        $expected = [
106
            "xmt" => 3,
107
            "rcv" => 3,
108
            "loss" => 0,
109
            "min" => 0.68,
110
            "max" => 0.91,
111
            "avg" => 0.79,
112
            "dup" => 2,
113
            "exitcode" => 1,
114
        ];
115
116
        $actual = fping('192.168.1.2');
117
118
        $this->assertSame($expected, $actual);
119
    }
120
121
    private function mockFpingProcess($output, $exitCode)
122
    {
123
        $process = \Mockery::mock(Process::class);
124
        $process->shouldReceive('getCommandLine', 'run');
125
        $process->shouldReceive('getErrorOutput')->andReturn($output);
126
        $process->shouldReceive('getExitCode')->andReturn($exitCode);
127
128
        $this->app->bind(Process::class, function ($app, $params) use ($process) {
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

128
        $this->app->bind(Process::class, function ($app, /** @scrutinizer ignore-unused */ $params) use ($process) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $app is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

128
        $this->app->bind(Process::class, function (/** @scrutinizer ignore-unused */ $app, $params) use ($process) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
129
            return $process;
130
        });
131
132
        return $process;
133
    }
134
}
135