1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Validator |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2017 pudelek.org.pl |
6
|
|
|
* |
7
|
|
|
* @license MIT License (MIT) |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view source file |
10
|
|
|
* that is bundled with this package in the file LICENSE |
11
|
|
|
* |
12
|
|
|
* @author Marcin Pudełek <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace mrcnpdlk\Validator; |
16
|
|
|
|
17
|
|
|
use mrcnpdlk\Validator\Types\IPv4; |
18
|
|
|
|
19
|
|
|
class IPv4Test extends TestCase |
20
|
|
|
{ |
21
|
|
View Code Duplication |
public function testIpv4IntValid() |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
$defNr = 1527088647; |
24
|
|
|
$this->assertTrue(IPv4::isValid($defNr, false)); |
25
|
|
|
$res = new IPv4($defNr); |
26
|
|
|
$this->assertEquals('91.5.134.7', $res->get()); |
27
|
|
|
$this->assertEquals('091.005.134.007', $res->getLeadingZeros()); |
28
|
|
|
$this->assertEquals(1527088647, $res->getLong()); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
View Code Duplication |
public function testIpv4StringValid() |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
$defNr = '91.5.134.007'; |
34
|
|
|
$this->assertTrue(IPv4::isValid($defNr, false)); |
35
|
|
|
$res = new IPv4($defNr); |
36
|
|
|
$this->assertEquals('91.5.134.7', $res->get()); |
37
|
|
|
$this->assertEquals('091.005.134.007', $res->getLeadingZeros()); |
38
|
|
|
$this->assertEquals(1527088647, $res->getLong()); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @expectedException \mrcnpdlk\Validator\Exception |
43
|
|
|
*/ |
44
|
|
|
public function testIpv4InvalidValid() |
45
|
|
|
{ |
46
|
|
|
IPv4::isValid('1.1.1.300',true); |
47
|
|
|
} |
48
|
|
|
public function testIpv4Local() |
49
|
|
|
{ |
50
|
|
|
$this->assertTrue((new IPv4('127.0.0.1'))->isLocalIPAddress()); |
51
|
|
|
$this->assertTrue((new IPv4('10.0.0.1'))->isLocalIPAddress()); |
52
|
|
|
$this->assertTrue((new IPv4('10.255.255.255'))->isLocalIPAddress()); |
53
|
|
|
$this->assertTrue((new IPv4('172.16.0.1'))->isLocalIPAddress()); |
54
|
|
|
$this->assertTrue((new IPv4('172.31.255.255'))->isLocalIPAddress()); |
55
|
|
|
$this->assertTrue((new IPv4('192.168.0.1'))->isLocalIPAddress()); |
56
|
|
|
$this->assertTrue((new IPv4('192.168.255.254'))->isLocalIPAddress()); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.