123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448 |
- <?php
- namespace React\Tests\Stream;
- use React\Stream\Buffer;
- class BufferTest extends TestCase
- {
- /**
- * @covers React\Stream\Buffer::__construct
- */
- public function testConstructor()
- {
- $stream = fopen('php://temp', 'r+');
- $loop = $this->createLoopMock();
- $buffer = new Buffer($stream, $loop);
- $buffer->on('error', $this->expectCallableNever());
- }
- /**
- * @covers React\Stream\Buffer::__construct
- * @expectedException InvalidArgumentException
- */
- public function testConstructorThrowsIfNotAValidStreamResource()
- {
- $stream = null;
- $loop = $this->createLoopMock();
- new Buffer($stream, $loop);
- }
- /**
- * @covers React\Stream\Buffer::write
- * @covers React\Stream\Buffer::handleWrite
- */
- public function testWrite()
- {
- $stream = fopen('php://temp', 'r+');
- $loop = $this->createWriteableLoopMock();
- $buffer = new Buffer($stream, $loop);
- $buffer->on('error', $this->expectCallableNever());
- $buffer->write("foobar\n");
- rewind($stream);
- $this->assertSame("foobar\n", fread($stream, 1024));
- }
- /**
- * @covers React\Stream\Buffer::write
- */
- public function testWriteWithDataDoesAddResourceToLoop()
- {
- $stream = fopen('php://temp', 'r+');
- $loop = $this->createLoopMock();
- $loop->expects($this->once())->method('addWriteStream')->with($this->equalTo($stream));
- $buffer = new Buffer($stream, $loop);
- $buffer->write("foobar\n");
- }
- /**
- * @covers React\Stream\Buffer::write
- * @covers React\Stream\Buffer::handleWrite
- */
- public function testEmptyWriteDoesNotAddToLoop()
- {
- $stream = fopen('php://temp', 'r+');
- $loop = $this->createLoopMock();
- $loop->expects($this->never())->method('addWriteStream');
- $buffer = new Buffer($stream, $loop);
- $buffer->write("");
- $buffer->write(null);
- }
- /**
- * @covers React\Stream\Buffer::write
- */
- public function testWriteWillAddStreamToLoop()
- {
- $stream = fopen('php://temp', 'r+');
- $loop = $this->createLoopMock();
- $buffer = new Buffer($stream, $loop);
- $loop->expects($this->once())->method('addWriteStream')->with($stream);
- $buffer->write('foo');
- }
- /**
- * @covers React\Stream\Buffer::write
- * @covers React\Stream\Buffer::handleWrite
- */
- public function testWriteReturnsFalseWhenBufferIsFull()
- {
- $stream = fopen('php://temp', 'r+');
- $loop = $this->createWriteableLoopMock();
- $loop->preventWrites = true;
- $buffer = new Buffer($stream, $loop);
- $buffer->softLimit = 4;
- $buffer->on('error', $this->expectCallableNever());
- $this->assertTrue($buffer->write("foo"));
- $loop->preventWrites = false;
- $this->assertFalse($buffer->write("bar\n"));
- }
- /**
- * @covers React\Stream\Buffer::write
- */
- public function testWriteReturnsFalseWhenBufferIsExactlyFull()
- {
- $stream = fopen('php://temp', 'r+');
- $loop = $this->createLoopMock();
- $buffer = new Buffer($stream, $loop);
- $buffer->softLimit = 3;
- $this->assertFalse($buffer->write("foo"));
- }
- /**
- * @covers React\Stream\Buffer::write
- * @covers React\Stream\Buffer::handleWrite
- */
- public function testWriteEmitsErrorWhenResourceIsNotWritable()
- {
- if (defined('HHVM_VERSION')) {
- // via https://github.com/reactphp/stream/pull/52/files#r75493076
- $this->markTestSkipped('HHVM allows writing to read-only memory streams');
- }
- $stream = fopen('php://temp', 'r');
- $loop = $this->createLoopMock();
- $buffer = new Buffer($stream, $loop);
- $buffer->on('error', $this->expectCallableOnce());
- //$buffer->on('close', $this->expectCallableOnce());
- $buffer->write('hello');
- $buffer->handleWrite();
- }
- /**
- * @covers React\Stream\Buffer::write
- * @covers React\Stream\Buffer::handleWrite
- */
- public function testWriteDetectsWhenOtherSideIsClosed()
- {
- list($a, $b) = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
- $loop = $this->createWriteableLoopMock();
- $buffer = new Buffer($a, $loop);
- $buffer->softLimit = 4;
- $buffer->on('error', $this->expectCallableOnce());
- fclose($b);
- $buffer->write("foo");
- }
- /**
- * @covers React\Stream\Buffer::write
- * @covers React\Stream\Buffer::handleWrite
- */
- public function testDrain()
- {
- $stream = fopen('php://temp', 'r+');
- $loop = $this->createWriteableLoopMock();
- $loop->preventWrites = true;
- $buffer = new Buffer($stream, $loop);
- $buffer->softLimit = 4;
- $buffer->on('error', $this->expectCallableNever());
- $buffer->on('drain', $this->expectCallableOnce());
- $buffer->write("foo");
- $loop->preventWrites = false;
- $buffer->listening = false;
- $buffer->write("bar\n");
- }
- /**
- * @covers React\Stream\Buffer::write
- * @covers React\Stream\Buffer::handleWrite
- */
- public function testWriteInDrain()
- {
- $stream = fopen('php://temp', 'r+');
- $loop = $this->createWriteableLoopMock();
- $loop->preventWrites = true;
- $buffer = new Buffer($stream, $loop);
- $buffer->softLimit = 2;
- $buffer->on('error', $this->expectCallableNever());
- $buffer->once('drain', function ($buffer) {
- $buffer->listening = false;
- $buffer->write("bar\n");
- });
- $this->assertFalse($buffer->write("foo"));
- $loop->preventWrites = false;
- $buffer->listening = false;
- $buffer->write("\n");
- fseek($stream, 0);
- $this->assertSame("foo\nbar\n", stream_get_contents($stream));
- }
- /**
- * @covers React\Stream\Buffer::write
- * @covers React\Stream\Buffer::handleWrite
- */
- public function testDrainAndFullDrainAfterWrite()
- {
- $stream = fopen('php://temp', 'r+');
- $loop = $this->createLoopMock();
- $buffer = new Buffer($stream, $loop);
- $buffer->softLimit = 2;
- $buffer->on('drain', $this->expectCallableOnce());
- $buffer->on('full-drain', $this->expectCallableOnce());
- $buffer->write("foo");
- $buffer->handleWrite();
- }
- /**
- * @covers React\Stream\Buffer::write
- * @covers React\Stream\Buffer::handleWrite
- */
- public function testCloseDuringDrainWillNotEmitFullDrain()
- {
- $stream = fopen('php://temp', 'r+');
- $loop = $this->createLoopMock();
- $buffer = new Buffer($stream, $loop);
- $buffer->softLimit = 2;
- // close buffer on drain event => expect close event, but no full-drain after
- $buffer->on('drain', $this->expectCallableOnce());
- $buffer->on('drain', array($buffer, 'close'));
- $buffer->on('close', $this->expectCallableOnce());
- $buffer->on('full-drain', $this->expectCallableNever());
- $buffer->write("foo");
- $buffer->handleWrite();
- }
- /**
- * @covers React\Stream\Buffer::end
- */
- public function testEnd()
- {
- $stream = fopen('php://temp', 'r+');
- $loop = $this->createLoopMock();
- $buffer = new Buffer($stream, $loop);
- $buffer->on('error', $this->expectCallableNever());
- $buffer->on('close', $this->expectCallableOnce());
- $this->assertTrue($buffer->isWritable());
- $buffer->end();
- $this->assertFalse($buffer->isWritable());
- }
- /**
- * @covers React\Stream\Buffer::end
- */
- public function testEndWithData()
- {
- $stream = fopen('php://temp', 'r+');
- $loop = $this->createWriteableLoopMock();
- $buffer = new Buffer($stream, $loop);
- $buffer->on('error', $this->expectCallableNever());
- $buffer->on('close', $this->expectCallableOnce());
- $buffer->end('final words');
- rewind($stream);
- $this->assertSame('final words', stream_get_contents($stream));
- }
- /**
- * @covers React\Stream\Buffer::isWritable
- * @covers React\Stream\Buffer::close
- */
- public function testClose()
- {
- $stream = fopen('php://temp', 'r+');
- $loop = $this->createLoopMock();
- $buffer = new Buffer($stream, $loop);
- $buffer->on('error', $this->expectCallableNever());
- $buffer->on('close', $this->expectCallableOnce());
- $this->assertTrue($buffer->isWritable());
- $buffer->close();
- $this->assertFalse($buffer->isWritable());
- $this->assertEquals(array(), $buffer->listeners('close'));
- }
- /**
- * @covers React\Stream\Buffer::close
- */
- public function testClosingAfterWriteRemovesStreamFromLoop()
- {
- $stream = fopen('php://temp', 'r+');
- $loop = $this->createLoopMock();
- $buffer = new Buffer($stream, $loop);
- $loop->expects($this->once())->method('removeWriteStream')->with($stream);
- $buffer->write('foo');
- $buffer->close();
- }
- /**
- * @covers React\Stream\Buffer::close
- */
- public function testClosingWithoutWritingDoesNotRemoveStreamFromLoop()
- {
- $stream = fopen('php://temp', 'r+');
- $loop = $this->createLoopMock();
- $buffer = new Buffer($stream, $loop);
- $loop->expects($this->never())->method('removeWriteStream');
- $buffer->close();
- }
- /**
- * @covers React\Stream\Buffer::close
- */
- public function testDoubleCloseWillEmitOnlyOnce()
- {
- $stream = fopen('php://temp', 'r+');
- $loop = $this->createLoopMock();
- $buffer = new Buffer($stream, $loop);
- $buffer->on('close', $this->expectCallableOnce());
- $buffer->close();
- $buffer->close();
- }
- /**
- * @covers React\Stream\Buffer::write
- * @covers React\Stream\Buffer::close
- */
- public function testWritingToClosedBufferShouldNotWriteToStream()
- {
- $stream = fopen('php://temp', 'r+');
- $loop = $this->createWriteableLoopMock();
- $buffer = new Buffer($stream, $loop);
- $buffer->close();
- $buffer->write('foo');
- rewind($stream);
- $this->assertSame('', stream_get_contents($stream));
- }
- /**
- * @covers React\Stream\Buffer::handleWrite
- */
- public function testErrorWhenStreamResourceIsInvalid()
- {
- $stream = fopen('php://temp', 'r+');
- $loop = $this->createWriteableLoopMock();
- $error = null;
- $buffer = new Buffer($stream, $loop);
- $buffer->on('error', function ($message) use (&$error) {
- $error = $message;
- });
- // invalidate stream resource
- fclose($stream);
- $buffer->write('Attempting to write to bad stream');
- $this->assertInstanceOf('Exception', $error);
- // the error messages differ between PHP versions, let's just check substrings
- $this->assertContains('Unable to write to stream: ', $error->getMessage());
- $this->assertContains(' not a valid stream resource', $error->getMessage(), '', true);
- }
- public function testWritingToClosedStream()
- {
- if ('Darwin' === PHP_OS) {
- $this->markTestSkipped('OS X issue with shutting down pair for writing');
- }
- list($a, $b) = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
- $loop = $this->createWriteableLoopMock();
- $error = null;
- $buffer = new Buffer($a, $loop);
- $buffer->on('error', function($message) use (&$error) {
- $error = $message;
- });
- $buffer->write('foo');
- stream_socket_shutdown($b, STREAM_SHUT_RD);
- stream_socket_shutdown($a, STREAM_SHUT_RD);
- $buffer->write('bar');
- $this->assertInstanceOf('Exception', $error);
- $this->assertSame('Unable to write to stream: 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');
- }
- }
|