123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- <?php
- namespace React\Tests\Stream;
- use React\Stream\ReadableStream;
- use React\Stream\ThroughStream;
- /**
- * @covers React\Stream\ThroughStream
- */
- class ThroughStreamTest extends TestCase
- {
- /** @test */
- public function itShouldEmitAnyDataWrittenToIt()
- {
- $through = new ThroughStream();
- $through->on('data', $this->expectCallableOnceWith('foo'));
- $through->write('foo');
- }
- /** @test */
- public function pipingStuffIntoItShouldWork()
- {
- $readable = new ReadableStream();
- $through = new ThroughStream();
- $through->on('data', $this->expectCallableOnceWith('foo'));
- $readable->pipe($through);
- $readable->emit('data', array('foo'));
- }
- /** @test */
- public function endShouldCloseTheStream()
- {
- $through = new ThroughStream();
- $through->on('data', $this->expectCallableNever());
- $through->end();
- $this->assertFalse($through->isReadable());
- $this->assertFalse($through->isWritable());
- }
- /** @test */
- public function endShouldWriteDataBeforeClosing()
- {
- $through = new ThroughStream();
- $through->on('data', $this->expectCallableOnceWith('foo'));
- $through->end('foo');
- $this->assertFalse($through->isReadable());
- $this->assertFalse($through->isWritable());
- }
- /** @test */
- public function itShouldBeReadableByDefault()
- {
- $through = new ThroughStream();
- $this->assertTrue($through->isReadable());
- }
- /** @test */
- public function itShouldBeWritableByDefault()
- {
- $through = new ThroughStream();
- $this->assertTrue($through->isWritable());
- }
- /** @test */
- public function pauseShouldDelegateToPipeSource()
- {
- $input = $this->getMock('React\Stream\ReadableStream', array('pause'));
- $input
- ->expects($this->once())
- ->method('pause');
- $through = new ThroughStream();
- $input->pipe($through);
- $through->pause();
- }
- /** @test */
- public function resumeShouldDelegateToPipeSource()
- {
- $input = $this->getMock('React\Stream\ReadableStream', array('resume'));
- $input
- ->expects($this->once())
- ->method('resume');
- $through = new ThroughStream();
- $input->pipe($through);
- $through->resume();
- }
- /** @test */
- public function closeShouldCloseOnce()
- {
- $through = new ThroughStream();
- $through->on('close', $this->expectCallableOnce());
- $through->close();
- $this->assertFalse($through->isReadable());
- $this->assertFalse($through->isWritable());
- }
- /** @test */
- public function doubleCloseShouldCloseOnce()
- {
- $through = new ThroughStream();
- $through->on('close', $this->expectCallableOnce());
- $through->close();
- $through->close();
- $this->assertFalse($through->isReadable());
- $this->assertFalse($through->isWritable());
- }
- /** @test */
- public function pipeShouldPipeCorrectly()
- {
- $output = $this->getMock('React\Stream\WritableStreamInterface');
- $output
- ->expects($this->once())
- ->method('write')
- ->with('foo');
- $through = new ThroughStream();
- $through->pipe($output);
- $through->write('foo');
- }
- protected function expectCallableOnceWith($arg)
- {
- $mock = $this->createCallableMock();
- $mock
- ->expects($this->once())
- ->method('__invoke')
- ->with($arg);
- return $mock;
- }
- }
|