CachingStreamTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. namespace GuzzleHttp\Tests\Psr7;
  3. use GuzzleHttp\Psr7;
  4. use GuzzleHttp\Psr7\CachingStream;
  5. use GuzzleHttp\Psr7\Stream;
  6. /**
  7. * @covers GuzzleHttp\Psr7\CachingStream
  8. */
  9. class CachingStreamTest extends BaseTest
  10. {
  11. /** @var CachingStream */
  12. private $body;
  13. /** @var Stream */
  14. private $decorated;
  15. protected function setUp()
  16. {
  17. $this->decorated = Psr7\stream_for('testing');
  18. $this->body = new CachingStream($this->decorated);
  19. }
  20. protected function tearDown()
  21. {
  22. $this->decorated->close();
  23. $this->body->close();
  24. }
  25. public function testUsesRemoteSizeIfPossible()
  26. {
  27. $body = Psr7\stream_for('test');
  28. $caching = new CachingStream($body);
  29. $this->assertEquals(4, $caching->getSize());
  30. }
  31. public function testReadsUntilCachedToByte()
  32. {
  33. $this->body->seek(5);
  34. $this->assertEquals('n', $this->body->read(1));
  35. $this->body->seek(0);
  36. $this->assertEquals('t', $this->body->read(1));
  37. }
  38. public function testCanSeekNearEndWithSeekEnd()
  39. {
  40. $baseStream = Psr7\stream_for(implode('', range('a', 'z')));
  41. $cached = new CachingStream($baseStream);
  42. $cached->seek(-1, SEEK_END);
  43. $this->assertEquals(25, $baseStream->tell());
  44. $this->assertEquals('z', $cached->read(1));
  45. $this->assertEquals(26, $cached->getSize());
  46. }
  47. public function testCanSeekToEndWithSeekEnd()
  48. {
  49. $baseStream = Psr7\stream_for(implode('', range('a', 'z')));
  50. $cached = new CachingStream($baseStream);
  51. $cached->seek(0, SEEK_END);
  52. $this->assertEquals(26, $baseStream->tell());
  53. $this->assertEquals('', $cached->read(1));
  54. $this->assertEquals(26, $cached->getSize());
  55. }
  56. public function testCanUseSeekEndWithUnknownSize()
  57. {
  58. $baseStream = Psr7\stream_for('testing');
  59. $decorated = Psr7\FnStream::decorate($baseStream, [
  60. 'getSize' => function () { return null; }
  61. ]);
  62. $cached = new CachingStream($decorated);
  63. $cached->seek(-1, SEEK_END);
  64. $this->assertEquals('g', $cached->read(1));
  65. }
  66. public function testRewindUsesSeek()
  67. {
  68. $a = Psr7\stream_for('foo');
  69. $d = $this->getMockBuilder('GuzzleHttp\Psr7\CachingStream')
  70. ->setMethods(array('seek'))
  71. ->setConstructorArgs(array($a))
  72. ->getMock();
  73. $d->expects($this->once())
  74. ->method('seek')
  75. ->with(0)
  76. ->will($this->returnValue(true));
  77. $d->seek(0);
  78. }
  79. public function testCanSeekToReadBytes()
  80. {
  81. $this->assertEquals('te', $this->body->read(2));
  82. $this->body->seek(0);
  83. $this->assertEquals('test', $this->body->read(4));
  84. $this->assertEquals(4, $this->body->tell());
  85. $this->body->seek(2);
  86. $this->assertEquals(2, $this->body->tell());
  87. $this->body->seek(2, SEEK_CUR);
  88. $this->assertEquals(4, $this->body->tell());
  89. $this->assertEquals('ing', $this->body->read(3));
  90. }
  91. public function testCanSeekToReadBytesWithPartialBodyReturned()
  92. {
  93. $stream = fopen('php://temp', 'r+');
  94. fwrite($stream, 'testing');
  95. fseek($stream, 0);
  96. $this->decorated = $this->getMockBuilder('\GuzzleHttp\Psr7\Stream')
  97. ->setConstructorArgs([$stream])
  98. ->setMethods(['read'])
  99. ->getMock();
  100. $this->decorated->expects($this->exactly(2))
  101. ->method('read')
  102. ->willReturnCallback(function($length) use ($stream){
  103. return fread($stream, 2);
  104. });
  105. $this->body = new CachingStream($this->decorated);
  106. $this->assertEquals(0, $this->body->tell());
  107. $this->body->seek(4, SEEK_SET);
  108. $this->assertEquals(4, $this->body->tell());
  109. $this->body->seek(0);
  110. $this->assertEquals('test', $this->body->read(4));
  111. }
  112. public function testWritesToBufferStream()
  113. {
  114. $this->body->read(2);
  115. $this->body->write('hi');
  116. $this->body->seek(0);
  117. $this->assertEquals('tehiing', (string) $this->body);
  118. }
  119. public function testSkipsOverwrittenBytes()
  120. {
  121. $decorated = Psr7\stream_for(
  122. implode("\n", array_map(function ($n) {
  123. return str_pad($n, 4, '0', STR_PAD_LEFT);
  124. }, range(0, 25)))
  125. );
  126. $body = new CachingStream($decorated);
  127. $this->assertEquals("0000\n", Psr7\readline($body));
  128. $this->assertEquals("0001\n", Psr7\readline($body));
  129. // Write over part of the body yet to be read, so skip some bytes
  130. $this->assertEquals(5, $body->write("TEST\n"));
  131. $this->assertEquals(5, $this->readAttribute($body, 'skipReadBytes'));
  132. // Read, which skips bytes, then reads
  133. $this->assertEquals("0003\n", Psr7\readline($body));
  134. $this->assertEquals(0, $this->readAttribute($body, 'skipReadBytes'));
  135. $this->assertEquals("0004\n", Psr7\readline($body));
  136. $this->assertEquals("0005\n", Psr7\readline($body));
  137. // Overwrite part of the cached body (so don't skip any bytes)
  138. $body->seek(5);
  139. $this->assertEquals(5, $body->write("ABCD\n"));
  140. $this->assertEquals(0, $this->readAttribute($body, 'skipReadBytes'));
  141. $this->assertEquals("TEST\n", Psr7\readline($body));
  142. $this->assertEquals("0003\n", Psr7\readline($body));
  143. $this->assertEquals("0004\n", Psr7\readline($body));
  144. $this->assertEquals("0005\n", Psr7\readline($body));
  145. $this->assertEquals("0006\n", Psr7\readline($body));
  146. $this->assertEquals(5, $body->write("1234\n"));
  147. $this->assertEquals(5, $this->readAttribute($body, 'skipReadBytes'));
  148. // Seek to 0 and ensure the overwritten bit is replaced
  149. $body->seek(0);
  150. $this->assertEquals("0000\nABCD\nTEST\n0003\n0004\n0005\n0006\n1234\n0008\n0009\n", $body->read(50));
  151. // Ensure that casting it to a string does not include the bit that was overwritten
  152. $this->assertContains("0000\nABCD\nTEST\n0003\n0004\n0005\n0006\n1234\n0008\n0009\n", (string) $body);
  153. }
  154. public function testClosesBothStreams()
  155. {
  156. $s = fopen('php://temp', 'r');
  157. $a = Psr7\stream_for($s);
  158. $d = new CachingStream($a);
  159. $d->close();
  160. $this->assertFalse(is_resource($s));
  161. }
  162. /**
  163. * @expectedException \InvalidArgumentException
  164. */
  165. public function testEnsuresValidWhence()
  166. {
  167. $this->body->seek(10, -123456);
  168. }
  169. }