-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
225 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace hollodotme\FastCGI\Sockets; | ||
|
||
use Exception; | ||
use InvalidArgumentException; | ||
use function random_int; | ||
|
||
final class SocketId | ||
{ | ||
/** @var int */ | ||
private $id; | ||
|
||
/** | ||
* @param int $id | ||
* | ||
* @throws InvalidArgumentException | ||
*/ | ||
private function __construct( int $id ) | ||
{ | ||
$this->guardValueIsValid( $id ); | ||
|
||
$this->id = $id; | ||
} | ||
|
||
/** | ||
* @param int $value | ||
* | ||
* @throws InvalidArgumentException | ||
*/ | ||
private function guardValueIsValid( int $value ) : void | ||
{ | ||
if ( $value < 1 || $value > ((1 << 16) - 1) ) | ||
{ | ||
throw new InvalidArgumentException( 'Invalid socket ID (out of range): ' . $value ); | ||
} | ||
} | ||
|
||
/** | ||
* @return SocketId | ||
* @throws InvalidArgumentException | ||
* @throws Exception | ||
*/ | ||
public static function new() : self | ||
{ | ||
return new self( random_int( 1, (1 << 16) - 1 ) ); | ||
} | ||
|
||
/** | ||
* @param int $id | ||
* | ||
* @return SocketId | ||
* @throws InvalidArgumentException | ||
*/ | ||
public static function fromInt( int $id ) : self | ||
{ | ||
return new self( $id ); | ||
} | ||
|
||
public function getValue() : int | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function equals( SocketId $other ) : bool | ||
{ | ||
return $this->id === $other->id; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace hollodotme\FastCGI\Tests\Unit\Sockets; | ||
|
||
use hollodotme\FastCGI\Sockets\SocketId; | ||
use PHPUnit\Framework\ExpectationFailedException; | ||
use PHPUnit\Framework\TestCase; | ||
use SebastianBergmann\RecursionContext\InvalidArgumentException; | ||
|
||
final class SocketIdTest extends TestCase | ||
{ | ||
/** | ||
* @throws \InvalidArgumentException | ||
* @throws ExpectationFailedException | ||
* @throws InvalidArgumentException | ||
*/ | ||
public function testCanGetNewInstance() : void | ||
{ | ||
for ( $i = 0; $i < 100; $i++ ) | ||
{ | ||
$socketId = SocketId::new(); | ||
|
||
$this->assertGreaterThanOrEqual( 1, $socketId->getValue() ); | ||
$this->assertLessThanOrEqual( (1 << 16) - 1, $socketId->getValue() ); | ||
} | ||
} | ||
|
||
/** | ||
* @throws ExpectationFailedException | ||
* @throws InvalidArgumentException | ||
* @throws \InvalidArgumentException | ||
*/ | ||
public function testGetValue() : void | ||
{ | ||
$socketId = SocketId::new(); | ||
|
||
$this->assertGreaterThanOrEqual( 1, $socketId->getValue() ); | ||
$this->assertLessThanOrEqual( (1 << 16) - 1, $socketId->getValue() ); | ||
} | ||
|
||
/** | ||
* @throws ExpectationFailedException | ||
* @throws InvalidArgumentException | ||
* @throws \InvalidArgumentException | ||
*/ | ||
public function testCanGetNewInstanceFromInt() : void | ||
{ | ||
for ( $i = 1; $i < 10; $i++ ) | ||
{ | ||
$socketId = SocketId::fromInt( $i ); | ||
|
||
$this->assertSame( $i, $socketId->getValue() ); | ||
} | ||
} | ||
|
||
/** | ||
* @param int $socketIdValue | ||
* | ||
* @throws \InvalidArgumentException | ||
* @dataProvider outOfRangeSocketIdValueProvider | ||
*/ | ||
public function testThrowsExceptionIfSocketIdValueIsOutOfRange( int $socketIdValue ) : void | ||
{ | ||
$this->expectException( \InvalidArgumentException::class ); | ||
$this->expectExceptionMessage( 'Invalid socket ID (out of range): ' . $socketIdValue ); | ||
|
||
SocketId::fromInt( $socketIdValue ); | ||
} | ||
|
||
public function outOfRangeSocketIdValueProvider() : array | ||
{ | ||
return [ | ||
[ | ||
'socketIdValue' => 0, | ||
], | ||
[ | ||
'socketIdValue' => 1 << 16, | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @throws ExpectationFailedException | ||
* @throws InvalidArgumentException | ||
* @throws \InvalidArgumentException | ||
*/ | ||
public function testEquals() : void | ||
{ | ||
$socketId = SocketId::fromInt( 123 ); | ||
$otherEquals = SocketId::fromInt( 123 ); | ||
$otherEqualsNot = SocketId::fromInt( 321 ); | ||
|
||
$this->assertNotSame( $socketId, $otherEquals ); | ||
$this->assertNotSame( $socketId, $otherEqualsNot ); | ||
$this->assertNotSame( $otherEquals, $otherEqualsNot ); | ||
|
||
$this->assertTrue( $socketId->equals( $otherEquals ) ); | ||
$this->assertTrue( $otherEquals->equals( $socketId ) ); | ||
|
||
$this->assertFalse( $socketId->equals( $otherEqualsNot ) ); | ||
$this->assertFalse( $otherEqualsNot->equals( $socketId ) ); | ||
|
||
$this->assertFalse( $otherEquals->equals( $otherEqualsNot ) ); | ||
$this->assertFalse( $otherEqualsNot->equals( $otherEquals ) ); | ||
} | ||
} |
Oops, something went wrong.