~tcuthbert/wordpress/openstack-objectstorage

« back to all changes in this revision

Viewing changes to vendor/guzzlehttp/streams/tests/CachingStreamTest.php

  • Committer: Jacek Nykis
  • Date: 2015-02-11 15:35:31 UTC
  • Revision ID: jacek.nykis@canonical.com-20150211153531-hmy6zi0ov2qfkl0b
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
namespace GuzzleHttp\Tests\Stream;
 
3
 
 
4
use GuzzleHttp\Stream\Stream;
 
5
use GuzzleHttp\Stream\CachingStream;
 
6
use GuzzleHttp\Stream\Utils;
 
7
 
 
8
/**
 
9
 * @covers GuzzleHttp\Stream\CachingStream
 
10
 */
 
11
class 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
            true
 
94
        );
 
95
 
 
96
        $body = new CachingStream($decorated);
 
97
 
 
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));
 
108
 
 
109
        // Overwrite part of the cached body (so don't skip any bytes)
 
110
        $body->seek(5);
 
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'));
 
120
 
 
121
        // Seek to 0 and ensure the overwritten bit is replaced
 
122
        $body->seek(0);
 
123
        $this->assertEquals("0000\nABCD\nTEST\n0003\n0004\n0005\n0006\n1234\n0008\n0009\n", $body->read(50));
 
124
 
 
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);
 
127
    }
 
128
 
 
129
    public function testClosesBothStreams()
 
130
    {
 
131
        $s = fopen('php://temp', 'r');
 
132
        $a = Stream::factory($s);
 
133
        $d = new CachingStream($a);
 
134
        $d->close();
 
135
        $this->assertFalse(is_resource($s));
 
136
    }
 
137
}