| 1 |
<?php |
| 2 |
namespace GuzzleHttp\Tests\Psr7; |
| 3 |
|
| 4 |
use GuzzleHttp\Psr7\AppendStream; |
| 5 |
use GuzzleHttp\Psr7; |
| 6 |
|
| 7 |
class AppendStreamTest extends BaseTest |
| 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 testDetachWithoutStreams() |
| 76 |
{ |
| 77 |
$s = new AppendStream(); |
| 78 |
$s->detach(); |
| 79 |
|
| 80 |
$this->assertSame(0, $s->getSize()); |
| 81 |
$this->assertTrue($s->eof()); |
| 82 |
$this->assertTrue($s->isReadable()); |
| 83 |
$this->assertSame('', (string) $s); |
| 84 |
$this->assertTrue($s->isSeekable()); |
| 85 |
$this->assertFalse($s->isWritable()); |
| 86 |
} |
| 87 |
|
| 88 |
public function testDetachesEachStream() |
| 89 |
{ |
| 90 |
$handle = fopen('php://temp', 'r'); |
| 91 |
|
| 92 |
$s1 = Psr7\stream_for($handle); |
| 93 |
$s2 = Psr7\stream_for('bar'); |
| 94 |
$a = new AppendStream([$s1, $s2]); |
| 95 |
|
| 96 |
$a->detach(); |
| 97 |
|
| 98 |
$this->assertSame(0, $a->getSize()); |
| 99 |
$this->assertTrue($a->eof()); |
| 100 |
$this->assertTrue($a->isReadable()); |
| 101 |
$this->assertSame('', (string) $a); |
| 102 |
$this->assertTrue($a->isSeekable()); |
| 103 |
$this->assertFalse($a->isWritable()); |
| 104 |
|
| 105 |
$this->assertNull($s1->detach()); |
| 106 |
$this->assertInternalType('resource', $handle, 'resource is not closed when detaching'); |
| 107 |
fclose($handle); |
| 108 |
} |
| 109 |
|
| 110 |
public function testClosesEachStream() |
| 111 |
{ |
| 112 |
$handle = fopen('php://temp', 'r'); |
| 113 |
|
| 114 |
$s1 = Psr7\stream_for($handle); |
| 115 |
$s2 = Psr7\stream_for('bar'); |
| 116 |
$a = new AppendStream([$s1, $s2]); |
| 117 |
|
| 118 |
$a->close(); |
| 119 |
|
| 120 |
$this->assertSame(0, $a->getSize()); |
| 121 |
$this->assertTrue($a->eof()); |
| 122 |
$this->assertTrue($a->isReadable()); |
| 123 |
$this->assertSame('', (string) $a); |
| 124 |
$this->assertTrue($a->isSeekable()); |
| 125 |
$this->assertFalse($a->isWritable()); |
| 126 |
|
| 127 |
$this->assertFalse(is_resource($handle)); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* @expectedExceptionMessage Cannot write to an AppendStream |
| 132 |
* @expectedException \RuntimeException |
| 133 |
*/ |
| 134 |
public function testIsNotWritable() |
| 135 |
{ |
| 136 |
$a = new AppendStream([Psr7\stream_for('foo')]); |
| 137 |
$this->assertFalse($a->isWritable()); |
| 138 |
$this->assertTrue($a->isSeekable()); |
| 139 |
$this->assertTrue($a->isReadable()); |
| 140 |
$a->write('foo'); |
| 141 |
} |
| 142 |
|
| 143 |
public function testDoesNotNeedStreams() |
| 144 |
{ |
| 145 |
$a = new AppendStream(); |
| 146 |
$this->assertEquals('', (string) $a); |
| 147 |
} |
| 148 |
|
| 149 |
public function testCanReadFromMultipleStreams() |
| 150 |
{ |
| 151 |
$a = new AppendStream([ |
| 152 |
Psr7\stream_for('foo'), |
| 153 |
Psr7\stream_for('bar'), |
| 154 |
Psr7\stream_for('baz'), |
| 155 |
]); |
| 156 |
$this->assertFalse($a->eof()); |
| 157 |
$this->assertSame(0, $a->tell()); |
| 158 |
$this->assertEquals('foo', $a->read(3)); |
| 159 |
$this->assertEquals('bar', $a->read(3)); |
| 160 |
$this->assertEquals('baz', $a->read(3)); |
| 161 |
$this->assertSame('', $a->read(1)); |
| 162 |
$this->assertTrue($a->eof()); |
| 163 |
$this->assertSame(9, $a->tell()); |
| 164 |
$this->assertEquals('foobarbaz', (string) $a); |
| 165 |
} |
| 166 |
|
| 167 |
public function testCanDetermineSizeFromMultipleStreams() |
| 168 |
{ |
| 169 |
$a = new AppendStream([ |
| 170 |
Psr7\stream_for('foo'), |
| 171 |
Psr7\stream_for('bar') |
| 172 |
]); |
| 173 |
$this->assertEquals(6, $a->getSize()); |
| 174 |
|
| 175 |
$s = $this->getMockBuilder('Psr\Http\Message\StreamInterface') |
| 176 |
->setMethods(['isSeekable', 'isReadable']) |
| 177 |
->getMockForAbstractClass(); |
| 178 |
$s->expects($this->once()) |
| 179 |
->method('isSeekable') |
| 180 |
->will($this->returnValue(null)); |
| 181 |
$s->expects($this->once()) |
| 182 |
->method('isReadable') |
| 183 |
->will($this->returnValue(true)); |
| 184 |
$a->addStream($s); |
| 185 |
$this->assertNull($a->getSize()); |
| 186 |
} |
| 187 |
|
| 188 |
public function testCatchesExceptionsWhenCastingToString() |
| 189 |
{ |
| 190 |
$s = $this->getMockBuilder('Psr\Http\Message\StreamInterface') |
| 191 |
->setMethods(['isSeekable', 'read', 'isReadable', 'eof']) |
| 192 |
->getMockForAbstractClass(); |
| 193 |
$s->expects($this->once()) |
| 194 |
->method('isSeekable') |
| 195 |
->will($this->returnValue(true)); |
| 196 |
$s->expects($this->once()) |
| 197 |
->method('read') |
| 198 |
->will($this->throwException(new \RuntimeException('foo'))); |
| 199 |
$s->expects($this->once()) |
| 200 |
->method('isReadable') |
| 201 |
->will($this->returnValue(true)); |
| 202 |
$s->expects($this->any()) |
| 203 |
->method('eof') |
| 204 |
->will($this->returnValue(false)); |
| 205 |
$a = new AppendStream([$s]); |
| 206 |
$this->assertFalse($a->eof()); |
| 207 |
$this->assertSame('', (string) $a); |
| 208 |
} |
| 209 |
|
| 210 |
public function testReturnsEmptyMetadata() |
| 211 |
{ |
| 212 |
$s = new AppendStream(); |
| 213 |
$this->assertEquals([], $s->getMetadata()); |
| 214 |
$this->assertNull($s->getMetadata('foo')); |
| 215 |
} |
| 216 |
} |
| 217 |
|