1<?php
2namespace GuzzleHttp\Tests\Stream;
3
4use GuzzleHttp\Stream\Stream;
5use GuzzleHttp\Stream\CachingStream;
6use GuzzleHttp\Stream\Utils;
7
8/**
9 * @covers GuzzleHttp\Stream\CachingStream
10 */
11class CachingStreamTest extends \PHPUnit_Framework_TestCase
12{
13    /** @var CachingStream */
14    protected $body;
15
16    /** @var Stream */
17    protected $decorated;
18
19    public function setUp()
20    {
21        $this->decorated = Stream::factory('testing');
22        $this->body = new CachingStream($this->decorated);
23    }
24
25    public function tearDown()
26    {
27        $this->decorated->close();
28        $this->body->close();
29    }
30
31    public function testUsesRemoteSizeIfPossible()
32    {
33        $body = Stream::factory('test');
34        $caching = new CachingStream($body);
35        $this->assertEquals(4, $caching->getSize());
36    }
37
38    /**
39     * @expectedException \RuntimeException
40     * @expectedExceptionMessage Cannot seek to byte 10
41     */
42    public function testCannotSeekPastWhatHasBeenRead()
43    {
44        $this->body->seek(10);
45    }
46
47    public function testCannotUseSeekEnd()
48    {
49        $this->assertFalse($this->body->seek(2, SEEK_END));
50    }
51
52    public function testRewindUsesSeek()
53    {
54        $a = Stream::factory('foo');
55        $d = $this->getMockBuilder('GuzzleHttp\Stream\CachingStream')
56            ->setMethods(array('seek'))
57            ->setConstructorArgs(array($a))
58            ->getMock();
59        $d->expects($this->once())
60            ->method('seek')
61            ->with(0)
62            ->will($this->returnValue(true));
63        $d->seek(0);
64    }
65
66    public function testCanSeekToReadBytes()
67    {
68        $this->assertEquals('te', $this->body->read(2));
69        $this->body->seek(0);
70        $this->assertEquals('test', $this->body->read(4));
71        $this->assertEquals(4, $this->body->tell());
72        $this->body->seek(2);
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));
77    }
78
79    public function testWritesToBufferStream()
80    {
81        $this->body->read(2);
82        $this->body->write('hi');
83        $this->body->seek(0);
84        $this->assertEquals('tehiing', (string) $this->body);
85    }
86
87    public function testSkipsOverwrittenBytes()
88    {
89        $decorated = Stream::factory(
90            implode("\n", array_map(function ($n) {
91                return str_pad($n, 4, '0', STR_PAD_LEFT);
92            }, range(0, 25)))
93        );
94
95        $body = new CachingStream($decorated);
96
97        $this->assertEquals("0000\n", Utils::readline($body));
98        $this->assertEquals("0001\n", Utils::readline($body));
99        // Write over part of the body yet to be read, so skip some bytes
100        $this->assertEquals(5, $body->write("TEST\n"));
101        $this->assertEquals(5, $this->readAttribute($body, 'skipReadBytes'));
102        // Read, which skips bytes, then reads
103        $this->assertEquals("0003\n", Utils::readline($body));
104        $this->assertEquals(0, $this->readAttribute($body, 'skipReadBytes'));
105        $this->assertEquals("0004\n", Utils::readline($body));
106        $this->assertEquals("0005\n", Utils::readline($body));
107
108        // Overwrite part of the cached body (so don't skip any bytes)
109        $body->seek(5);
110        $this->assertEquals(5, $body->write("ABCD\n"));
111        $this->assertEquals(0, $this->readAttribute($body, 'skipReadBytes'));
112        $this->assertEquals("TEST\n", Utils::readline($body));
113        $this->assertEquals("0003\n", Utils::readline($body));
114        $this->assertEquals("0004\n", Utils::readline($body));
115        $this->assertEquals("0005\n", Utils::readline($body));
116        $this->assertEquals("0006\n", Utils::readline($body));
117        $this->assertEquals(5, $body->write("1234\n"));
118        $this->assertEquals(5, $this->readAttribute($body, 'skipReadBytes'));
119
120        // Seek to 0 and ensure the overwritten bit is replaced
121        $body->seek(0);
122        $this->assertEquals("0000\nABCD\nTEST\n0003\n0004\n0005\n0006\n1234\n0008\n0009\n", $body->read(50));
123
124        // Ensure that casting it to a string does not include the bit that was overwritten
125        $this->assertContains("0000\nABCD\nTEST\n0003\n0004\n0005\n0006\n1234\n0008\n0009\n", (string) $body);
126    }
127
128    public function testClosesBothStreams()
129    {
130        $s = fopen('php://temp', 'r');
131        $a = Stream::factory($s);
132        $d = new CachingStream($a);
133        $d->close();
134        $this->assertFalse(is_resource($s));
135    }
136}
137