Passed
Pull Request — master (#1312)
by Michael
05:26
created

IPAddressTest::testSameSubnet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 15
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 19
rs 9.7666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Xmf\Test;
6
7
use PHPUnit\Framework\TestCase;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
9
10
use Xmf\IPAddress;
11
12
class IPAddressTest extends TestCase
13
{
14
    /**
15
     * @var IPAddress
16
     */
17
    protected $object;
18
    /**
19
     * @var IPAddress
20
     */
21
    protected $objectV6;
22
    /** @var string ip address of setUp object */
23
    protected $testIPV4 = '192.168.20.12';
24
    /** @var string ip address of setUp object */
25
    protected $testIPV6 = '3ffe:2a00:100:7031::1';
26
27
    /**
28
     * Sets up the fixture, for example, opens a network connection.
29
     * This method is called before a test is executed.
30
     */
31
    protected function setUp(): void
32
    {
33
        $this->object   = new IPAddress($this->testIPV4);
34
        $this->objectV6 = new IPAddress($this->testIPV6);
35
    }
36
37
    /**
38
     * Tears down the fixture, for example, closes a network connection.
39
     * This method is called after a test is executed.
40
     */
41
    protected function tearDown(): void
42
    {
43
    }
44
45
    public function testFromRequest()
46
    {
47
        $testAddress            = '192.168.20.16';
48
        $_SERVER['REMOTE_ADDR'] = $testAddress;
49
        $instance               = IPAddress::fromRequest();
50
        $actual                 = $instance->asReadable();
51
        $this->assertEquals($testAddress, $actual);
52
53
        $testAddress            = '3ffe:2a00:100:7031::1';
54
        $_SERVER['REMOTE_ADDR'] = $testAddress;
55
        $instance               = IPAddress::fromRequest();
56
        $actual                 = $instance->asReadable();
57
        $this->assertEquals($testAddress, $actual);
58
    }
59
60
    public function testAsReadable()
61
    {
62
        $this->assertEquals($this->testIPV4, $this->object->asReadable());
63
        $this->assertEquals($this->testIPV6, $this->objectV6->asReadable());
64
    }
65
66
    public function testAsBinary()
67
    {
68
        $addressV6  = '3031:3233:3435:3637:3839:584F:4F50:5334';
69
        $instanceV6 = new IPAddress($addressV6);
70
        $this->assertEquals('0123456789XOOPS4', $instanceV6->asBinary());
71
72
        $addressV4  = '67.48.68.69';
73
        $instanceV4 = new IPAddress($addressV4);
74
        $this->assertEquals('C0DE', $instanceV4->asBinary());
75
    }
76
77
    public function testIpVersion()
78
    {
79
        $this->assertSame(4, $this->object->ipVersion());
80
        $this->assertSame(6, $this->objectV6->ipVersion());
81
82
        $instance = new IPAddress('garbage');
83
        $this->assertFalse($instance->ipVersion());
84
    }
85
86
    public function testSameSubnet()
87
    {
88
        $instanceV6 = new IPAddress('FE80:0000:0000:0000:0202:B3FF:FE1E:8329');
89
        $addressV6  = 'FE80:0000:0000:0000:8000:0000:0000:0000';
90
91
        $this->assertFalse($instanceV6->sameSubnet($this->testIPV4, 16, 64));
92
        $this->assertTrue($instanceV6->sameSubnet($addressV6, 14, 56));
93
        $this->assertTrue($instanceV6->sameSubnet($addressV6, 16, 64));
94
        $this->assertFalse($instanceV6->sameSubnet($addressV6, 17, 65));
95
        $this->assertTrue($instanceV6->sameSubnet($instanceV6->asReadable(), 32, 128));
96
97
        $instanceV4 = new IPAddress('255.255.255.1');
98
        $addressV4  = '255.255.255.129';
99
        $this->assertFalse($instanceV4->sameSubnet($instanceV6->asReadable(), 1, 1));
100
        $this->assertTrue($instanceV4->sameSubnet($addressV4, 8, 32));
101
        $this->assertTrue($instanceV4->sameSubnet($addressV4, 16, 64));
102
        $this->assertTrue($instanceV4->sameSubnet($addressV4, 24, 96));
103
        $this->assertFalse($instanceV4->sameSubnet($addressV6, 25, 98));
104
        $this->assertTrue($instanceV4->sameSubnet($instanceV4->asReadable(), 32, 128));
105
    }
106
}
107