| 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 \PHPUnit_Framework_TestCase
|
|---|
| 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 | // This fails on HHVM for some reason
|
|---|
| 25 | if (!defined('HHVM_VERSION')) {
|
|---|
| 26 | $this->assertEquals([
|
|---|
| 27 | 'dev' => 0,
|
|---|
| 28 | 'ino' => 0,
|
|---|
| 29 | 'mode' => 33206,
|
|---|
| 30 | 'nlink' => 0,
|
|---|
| 31 | 'uid' => 0,
|
|---|
| 32 | 'gid' => 0,
|
|---|
| 33 | 'rdev' => 0,
|
|---|
| 34 | 'size' => 6,
|
|---|
| 35 | 'atime' => 0,
|
|---|
| 36 | 'mtime' => 0,
|
|---|
| 37 | 'ctime' => 0,
|
|---|
| 38 | 'blksize' => 0,
|
|---|
| 39 | 'blocks' => 0,
|
|---|
| 40 | 0 => 0,
|
|---|
| 41 | 1 => 0,
|
|---|
| 42 | 2 => 33206,
|
|---|
| 43 | 3 => 0,
|
|---|
| 44 | 4 => 0,
|
|---|
| 45 | 5 => 0,
|
|---|
| 46 | 6 => 0,
|
|---|
| 47 | 7 => 6,
|
|---|
| 48 | 8 => 0,
|
|---|
| 49 | 9 => 0,
|
|---|
| 50 | 10 => 0,
|
|---|
| 51 | 11 => 0,
|
|---|
| 52 | 12 => 0,
|
|---|
| 53 | ], fstat($handle));
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | $this->assertTrue(fclose($handle));
|
|---|
| 57 | $this->assertSame('foobar', (string) $stream);
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | /**
|
|---|
| 61 | * @expectedException \InvalidArgumentException
|
|---|
| 62 | */
|
|---|
| 63 | public function testValidatesStream()
|
|---|
| 64 | {
|
|---|
| 65 | $stream = $this->getMockBuilder('Psr\Http\Message\StreamInterface')
|
|---|
| 66 | ->setMethods(['isReadable', 'isWritable'])
|
|---|
| 67 | ->getMockForAbstractClass();
|
|---|
| 68 | $stream->expects($this->once())
|
|---|
| 69 | ->method('isReadable')
|
|---|
| 70 | ->will($this->returnValue(false));
|
|---|
| 71 | $stream->expects($this->once())
|
|---|
| 72 | ->method('isWritable')
|
|---|
| 73 | ->will($this->returnValue(false));
|
|---|
| 74 | StreamWrapper::getResource($stream);
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | /**
|
|---|
| 78 | * @expectedException \PHPUnit_Framework_Error_Warning
|
|---|
| 79 | */
|
|---|
| 80 | public function testReturnsFalseWhenStreamDoesNotExist()
|
|---|
| 81 | {
|
|---|
| 82 | fopen('guzzle://foo', 'r');
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | public function testCanOpenReadonlyStream()
|
|---|
| 86 | {
|
|---|
| 87 | $stream = $this->getMockBuilder('Psr\Http\Message\StreamInterface')
|
|---|
| 88 | ->setMethods(['isReadable', 'isWritable'])
|
|---|
| 89 | ->getMockForAbstractClass();
|
|---|
| 90 | $stream->expects($this->once())
|
|---|
| 91 | ->method('isReadable')
|
|---|
| 92 | ->will($this->returnValue(false));
|
|---|
| 93 | $stream->expects($this->once())
|
|---|
| 94 | ->method('isWritable')
|
|---|
| 95 | ->will($this->returnValue(true));
|
|---|
| 96 | $r = StreamWrapper::getResource($stream);
|
|---|
| 97 | $this->assertInternalType('resource', $r);
|
|---|
| 98 | fclose($r);
|
|---|
| 99 | }
|
|---|
| 100 | }
|
|---|