1<?php
2namespace GuzzleHttp\Tests\Stream;
3
4use GuzzleHttp\Stream\GuzzleStreamWrapper;
5use GuzzleHttp\Stream\Stream;
6use InvalidArgumentException;
7use PHPUnit\Framework\TestCase;
8
9/**
10 * @covers GuzzleHttp\Stream\GuzzleStreamWrapper
11 */
12class GuzzleStreamWrapperTest extends TestCase
13{
14    public function testResource()
15    {
16        $stream = Stream::factory('foo');
17        $handle = GuzzleStreamWrapper::getResource($stream);
18        $this->assertSame('foo', fread($handle, 3));
19        $this->assertSame(3, ftell($handle));
20        $this->assertSame(3, fwrite($handle, 'bar'));
21        $this->assertSame(0, fseek($handle, 0));
22        $this->assertSame('foobar', fread($handle, 6));
23        $this->assertEmpty(fread($handle, 1));
24        $this->assertTrue(feof($handle));
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' => 0,
41                'blocks'  => 0,
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        => 0,
54                12        => 0,
55            ], fstat($handle));
56        }
57
58        $this->assertTrue(fclose($handle));
59        $this->assertSame('foobar', (string) $stream);
60    }
61
62    public function testValidatesStream()
63    {
64        $this->expectException(InvalidArgumentException::class);
65        $stream = $this->getMockBuilder('GuzzleHttp\Stream\StreamInterface')
66            ->getMockForAbstractClass();
67        $stream->expects($this->once())
68            ->method('isReadable')
69            ->will($this->returnValue(false));
70        $stream->expects($this->once())
71            ->method('isWritable')
72            ->will($this->returnValue(false));
73        GuzzleStreamWrapper::getResource($stream);
74    }
75
76    public function testReturnsFalseWhenStreamDoesNotExist()
77    {
78
79        $this->expectWarning();
80        fopen('guzzle://foo', 'r');
81    }
82
83    public function testCanOpenReadonlyStream()
84    {
85        $stream = $this->getMockBuilder('GuzzleHttp\Stream\StreamInterface')
86            ->getMockForAbstractClass();
87        $stream->expects($this->once())
88            ->method('isReadable')
89            ->will($this->returnValue(false));
90        $stream->expects($this->once())
91            ->method('isWritable')
92            ->will($this->returnValue(true));
93        $r = GuzzleStreamWrapper::getResource($stream);
94        $this->assertIsResource($r);
95        fclose($r);
96    }
97}
98