UtilTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace React\Tests\Stream;
  3. use React\Stream\Buffer;
  4. use React\Stream\ReadableStream;
  5. use React\Stream\Util;
  6. /**
  7. * @covers React\Stream\Util
  8. */
  9. class UtilTest extends TestCase
  10. {
  11. public function testPipeShouldEmitEvents()
  12. {
  13. $readable = $this->getMock('React\Stream\ReadableStreamInterface');
  14. $readable
  15. ->expects($this->at(0))
  16. ->method('on')
  17. ->with('data', $this->isInstanceOf('Closure'));
  18. $readable
  19. ->expects($this->at(1))
  20. ->method('on')
  21. ->with('end', $this->isInstanceOf('Closure'));
  22. $writable = $this->getMock('React\Stream\WritableStreamInterface');
  23. $writable
  24. ->expects($this->at(0))
  25. ->method('emit')
  26. ->with('pipe', array($readable));
  27. Util::pipe($readable, $writable);
  28. }
  29. public function testPipeWithEnd()
  30. {
  31. $readable = new Stub\ReadableStreamStub();
  32. $writable = $this->getMock('React\Stream\WritableStreamInterface');
  33. $writable
  34. ->expects($this->once())
  35. ->method('end');
  36. Util::pipe($readable, $writable);
  37. $readable->end();
  38. }
  39. public function testPipeWithoutEnd()
  40. {
  41. $readable = new Stub\ReadableStreamStub();
  42. $writable = $this->getMock('React\Stream\WritableStreamInterface');
  43. $writable
  44. ->expects($this->never())
  45. ->method('end');
  46. Util::pipe($readable, $writable, array('end' => false));
  47. $readable->end();
  48. }
  49. public function testPipeWithTooSlowWritableShouldPauseReadable()
  50. {
  51. $readable = new Stub\ReadableStreamStub();
  52. $writable = $this->getMock('React\Stream\WritableStreamInterface');
  53. $writable
  54. ->expects($this->once())
  55. ->method('write')
  56. ->with('some data')
  57. ->will($this->returnValue(false));
  58. $readable->pipe($writable);
  59. $this->assertFalse($readable->paused);
  60. $readable->write('some data');
  61. $this->assertTrue($readable->paused);
  62. }
  63. public function testPipeWithTooSlowWritableShouldResumeOnDrain()
  64. {
  65. $readable = new Stub\ReadableStreamStub();
  66. $onDrain = null;
  67. $writable = $this->getMock('React\Stream\WritableStreamInterface');
  68. $writable
  69. ->expects($this->once())
  70. ->method('on')
  71. ->with('drain', $this->isInstanceOf('Closure'))
  72. ->will($this->returnCallback(function ($name, $callback) use (&$onDrain) {
  73. $onDrain = $callback;
  74. }));
  75. $readable->pipe($writable);
  76. $readable->pause();
  77. $this->assertTrue($readable->paused);
  78. $onDrain();
  79. $this->assertFalse($readable->paused);
  80. }
  81. public function testPipeWithBuffer()
  82. {
  83. $readable = new Stub\ReadableStreamStub();
  84. $stream = fopen('php://temp', 'r+');
  85. $loop = $this->createWriteableLoopMock();
  86. $buffer = new Buffer($stream, $loop);
  87. $readable->pipe($buffer);
  88. $readable->write('hello, I am some ');
  89. $readable->write('random data');
  90. rewind($stream);
  91. $this->assertSame('hello, I am some random data', stream_get_contents($stream));
  92. }
  93. /** @test */
  94. public function forwardEventsShouldSetupForwards()
  95. {
  96. $source = new ReadableStream();
  97. $target = new ReadableStream();
  98. Util::forwardEvents($source, $target, array('data'));
  99. $target->on('data', $this->expectCallableOnce());
  100. $target->on('foo', $this->expectCallableNever());
  101. $source->emit('data', array('hello'));
  102. $source->emit('foo', array('bar'));
  103. }
  104. private function createWriteableLoopMock()
  105. {
  106. $loop = $this->createLoopMock();
  107. $loop
  108. ->expects($this->any())
  109. ->method('addWriteStream')
  110. ->will($this->returnCallback(function ($stream, $listener) {
  111. call_user_func($listener, $stream);
  112. }));
  113. return $loop;
  114. }
  115. private function createLoopMock()
  116. {
  117. return $this->getMock('React\EventLoop\LoopInterface');
  118. }
  119. private function notEqualTo($value)
  120. {
  121. return new \PHPUnit_Framework_Constraint_Not($value);
  122. }
  123. }