File tree Expand file tree Collapse file tree 4 files changed +96
-0
lines changed
Expand file tree Collapse file tree 4 files changed +96
-0
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace PhpValueObjects \Tests \Network ;
4+
5+ use PhpValueObjects \Network \TcpPort ;
6+
7+ class TcpPortValueObject extends TcpPort
8+ {
9+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments