Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/InternetAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static function fromString(string $address): self
}

$ip = \substr($address, 0, $colon);
$port = \substr($address, $colon);
$port = \substr($address, $colon + 1);

if (!\preg_match('/^[1-9][0-9]{0,4}$/', $port)) {
throw new SocketException('Invalid port: ' . $port);
Expand Down
44 changes: 44 additions & 0 deletions test/InternetAddressTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
declare(strict_types=1);

namespace Amp\Socket;

use PHPUnit\Framework\TestCase;

/**
* @see InternetAddress
*/
final class InternetAddressTest extends TestCase
{
/**
* Tests that when an InternetAddress is constructed from a string with valid IP and port, no exception is thrown.
*/
public function testFromString(): void
{
$this->expectNotToPerformAssertions();

InternetAddress::fromString('1.1.1.1:1');
}

/**
* Tests that when an InternetAddress is constructed from a string with an IP but no port, an exception is thrown.
*/
public function testFromStringMissingPort(): void
{
$this->expectException(SocketException::class);
$this->expectExceptionMessage('Missing port');

InternetAddress::fromString('1.1.1.1');
}

/**
* Tests that when an InternetAddress is constructed from a string with an invalid port, an exception is thrown.
*/
public function testFromStringInvalidPort(): void
{
$this->expectException(SocketException::class);
$this->expectExceptionMessage('Invalid port');

InternetAddress::fromString('1.1.1.1:-1');
}
}