LimitStreamTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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->assertTrue($body->eof());
  30. }
  31. public function testReturnsSubsetWhenCastToString()
  32. {
  33. $body = Stream::factory('foo_baz_bar');
  34. $limited = new LimitStream($body, 3, 4);
  35. $this->assertEquals('baz', (string) $limited);
  36. }
  37. public function testReturnsSubsetOfEmptyBodyWhenCastToString()
  38. {
  39. $body = Stream::factory('');
  40. $limited = new LimitStream($body, 0, 10);
  41. $this->assertEquals('', (string) $limited);
  42. }
  43. public function testSeeksWhenConstructed()
  44. {
  45. $this->assertEquals(0, $this->body->tell());
  46. $this->assertEquals(3, $this->decorated->tell());
  47. }
  48. public function testAllowsBoundedSeek()
  49. {
  50. $this->assertEquals(true, $this->body->seek(100));
  51. $this->assertEquals(10, $this->body->tell());
  52. $this->assertEquals(13, $this->decorated->tell());
  53. $this->assertEquals(true, $this->body->seek(0));
  54. $this->assertEquals(0, $this->body->tell());
  55. $this->assertEquals(3, $this->decorated->tell());
  56. $this->assertEquals(false, $this->body->seek(-10));
  57. $this->assertEquals(0, $this->body->tell());
  58. $this->assertEquals(3, $this->decorated->tell());
  59. $this->assertEquals(true, $this->body->seek(5));
  60. $this->assertEquals(5, $this->body->tell());
  61. $this->assertEquals(8, $this->decorated->tell());
  62. $this->assertEquals(false, $this->body->seek(1000, SEEK_END));
  63. }
  64. public function testReadsOnlySubsetOfData()
  65. {
  66. $data = $this->body->read(100);
  67. $this->assertEquals(10, strlen($data));
  68. $this->assertFalse($this->body->read(1000));
  69. $this->body->setOffset(10);
  70. $newData = $this->body->read(100);
  71. $this->assertEquals(10, strlen($newData));
  72. $this->assertNotSame($data, $newData);
  73. }
  74. /**
  75. * @expectedException \GuzzleHttp\Stream\Exception\SeekException
  76. * @expectedExceptionMessage Could not seek the stream to position 2
  77. */
  78. public function testThrowsWhenCurrentGreaterThanOffsetSeek()
  79. {
  80. $a = Stream::factory('foo_bar');
  81. $b = new NoSeekStream($a);
  82. $c = new LimitStream($b);
  83. $a->getContents();
  84. $c->setOffset(2);
  85. }
  86. public function testClaimsConsumedWhenReadLimitIsReached()
  87. {
  88. $this->assertFalse($this->body->eof());
  89. $this->body->read(1000);
  90. $this->assertTrue($this->body->eof());
  91. }
  92. public function testContentLengthIsBounded()
  93. {
  94. $this->assertEquals(10, $this->body->getSize());
  95. }
  96. public function testGetContentsIsBasedOnSubset()
  97. {
  98. $body = new LimitStream(Stream::factory('foobazbar'), 3, 3);
  99. $this->assertEquals('baz', $body->getContents());
  100. }
  101. public function testReturnsNullIfSizeCannotBeDetermined()
  102. {
  103. $a = new FnStream([
  104. 'getSize' => function () { return null; },
  105. 'tell' => function () { return 0; },
  106. ]);
  107. $b = new LimitStream($a);
  108. $this->assertNull($b->getSize());
  109. }
  110. public function testLengthLessOffsetWhenNoLimitSize()
  111. {
  112. $a = Stream::factory('foo_bar');
  113. $b = new LimitStream($a, -1, 4);
  114. $this->assertEquals(3, $b->getSize());
  115. }
  116. }