2
namespace GuzzleHttp\Tests\Stream;
4
use GuzzleHttp\Stream\Stream;
5
use GuzzleHttp\Stream\CachingStream;
6
use GuzzleHttp\Stream\Utils;
9
* @covers GuzzleHttp\Stream\CachingStream
11
class CachingStreamTest extends \PHPUnit_Framework_TestCase
13
/** @var CachingStream */
19
public function setUp()
21
$this->decorated = Stream::factory('testing');
22
$this->body = new CachingStream($this->decorated);
25
public function tearDown()
27
$this->decorated->close();
31
public function testUsesRemoteSizeIfPossible()
33
$body = Stream::factory('test');
34
$caching = new CachingStream($body);
35
$this->assertEquals(4, $caching->getSize());
39
* @expectedException \RuntimeException
40
* @expectedExceptionMessage Cannot seek to byte 10
42
public function testCannotSeekPastWhatHasBeenRead()
44
$this->body->seek(10);
47
public function testCannotUseSeekEnd()
49
$this->assertFalse($this->body->seek(2, SEEK_END));
52
public function testRewindUsesSeek()
54
$a = Stream::factory('foo');
55
$d = $this->getMockBuilder('GuzzleHttp\Stream\CachingStream')
56
->setMethods(array('seek'))
57
->setConstructorArgs(array($a))
59
$d->expects($this->once())
62
->will($this->returnValue(true));
66
public function testCanSeekToReadBytes()
68
$this->assertEquals('te', $this->body->read(2));
70
$this->assertEquals('test', $this->body->read(4));
71
$this->assertEquals(4, $this->body->tell());
73
$this->assertEquals(2, $this->body->tell());
74
$this->body->seek(2, SEEK_CUR);
75
$this->assertEquals(4, $this->body->tell());
76
$this->assertEquals('ing', $this->body->read(3));
79
public function testWritesToBufferStream()
82
$this->body->write('hi');
84
$this->assertEquals('tehiing', (string) $this->body);
87
public function testSkipsOverwrittenBytes()
89
$decorated = Stream::factory(
90
implode("\n", array_map(function ($n) {
91
return str_pad($n, 4, '0', STR_PAD_LEFT);
96
$body = new CachingStream($decorated);
98
$this->assertEquals("0000\n", Utils::readline($body));
99
$this->assertEquals("0001\n", Utils::readline($body));
100
// Write over part of the body yet to be read, so skip some bytes
101
$this->assertEquals(5, $body->write("TEST\n"));
102
$this->assertEquals(5, $this->readAttribute($body, 'skipReadBytes'));
103
// Read, which skips bytes, then reads
104
$this->assertEquals("0003\n", Utils::readline($body));
105
$this->assertEquals(0, $this->readAttribute($body, 'skipReadBytes'));
106
$this->assertEquals("0004\n", Utils::readline($body));
107
$this->assertEquals("0005\n", Utils::readline($body));
109
// Overwrite part of the cached body (so don't skip any bytes)
111
$this->assertEquals(5, $body->write("ABCD\n"));
112
$this->assertEquals(0, $this->readAttribute($body, 'skipReadBytes'));
113
$this->assertEquals("TEST\n", Utils::readline($body));
114
$this->assertEquals("0003\n", Utils::readline($body));
115
$this->assertEquals("0004\n", Utils::readline($body));
116
$this->assertEquals("0005\n", Utils::readline($body));
117
$this->assertEquals("0006\n", Utils::readline($body));
118
$this->assertEquals(5, $body->write("1234\n"));
119
$this->assertEquals(5, $this->readAttribute($body, 'skipReadBytes'));
121
// Seek to 0 and ensure the overwritten bit is replaced
123
$this->assertEquals("0000\nABCD\nTEST\n0003\n0004\n0005\n0006\n1234\n0008\n0009\n", $body->read(50));
125
// Ensure that casting it to a string does not include the bit that was overwritten
126
$this->assertContains("0000\nABCD\nTEST\n0003\n0004\n0005\n0006\n1234\n0008\n0009\n", (string) $body);
129
public function testClosesBothStreams()
131
$s = fopen('php://temp', 'r');
132
$a = Stream::factory($s);
133
$d = new CachingStream($a);
135
$this->assertFalse(is_resource($s));