Skip to content

Commit

Permalink
Improve code coverage to 100%
Browse files Browse the repository at this point in the history
  • Loading branch information
clue committed Feb 1, 2021
1 parent adbf2d6 commit b009a6a
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 7 deletions.
4 changes: 0 additions & 4 deletions src/Buffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,6 @@ public function close()

public function end()
{
if ($this->writable === false) {
return;
}

$this->writable = false;

if (!$this->outgoing) {
Expand Down
5 changes: 2 additions & 3 deletions src/Socket.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
72 changes: 72 additions & 0 deletions tests/BufferTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace React\Tests\Datagram;

use PHPUnit\Framework\TestCase;
use React\Datagram\Buffer;

class BufferTest extends TestCase
{
public function testSendAddsSocketToLoop()
{
$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);

$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);
}
}

0 comments on commit b009a6a

Please sign in to comment.