ThroughStreamTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace React\Tests\Stream;
  3. use React\Stream\ReadableStream;
  4. use React\Stream\ThroughStream;
  5. /**
  6. * @covers React\Stream\ThroughStream
  7. */
  8. class ThroughStreamTest extends TestCase
  9. {
  10. /** @test */
  11. public function itShouldEmitAnyDataWrittenToIt()
  12. {
  13. $through = new ThroughStream();
  14. $through->on('data', $this->expectCallableOnceWith('foo'));
  15. $through->write('foo');
  16. }
  17. /** @test */
  18. public function pipingStuffIntoItShouldWork()
  19. {
  20. $readable = new ReadableStream();
  21. $through = new ThroughStream();
  22. $through->on('data', $this->expectCallableOnceWith('foo'));
  23. $readable->pipe($through);
  24. $readable->emit('data', array('foo'));
  25. }
  26. /** @test */
  27. public function endShouldCloseTheStream()
  28. {
  29. $through = new ThroughStream();
  30. $through->on('data', $this->expectCallableNever());
  31. $through->end();
  32. $this->assertFalse($through->isReadable());
  33. $this->assertFalse($through->isWritable());
  34. }
  35. /** @test */
  36. public function endShouldWriteDataBeforeClosing()
  37. {
  38. $through = new ThroughStream();
  39. $through->on('data', $this->expectCallableOnceWith('foo'));
  40. $through->end('foo');
  41. $this->assertFalse($through->isReadable());
  42. $this->assertFalse($through->isWritable());
  43. }
  44. /** @test */
  45. public function itShouldBeReadableByDefault()
  46. {
  47. $through = new ThroughStream();
  48. $this->assertTrue($through->isReadable());
  49. }
  50. /** @test */
  51. public function itShouldBeWritableByDefault()
  52. {
  53. $through = new ThroughStream();
  54. $this->assertTrue($through->isWritable());
  55. }
  56. /** @test */
  57. public function pauseShouldDelegateToPipeSource()
  58. {
  59. $input = $this->getMock('React\Stream\ReadableStream', array('pause'));
  60. $input
  61. ->expects($this->once())
  62. ->method('pause');
  63. $through = new ThroughStream();
  64. $input->pipe($through);
  65. $through->pause();
  66. }
  67. /** @test */
  68. public function resumeShouldDelegateToPipeSource()
  69. {
  70. $input = $this->getMock('React\Stream\ReadableStream', array('resume'));
  71. $input
  72. ->expects($this->once())
  73. ->method('resume');
  74. $through = new ThroughStream();
  75. $input->pipe($through);
  76. $through->resume();
  77. }
  78. /** @test */
  79. public function closeShouldCloseOnce()
  80. {
  81. $through = new ThroughStream();
  82. $through->on('close', $this->expectCallableOnce());
  83. $through->close();
  84. $this->assertFalse($through->isReadable());
  85. $this->assertFalse($through->isWritable());
  86. }
  87. /** @test */
  88. public function doubleCloseShouldCloseOnce()
  89. {
  90. $through = new ThroughStream();
  91. $through->on('close', $this->expectCallableOnce());
  92. $through->close();
  93. $through->close();
  94. $this->assertFalse($through->isReadable());
  95. $this->assertFalse($through->isWritable());
  96. }
  97. /** @test */
  98. public function pipeShouldPipeCorrectly()
  99. {
  100. $output = $this->getMock('React\Stream\WritableStreamInterface');
  101. $output
  102. ->expects($this->once())
  103. ->method('write')
  104. ->with('foo');
  105. $through = new ThroughStream();
  106. $through->pipe($output);
  107. $through->write('foo');
  108. }
  109. protected function expectCallableOnceWith($arg)
  110. {
  111. $mock = $this->createCallableMock();
  112. $mock
  113. ->expects($this->once())
  114. ->method('__invoke')
  115. ->with($arg);
  116. return $mock;
  117. }
  118. }