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 / CachingStreamTest.php

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

196 lines 6.4 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\CachingStream;
6 use GuzzleHttp\Psr7\Stream;
7
8 /**
9 * @covers GuzzleHttp\Psr7\CachingStream
10 */
11 class CachingStreamTest extends BaseTest
12 {
13 /** @var CachingStream */
14 private $body;
15 /** @var Stream */
16 private $decorated;
17
18 protected function setUp()
19 {
20 $this->decorated = Psr7\stream_for('testing');
21 $this->body = new CachingStream($this->decorated);
22 }
23
24 protected function tearDown()
25 {
26 $this->decorated->close();
27 $this->body->close();
28 }
29
30 public function testUsesRemoteSizeIfPossible()
31 {
32 $body = Psr7\stream_for('test');
33 $caching = new CachingStream($body);
34 $this->assertEquals(4, $caching->getSize());
35 }
36
37 public function testReadsUntilCachedToByte()
38 {
39 $this->body->seek(5);
40 $this->assertEquals('n', $this->body->read(1));
41 $this->body->seek(0);
42 $this->assertEquals('t', $this->body->read(1));
43 }
44
45 public function testCanSeekNearEndWithSeekEnd()
46 {
47 $baseStream = Psr7\stream_for(implode('', range('a', 'z')));
48 $cached = new CachingStream($baseStream);
49 $cached->seek(-1, SEEK_END);
50 $this->assertEquals(25, $baseStream->tell());
51 $this->assertEquals('z', $cached->read(1));
52 $this->assertEquals(26, $cached->getSize());
53 }
54
55 public function testCanSeekToEndWithSeekEnd()
56 {
57 $baseStream = Psr7\stream_for(implode('', range('a', 'z')));
58 $cached = new CachingStream($baseStream);
59 $cached->seek(0, SEEK_END);
60 $this->assertEquals(26, $baseStream->tell());
61 $this->assertEquals('', $cached->read(1));
62 $this->assertEquals(26, $cached->getSize());
63 }
64
65 public function testCanUseSeekEndWithUnknownSize()
66 {
67 $baseStream = Psr7\stream_for('testing');
68 $decorated = Psr7\FnStream::decorate($baseStream, [
69 'getSize' => function () { return null; }
70 ]);
71 $cached = new CachingStream($decorated);
72 $cached->seek(-1, SEEK_END);
73 $this->assertEquals('g', $cached->read(1));
74 }
75
76 public function testRewindUsesSeek()
77 {
78 $a = Psr7\stream_for('foo');
79 $d = $this->getMockBuilder('GuzzleHttp\Psr7\CachingStream')
80 ->setMethods(array('seek'))
81 ->setConstructorArgs(array($a))
82 ->getMock();
83 $d->expects($this->once())
84 ->method('seek')
85 ->with(0)
86 ->will($this->returnValue(true));
87 $d->seek(0);
88 }
89
90 public function testCanSeekToReadBytes()
91 {
92 $this->assertEquals('te', $this->body->read(2));
93 $this->body->seek(0);
94 $this->assertEquals('test', $this->body->read(4));
95 $this->assertEquals(4, $this->body->tell());
96 $this->body->seek(2);
97 $this->assertEquals(2, $this->body->tell());
98 $this->body->seek(2, SEEK_CUR);
99 $this->assertEquals(4, $this->body->tell());
100 $this->assertEquals('ing', $this->body->read(3));
101 }
102
103 public function testCanSeekToReadBytesWithPartialBodyReturned()
104 {
105 $stream = fopen('php://temp', 'r+');
106 fwrite($stream, 'testing');
107 fseek($stream, 0);
108
109 $this->decorated = $this->getMockBuilder('\GuzzleHttp\Psr7\Stream')
110 ->setConstructorArgs([$stream])
111 ->setMethods(['read'])
112 ->getMock();
113
114 $this->decorated->expects($this->exactly(2))
115 ->method('read')
116 ->willReturnCallback(function($length) use ($stream){
117 return fread($stream, 2);
118 });
119
120 $this->body = new CachingStream($this->decorated);
121
122 $this->assertEquals(0, $this->body->tell());
123 $this->body->seek(4, SEEK_SET);
124 $this->assertEquals(4, $this->body->tell());
125
126 $this->body->seek(0);
127 $this->assertEquals('test', $this->body->read(4));
128 }
129
130 public function testWritesToBufferStream()
131 {
132 $this->body->read(2);
133 $this->body->write('hi');
134 $this->body->seek(0);
135 $this->assertEquals('tehiing', (string) $this->body);
136 }
137
138 public function testSkipsOverwrittenBytes()
139 {
140 $decorated = Psr7\stream_for(
141 implode("\n", array_map(function ($n) {
142 return str_pad($n, 4, '0', STR_PAD_LEFT);
143 }, range(0, 25)))
144 );
145
146 $body = new CachingStream($decorated);
147
148 $this->assertEquals("0000\n", Psr7\readline($body));
149 $this->assertEquals("0001\n", Psr7\readline($body));
150 // Write over part of the body yet to be read, so skip some bytes
151 $this->assertEquals(5, $body->write("TEST\n"));
152 $this->assertEquals(5, $this->readAttribute($body, 'skipReadBytes'));
153 // Read, which skips bytes, then reads
154 $this->assertEquals("0003\n", Psr7\readline($body));
155 $this->assertEquals(0, $this->readAttribute($body, 'skipReadBytes'));
156 $this->assertEquals("0004\n", Psr7\readline($body));
157 $this->assertEquals("0005\n", Psr7\readline($body));
158
159 // Overwrite part of the cached body (so don't skip any bytes)
160 $body->seek(5);
161 $this->assertEquals(5, $body->write("ABCD\n"));
162 $this->assertEquals(0, $this->readAttribute($body, 'skipReadBytes'));
163 $this->assertEquals("TEST\n", Psr7\readline($body));
164 $this->assertEquals("0003\n", Psr7\readline($body));
165 $this->assertEquals("0004\n", Psr7\readline($body));
166 $this->assertEquals("0005\n", Psr7\readline($body));
167 $this->assertEquals("0006\n", Psr7\readline($body));
168 $this->assertEquals(5, $body->write("1234\n"));
169 $this->assertEquals(5, $this->readAttribute($body, 'skipReadBytes'));
170
171 // Seek to 0 and ensure the overwritten bit is replaced
172 $body->seek(0);
173 $this->assertEquals("0000\nABCD\nTEST\n0003\n0004\n0005\n0006\n1234\n0008\n0009\n", $body->read(50));
174
175 // Ensure that casting it to a string does not include the bit that was overwritten
176 $this->assertContains("0000\nABCD\nTEST\n0003\n0004\n0005\n0006\n1234\n0008\n0009\n", (string) $body);
177 }
178
179 public function testClosesBothStreams()
180 {
181 $s = fopen('php://temp', 'r');
182 $a = Psr7\stream_for($s);
183 $d = new CachingStream($a);
184 $d->close();
185 $this->assertFalse(is_resource($s));
186 }
187
188 /**
189 * @expectedException \InvalidArgumentException
190 */
191 public function testEnsuresValidWhence()
192 {
193 $this->body->seek(10, -123456);
194 }
195 }
196