CachingStreamTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace GuzzleHttp\Tests\Stream;
  3. use GuzzleHttp\Stream\Stream;
  4. use GuzzleHttp\Stream\CachingStream;
  5. use GuzzleHttp\Stream\Utils;
  6. use PHPUnit\Framework\TestCase;
  7. use ReflectionProperty;
  8. use RuntimeException;
  9. /**
  10. * @covers GuzzleHttp\Stream\CachingStream
  11. */
  12. class CachingStreamTest extends TestCase
  13. {
  14. /** @var CachingStream */
  15. protected $body;
  16. /** @var Stream */
  17. protected $decorated;
  18. public function setUp(): void
  19. {
  20. $this->decorated = Stream::factory('testing');
  21. $this->body = new CachingStream($this->decorated);
  22. }
  23. public function tearDown(): void
  24. {
  25. $this->decorated->close();
  26. $this->body->close();
  27. }
  28. public function testUsesRemoteSizeIfPossible()
  29. {
  30. $body = Stream::factory('test');
  31. $caching = new CachingStream($body);
  32. $this->assertEquals(4, $caching->getSize());
  33. }
  34. public function testCannotSeekPastWhatHasBeenRead()
  35. {
  36. $this->expectException(RuntimeException::class);
  37. $this->expectErrorMessage('Cannot seek to byte 10');
  38. $this->body->seek(10);
  39. }
  40. public function testCannotUseSeekEnd()
  41. {
  42. $this->assertFalse($this->body->seek(2, SEEK_END));
  43. }
  44. public function testRewindUsesSeek()
  45. {
  46. $a = Stream::factory('foo');
  47. $d = $this->getMockBuilder('GuzzleHttp\Stream\CachingStream')
  48. ->setConstructorArgs(array($a))
  49. ->getMock();
  50. $d->expects($this->once())
  51. ->method('seek')
  52. ->with(0)
  53. ->will($this->returnValue(true));
  54. $d->seek(0);
  55. }
  56. public function testCanSeekToReadBytes()
  57. {
  58. $this->assertEquals('te', $this->body->read(2));
  59. $this->body->seek(0);
  60. $this->assertEquals('test', $this->body->read(4));
  61. $this->assertEquals(4, $this->body->tell());
  62. $this->body->seek(2);
  63. $this->assertEquals(2, $this->body->tell());
  64. $this->body->seek(2, SEEK_CUR);
  65. $this->assertEquals(4, $this->body->tell());
  66. $this->assertEquals('ing', $this->body->read(3));
  67. }
  68. public function testWritesToBufferStream()
  69. {
  70. $this->body->read(2);
  71. $this->body->write('hi');
  72. $this->body->seek(0);
  73. $this->assertEquals('tehiing', (string) $this->body);
  74. }
  75. public function testSkipsOverwrittenBytes()
  76. {
  77. $decorated = Stream::factory(
  78. implode("\n", array_map(function ($n) {
  79. return str_pad($n, 4, '0', STR_PAD_LEFT);
  80. }, range(0, 25)))
  81. );
  82. $body = new CachingStream($decorated);
  83. $skipReadBytes = new ReflectionProperty(CachingStream::class, 'skipReadBytes');
  84. $skipReadBytes->setAccessible(true);
  85. $this->assertEquals("0000\n", Utils::readline($body));
  86. $this->assertEquals("0001\n", Utils::readline($body));
  87. // Write over part of the body yet to be read, so skip some bytes
  88. $this->assertEquals(5, $body->write("TEST\n"));
  89. $this->assertEquals(5, $skipReadBytes->getValue($body));
  90. // Read, which skips bytes, then reads
  91. $this->assertEquals("0003\n", Utils::readline($body));
  92. $this->assertEquals(0, $skipReadBytes->getValue($body));
  93. $this->assertEquals("0004\n", Utils::readline($body));
  94. $this->assertEquals("0005\n", Utils::readline($body));
  95. // Overwrite part of the cached body (so don't skip any bytes)
  96. $body->seek(5);
  97. $this->assertEquals(5, $body->write("ABCD\n"));
  98. $this->assertEquals(0, $skipReadBytes->getValue($body));
  99. $this->assertEquals("TEST\n", Utils::readline($body));
  100. $this->assertEquals("0003\n", Utils::readline($body));
  101. $this->assertEquals("0004\n", Utils::readline($body));
  102. $this->assertEquals("0005\n", Utils::readline($body));
  103. $this->assertEquals("0006\n", Utils::readline($body));
  104. $this->assertEquals(5, $body->write("1234\n"));
  105. $this->assertEquals(5, $skipReadBytes->getValue($body));
  106. // Seek to 0 and ensure the overwritten bit is replaced
  107. $body->seek(0);
  108. $this->assertEquals("0000\nABCD\nTEST\n0003\n0004\n0005\n0006\n1234\n0008\n0009\n", $body->read(50));
  109. // Ensure that casting it to a string does not include the bit that was overwritten
  110. $this->assertStringContainsString("0000\nABCD\nTEST\n0003\n0004\n0005\n0006\n1234\n0008\n0009\n", (string) $body);
  111. }
  112. public function testClosesBothStreams()
  113. {
  114. $s = fopen('php://temp', 'r');
  115. $a = Stream::factory($s);
  116. $d = new CachingStream($a);
  117. $d->close();
  118. $this->assertFalse(is_resource($s));
  119. }
  120. }