LimitStreamTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace RingCentral\Tests\Psr7;
  3. use RingCentral\Psr7;
  4. use RingCentral\Psr7\FnStream;
  5. use RingCentral\Psr7\Stream;
  6. use RingCentral\Psr7\LimitStream;
  7. use RingCentral\Psr7\NoSeekStream;
  8. /**
  9. * @covers RingCentral\Psr7\LimitStream
  10. */
  11. class LimitStreamTest extends \PHPUnit_Framework_TestCase
  12. {
  13. /** @var LimitStream */
  14. protected $body;
  15. /** @var Stream */
  16. protected $decorated;
  17. public 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. /**
  40. * @expectedException \RuntimeException
  41. * @expectedExceptionMessage Unable to seek to stream position 10 with whence 0
  42. */
  43. public function testEnsuresPositionCanBeekSeekedTo()
  44. {
  45. new LimitStream(Psr7\stream_for(''), 0, 10);
  46. }
  47. public function testReturnsSubsetOfEmptyBodyWhenCastToString()
  48. {
  49. $body = Psr7\stream_for('01234567891234');
  50. $limited = new LimitStream($body, 0, 10);
  51. $this->assertEquals('', (string) $limited);
  52. }
  53. public function testReturnsSpecificSubsetOBodyWhenCastToString()
  54. {
  55. $body = Psr7\stream_for('0123456789abcdef');
  56. $limited = new LimitStream($body, 3, 10);
  57. $this->assertEquals('abc', (string) $limited);
  58. }
  59. public function testSeeksWhenConstructed()
  60. {
  61. $this->assertEquals(0, $this->body->tell());
  62. $this->assertEquals(3, $this->decorated->tell());
  63. }
  64. public function testAllowsBoundedSeek()
  65. {
  66. $this->body->seek(100);
  67. $this->assertEquals(10, $this->body->tell());
  68. $this->assertEquals(13, $this->decorated->tell());
  69. $this->body->seek(0);
  70. $this->assertEquals(0, $this->body->tell());
  71. $this->assertEquals(3, $this->decorated->tell());
  72. try {
  73. $this->body->seek(-10);
  74. $this->fail();
  75. } catch (\RuntimeException $e) {}
  76. $this->assertEquals(0, $this->body->tell());
  77. $this->assertEquals(3, $this->decorated->tell());
  78. $this->body->seek(5);
  79. $this->assertEquals(5, $this->body->tell());
  80. $this->assertEquals(8, $this->decorated->tell());
  81. // Fail
  82. try {
  83. $this->body->seek(1000, SEEK_END);
  84. $this->fail();
  85. } catch (\RuntimeException $e) {}
  86. }
  87. public function testReadsOnlySubsetOfData()
  88. {
  89. $data = $this->body->read(100);
  90. $this->assertEquals(10, strlen($data));
  91. $this->assertSame('', $this->body->read(1000));
  92. $this->body->setOffset(10);
  93. $newData = $this->body->read(100);
  94. $this->assertEquals(10, strlen($newData));
  95. $this->assertNotSame($data, $newData);
  96. }
  97. /**
  98. * @expectedException \RuntimeException
  99. * @expectedExceptionMessage Could not seek to stream offset 2
  100. */
  101. public function testThrowsWhenCurrentGreaterThanOffsetSeek()
  102. {
  103. $a = Psr7\stream_for('foo_bar');
  104. $b = new NoSeekStream($a);
  105. $c = new LimitStream($b);
  106. $a->getContents();
  107. $c->setOffset(2);
  108. }
  109. public function testCanGetContentsWithoutSeeking()
  110. {
  111. $a = Psr7\stream_for('foo_bar');
  112. $b = new NoSeekStream($a);
  113. $c = new LimitStream($b);
  114. $this->assertEquals('foo_bar', $c->getContents());
  115. }
  116. public function testClaimsConsumedWhenReadLimitIsReached()
  117. {
  118. $this->assertFalse($this->body->eof());
  119. $this->body->read(1000);
  120. $this->assertTrue($this->body->eof());
  121. }
  122. public function testContentLengthIsBounded()
  123. {
  124. $this->assertEquals(10, $this->body->getSize());
  125. }
  126. public function testGetContentsIsBasedOnSubset()
  127. {
  128. $body = new LimitStream(Psr7\stream_for('foobazbar'), 3, 3);
  129. $this->assertEquals('baz', $body->getContents());
  130. }
  131. public function testReturnsNullIfSizeCannotBeDetermined()
  132. {
  133. $a = new FnStream(array(
  134. 'getSize' => function () { return null; },
  135. 'tell' => function () { return 0; },
  136. ));
  137. $b = new LimitStream($a);
  138. $this->assertNull($b->getSize());
  139. }
  140. public function testLengthLessOffsetWhenNoLimitSize()
  141. {
  142. $a = Psr7\stream_for('foo_bar');
  143. $b = new LimitStream($a, -1, 4);
  144. $this->assertEquals(3, $b->getSize());
  145. }
  146. }