Completed
Pull Request — master (#10)
by Alejandro
07:33
created

IpAddressTest::getPropFromIpAddress()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
dl 0
loc 7
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
declare(strict_types=1);
3
4
namespace ShlinkioTest\Shlink\Common\Util;
5
6
use PHPUnit\Framework\TestCase;
7
use ReflectionObject;
8
use Shlinkio\Shlink\Common\Exception\InvalidArgumentException;
9
use Shlinkio\Shlink\Common\Util\IpAddress;
10
11
use function sprintf;
12
13
class IpAddressTest extends TestCase
14
{
15
    /**
16
     * @test
17
     * @dataProvider provideInvalidAddresses
18
     */
19
    public function exceptionIsThrownWhenTryingToParseInvalidIp(string $invalidAddress): void
20
    {
21
        $this->expectException(InvalidArgumentException::class);
22
        $this->expectExceptionMessage(sprintf('Provided IP "%s" is invalid', $invalidAddress));
23
24
        IpAddress::fromString($invalidAddress);
25
    }
26
27
    public function provideInvalidAddresses(): iterable
28
    {
29
        yield [''];
30
        yield ['1'];
31
        yield ['1.1'];
32
        yield ['1.1.1'];
33
        yield ['1.1.1.1.1'];
34
        yield ['1.1.1.1.1.1'];
35
    }
36
37
    /**
38
     * @test
39
     * @dataProvider provideValidAddresses
40
     */
41
    public function validAddressesAreProperlyParsed(
42
        string $validAddress,
43
        string $firstOctet,
44
        string $secondOctet,
45
        string $thirdOctet,
46
        string $fourthOctet
47
    ): void {
48
        $address = IpAddress::fromString($validAddress);
49
50
        $this->assertEquals($firstOctet, $this->getPropFromIpAddress($address, 'firstOctet'));
51
        $this->assertEquals($secondOctet, $this->getPropFromIpAddress($address, 'secondOctet'));
52
        $this->assertEquals($thirdOctet, $this->getPropFromIpAddress($address, 'thirdOctet'));
53
        $this->assertEquals($fourthOctet, $this->getPropFromIpAddress($address, 'fourthOctet'));
54
        $this->assertEquals($validAddress, (string) $address);
55
    }
56
57
    /**
58
     * @test
59
     * @dataProvider provideValidAddresses
60
     */
61
    public function addressesRemoveLastOctetWhenObfuscated(
62
        string $validAddress,
63
        string $firstOctet,
64
        string $secondOctet,
65
        string $thirdOctet
66
    ): void {
67
        $obfuscatedAddress = IpAddress::fromString($validAddress)->getObfuscatedCopy();
68
69
        $this->assertEquals($firstOctet, $this->getPropFromIpAddress($obfuscatedAddress, 'firstOctet'));
70
        $this->assertEquals($secondOctet, $this->getPropFromIpAddress($obfuscatedAddress, 'secondOctet'));
71
        $this->assertEquals($thirdOctet, $this->getPropFromIpAddress($obfuscatedAddress, 'thirdOctet'));
72
        $this->assertEquals(0, $this->getPropFromIpAddress($obfuscatedAddress, 'fourthOctet'));
73
    }
74
75
    public function provideValidAddresses(): iterable
76
    {
77
        yield ['1.1.1.1', '1', '1', '1', '1'];
78
        yield ['2.2.2.2', '2', '2', '2', '2'];
79
        yield ['1.2.3.4', '1', '2', '3', '4'];
80
        yield ['192.168.1.254', '192', '168', '1', '254'];
81
        yield ['8.8.8.8', '8', '8', '8', '8'];
82
        yield ['8.8.4.4', '8', '8', '4', '4'];
83
    }
84
85
    private function getPropFromIpAddress(IpAddress $address, string $propName)
86
    {
87
        $ref = new ReflectionObject($address);
88
        $prop = $ref->getProperty($propName);
89
        $prop->setAccessible(true);
90
91
        return $prop->getValue($address);
92
    }
93
}
94