1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/movim_ynh.git synced 2024-09-03 19:46:19 +02:00
movim_ynh/sources/vendor/react/stream/tests/BufferedSinkTest.php
2016-03-14 23:16:11 +01:00

194 lines
4.6 KiB
PHP

<?php
namespace React\Tests\Stream;
use React\Stream\BufferedSink;
use React\Stream\ReadableStream;
/**
* @covers React\Stream\BufferedSink
*/
class BufferedSinkTest extends TestCase
{
/** @test */
public function promiseShouldReturnPromise()
{
$sink = new BufferedSink();
$contents = $sink->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 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();
}
private function expectCallableOnceWith($value)
{
$callback = $this->createCallableMock();
$callback
->expects($this->once())
->method('__invoke')
->with($value);
return $callback;
}
}