source: externals/php/vendor/guzzlehttp/psr7/tests/CachingStreamTest.php@ 01a620f

Last change on this file since 01a620f was 01a620f, checked in by Joachim Langenbach <joachim.langenbach@…>, 11 years ago

Removed submodules for php externals and use composer to provide them and versioned them afterwards

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