RealWorldTest::testConsolidateSubnets()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace AndrewAndante\SubMuncher\Test;
4
5
use AndrewAndante\SubMuncher\SubMuncher;
6
use AndrewAndante\SubMuncher\Util;
7
8
class RealWorldTest extends \PHPUnit_Framework_TestCase
9
{
10
    private $json_data;
11
12
    protected function setUp()
13
    {
14
        parent::setUp();
15
        $this->json_data = json_decode(file_get_contents(__DIR__ . '/../data/real_world_data.json'), true);
16
    }
17
18
    public function testConsolidateSubnets()
19
    {
20
        $this->assertEquals(
21
            $this->json_data['consolidated_subnets'],
22
            SubMuncher::consolidate_subnets($this->json_data['subnets'])
23
        );
24
    }
25
26
    public function testConsolidateSubnetsVerbose()
27
    {
28
        $this->assertEquals(
29
            [
30
                'consolidated_subnets' => $this->json_data['consolidated_subnets'],
31
                'initial_IPs' => $this->json_data['raw_ips'],
32
                'total_IPs' => $this->json_data['raw_ips'],
33
            ],
34
            SubMuncher::consolidate_subnets_verbose($this->json_data['subnets'])
35
        );
36
    }
37
38
    public function testConsolidateSubnetsPerformance()
39
    {
40
        $result = SubMuncher::consolidate_subnets($this->json_data['subnets'], 1);
41
        $this->assertLessThanOrEqual(1, count($result));
42
    }
43
44
    public function testConsolidateSubnetsWithMaxRules()
45
    {
46
        $result = SubMuncher::consolidate_subnets($this->json_data['subnets'], 25);
47
        $fail = true;
48
        foreach ($this->json_data['raw_ips'] as $ip) {
49
            foreach ($result as $subnet) {
50
                if (Util::cidr_contains($subnet, $ip)) {
51
                    $fail = false;
52
                    break;
53
                }
54
            }
55
        }
56
57
        if ($fail) {
58
            $this->fail($ip . " not contained in any subnets returned");
0 ignored issues
show
Bug introduced by
The variable $ip seems to be defined by a foreach iteration on line 48. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
59
        }
60
    }
61
}
62