Skip to content

Commit 459f4e7

Browse files
authored
Merge pull request bruli#2 from rastafermo/add_tcp_port_VO
Add tcp port vo
2 parents ea25230 + d236e58 commit 459f4e7

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 (!is_int($value)) {
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\InvalidTcpPortException;
6+
use PhpValueObjects\Tests\BaseUnitTestCase;
7+
8+
class TcpPortValueObjectTest extends BaseUnitTestCase
9+
{
10+
/**
11+
* @test
12+
*/
13+
public function itShouldReturnTcpPort()
14+
{
15+
$portNumber = $this->faker()->numberBetween(0, 65535);
16+
17+
$tcpPort = new TcpPortValueObject($portNumber);
18+
19+
$this->assertSame($portNumber, $tcpPort->value());
20+
}
21+
22+
/**
23+
* @return array
24+
*/
25+
public function invalidTcpPortProvider()
26+
{
27+
return [
28+
[null],
29+
[11362.0],
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)