/var/www/hkosl.com/littleark/webadmin/libraies/guzzlehttp/psr7/tests/LimitStreamTest.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
namespace GuzzleHttp\Tests\Psr7;

use 
GuzzleHttp\Psr7;
use 
GuzzleHttp\Psr7\FnStream;
use 
GuzzleHttp\Psr7\Stream;
use 
GuzzleHttp\Psr7\LimitStream;
use 
GuzzleHttp\Psr7\NoSeekStream;

/**
 * @covers GuzzleHttp\Psr7\LimitStream
 */
class LimitStreamTest extends \PHPUnit_Framework_TestCase
{
    
/** @var LimitStream */
    
protected $body;

    
/** @var Stream */
    
protected $decorated;

    public function 
setUp()
    {
        
$this->decorated Psr7\stream_for(fopen(__FILE__'r'));
        
$this->body = new LimitStream($this->decorated103);
    }

    public function 
testReturnsSubset()
    {
        
$body = new LimitStream(Psr7\stream_for('foo'), -11);
        
$this->assertEquals('oo', (string) $body);
        
$this->assertTrue($body->eof());
        
$body->seek(0);
        
$this->assertFalse($body->eof());
        
$this->assertEquals('oo'$body->read(100));
        
$this->assertSame(''$body->read(1));
        
$this->assertTrue($body->eof());
    }

    public function 
testReturnsSubsetWhenCastToString()
    {
        
$body Psr7\stream_for('foo_baz_bar');
        
$limited = new LimitStream($body34);
        
$this->assertEquals('baz', (string) $limited);
    }

    
/**
     * @expectedException \RuntimeException
     * @expectedExceptionMessage Unable to seek to stream position 10 with whence 0
     */
    
public function testEnsuresPositionCanBeekSeekedTo()
    {
        new 
LimitStream(Psr7\stream_for(''), 010);
    }

    public function 
testReturnsSubsetOfEmptyBodyWhenCastToString()
    {
        
$body Psr7\stream_for('01234567891234');
        
$limited = new LimitStream($body010);
        
$this->assertEquals('', (string) $limited);
    }

    public function 
testReturnsSpecificSubsetOBodyWhenCastToString()
    {
        
$body Psr7\stream_for('0123456789abcdef');
        
$limited = new LimitStream($body310);
        
$this->assertEquals('abc', (string) $limited);
    }

    public function 
testSeeksWhenConstructed()
    {
        
$this->assertEquals(0$this->body->tell());
        
$this->assertEquals(3$this->decorated->tell());
    }

    public function 
testAllowsBoundedSeek()
    {
        
$this->body->seek(100);
        
$this->assertEquals(10$this->body->tell());
        
$this->assertEquals(13$this->decorated->tell());
        
$this->body->seek(0);
        
$this->assertEquals(0$this->body->tell());
        
$this->assertEquals(3$this->decorated->tell());
        try {
            
$this->body->seek(-10);
            
$this->fail();
        } catch (\
RuntimeException $e) {}
        
$this->assertEquals(0$this->body->tell());
        
$this->assertEquals(3$this->decorated->tell());
        
$this->body->seek(5);
        
$this->assertEquals(5$this->body->tell());
        
$this->assertEquals(8$this->decorated->tell());
        
// Fail
        
try {
            
$this->body->seek(1000SEEK_END);
            
$this->fail();
        } catch (\
RuntimeException $e) {}
    }

    public function 
testReadsOnlySubsetOfData()
    {
        
$data $this->body->read(100);
        
$this->assertEquals(10strlen($data));
        
$this->assertSame(''$this->body->read(1000));

        
$this->body->setOffset(10);
        
$newData $this->body->read(100);
        
$this->assertEquals(10strlen($newData));
        
$this->assertNotSame($data$newData);
    }

    
/**
     * @expectedException \RuntimeException
     * @expectedExceptionMessage Could not seek to stream offset 2
     */
    
public function testThrowsWhenCurrentGreaterThanOffsetSeek()
    {
        
$a Psr7\stream_for('foo_bar');
        
$b = new NoSeekStream($a);
        
$c = new LimitStream($b);
        
$a->getContents();
        
$c->setOffset(2);
    }

    public function 
testCanGetContentsWithoutSeeking()
    {
        
$a Psr7\stream_for('foo_bar');
        
$b = new NoSeekStream($a);
        
$c = new LimitStream($b);
        
$this->assertEquals('foo_bar'$c->getContents());
    }

    public function 
testClaimsConsumedWhenReadLimitIsReached()
    {
        
$this->assertFalse($this->body->eof());
        
$this->body->read(1000);
        
$this->assertTrue($this->body->eof());
    }

    public function 
testContentLengthIsBounded()
    {
        
$this->assertEquals(10$this->body->getSize());
    }

    public function 
testGetContentsIsBasedOnSubset()
    {
        
$body = new LimitStream(Psr7\stream_for('foobazbar'), 33);
        
$this->assertEquals('baz'$body->getContents());
    }

    public function 
testReturnsNullIfSizeCannotBeDetermined()
    {
        
$a = new FnStream([
            
'getSize' => function () { return null; },
            
'tell'    => function () { return 0; },
        ]);
        
$b = new LimitStream($a);
        
$this->assertNull($b->getSize());
    }

    public function 
testLengthLessOffsetWhenNoLimitSize()
    {
        
$a Psr7\stream_for('foo_bar');
        
$b = new LimitStream($a, -14);
        
$this->assertEquals(3$b->getSize());
    }
}