LimitStreamTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace GuzzleHttp\Tests\Http;
  3. use GuzzleHttp\Stream\FnStream;
  4. use GuzzleHttp\Stream\Stream;
  5. use GuzzleHttp\Stream\LimitStream;
  6. use GuzzleHttp\Stream\NoSeekStream;
  7. /**
  8. * @covers GuzzleHttp\Stream\LimitStream
  9. */
  10. class LimitStreamTest extends \PHPUnit_Framework_TestCase
  11. {
  12. /** @var LimitStream */
  13. protected $body;
  14. /** @var Stream */
  15. protected $decorated;
  16. public function setUp()
  17. {
  18. $this->decorated = Stream::factory(fopen(__FILE__, 'r'));
  19. $this->body = new LimitStream($this->decorated, 10, 3);
  20. }
  21. public function testReturnsSubset()
  22. {
  23. $body = new LimitStream(Stream::factory('foo'), -1, 1);
  24. $this->assertEquals('oo', (string) $body);
  25. $this->assertTrue($body->eof());
  26. $body->seek(0);
  27. $this->assertFalse($body->eof());
  28. $this->assertEquals('oo', $body->read(100));
  29. $this->assertEmpty($body->read(1));
  30. $this->assertTrue($body->eof());
  31. }
  32. public function testReturnsSubsetWhenCastToString()
  33. {
  34. $body = Stream::factory('foo_baz_bar');
  35. $limited = new LimitStream($body, 3, 4);
  36. $this->assertEquals('baz', (string) $limited);
  37. }
  38. public function testReturnsSubsetOfEmptyBodyWhenCastToString()
  39. {
  40. $body = Stream::factory('');
  41. $limited = new LimitStream($body, 0, 10);
  42. $this->assertEquals('', (string) $limited);
  43. }
  44. public function testSeeksWhenConstructed()
  45. {
  46. $this->assertEquals(0, $this->body->tell());
  47. $this->assertEquals(3, $this->decorated->tell());
  48. }
  49. public function testAllowsBoundedSeek()
  50. {
  51. $this->assertEquals(true, $this->body->seek(100));
  52. $this->assertEquals(10, $this->body->tell());
  53. $this->assertEquals(13, $this->decorated->tell());
  54. $this->assertEquals(true, $this->body->seek(0));
  55. $this->assertEquals(0, $this->body->tell());
  56. $this->assertEquals(3, $this->decorated->tell());
  57. $this->assertEquals(false, $this->body->seek(-10));
  58. $this->assertEquals(0, $this->body->tell());
  59. $this->assertEquals(3, $this->decorated->tell());
  60. $this->assertEquals(true, $this->body->seek(5));
  61. $this->assertEquals(5, $this->body->tell());
  62. $this->assertEquals(8, $this->decorated->tell());
  63. $this->assertEquals(false, $this->body->seek(1000, SEEK_END));
  64. }
  65. public function testReadsOnlySubsetOfData()
  66. {
  67. $data = $this->body->read(100);
  68. $this->assertEquals(10, strlen($data));
  69. $this->assertFalse($this->body->read(1000));
  70. $this->body->setOffset(10);
  71. $newData = $this->body->read(100);
  72. $this->assertEquals(10, strlen($newData));
  73. $this->assertNotSame($data, $newData);
  74. }
  75. /**
  76. * @expectedException \GuzzleHttp\Stream\Exception\SeekException
  77. * @expectedExceptionMessage Could not seek the stream to position 2
  78. */
  79. public function testThrowsWhenCurrentGreaterThanOffsetSeek()
  80. {
  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. }