CachingStreamTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace GuzzleHttp\Tests\Stream;
  3. use GuzzleHttp\Stream\Stream;
  4. use GuzzleHttp\Stream\CachingStream;
  5. use GuzzleHttp\Stream\Utils;
  6. /**
  7. * @covers GuzzleHttp\Stream\CachingStream
  8. */
  9. class CachingStreamTest extends \PHPUnit_Framework_TestCase
  10. {
  11. /** @var CachingStream */
  12. protected $body;
  13. /** @var Stream */
  14. protected $decorated;
  15. public function setUp()
  16. {
  17. $this->decorated = Stream::factory('testing');
  18. $this->body = new CachingStream($this->decorated);
  19. }
  20. public function tearDown()
  21. {
  22. $this->decorated->close();
  23. $this->body->close();
  24. }
  25. public function testUsesRemoteSizeIfPossible()
  26. {
  27. $body = Stream::factory('test');
  28. $caching = new CachingStream($body);
  29. $this->assertEquals(4, $caching->getSize());
  30. }
  31. /**
  32. * @expectedException \RuntimeException
  33. * @expectedExceptionMessage Cannot seek to byte 10
  34. */
  35. public function testCannotSeekPastWhatHasBeenRead()
  36. {
  37. $this->body->seek(10);
  38. }
  39. public function testCannotUseSeekEnd()
  40. {
  41. $this->assertFalse($this->body->seek(2, SEEK_END));
  42. }
  43. public function testRewindUsesSeek()
  44. {
  45. $a = Stream::factory('foo');
  46. $d = $this->getMockBuilder('GuzzleHttp\Stream\CachingStream')
  47. ->setMethods(array('seek'))
  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. $this->assertEquals("0000\n", Utils::readline($body));
  84. $this->assertEquals("0001\n", Utils::readline($body));
  85. // Write over part of the body yet to be read, so skip some bytes
  86. $this->assertEquals(5, $body->write("TEST\n"));
  87. $this->assertEquals(5, $this->readAttribute($body, 'skipReadBytes'));
  88. // Read, which skips bytes, then reads
  89. $this->assertEquals("0003\n", Utils::readline($body));
  90. $this->assertEquals(0, $this->readAttribute($body, 'skipReadBytes'));
  91. $this->assertEquals("0004\n", Utils::readline($body));
  92. $this->assertEquals("0005\n", Utils::readline($body));
  93. // Overwrite part of the cached body (so don't skip any bytes)
  94. $body->seek(5);
  95. $this->assertEquals(5, $body->write("ABCD\n"));
  96. $this->assertEquals(0, $this->readAttribute($body, 'skipReadBytes'));
  97. $this->assertEquals("TEST\n", Utils::readline($body));
  98. $this->assertEquals("0003\n", Utils::readline($body));
  99. $this->assertEquals("0004\n", Utils::readline($body));
  100. $this->assertEquals("0005\n", Utils::readline($body));
  101. $this->assertEquals("0006\n", Utils::readline($body));
  102. $this->assertEquals(5, $body->write("1234\n"));
  103. $this->assertEquals(5, $this->readAttribute($body, 'skipReadBytes'));
  104. // Seek to 0 and ensure the overwritten bit is replaced
  105. $body->seek(0);
  106. $this->assertEquals("0000\nABCD\nTEST\n0003\n0004\n0005\n0006\n1234\n0008\n0009\n", $body->read(50));
  107. // Ensure that casting it to a string does not include the bit that was overwritten
  108. $this->assertContains("0000\nABCD\nTEST\n0003\n0004\n0005\n0006\n1234\n0008\n0009\n", (string) $body);
  109. }
  110. public function testClosesBothStreams()
  111. {
  112. $s = fopen('php://temp', 'r');
  113. $a = Stream::factory($s);
  114. $d = new CachingStream($a);
  115. $d->close();
  116. $this->assertFalse(is_resource($s));
  117. }
  118. }