| 1 |
<?php |
| 2 |
namespace GuzzleHttp\Tests\Psr7; |
| 3 |
|
| 4 |
use GuzzleHttp\Psr7\StreamWrapper; |
| 5 |
use GuzzleHttp\Psr7; |
| 6 |
|
| 7 |
/** |
| 8 |
* @covers GuzzleHttp\Psr7\StreamWrapper |
| 9 |
*/ |
| 10 |
class StreamWrapperTest extends BaseTest |
| 11 |
{ |
| 12 |
public function testResource() |
| 13 |
{ |
| 14 |
$stream = Psr7\stream_for('foo'); |
| 15 |
$handle = StreamWrapper::getResource($stream); |
| 16 |
$this->assertSame('foo', fread($handle, 3)); |
| 17 |
$this->assertSame(3, ftell($handle)); |
| 18 |
$this->assertSame(3, fwrite($handle, 'bar')); |
| 19 |
$this->assertSame(0, fseek($handle, 0)); |
| 20 |
$this->assertSame('foobar', fread($handle, 6)); |
| 21 |
$this->assertSame('', fread($handle, 1)); |
| 22 |
$this->assertTrue(feof($handle)); |
| 23 |
|
| 24 |
$stBlksize = defined('PHP_WINDOWS_VERSION_BUILD') ? -1 : 0; |
| 25 |
|
| 26 |
// This fails on HHVM for some reason |
| 27 |
if (!defined('HHVM_VERSION')) { |
| 28 |
$this->assertEquals([ |
| 29 |
'dev' => 0, |
| 30 |
'ino' => 0, |
| 31 |
'mode' => 33206, |
| 32 |
'nlink' => 0, |
| 33 |
'uid' => 0, |
| 34 |
'gid' => 0, |
| 35 |
'rdev' => 0, |
| 36 |
'size' => 6, |
| 37 |
'atime' => 0, |
| 38 |
'mtime' => 0, |
| 39 |
'ctime' => 0, |
| 40 |
'blksize' => $stBlksize, |
| 41 |
'blocks' => $stBlksize, |
| 42 |
0 => 0, |
| 43 |
1 => 0, |
| 44 |
2 => 33206, |
| 45 |
3 => 0, |
| 46 |
4 => 0, |
| 47 |
5 => 0, |
| 48 |
6 => 0, |
| 49 |
7 => 6, |
| 50 |
8 => 0, |
| 51 |
9 => 0, |
| 52 |
10 => 0, |
| 53 |
11 => $stBlksize, |
| 54 |
12 => $stBlksize, |
| 55 |
], fstat($handle)); |
| 56 |
} |
| 57 |
|
| 58 |
$this->assertTrue(fclose($handle)); |
| 59 |
$this->assertSame('foobar', (string) $stream); |
| 60 |
} |
| 61 |
|
| 62 |
public function testStreamContext() |
| 63 |
{ |
| 64 |
$stream = Psr7\stream_for('foo'); |
| 65 |
|
| 66 |
$this->assertEquals('foo', file_get_contents('guzzle://stream', false, StreamWrapper::createStreamContext($stream))); |
| 67 |
} |
| 68 |
|
| 69 |
public function testStreamCast() |
| 70 |
{ |
| 71 |
$streams = [ |
| 72 |
StreamWrapper::getResource(Psr7\stream_for('foo')), |
| 73 |
StreamWrapper::getResource(Psr7\stream_for('bar')) |
| 74 |
]; |
| 75 |
$write = null; |
| 76 |
$except = null; |
| 77 |
$this->assertInternalType('integer', stream_select($streams, $write, $except, 0)); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* @expectedException \InvalidArgumentException |
| 82 |
*/ |
| 83 |
public function testValidatesStream() |
| 84 |
{ |
| 85 |
$stream = $this->getMockBuilder('Psr\Http\Message\StreamInterface') |
| 86 |
->setMethods(['isReadable', 'isWritable']) |
| 87 |
->getMockForAbstractClass(); |
| 88 |
$stream->expects($this->once()) |
| 89 |
->method('isReadable') |
| 90 |
->will($this->returnValue(false)); |
| 91 |
$stream->expects($this->once()) |
| 92 |
->method('isWritable') |
| 93 |
->will($this->returnValue(false)); |
| 94 |
StreamWrapper::getResource($stream); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* @expectedException \PHPUnit\Framework\Error\Warning |
| 99 |
*/ |
| 100 |
public function testReturnsFalseWhenStreamDoesNotExist() |
| 101 |
{ |
| 102 |
fopen('guzzle://foo', 'r'); |
| 103 |
} |
| 104 |
|
| 105 |
public function testCanOpenReadonlyStream() |
| 106 |
{ |
| 107 |
$stream = $this->getMockBuilder('Psr\Http\Message\StreamInterface') |
| 108 |
->setMethods(['isReadable', 'isWritable']) |
| 109 |
->getMockForAbstractClass(); |
| 110 |
$stream->expects($this->once()) |
| 111 |
->method('isReadable') |
| 112 |
->will($this->returnValue(false)); |
| 113 |
$stream->expects($this->once()) |
| 114 |
->method('isWritable') |
| 115 |
->will($this->returnValue(true)); |
| 116 |
$r = StreamWrapper::getResource($stream); |
| 117 |
$this->assertInternalType('resource', $r); |
| 118 |
fclose($r); |
| 119 |
} |
| 120 |
|
| 121 |
public function testUrlStat() |
| 122 |
{ |
| 123 |
StreamWrapper::register(); |
| 124 |
|
| 125 |
$this->assertEquals( |
| 126 |
[ |
| 127 |
'dev' => 0, |
| 128 |
'ino' => 0, |
| 129 |
'mode' => 0, |
| 130 |
'nlink' => 0, |
| 131 |
'uid' => 0, |
| 132 |
'gid' => 0, |
| 133 |
'rdev' => 0, |
| 134 |
'size' => 0, |
| 135 |
'atime' => 0, |
| 136 |
'mtime' => 0, |
| 137 |
'ctime' => 0, |
| 138 |
'blksize' => 0, |
| 139 |
'blocks' => 0, |
| 140 |
0 => 0, |
| 141 |
1 => 0, |
| 142 |
2 => 0, |
| 143 |
3 => 0, |
| 144 |
4 => 0, |
| 145 |
5 => 0, |
| 146 |
6 => 0, |
| 147 |
7 => 0, |
| 148 |
8 => 0, |
| 149 |
9 => 0, |
| 150 |
10 => 0, |
| 151 |
11 => 0, |
| 152 |
12 => 0, |
| 153 |
], |
| 154 |
stat('guzzle://stream') |
| 155 |
); |
| 156 |
} |
| 157 |
|
| 158 |
public function testXmlReaderWithStream() |
| 159 |
{ |
| 160 |
if (!class_exists('XMLReader')) { |
| 161 |
$this->markTestSkipped('XML Reader is not available.'); |
| 162 |
} |
| 163 |
if (defined('HHVM_VERSION')) { |
| 164 |
$this->markTestSkipped('This does not work on HHVM.'); |
| 165 |
} |
| 166 |
|
| 167 |
$stream = Psr7\stream_for('<?xml version="1.0" encoding="utf-8"?><foo />'); |
| 168 |
|
| 169 |
StreamWrapper::register(); |
| 170 |
libxml_set_streams_context(StreamWrapper::createStreamContext($stream)); |
| 171 |
$reader = new \XMLReader(); |
| 172 |
|
| 173 |
$this->assertTrue($reader->open('guzzle://stream')); |
| 174 |
$this->assertTrue($reader->read()); |
| 175 |
$this->assertEquals('foo', $reader->name); |
| 176 |
} |
| 177 |
|
| 178 |
public function testXmlWriterWithStream() |
| 179 |
{ |
| 180 |
if (!class_exists('XMLWriter')) { |
| 181 |
$this->markTestSkipped('XML Writer is not available.'); |
| 182 |
} |
| 183 |
if (defined('HHVM_VERSION')) { |
| 184 |
$this->markTestSkipped('This does not work on HHVM.'); |
| 185 |
} |
| 186 |
|
| 187 |
$stream = Psr7\stream_for(fopen('php://memory', 'wb')); |
| 188 |
|
| 189 |
StreamWrapper::register(); |
| 190 |
libxml_set_streams_context(StreamWrapper::createStreamContext($stream)); |
| 191 |
$writer = new \XMLWriter(); |
| 192 |
|
| 193 |
$this->assertTrue($writer->openURI('guzzle://stream')); |
| 194 |
$this->assertTrue($writer->startDocument()); |
| 195 |
$this->assertTrue($writer->writeElement('foo')); |
| 196 |
$this->assertTrue($writer->endDocument()); |
| 197 |
|
| 198 |
$stream->rewind(); |
| 199 |
$this->assertXmlStringEqualsXmlString('<?xml version="1.0"?><foo />', (string) $stream); |
| 200 |
} |
| 201 |
} |
| 202 |
|