index/tests/IpAddressTest.php

86 lines
2.6 KiB
PHP
Raw Permalink Normal View History

2022-09-13 13:13:11 +00:00
<?php
2024-10-02 01:35:05 +00:00
// IpAddressTest.php
2022-09-13 13:13:11 +00:00
// Created: 2021-04-26
2024-10-02 01:35:05 +00:00
// Updated: 2024-10-02
2022-09-13 13:13:11 +00:00
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\CoversClass;
2024-10-02 01:35:05 +00:00
use Index\Net\IpAddress;
2022-09-13 13:13:11 +00:00
2024-10-02 01:35:05 +00:00
#[CoversClass(IpAddress::class)]
final class IpAddressTest extends TestCase {
2022-09-13 13:13:11 +00:00
public function testParsing(): void {
2024-10-02 01:35:05 +00:00
$v4 = IpAddress::parse('127.0.0.1');
2022-09-13 13:13:11 +00:00
$this->assertEquals('127.0.0.1', $v4->getAddress());
2024-10-02 01:35:05 +00:00
$v6 = IpAddress::parse('::1');
2022-09-13 13:13:11 +00:00
$this->assertEquals('[::1]', $v6->getAddress());
$this->expectException(InvalidArgumentException::class);
2024-10-02 01:35:05 +00:00
$v0 = IpAddress::parse('12.4:424');
2022-09-13 13:13:11 +00:00
}
public function testVersion(): void {
2024-10-02 01:35:05 +00:00
$v4 = IpAddress::parse('123.53.65.34');
2022-09-13 13:13:11 +00:00
$this->assertEquals(4, $v4->getVersion());
$this->assertEquals(4, $v4->getWidth());
$this->assertTrue($v4->isV4());
$this->assertFalse($v4->isV6());
2024-10-02 01:35:05 +00:00
$v6 = IpAddress::parse('23cd::43c4:2342:acde');
2022-09-13 13:13:11 +00:00
$this->assertEquals(6, $v6->getVersion());
$this->assertEquals(16, $v6->getWidth());
$this->assertTrue($v6->isV6());
$this->assertFalse($v6->isV4());
}
public function testToString(): void {
2024-10-02 01:35:05 +00:00
$v4 = IpAddress::parse('53.64.123.86');
2022-09-13 13:13:11 +00:00
$this->assertEquals('53.64.123.86', (string)$v4);
2024-10-02 01:35:05 +00:00
$v6 = IpAddress::parse('abcd:1234::43:211a');
2022-09-13 13:13:11 +00:00
$this->assertEquals('abcd:1234::43:211a', (string)$v6);
}
public function testEquals(): void {
2024-10-02 01:35:05 +00:00
$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');
2022-09-13 13:13:11 +00:00
$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)
2024-10-02 01:35:05 +00:00
$this->assertEquals(json_encode($addr), json_encode(IpAddress::parse($addr)));
2022-09-13 13:13:11 +00:00
}
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) {
2024-10-02 01:35:05 +00:00
$obj = IpAddress::parse($addr);
2022-09-13 13:13:11 +00:00
$ser = serialize($obj);
$unser = unserialize($ser);
$this->assertTrue($obj->equals($unser));
}
}
}