PluginProbe
StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more / 2.4.2
StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more v2.4.2
3.7.2 3.7.1 3.7.0 3.6.0 3.6.1 3.5.2 trunk 2.0 2.1 2.2 2.3 2.4 2.4.1 2.4.2 2.4.3 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.6.0 2.6.1 All 73 releases
stockpack / vendor / guzzlehttp / psr7 / tests / LimitStreamTest.php

LimitStreamTest.php in StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more 2.4.2, at vendor/guzzlehttp/psr7/tests/LimitStreamTest.php

158 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 BaseTest
14 {
15 /** @var LimitStream */
16 private $body;
17
18 /** @var Stream */
19 private $decorated;
20
21 protected 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 public function testReturnsSubsetOfEmptyBodyWhenCastToString()
47 {
48 $body = Psr7\stream_for('01234567891234');
49 $limited = new LimitStream($body, 0, 10);
50 $this->assertEquals('', (string) $limited);
51 }
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
60 public function testSeeksWhenConstructed()
61 {
62 $this->assertEquals(0, $this->body->tell());
63 $this->assertEquals(3, $this->decorated->tell());
64 }
65
66 public function testAllowsBoundedSeek()
67 {
68 $this->body->seek(100);
69 $this->assertEquals(10, $this->body->tell());
70 $this->assertEquals(13, $this->decorated->tell());
71 $this->body->seek(0);
72 $this->assertEquals(0, $this->body->tell());
73 $this->assertEquals(3, $this->decorated->tell());
74 try {
75 $this->body->seek(-10);
76 $this->fail();
77 } catch (\RuntimeException $e) {}
78 $this->assertEquals(0, $this->body->tell());
79 $this->assertEquals(3, $this->decorated->tell());
80 $this->body->seek(5);
81 $this->assertEquals(5, $this->body->tell());
82 $this->assertEquals(8, $this->decorated->tell());
83 // Fail
84 try {
85 $this->body->seek(1000, SEEK_END);
86 $this->fail();
87 } catch (\RuntimeException $e) {}
88 }
89
90 public function testReadsOnlySubsetOfData()
91 {
92 $data = $this->body->read(100);
93 $this->assertEquals(10, strlen($data));
94 $this->assertSame('', $this->body->read(1000));
95
96 $this->body->setOffset(10);
97 $newData = $this->body->read(100);
98 $this->assertEquals(10, strlen($newData));
99 $this->assertNotSame($data, $newData);
100 }
101
102 /**
103 * @expectedException \RuntimeException
104 * @expectedExceptionMessage Could not seek to stream offset 2
105 */
106 public function testThrowsWhenCurrentGreaterThanOffsetSeek()
107 {
108 $a = Psr7\stream_for('foo_bar');
109 $b = new NoSeekStream($a);
110 $c = new LimitStream($b);
111 $a->getContents();
112 $c->setOffset(2);
113 }
114
115 public function testCanGetContentsWithoutSeeking()
116 {
117 $a = Psr7\stream_for('foo_bar');
118 $b = new NoSeekStream($a);
119 $c = new LimitStream($b);
120 $this->assertEquals('foo_bar', $c->getContents());
121 }
122
123 public function testClaimsConsumedWhenReadLimitIsReached()
124 {
125 $this->assertFalse($this->body->eof());
126 $this->body->read(1000);
127 $this->assertTrue($this->body->eof());
128 }
129
130 public function testContentLengthIsBounded()
131 {
132 $this->assertEquals(10, $this->body->getSize());
133 }
134
135 public function testGetContentsIsBasedOnSubset()
136 {
137 $body = new LimitStream(Psr7\stream_for('foobazbar'), 3, 3);
138 $this->assertEquals('baz', $body->getContents());
139 }
140
141 public function testReturnsNullIfSizeCannotBeDetermined()
142 {
143 $a = new FnStream([
144 'getSize' => function () { return null; },
145 'tell' => function () { return 0; },
146 ]);
147 $b = new LimitStream($a);
148 $this->assertNull($b->getSize());
149 }
150
151 public function testLengthLessOffsetWhenNoLimitSize()
152 {
153 $a = Psr7\stream_for('foo_bar');
154 $b = new LimitStream($a, -1, 4);
155 $this->assertEquals(3, $b->getSize());
156 }
157 }
158