CompositeStreamTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace React\Tests\Stream;
  3. use React\Stream\CompositeStream;
  4. use React\Stream\ReadableStream;
  5. use React\Stream\WritableStream;
  6. /**
  7. * @covers React\Stream\CompositeStream
  8. */
  9. class CompositeStreamTest extends TestCase
  10. {
  11. /** @test */
  12. public function itShouldForwardWritableCallsToWritableStream()
  13. {
  14. $readable = $this->getMock('React\Stream\ReadableStreamInterface');
  15. $writable = $this->getMock('React\Stream\WritableStreamInterface');
  16. $writable
  17. ->expects($this->once())
  18. ->method('write')
  19. ->with('foo');
  20. $writable
  21. ->expects($this->once())
  22. ->method('isWritable');
  23. $composite = new CompositeStream($readable, $writable);
  24. $composite->write('foo');
  25. $composite->isWritable();
  26. }
  27. /** @test */
  28. public function itShouldForwardReadableCallsToReadableStream()
  29. {
  30. $readable = $this->getMock('React\Stream\ReadableStreamInterface');
  31. $readable
  32. ->expects($this->once())
  33. ->method('isReadable');
  34. $readable
  35. ->expects($this->once())
  36. ->method('pause');
  37. $readable
  38. ->expects($this->once())
  39. ->method('resume');
  40. $writable = $this->getMock('React\Stream\WritableStreamInterface');
  41. $composite = new CompositeStream($readable, $writable);
  42. $composite->isReadable();
  43. $composite->pause();
  44. $composite->resume();
  45. }
  46. /** @test */
  47. public function endShouldDelegateToWritableWithData()
  48. {
  49. $readable = $this->getMock('React\Stream\ReadableStreamInterface');
  50. $writable = $this->getMock('React\Stream\WritableStreamInterface');
  51. $writable
  52. ->expects($this->once())
  53. ->method('end')
  54. ->with('foo');
  55. $composite = new CompositeStream($readable, $writable);
  56. $composite->end('foo');
  57. }
  58. /** @test */
  59. public function closeShouldCloseBothStreams()
  60. {
  61. $readable = $this->getMock('React\Stream\ReadableStreamInterface');
  62. $readable
  63. ->expects($this->once())
  64. ->method('close');
  65. $writable = $this->getMock('React\Stream\WritableStreamInterface');
  66. $writable
  67. ->expects($this->once())
  68. ->method('close');
  69. $composite = new CompositeStream($readable, $writable);
  70. $composite->close();
  71. }
  72. /** @test */
  73. public function itShouldForwardCloseOnlyOnce()
  74. {
  75. $readable = new ReadableStream();
  76. $writable = new WritableStream();
  77. $composite = new CompositeStream($readable, $writable);
  78. $composite->on('close', $this->expectCallableOnce());
  79. $readable->close();
  80. $writable->close();
  81. }
  82. /** @test */
  83. public function itShouldReceiveForwardedEvents()
  84. {
  85. $readable = new ReadableStream();
  86. $writable = new WritableStream();
  87. $composite = new CompositeStream($readable, $writable);
  88. $composite->on('data', $this->expectCallableOnce());
  89. $composite->on('drain', $this->expectCallableOnce());
  90. $readable->emit('data', array('foo'));
  91. $writable->emit('drain');
  92. }
  93. /** @test */
  94. public function itShouldHandlePipingCorrectly()
  95. {
  96. $readable = $this->getMock('React\Stream\ReadableStreamInterface');
  97. $writable = $this->getMock('React\Stream\WritableStreamInterface');
  98. $writable
  99. ->expects($this->once())
  100. ->method('write')
  101. ->with('foo');
  102. $composite = new CompositeStream($readable, $writable);
  103. $input = new ReadableStream();
  104. $input->pipe($composite);
  105. $input->emit('data', array('foo'));
  106. }
  107. /** @test */
  108. public function itShouldForwardPauseAndResumeUpstreamWhenPipedTo()
  109. {
  110. $readable = $this->getMock('React\Stream\ReadableStreamInterface');
  111. $writable = $this->getMock('React\Stream\WritableStream', array('write'));
  112. $writable
  113. ->expects($this->once())
  114. ->method('write')
  115. ->will($this->returnValue(false));
  116. $composite = new CompositeStream($readable, $writable);
  117. $input = $this->getMock('React\Stream\ReadableStream', array('pause', 'resume'));
  118. $input
  119. ->expects($this->once())
  120. ->method('pause');
  121. $input
  122. ->expects($this->once())
  123. ->method('resume');
  124. $input->pipe($composite);
  125. $input->emit('data', array('foo'));
  126. $writable->emit('drain');
  127. }
  128. /** @test */
  129. public function itShouldForwardPipeCallsToReadableStream()
  130. {
  131. $readable = new ReadableStream();
  132. $writable = $this->getMock('React\Stream\WritableStreamInterface');
  133. $composite = new CompositeStream($readable, $writable);
  134. $output = $this->getMock('React\Stream\WritableStreamInterface');
  135. $output
  136. ->expects($this->once())
  137. ->method('write')
  138. ->with('foo');
  139. $composite->pipe($output);
  140. $readable->emit('data', array('foo'));
  141. }
  142. }