LimitStreamTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace GuzzleHttp\Tests\Psr7;
  3. use GuzzleHttp\Psr7;
  4. use GuzzleHttp\Psr7\FnStream;
  5. use GuzzleHttp\Psr7\Stream;
  6. use GuzzleHttp\Psr7\LimitStream;
  7. use GuzzleHttp\Psr7\NoSeekStream;
  8. /**
  9. * @covers GuzzleHttp\Psr7\LimitStream
  10. */
  11. class LimitStreamTest extends BaseTest
  12. {
  13. /** @var LimitStream */
  14. private $body;
  15. /** @var Stream */
  16. private $decorated;
  17. protected function setUp()
  18. {
  19. $this->decorated = Psr7\stream_for(fopen(__FILE__, 'r'));
  20. $this->body = new LimitStream($this->decorated, 10, 3);
  21. }
  22. public function testReturnsSubset()
  23. {
  24. $body = new LimitStream(Psr7\stream_for('foo'), -1, 1);
  25. $this->assertEquals('oo', (string) $body);
  26. $this->assertTrue($body->eof());
  27. $body->seek(0);
  28. $this->assertFalse($body->eof());
  29. $this->assertEquals('oo', $body->read(100));
  30. $this->assertSame('', $body->read(1));
  31. $this->assertTrue($body->eof());
  32. }
  33. public function testReturnsSubsetWhenCastToString()
  34. {
  35. $body = Psr7\stream_for('foo_baz_bar');
  36. $limited = new LimitStream($body, 3, 4);
  37. $this->assertEquals('baz', (string) $limited);
  38. }
  39. public function testReturnsSubsetOfEmptyBodyWhenCastToString()
  40. {
  41. $body = Psr7\stream_for('01234567891234');
  42. $limited = new LimitStream($body, 0, 10);
  43. $this->assertEquals('', (string) $limited);
  44. }
  45. public function testReturnsSpecificSubsetOBodyWhenCastToString()
  46. {
  47. $body = Psr7\stream_for('0123456789abcdef');
  48. $limited = new LimitStream($body, 3, 10);
  49. $this->assertEquals('abc', (string) $limited);
  50. }
  51. public function testSeeksWhenConstructed()
  52. {
  53. $this->assertEquals(0, $this->body->tell());
  54. $this->assertEquals(3, $this->decorated->tell());
  55. }
  56. public function testAllowsBoundedSeek()
  57. {
  58. $this->body->seek(100);
  59. $this->assertEquals(10, $this->body->tell());
  60. $this->assertEquals(13, $this->decorated->tell());
  61. $this->body->seek(0);
  62. $this->assertEquals(0, $this->body->tell());
  63. $this->assertEquals(3, $this->decorated->tell());
  64. try {
  65. $this->body->seek(-10);
  66. $this->fail();
  67. } catch (\RuntimeException $e) {}
  68. $this->assertEquals(0, $this->body->tell());
  69. $this->assertEquals(3, $this->decorated->tell());
  70. $this->body->seek(5);
  71. $this->assertEquals(5, $this->body->tell());
  72. $this->assertEquals(8, $this->decorated->tell());
  73. // Fail
  74. try {
  75. $this->body->seek(1000, SEEK_END);
  76. $this->fail();
  77. } catch (\RuntimeException $e) {}
  78. }
  79. public function testReadsOnlySubsetOfData()
  80. {
  81. $data = $this->body->read(100);
  82. $this->assertEquals(10, strlen($data));
  83. $this->assertSame('', $this->body->read(1000));
  84. $this->body->setOffset(10);
  85. $newData = $this->body->read(100);
  86. $this->assertEquals(10, strlen($newData));
  87. $this->assertNotSame($data, $newData);
  88. }
  89. /**
  90. * @expectedException \RuntimeException
  91. * @expectedExceptionMessage Could not seek to stream offset 2
  92. */
  93. public function testThrowsWhenCurrentGreaterThanOffsetSeek()
  94. {
  95. $a = Psr7\stream_for('foo_bar');
  96. $b = new NoSeekStream($a);
  97. $c = new LimitStream($b);
  98. $a->getContents();
  99. $c->setOffset(2);
  100. }
  101. public function testCanGetContentsWithoutSeeking()
  102. {
  103. $a = Psr7\stream_for('foo_bar');
  104. $b = new NoSeekStream($a);
  105. $c = new LimitStream($b);
  106. $this->assertEquals('foo_bar', $c->getContents());
  107. }
  108. public function testClaimsConsumedWhenReadLimitIsReached()
  109. {
  110. $this->assertFalse($this->body->eof());
  111. $this->body->read(1000);
  112. $this->assertTrue($this->body->eof());
  113. }
  114. public function testContentLengthIsBounded()
  115. {
  116. $this->assertEquals(10, $this->body->getSize());
  117. }
  118. public function testGetContentsIsBasedOnSubset()
  119. {
  120. $body = new LimitStream(Psr7\stream_for('foobazbar'), 3, 3);
  121. $this->assertEquals('baz', $body->getContents());
  122. }
  123. public function testReturnsNullIfSizeCannotBeDetermined()
  124. {
  125. $a = new FnStream([
  126. 'getSize' => function () { return null; },
  127. 'tell' => function () { return 0; },
  128. ]);
  129. $b = new LimitStream($a);
  130. $this->assertNull($b->getSize());
  131. }
  132. public function testLengthLessOffsetWhenNoLimitSize()
  133. {
  134. $a = Psr7\stream_for('foo_bar');
  135. $b = new LimitStream($a, -1, 4);
  136. $this->assertEquals(3, $b->getSize());
  137. }
  138. }