scratch – Blame information for rev 87

Subversion Repositories:
Rev:
Rev Author Line No. Line
87 office 1 <?php
2 namespace GuzzleHttp\Tests\Http;
3  
4 use GuzzleHttp\Stream\FnStream;
5 use GuzzleHttp\Stream\Stream;
6 use GuzzleHttp\Stream\LimitStream;
7 use GuzzleHttp\Stream\NoSeekStream;
8  
9 /**
10 * @covers GuzzleHttp\Stream\LimitStream
11 */
12 class LimitStreamTest extends \PHPUnit_Framework_TestCase
13 {
14 /** @var LimitStream */
15 protected $body;
16  
17 /** @var Stream */
18 protected $decorated;
19  
20 public function setUp()
21 {
22 $this->decorated = Stream::factory(fopen(__FILE__, 'r'));
23 $this->body = new LimitStream($this->decorated, 10, 3);
24 }
25  
26 public function testReturnsSubset()
27 {
28 $body = new LimitStream(Stream::factory('foo'), -1, 1);
29 $this->assertEquals('oo', (string) $body);
30 $this->assertTrue($body->eof());
31 $body->seek(0);
32 $this->assertFalse($body->eof());
33 $this->assertEquals('oo', $body->read(100));
34 $this->assertTrue($body->eof());
35 }
36  
37 public function testReturnsSubsetWhenCastToString()
38 {
39 $body = Stream::factory('foo_baz_bar');
40 $limited = new LimitStream($body, 3, 4);
41 $this->assertEquals('baz', (string) $limited);
42 }
43  
44 public function testReturnsSubsetOfEmptyBodyWhenCastToString()
45 {
46 $body = Stream::factory('');
47 $limited = new LimitStream($body, 0, 10);
48 $this->assertEquals('', (string) $limited);
49 }
50  
51 public function testSeeksWhenConstructed()
52 {
53 $this->assertEquals(0, $this->body->tell());
54 $this->assertEquals(3, $this->decorated->tell());
55 }
56  
57 public function testAllowsBoundedSeek()
58 {
59 $this->body->seek(100);
60 $this->assertEquals(13, $this->decorated->tell());
61 $this->body->seek(0);
62 $this->assertEquals(0, $this->body->tell());
63 $this->assertEquals(3, $this->decorated->tell());
64 $this->assertEquals(false, $this->body->seek(1000, SEEK_END));
65 }
66  
67 public function testReadsOnlySubsetOfData()
68 {
69 $data = $this->body->read(100);
70 $this->assertEquals(10, strlen($data));
71 $this->assertFalse($this->body->read(1000));
72  
73 $this->body->setOffset(10);
74 $newData = $this->body->read(100);
75 $this->assertEquals(10, strlen($newData));
76 $this->assertNotSame($data, $newData);
77 }
78  
79 /**
80 * @expectedException \GuzzleHttp\Stream\Exception\SeekException
81 * @expectedExceptionMessage Could not seek the stream to position 2
82 */
83 public function testThrowsWhenCurrentGreaterThanOffsetSeek()
84 {
85 $a = Stream::factory('foo_bar');
86 $b = new NoSeekStream($a);
87 $c = new LimitStream($b);
88 $a->getContents();
89 $c->setOffset(2);
90 }
91  
92 public function testClaimsConsumedWhenReadLimitIsReached()
93 {
94 $this->assertFalse($this->body->eof());
95 $this->body->read(1000);
96 $this->assertTrue($this->body->eof());
97 }
98  
99 public function testContentLengthIsBounded()
100 {
101 $this->assertEquals(10, $this->body->getSize());
102 }
103  
104 public function testGetContentsIsBasedOnSubset()
105 {
106 $body = new LimitStream(Stream::factory('foobazbar'), 3, 3);
107 $this->assertEquals('baz', $body->getContents());
108 }
109  
110 public function testReturnsNullIfSizeCannotBeDetermined()
111 {
112 $a = new FnStream([
113 'getSize' => function () { return null; },
114 'tell' => function () { return 0; },
115 ]);
116 $b = new LimitStream($a);
117 $this->assertNull($b->getSize());
118 }
119  
120 public function testLengthLessOffsetWhenNoLimitSize()
121 {
122 $a = Stream::factory('foo_bar');
123 $b = new LimitStream($a, -1, 4);
124 $this->assertEquals(3, $b->getSize());
125 }
126 }