LimitStreamTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace GuzzleHttp\Tests\Http;
  3. use GuzzleHttp\Stream\Exception\SeekException;
  4. use GuzzleHttp\Stream\FnStream;
  5. use GuzzleHttp\Stream\Stream;
  6. use GuzzleHttp\Stream\LimitStream;
  7. use GuzzleHttp\Stream\NoSeekStream;
  8. use PHPUnit\Framework\TestCase;
  9. /**
  10. * @covers GuzzleHttp\Stream\LimitStream
  11. */
  12. class LimitStreamTest extends TestCase
  13. {
  14. /** @var LimitStream */
  15. protected $body;
  16. /** @var Stream */
  17. protected $decorated;
  18. public function setUp(): void
  19. {
  20. $this->decorated = Stream::factory(fopen(__FILE__, 'r'));
  21. $this->body = new LimitStream($this->decorated, 10, 3);
  22. }
  23. public function testReturnsSubset()
  24. {
  25. $body = new LimitStream(Stream::factory('foo'), -1, 1);
  26. $this->assertEquals('oo', (string) $body);
  27. $this->assertTrue($body->eof());
  28. $body->seek(0);
  29. $this->assertFalse($body->eof());
  30. $this->assertEquals('oo', $body->read(100));
  31. $this->assertEmpty($body->read(1));
  32. $this->assertTrue($body->eof());
  33. }
  34. public function testReturnsSubsetWhenCastToString()
  35. {
  36. $body = Stream::factory('foo_baz_bar');
  37. $limited = new LimitStream($body, 3, 4);
  38. $this->assertEquals('baz', (string) $limited);
  39. }
  40. public function testReturnsSubsetOfEmptyBodyWhenCastToString()
  41. {
  42. $body = Stream::factory('');
  43. $limited = new LimitStream($body, 0, 10);
  44. $this->assertEquals('', (string) $limited);
  45. }
  46. public function testSeeksWhenConstructed()
  47. {
  48. $this->assertEquals(0, $this->body->tell());
  49. $this->assertEquals(3, $this->decorated->tell());
  50. }
  51. public function testAllowsBoundedSeek()
  52. {
  53. $this->assertEquals(true, $this->body->seek(100));
  54. $this->assertEquals(10, $this->body->tell());
  55. $this->assertEquals(13, $this->decorated->tell());
  56. $this->assertEquals(true, $this->body->seek(0));
  57. $this->assertEquals(0, $this->body->tell());
  58. $this->assertEquals(3, $this->decorated->tell());
  59. $this->assertEquals(false, $this->body->seek(-10));
  60. $this->assertEquals(0, $this->body->tell());
  61. $this->assertEquals(3, $this->decorated->tell());
  62. $this->assertEquals(true, $this->body->seek(5));
  63. $this->assertEquals(5, $this->body->tell());
  64. $this->assertEquals(8, $this->decorated->tell());
  65. $this->assertEquals(false, $this->body->seek(1000, SEEK_END));
  66. }
  67. public function testReadsOnlySubsetOfData()
  68. {
  69. $data = $this->body->read(100);
  70. $this->assertEquals(10, strlen($data));
  71. $this->assertFalse($this->body->read(1000));
  72. $this->body->setOffset(10);
  73. $newData = $this->body->read(100);
  74. $this->assertEquals(10, strlen($newData));
  75. $this->assertNotSame($data, $newData);
  76. }
  77. public function testThrowsWhenCurrentGreaterThanOffsetSeek()
  78. {
  79. $this->expectException(SeekException::class);
  80. $this->expectExceptionMessage('Could not seek the stream to position 2');
  81. $a = Stream::factory('foo_bar');
  82. $b = new NoSeekStream($a);
  83. $c = new LimitStream($b);
  84. $a->getContents();
  85. $c->setOffset(2);
  86. }
  87. public function testClaimsConsumedWhenReadLimitIsReached()
  88. {
  89. $this->assertFalse($this->body->eof());
  90. $this->body->read(1000);
  91. $this->assertTrue($this->body->eof());
  92. }
  93. public function testContentLengthIsBounded()
  94. {
  95. $this->assertEquals(10, $this->body->getSize());
  96. }
  97. public function testGetContentsIsBasedOnSubset()
  98. {
  99. $body = new LimitStream(Stream::factory('foobazbar'), 3, 3);
  100. $this->assertEquals('baz', $body->getContents());
  101. }
  102. public function testReturnsNullIfSizeCannotBeDetermined()
  103. {
  104. $a = new FnStream([
  105. 'getSize' => function () { return null; },
  106. 'tell' => function () { return 0; },
  107. ]);
  108. $b = new LimitStream($a);
  109. $this->assertNull($b->getSize());
  110. }
  111. public function testLengthLessOffsetWhenNoLimitSize()
  112. {
  113. $a = Stream::factory('foo_bar');
  114. $b = new LimitStream($a, -1, 4);
  115. $this->assertEquals(3, $b->getSize());
  116. }
  117. }