85 lines
2.6 KiB
PHP
85 lines
2.6 KiB
PHP
<?php
|
|
// IpAddressTest.php
|
|
// Created: 2021-04-26
|
|
// Updated: 2024-10-02
|
|
|
|
declare(strict_types=1);
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
use PHPUnit\Framework\Attributes\CoversClass;
|
|
use Index\Net\IpAddress;
|
|
|
|
#[CoversClass(IpAddress::class)]
|
|
final class IpAddressTest extends TestCase {
|
|
public function testParsing(): void {
|
|
$v4 = IpAddress::parse('127.0.0.1');
|
|
$this->assertEquals('127.0.0.1', $v4->getAddress());
|
|
$v6 = IpAddress::parse('::1');
|
|
$this->assertEquals('[::1]', $v6->getAddress());
|
|
|
|
$this->expectException(InvalidArgumentException::class);
|
|
$v0 = IpAddress::parse('12.4:424');
|
|
}
|
|
|
|
public function testVersion(): void {
|
|
$v4 = IpAddress::parse('123.53.65.34');
|
|
$this->assertEquals(4, $v4->getVersion());
|
|
$this->assertEquals(4, $v4->getWidth());
|
|
$this->assertTrue($v4->isV4());
|
|
$this->assertFalse($v4->isV6());
|
|
|
|
$v6 = IpAddress::parse('23cd::43c4:2342:acde');
|
|
$this->assertEquals(6, $v6->getVersion());
|
|
$this->assertEquals(16, $v6->getWidth());
|
|
$this->assertTrue($v6->isV6());
|
|
$this->assertFalse($v6->isV4());
|
|
}
|
|
|
|
public function testToString(): void {
|
|
$v4 = IpAddress::parse('53.64.123.86');
|
|
$this->assertEquals('53.64.123.86', (string)$v4);
|
|
|
|
$v6 = IpAddress::parse('abcd:1234::43:211a');
|
|
$this->assertEquals('abcd:1234::43:211a', (string)$v6);
|
|
}
|
|
|
|
public function testEquals(): void {
|
|
$v41 = IpAddress::parse('53.64.123.86');
|
|
$v42 = IpAddress::parse('123.53.65.34');
|
|
$v61 = IpAddress::parse('abcd:1234::43:211a');
|
|
$v62 = IpAddress::parse('23cd::43c4:2342:acde');
|
|
|
|
$this->assertTrue($v41->equals($v41));
|
|
$this->assertFalse($v42->equals($v41));
|
|
$this->assertFalse($v61->equals($v42));
|
|
$this->assertTrue($v62->equals($v62));
|
|
}
|
|
|
|
public function testJsonSerialize(): void {
|
|
$addrs = [
|
|
'53.64.123.86',
|
|
'123.53.65.34',
|
|
'abcd:1234::43:211a',
|
|
'23cd::43c4:2342:acde',
|
|
];
|
|
|
|
foreach($addrs as $addr)
|
|
$this->assertEquals(json_encode($addr), json_encode(IpAddress::parse($addr)));
|
|
}
|
|
|
|
public function testSerializable(): void {
|
|
$addrs = [
|
|
'53.64.123.86',
|
|
'123.53.65.34',
|
|
'abcd:1234::43:211a',
|
|
'23cd::43c4:2342:acde',
|
|
];
|
|
|
|
foreach($addrs as $addr) {
|
|
$obj = IpAddress::parse($addr);
|
|
$ser = serialize($obj);
|
|
$unser = unserialize($ser);
|
|
$this->assertTrue($obj->equals($unser));
|
|
}
|
|
}
|
|
}
|