From b009a6af2d9cf6d63f69082fa993e355e68a684b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Mon, 1 Feb 2021 09:02:37 +0100 Subject: [PATCH] Improve code coverage to 100% --- src/Buffer.php | 4 --- src/Socket.php | 5 ++- tests/BufferTest.php | 72 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 7 deletions(-) create mode 100644 tests/BufferTest.php diff --git a/src/Buffer.php b/src/Buffer.php index dc02953..e5aa2f6 100644 --- a/src/Buffer.php +++ b/src/Buffer.php @@ -79,10 +79,6 @@ public function close() public function end() { - if ($this->writable === false) { - return; - } - $this->writable = false; if (!$this->outgoing) { diff --git a/src/Socket.php b/src/Socket.php index 1315d8c..8a1d294 100644 --- a/src/Socket.php +++ b/src/Socket.php @@ -102,11 +102,10 @@ private function sanitizeAddress($address) return null; } - // this is an IPv6 address which includes colons but no square brackets + // check if this is an IPv6 address which includes multiple colons but no square brackets (PHP < 7.3) $pos = \strrpos($address, ':'); if ($pos !== false && \strpos($address, ':') < $pos && \substr($address, 0, 1) !== '[') { - $port = \substr($address, $pos + 1); - $address = '[' . \substr($address, 0, $pos) . ']:' . $port; + $address = '[' . \substr($address, 0, $pos) . ']:' . \substr($address, $pos + 1); // @codeCoverageIgnore } return $address; } diff --git a/tests/BufferTest.php b/tests/BufferTest.php new file mode 100644 index 0000000..2149ce4 --- /dev/null +++ b/tests/BufferTest.php @@ -0,0 +1,72 @@ +getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop->expects($this->once())->method('addWriteStream')->with($socket); + + $client = new Buffer($loop, $socket); + + $client->send('foo'); + } + + public function testSendAfterCloseIsNoop() + { + $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop->expects($this->never())->method('addWriteStream'); + + $socket = stream_socket_client('udp://127.0.0.1:8000'); + + $client = new Buffer($loop, $socket); + + $client->close(); + $client->send('foo'); + } + + public function testCloseAfterSendAddsSocketToLoopRemovesSocketFromLoopAgain() + { + $socket = stream_socket_client('udp://127.0.0.1:8000'); + + $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop->expects($this->once())->method('addWriteStream')->with($socket); + $loop->expects($this->once())->method('removeWriteStream')->with($socket); + + $client = new Buffer($loop, $socket); + + $client->send('foo'); + $client->close(); + } + + public function testCloseTwiceEmitsCloseEventOnce() + { + $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + + $socket = stream_socket_client('udp://127.0.0.1:8000'); + + $client = new Buffer($loop, $socket); + + $closed = 0; + $client->on('close', function () use (&$closed) { + ++$closed; + }); + + $this->assertEquals(0, $closed); + + $client->close(); + + $this->assertEquals(1, $closed); + + $client->close(); + + $this->assertEquals(1, $closed); + } +}