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