Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Io/ConnectionUpcaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function __construct(DuplexStreamInterface $stream)
$this->stream = $stream;

Util::forwardEvents($stream, $this, array('data', 'end', 'close', 'error', 'drain'));
$this->stream->on('close', array($this, 'close'));
}

public function isReadable()
Expand Down
21 changes: 14 additions & 7 deletions src/Message/ReadableBodyStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

namespace Clue\React\Buzz\Message;

use Evenement\EventEmitter;
use Psr\Http\Message\StreamInterface;
use React\Stream\ReadableStream;
use React\Stream\ReadableStreamInterface;
use Evenement\EventEmitter;
use React\Stream\Util;
use React\Stream\WritableStreamInterface;

Expand All @@ -14,6 +13,9 @@
*/
class ReadableBodyStream extends EventEmitter implements ReadableStreamInterface, StreamInterface
{
private $input;
private $closed = false;

public function __construct(ReadableStreamInterface $input)
{
$this->input = $input;
Expand All @@ -24,19 +26,24 @@ public function __construct(ReadableStreamInterface $input)
});
$input->on('error', function ($error) use ($that) {
$that->emit('error', array($error, $that));
$that->close();
});
$input->on('end', function () use ($that) {
$that->emit('end', array($that));
$that->emit('close', array($that));
});
$input->on('close', function () use ($that) {
$that->emit('close', array($that));
$that->close();
});
$input->on('close', array($that, 'close'));
}

public function close()
{
$this->input->close();
if (!$this->closed) {
$this->closed = true;
$this->input->close();

$this->emit('close', array($this));
$this->removeAllListeners();
}
}

public function isReadable()
Expand Down
1 change: 1 addition & 0 deletions tests/FunctionalBrowserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ public function testPostString()
$this->assertEquals('hello world', $data['data']);
}

/** @group online */
public function testPostStreamChunked()
{
// httpbin used to support `Transfer-Encoding: chunked` for requests,
Expand Down
49 changes: 49 additions & 0 deletions tests/Message/ReadableBodyStreamTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Clue\React\Buzz\Message\ReadableBodyStream;
use React\Stream\ThroughStream;

class ReadableBodyStreamTest extends TestCase
{
Expand Down Expand Up @@ -34,6 +35,54 @@ public function testCloseWillCloseInputStream()
$this->stream->close();
}

public function testCloseWillEmitCloseEvent()
{
$this->input = new ThroughStream();
$this->stream = new ReadableBodyStream($this->input);

$called = 0;
$this->stream->on('close', function () use (&$called) {
++$called;
});

$this->stream->close();
$this->stream->close();

$this->assertEquals(1, $called);
}

public function testCloseInputWillEmitCloseEvent()
{
$this->input = new ThroughStream();
$this->stream = new ReadableBodyStream($this->input);

$called = 0;
$this->stream->on('close', function () use (&$called) {
++$called;
});

$this->input->close();
$this->input->close();

$this->assertEquals(1, $called);
}

public function testEndInputWillEmitCloseEvent()
{
$this->input = new ThroughStream();
$this->stream = new ReadableBodyStream($this->input);

$called = 0;
$this->stream->on('close', function () use (&$called) {
++$called;
});

$this->input->end();
$this->input->end();

$this->assertEquals(1, $called);
}

public function testPauseWillPauseInputStream()
{
$this->input->expects($this->once())->method('pause');
Expand Down