Skip to content

Commit d97ec8b

Browse files
committed
Added Tcp port value object + tests
1 parent ea25230 commit d97ec8b

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace PhpValueObjects\Network\Exception;
4+
5+
class InvalidTcpPortException extends \Exception
6+
{
7+
/**
8+
* InvalidUrlException constructor.
9+
*
10+
* @param string $value
11+
*/
12+
public function __construct($value)
13+
{
14+
parent::__construct(sprintf('"%" is not a valid tcp port.', $value));
15+
}
16+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace PhpValueObjects\Network;
4+
5+
use PhpValueObjects\AbstractValueObject;
6+
use PhpValueObjects\Network\Exception\InvalidTcpPortException;
7+
8+
abstract class TcpPort extends AbstractValueObject
9+
{
10+
/**
11+
* @param string $value
12+
*
13+
* @throws InvalidTcpPortException
14+
*/
15+
protected function guard($value)
16+
{
17+
if (false === filter_var($value, FILTER_VALIDATE_INT)) {
18+
throw new InvalidTcpPortException($value);
19+
}
20+
21+
if ($value < 0 || $value > 65535) {
22+
throw new InvalidTcpPortException($value);
23+
}
24+
}
25+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace PhpValueObjects\Tests\Network;
4+
5+
use PhpValueObjects\Network\TcpPort;
6+
7+
class TcpPortValueObject extends TcpPort
8+
{
9+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace PhpValueObjects\Tests\Network;
4+
5+
use PhpValueObjects\Network\Exception\InvalidIpv6Exception;
6+
use PhpValueObjects\Network\Exception\InvalidTcpPortException;
7+
use PhpValueObjects\Tests\BaseUnitTestCase;
8+
9+
class TcpPortValueObjectTest extends BaseUnitTestCase
10+
{
11+
/**
12+
* @test
13+
*/
14+
public function itShouldReturnTcpPort()
15+
{
16+
$portNumber = $this->faker()->numberBetween(0, 65535);
17+
18+
$tcpPort = new TcpPortValueObject($portNumber);
19+
20+
$this->assertSame($portNumber, $tcpPort->value());
21+
}
22+
23+
/**
24+
* @return array
25+
*/
26+
public function invalidTcpPortProvider()
27+
{
28+
return [
29+
[null],
30+
[$this->faker()->randomFloat()],
31+
[$this->faker()->numberBetween(65536)],
32+
[-$this->faker()->numberBetween()],
33+
];
34+
}
35+
36+
/**
37+
* @test
38+
* @dataProvider invalidTcpPortProvider
39+
*/
40+
public function itShouldThrowsException($data)
41+
{
42+
$this->expectException(InvalidTcpPortException::class);
43+
44+
new TcpPortValueObject($data);
45+
}
46+
}

0 commit comments

Comments
 (0)