Skip to content

Commit b009a6a

Browse files
committed
Improve code coverage to 100%
1 parent adbf2d6 commit b009a6a

File tree

3 files changed

+74
-7
lines changed

3 files changed

+74
-7
lines changed

src/Buffer.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,6 @@ public function close()
7979

8080
public function end()
8181
{
82-
if ($this->writable === false) {
83-
return;
84-
}
85-
8682
$this->writable = false;
8783

8884
if (!$this->outgoing) {

src/Socket.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,10 @@ private function sanitizeAddress($address)
102102
return null;
103103
}
104104

105-
// this is an IPv6 address which includes colons but no square brackets
105+
// check if this is an IPv6 address which includes multiple colons but no square brackets (PHP < 7.3)
106106
$pos = \strrpos($address, ':');
107107
if ($pos !== false && \strpos($address, ':') < $pos && \substr($address, 0, 1) !== '[') {
108-
$port = \substr($address, $pos + 1);
109-
$address = '[' . \substr($address, 0, $pos) . ']:' . $port;
108+
$address = '[' . \substr($address, 0, $pos) . ']:' . \substr($address, $pos + 1); // @codeCoverageIgnore
110109
}
111110
return $address;
112111
}

tests/BufferTest.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace React\Tests\Datagram;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use React\Datagram\Buffer;
7+
8+
class BufferTest extends TestCase
9+
{
10+
public function testSendAddsSocketToLoop()
11+
{
12+
$socket = stream_socket_client('udp://127.0.0.1:8000');
13+
14+
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
15+
$loop->expects($this->once())->method('addWriteStream')->with($socket);
16+
17+
$client = new Buffer($loop, $socket);
18+
19+
$client->send('foo');
20+
}
21+
22+
public function testSendAfterCloseIsNoop()
23+
{
24+
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
25+
$loop->expects($this->never())->method('addWriteStream');
26+
27+
$socket = stream_socket_client('udp://127.0.0.1:8000');
28+
29+
$client = new Buffer($loop, $socket);
30+
31+
$client->close();
32+
$client->send('foo');
33+
}
34+
35+
public function testCloseAfterSendAddsSocketToLoopRemovesSocketFromLoopAgain()
36+
{
37+
$socket = stream_socket_client('udp://127.0.0.1:8000');
38+
39+
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
40+
$loop->expects($this->once())->method('addWriteStream')->with($socket);
41+
$loop->expects($this->once())->method('removeWriteStream')->with($socket);
42+
43+
$client = new Buffer($loop, $socket);
44+
45+
$client->send('foo');
46+
$client->close();
47+
}
48+
49+
public function testCloseTwiceEmitsCloseEventOnce()
50+
{
51+
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
52+
53+
$socket = stream_socket_client('udp://127.0.0.1:8000');
54+
55+
$client = new Buffer($loop, $socket);
56+
57+
$closed = 0;
58+
$client->on('close', function () use (&$closed) {
59+
++$closed;
60+
});
61+
62+
$this->assertEquals(0, $closed);
63+
64+
$client->close();
65+
66+
$this->assertEquals(1, $closed);
67+
68+
$client->close();
69+
70+
$this->assertEquals(1, $closed);
71+
}
72+
}

0 commit comments

Comments
 (0)