AsyncReadStreamTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. namespace GuzzleHttp\Tests\Stream;
  3. use GuzzleHttp\Stream\AsyncReadStream;
  4. use GuzzleHttp\Stream\BufferStream;
  5. use GuzzleHttp\Stream\FnStream;
  6. use GuzzleHttp\Stream\Stream;
  7. class AsyncReadStreamTest extends \PHPUnit_Framework_TestCase
  8. {
  9. /**
  10. * @expectedException \InvalidArgumentException
  11. * @expectedExceptionMessage Buffer must be readable and writable
  12. */
  13. public function testValidatesReadableBuffer()
  14. {
  15. new AsyncReadStream(FnStream::decorate(
  16. Stream::factory(),
  17. ['isReadable' => function () { return false; }]
  18. ));
  19. }
  20. /**
  21. * @expectedException \InvalidArgumentException
  22. * @expectedExceptionMessage Buffer must be readable and writable
  23. */
  24. public function testValidatesWritableBuffer()
  25. {
  26. new AsyncReadStream(FnStream::decorate(
  27. Stream::factory(),
  28. ['isWritable' => function () { return false; }]
  29. ));
  30. }
  31. public function testValidatesHwmMetadata()
  32. {
  33. $a = new AsyncReadStream(Stream::factory(), [
  34. 'drain' => function() {}
  35. ]);
  36. $this->assertNull($this->readAttribute($a, 'drain'));
  37. }
  38. /**
  39. * @expectedException \InvalidArgumentException
  40. * @expectedExceptionMessage pump must be callable
  41. */
  42. public function testValidatesPumpIsCallable()
  43. {
  44. new AsyncReadStream(new BufferStream(), ['pump' => true]);
  45. }
  46. /**
  47. * @expectedException \InvalidArgumentException
  48. * @expectedExceptionMessage drain must be callable
  49. */
  50. public function testValidatesDrainIsCallable()
  51. {
  52. new AsyncReadStream(new BufferStream(), ['drain' => true]);
  53. }
  54. public function testCanInitialize()
  55. {
  56. $buffer = new BufferStream();
  57. $a = new AsyncReadStream($buffer, [
  58. 'size' => 10,
  59. 'drain' => function () {},
  60. 'pump' => function () {},
  61. ]);
  62. $this->assertSame($buffer, $this->readAttribute($a, 'stream'));
  63. $this->assertTrue(is_callable($this->readAttribute($a, 'drain')));
  64. $this->assertTrue(is_callable($this->readAttribute($a, 'pump')));
  65. $this->assertTrue($a->isReadable());
  66. $this->assertFalse($a->isSeekable());
  67. $this->assertFalse($a->isWritable());
  68. $this->assertFalse($a->write('foo'));
  69. $this->assertEquals(10, $a->getSize());
  70. }
  71. public function testReadsFromBufferWithNoDrainOrPump()
  72. {
  73. $buffer = new BufferStream();
  74. $a = new AsyncReadStream($buffer);
  75. $buffer->write('foo');
  76. $this->assertNull($a->getSize());
  77. $this->assertEquals('foo', $a->read(10));
  78. $this->assertEquals('', $a->read(10));
  79. }
  80. public function testCallsPumpForMoreDataWhenRequested()
  81. {
  82. $called = 0;
  83. $buffer = new BufferStream();
  84. $a = new AsyncReadStream($buffer, [
  85. 'pump' => function ($size) use (&$called) {
  86. $called++;
  87. return str_repeat('.', $size);
  88. }
  89. ]);
  90. $buffer->write('foobar');
  91. $this->assertEquals('foo', $a->read(3));
  92. $this->assertEquals(0, $called);
  93. $this->assertEquals('bar.....', $a->read(8));
  94. $this->assertEquals(1, $called);
  95. $this->assertEquals('..', $a->read(2));
  96. $this->assertEquals(2, $called);
  97. }
  98. public function testCallsDrainWhenNeeded()
  99. {
  100. $called = 0;
  101. $buffer = new BufferStream(5);
  102. $a = new AsyncReadStream($buffer, [
  103. 'drain' => function (BufferStream $b) use (&$called, $buffer) {
  104. $this->assertSame($b, $buffer);
  105. $called++;
  106. }
  107. ]);
  108. $buffer->write('foobar');
  109. $this->assertEquals(6, $buffer->getSize());
  110. $this->assertEquals(0, $called);
  111. $a->read(3);
  112. $this->assertTrue($this->readAttribute($a, 'needsDrain'));
  113. $this->assertEquals(3, $buffer->getSize());
  114. $this->assertEquals(0, $called);
  115. $a->read(3);
  116. $this->assertEquals(0, $buffer->getSize());
  117. $this->assertFalse($this->readAttribute($a, 'needsDrain'));
  118. $this->assertEquals(1, $called);
  119. }
  120. public function testCreatesBufferWithNoConfig()
  121. {
  122. list($buffer, $async) = AsyncReadStream::create();
  123. $this->assertInstanceOf('GuzzleHttp\Stream\BufferStream', $buffer);
  124. $this->assertInstanceOf('GuzzleHttp\Stream\AsyncReadStream', $async);
  125. }
  126. public function testCreatesBufferWithSpecifiedBuffer()
  127. {
  128. $buf = new BufferStream();
  129. list($buffer, $async) = AsyncReadStream::create(['buffer' => $buf]);
  130. $this->assertSame($buf, $buffer);
  131. $this->assertInstanceOf('GuzzleHttp\Stream\AsyncReadStream', $async);
  132. }
  133. public function testCreatesNullStream()
  134. {
  135. list($buffer, $async) = AsyncReadStream::create(['max_buffer' => 0]);
  136. $this->assertInstanceOf('GuzzleHttp\Stream\NullStream', $buffer);
  137. $this->assertInstanceOf('GuzzleHttp\Stream\AsyncReadStream', $async);
  138. }
  139. public function testCreatesDroppingStream()
  140. {
  141. list($buffer, $async) = AsyncReadStream::create(['max_buffer' => 5]);
  142. $this->assertInstanceOf('GuzzleHttp\Stream\DroppingStream', $buffer);
  143. $this->assertInstanceOf('GuzzleHttp\Stream\AsyncReadStream', $async);
  144. $buffer->write('12345678910');
  145. $this->assertEquals(5, $buffer->getSize());
  146. }
  147. public function testCreatesOnWriteStream()
  148. {
  149. $c = 0;
  150. $b = new BufferStream();
  151. list($buffer, $async) = AsyncReadStream::create([
  152. 'buffer' => $b,
  153. 'write' => function (BufferStream $buf, $data) use (&$c, $b) {
  154. $this->assertSame($buf, $b);
  155. $this->assertEquals('foo', $data);
  156. $c++;
  157. }
  158. ]);
  159. $this->assertInstanceOf('GuzzleHttp\Stream\FnStream', $buffer);
  160. $this->assertInstanceOf('GuzzleHttp\Stream\AsyncReadStream', $async);
  161. $this->assertEquals(0, $c);
  162. $this->assertEquals(3, $buffer->write('foo'));
  163. $this->assertEquals(1, $c);
  164. $this->assertEquals(3, $buffer->write('foo'));
  165. $this->assertEquals(2, $c);
  166. $this->assertEquals('foofoo', (string) $buffer);
  167. }
  168. }