diff --git a/composer.json b/composer.json index e18396b..0be86d8 100644 --- a/composer.json +++ b/composer.json @@ -10,12 +10,10 @@ "require-dev": { "phpunit/phpunit": "^5.0 || ^4.8.10", "react/event-loop": "^0.4|^0.3", - "react/promise": "^2.0|^1.0", "clue/stream-filter": "~1.2" }, "suggest": { - "react/event-loop": "^0.4", - "react/promise": "^2.0" + "react/event-loop": "^0.4" }, "autoload": { "psr-4": { diff --git a/src/BufferedSink.php b/src/BufferedSink.php deleted file mode 100644 index 1ef409e..0000000 --- a/src/BufferedSink.php +++ /dev/null @@ -1,66 +0,0 @@ -deferred = new Deferred(); - - $this->on('pipe', array($this, 'handlePipeEvent')); - $this->on('error', array($this, 'handleErrorEvent')); - } - - public function handlePipeEvent($source) - { - Util::forwardEvents($source, $this, array('error')); - $source->on('close', array($this, 'close')); - } - - public function handleErrorEvent($e) - { - $this->deferred->reject($e); - } - - public function write($data) - { - if ($this->closed) { - return false; - } - - $this->buffer .= $data; - $this->deferred->progress($data); - - return true; - } - - public function close() - { - if ($this->closed) { - return; - } - - parent::close(); - $this->deferred->resolve($this->buffer); - } - - public function promise() - { - return $this->deferred->promise(); - } - - public static function createPromise(ReadableStreamInterface $stream) - { - $sink = new static(); - $stream->pipe($sink); - - return $sink->promise(); - } -} diff --git a/tests/BufferedSinkTest.php b/tests/BufferedSinkTest.php deleted file mode 100644 index d5ebcfb..0000000 --- a/tests/BufferedSinkTest.php +++ /dev/null @@ -1,192 +0,0 @@ -promise(); - - $this->assertInstanceOf('React\Promise\PromiseInterface', $contents); - } - - /** @test */ - public function endShouldResolvePromiseWithBufferContents() - { - $callback = $this->expectCallableOnceWith('foo'); - - $sink = new BufferedSink(); - $sink - ->promise() - ->then($callback); - - $sink->write('foo'); - $sink->end(); - } - - /** @test */ - public function closeWithEmptyBufferShouldResolveToEmptyString() - { - $callback = $this->expectCallableOnceWith(''); - - $sink = new BufferedSink(); - $sink - ->promise() - ->then($callback); - - $sink->close(); - $sink->close(); - } - - /** @test */ - public function closeTwiceShouldBeFine() - { - $callback = $this->expectCallableOnce(); - - $sink = new BufferedSink(); - $sink - ->promise() - ->then($callback); - - $sink->close(); - $sink->close(); - } - - /** @test */ - public function resovedValueShouldContainMultipleWrites() - { - $callback = $this->expectCallableOnceWith('foobarbaz'); - - $sink = new BufferedSink(); - $sink - ->promise() - ->then($callback); - - $sink->write('foo'); - $sink->write('bar'); - $sink->write('baz'); - $sink->end(); - } - - /** @test */ - public function dataWrittenOnEndShouldBeBuffered() - { - $callback = $this->expectCallableOnceWith('foobar'); - - $sink = new BufferedSink(); - $sink - ->promise() - ->then($callback); - - $sink->write('foo'); - $sink->end('bar'); - } - - /** @test */ - public function errorsShouldRejectPromise() - { - $errback = $this->expectCallableOnceWith($this->callback(function ($e) { - return $e instanceof \Exception && 'Shit happens' === $e->getMessage(); - })); - - $sink = new BufferedSink(); - $sink - ->promise() - ->then($this->expectCallableNever(), $errback); - - $sink->emit('error', array(new \Exception('Shit happens'))); - } - - /** @test */ - public function writeShouldTriggerProgressOnPromise() - { - $callback = $this->createCallableMock(); - $callback - ->expects($this->at(0)) - ->method('__invoke') - ->with('foo'); - $callback - ->expects($this->at(1)) - ->method('__invoke') - ->with('bar'); - $callback - ->expects($this->at(2)) - ->method('__invoke') - ->with('baz'); - - $sink = new BufferedSink(); - $sink - ->promise() - ->then(null, null, $callback); - - $sink->write('foo'); - $sink->write('bar'); - $sink->end('baz'); - } - - /** @test */ - public function writeAfterEndShouldReturnFalse() - { - $sink = new BufferedSink(); - $sink->end(); - - $this->assertFalse($sink->write('foo')); - } - - /** @test */ - public function forwardedErrorsFromPipeShouldRejectPromise() - { - $errback = $this->expectCallableOnceWith($this->callback(function ($e) { - return $e instanceof \Exception && 'Shit happens' === $e->getMessage(); - })); - - $sink = new BufferedSink(); - $sink - ->promise() - ->then($this->expectCallableNever(), $errback); - - $readable = new ReadableStream(); - $readable->pipe($sink); - $readable->emit('error', array(new \Exception('Shit happens'))); - } - - /** @test */ - public function pipeShouldSucceedAndResolve() - { - $callback = $this->expectCallableOnceWith('foobar'); - - $sink = new BufferedSink(); - $sink - ->promise() - ->then($callback); - - $readable = new ReadableStream(); - $readable->pipe($sink); - $readable->emit('data', array('foo')); - $readable->emit('data', array('bar')); - $readable->close(); - } - - /** @test */ - public function factoryMethodShouldImplicitlyPipeAndPromise() - { - $callback = $this->expectCallableOnceWith('foo'); - - $readable = new ReadableStream(); - - BufferedSink::createPromise($readable) - ->then($callback); - - $readable->emit('data', array('foo')); - $readable->close(); - } -}