diff --git a/tests/BufferTest.php b/tests/BufferTest.php index 847de25..c46aa7c 100644 --- a/tests/BufferTest.php +++ b/tests/BufferTest.php @@ -25,7 +25,7 @@ public function testConstructor() public function testWrite() { $stream = fopen('php://temp', 'r+'); - $loop = $this->createWriteableLoopMock(); + $loop = $this->createLoopMock(); $buffer = new Buffer($stream, $loop); $buffer->on('error', $this->expectCallableNever()); @@ -84,22 +84,18 @@ public function testWriteCanFlushImmediatelyWithoutBufferGettingFull() /** * @covers React\Stream\Buffer::write - * @covers React\Stream\Buffer::handleWrite */ public function testWriteReturnsFalseWhenBufferIsFull() { $stream = fopen('php://temp', 'r+'); - $loop = $this->createWriteableLoopMock(); - $loop->preventWrites = true; + $loop = $this->createLoopMock(); $buffer = new Buffer($stream, $loop); $buffer->softLimit = 4; $buffer->listening = true; $buffer->on('error', $this->expectCallableNever()); - $this->assertTrue($buffer->write("foo")); - $loop->preventWrites = false; - $this->assertFalse($buffer->write("bar\n")); + $this->assertFalse($buffer->write("foobar")); } /** @@ -121,7 +117,6 @@ public function testWriteEmitsErrorWhenResourceIsNotWritable() //$buffer->on('close', $this->expectCallableOnce()); $buffer->write('hello'); - $buffer->handleWrite(); } /** @@ -146,21 +141,34 @@ public function testWriteDetectsWhenOtherSideIsClosed() * @covers React\Stream\Buffer::write * @covers React\Stream\Buffer::handleWrite */ - public function testDrain() + public function testEmitsDrainIfBufferIsFullAndThenEmpties() { $stream = fopen('php://temp', 'r+'); - $loop = $this->createWriteableLoopMock(); - $loop->preventWrites = true; + $loop = $this->createLoopMock(); $buffer = new Buffer($stream, $loop); $buffer->softLimit = 4; $buffer->on('error', $this->expectCallableNever()); $buffer->on('drain', $this->expectCallableOnce()); + $buffer->write("foobar"); + } + + /** + * @covers React\Stream\Buffer::write + * @covers React\Stream\Buffer::handleWrite + */ + public function testDoesNotEmitDrainIfBufferIsNotFull() + { + $stream = fopen('php://temp', 'r+'); + $loop = $this->createLoopMock(); + + $buffer = new Buffer($stream, $loop); + $buffer->softLimit = 4; + $buffer->on('error', $this->expectCallableNever()); + $buffer->on('drain', $this->expectCallableNever()); + $buffer->write("foo"); - $loop->preventWrites = false; - $buffer->listening = false; - $buffer->write("bar\n"); } /** @@ -170,8 +178,7 @@ public function testDrain() public function testWriteInDrain() { $stream = fopen('php://temp', 'r+'); - $loop = $this->createWriteableLoopMock(); - $loop->preventWrites = true; + $loop = $this->createLoopMock(); $buffer = new Buffer($stream, $loop); $buffer->softLimit = 2; @@ -184,7 +191,6 @@ public function testWriteInDrain() $buffer->listening = true; $this->assertFalse($buffer->write("foo")); - $loop->preventWrites = false; $buffer->listening = false; $buffer->write("\n"); @@ -215,7 +221,7 @@ public function testEnd() public function testEndWithData() { $stream = fopen('php://temp', 'r+'); - $loop = $this->createWriteableLoopMock(); + $loop = $this->createLoopMock(); $buffer = new Buffer($stream, $loop); $buffer->on('error', $this->expectCallableNever()); @@ -252,7 +258,7 @@ public function testClose() public function testWritingToClosedBufferShouldNotWriteToStream() { $stream = fopen('php://temp', 'r+'); - $loop = $this->createWriteableLoopMock(); + $loop = $this->createLoopMock(); $buffer = new Buffer($stream, $loop); $buffer->close(); @@ -270,7 +276,7 @@ public function testWritingToClosedBufferShouldNotWriteToStream() public function testError() { $stream = null; - $loop = $this->createWriteableLoopMock(); + $loop = $this->createLoopMock(); $error = null; @@ -291,7 +297,7 @@ public function testWritingToClosedStream() } list($a, $b) = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP); - $loop = $this->createWriteableLoopMock(); + $loop = $this->createLoopMock(); $error = null; @@ -309,22 +315,6 @@ public function testWritingToClosedStream() $this->assertSame('fwrite(): send of 3 bytes failed with errno=32 Broken pipe', $error->getMessage()); } - private function createWriteableLoopMock() - { - $loop = $this->createLoopMock(); - $loop->preventWrites = false; - $loop - ->expects($this->any()) - ->method('addWriteStream') - ->will($this->returnCallback(function ($stream, $listener) use ($loop) { - if (!$loop->preventWrites) { - call_user_func($listener, $stream); - } - })); - - return $loop; - } - private function createLoopMock() { return $this->getMock('React\EventLoop\LoopInterface'); diff --git a/tests/StreamTest.php b/tests/StreamTest.php index 0c913ac..c91dea9 100644 --- a/tests/StreamTest.php +++ b/tests/StreamTest.php @@ -71,7 +71,7 @@ public function testEmptyStreamShouldNotEmitData() public function testWrite() { $stream = fopen('php://temp', 'r+'); - $loop = $this->createWriteableLoopMock(); + $loop = $this->createLoopMock(); $conn = new Stream($stream, $loop); $conn->write("foo\n"); @@ -184,19 +184,6 @@ public function testDataErrorShouldEmitErrorAndClose() $conn->handleData($stream); } - private function createWriteableLoopMock() - { - $loop = $this->createLoopMock(); - $loop - ->expects($this->once()) - ->method('addWriteStream') - ->will($this->returnCallback(function ($stream, $listener) { - call_user_func($listener, $stream); - })); - - return $loop; - } - private function createLoopMock() { return $this->getMock('React\EventLoop\LoopInterface');