source: externals/php/vendor/guzzlehttp/psr7/tests/AppendStreamTest.php@ 1ebaa4f

Last change on this file since 1ebaa4f was 01a620f, checked in by Joachim Langenbach <joachim.langenbach@…>, 11 years ago

Removed submodules for php externals and use composer to provide them and versioned them afterwards

  • Property mode set to 100644
File size: 5.6 KB
RevLine 
[01a620f]1<?php
2namespace GuzzleHttp\Tests\Psr7;
3
4use GuzzleHttp\Psr7\AppendStream;
5use GuzzleHttp\Psr7;
6
7class AppendStreamTest extends \PHPUnit_Framework_TestCase
8{
9 /**
10 * @expectedException \InvalidArgumentException
11 * @expectedExceptionMessage Each stream must be readable
12 */
13 public function testValidatesStreamsAreReadable()
14 {
15 $a = new AppendStream();
16 $s = $this->getMockBuilder('Psr\Http\Message\StreamInterface')
17 ->setMethods(['isReadable'])
18 ->getMockForAbstractClass();
19 $s->expects($this->once())
20 ->method('isReadable')
21 ->will($this->returnValue(false));
22 $a->addStream($s);
23 }
24
25 /**
26 * @expectedException \RuntimeException
27 * @expectedExceptionMessage The AppendStream can only seek with SEEK_SET
28 */
29 public function testValidatesSeekType()
30 {
31 $a = new AppendStream();
32 $a->seek(100, SEEK_CUR);
33 }
34
35 /**
36 * @expectedException \RuntimeException
37 * @expectedExceptionMessage Unable to seek stream 0 of the AppendStream
38 */
39 public function testTriesToRewindOnSeek()
40 {
41 $a = new AppendStream();
42 $s = $this->getMockBuilder('Psr\Http\Message\StreamInterface')
43 ->setMethods(['isReadable', 'rewind', 'isSeekable'])
44 ->getMockForAbstractClass();
45 $s->expects($this->once())
46 ->method('isReadable')
47 ->will($this->returnValue(true));
48 $s->expects($this->once())
49 ->method('isSeekable')
50 ->will($this->returnValue(true));
51 $s->expects($this->once())
52 ->method('rewind')
53 ->will($this->throwException(new \RuntimeException()));
54 $a->addStream($s);
55 $a->seek(10);
56 }
57
58 public function testSeeksToPositionByReading()
59 {
60 $a = new AppendStream([
61 Psr7\stream_for('foo'),
62 Psr7\stream_for('bar'),
63 Psr7\stream_for('baz'),
64 ]);
65
66 $a->seek(3);
67 $this->assertEquals(3, $a->tell());
68 $this->assertEquals('bar', $a->read(3));
69
70 $a->seek(6);
71 $this->assertEquals(6, $a->tell());
72 $this->assertEquals('baz', $a->read(3));
73 }
74
75 public function testDetachesEachStream()
76 {
77 $s1 = Psr7\stream_for('foo');
78 $s2 = Psr7\stream_for('bar');
79 $a = new AppendStream([$s1, $s2]);
80 $this->assertSame('foobar', (string) $a);
81 $a->detach();
82 $this->assertSame('', (string) $a);
83 $this->assertSame(0, $a->getSize());
84 }
85
86 public function testClosesEachStream()
87 {
88 $s1 = Psr7\stream_for('foo');
89 $a = new AppendStream([$s1]);
90 $a->close();
91 $this->assertSame('', (string) $a);
92 }
93
94 /**
95 * @expectedExceptionMessage Cannot write to an AppendStream
96 * @expectedException \RuntimeException
97 */
98 public function testIsNotWritable()
99 {
100 $a = new AppendStream([Psr7\stream_for('foo')]);
101 $this->assertFalse($a->isWritable());
102 $this->assertTrue($a->isSeekable());
103 $this->assertTrue($a->isReadable());
104 $a->write('foo');
105 }
106
107 public function testDoesNotNeedStreams()
108 {
109 $a = new AppendStream();
110 $this->assertEquals('', (string) $a);
111 }
112
113 public function testCanReadFromMultipleStreams()
114 {
115 $a = new AppendStream([
116 Psr7\stream_for('foo'),
117 Psr7\stream_for('bar'),
118 Psr7\stream_for('baz'),
119 ]);
120 $this->assertFalse($a->eof());
121 $this->assertSame(0, $a->tell());
122 $this->assertEquals('foo', $a->read(3));
123 $this->assertEquals('bar', $a->read(3));
124 $this->assertEquals('baz', $a->read(3));
125 $this->assertSame('', $a->read(1));
126 $this->assertTrue($a->eof());
127 $this->assertSame(9, $a->tell());
128 $this->assertEquals('foobarbaz', (string) $a);
129 }
130
131 public function testCanDetermineSizeFromMultipleStreams()
132 {
133 $a = new AppendStream([
134 Psr7\stream_for('foo'),
135 Psr7\stream_for('bar')
136 ]);
137 $this->assertEquals(6, $a->getSize());
138
139 $s = $this->getMockBuilder('Psr\Http\Message\StreamInterface')
140 ->setMethods(['isSeekable', 'isReadable'])
141 ->getMockForAbstractClass();
142 $s->expects($this->once())
143 ->method('isSeekable')
144 ->will($this->returnValue(null));
145 $s->expects($this->once())
146 ->method('isReadable')
147 ->will($this->returnValue(true));
148 $a->addStream($s);
149 $this->assertNull($a->getSize());
150 }
151
152 public function testCatchesExceptionsWhenCastingToString()
153 {
154 $s = $this->getMockBuilder('Psr\Http\Message\StreamInterface')
155 ->setMethods(['isSeekable', 'read', 'isReadable', 'eof'])
156 ->getMockForAbstractClass();
157 $s->expects($this->once())
158 ->method('isSeekable')
159 ->will($this->returnValue(true));
160 $s->expects($this->once())
161 ->method('read')
162 ->will($this->throwException(new \RuntimeException('foo')));
163 $s->expects($this->once())
164 ->method('isReadable')
165 ->will($this->returnValue(true));
166 $s->expects($this->any())
167 ->method('eof')
168 ->will($this->returnValue(false));
169 $a = new AppendStream([$s]);
170 $this->assertFalse($a->eof());
171 $this->assertSame('', (string) $a);
172 }
173
174 public function testCanDetach()
175 {
176 $s = new AppendStream();
177 $s->detach();
178 }
179
180 public function testReturnsEmptyMetadata()
181 {
182 $s = new AppendStream();
183 $this->assertEquals([], $s->getMetadata());
184 $this->assertNull($s->getMetadata('foo'));
185 }
186}
Note: See TracBrowser for help on using the repository browser.