2015-08-17 17:00:26 -07:00
< ? php
2015-08-27 12:03:05 -07:00
namespace GuzzleHttp\Tests\Psr7 ;
2015-08-17 17:00:26 -07:00
2015-08-27 12:03:05 -07:00
use GuzzleHttp\Psr7\StreamWrapper ;
use GuzzleHttp\Psr7 ;
2015-08-17 17:00:26 -07:00
/**
2015-08-27 12:03:05 -07:00
* @ covers GuzzleHttp\Psr7\StreamWrapper
2015-08-17 17:00:26 -07:00
*/
2015-08-27 12:03:05 -07:00
class StreamWrapperTest extends \PHPUnit_Framework_TestCase
2015-08-17 17:00:26 -07:00
{
public function testResource ()
{
2015-08-27 12:03:05 -07:00
$stream = Psr7\stream_for ( 'foo' );
$handle = StreamWrapper :: getResource ( $stream );
2015-08-17 17:00:26 -07:00
$this -> assertSame ( 'foo' , fread ( $handle , 3 ));
$this -> assertSame ( 3 , ftell ( $handle ));
$this -> assertSame ( 3 , fwrite ( $handle , 'bar' ));
$this -> assertSame ( 0 , fseek ( $handle , 0 ));
$this -> assertSame ( 'foobar' , fread ( $handle , 6 ));
2015-08-27 12:03:05 -07:00
$this -> assertSame ( '' , fread ( $handle , 1 ));
2015-08-17 17:00:26 -07:00
$this -> assertTrue ( feof ( $handle ));
// This fails on HHVM for some reason
if ( ! defined ( 'HHVM_VERSION' )) {
$this -> assertEquals ([
'dev' => 0 ,
'ino' => 0 ,
'mode' => 33206 ,
'nlink' => 0 ,
'uid' => 0 ,
'gid' => 0 ,
'rdev' => 0 ,
'size' => 6 ,
'atime' => 0 ,
'mtime' => 0 ,
'ctime' => 0 ,
'blksize' => 0 ,
'blocks' => 0 ,
0 => 0 ,
1 => 0 ,
2 => 33206 ,
3 => 0 ,
4 => 0 ,
5 => 0 ,
6 => 0 ,
7 => 6 ,
8 => 0 ,
9 => 0 ,
10 => 0 ,
11 => 0 ,
12 => 0 ,
], fstat ( $handle ));
}
$this -> assertTrue ( fclose ( $handle ));
$this -> assertSame ( 'foobar' , ( string ) $stream );
}
/**
* @ expectedException \InvalidArgumentException
*/
public function testValidatesStream ()
{
2015-08-27 12:03:05 -07:00
$stream = $this -> getMockBuilder ( 'Psr\Http\Message\StreamInterface' )
2015-08-17 17:00:26 -07:00
-> setMethods ([ 'isReadable' , 'isWritable' ])
-> getMockForAbstractClass ();
$stream -> expects ( $this -> once ())
-> method ( 'isReadable' )
-> will ( $this -> returnValue ( false ));
$stream -> expects ( $this -> once ())
-> method ( 'isWritable' )
-> will ( $this -> returnValue ( false ));
2015-08-27 12:03:05 -07:00
StreamWrapper :: getResource ( $stream );
2015-08-17 17:00:26 -07:00
}
/**
* @ expectedException \PHPUnit_Framework_Error_Warning
*/
public function testReturnsFalseWhenStreamDoesNotExist ()
{
fopen ( 'guzzle://foo' , 'r' );
}
public function testCanOpenReadonlyStream ()
{
2015-08-27 12:03:05 -07:00
$stream = $this -> getMockBuilder ( 'Psr\Http\Message\StreamInterface' )
2015-08-17 17:00:26 -07:00
-> setMethods ([ 'isReadable' , 'isWritable' ])
-> getMockForAbstractClass ();
$stream -> expects ( $this -> once ())
-> method ( 'isReadable' )
-> will ( $this -> returnValue ( false ));
$stream -> expects ( $this -> once ())
-> method ( 'isWritable' )
-> will ( $this -> returnValue ( true ));
2015-08-27 12:03:05 -07:00
$r = StreamWrapper :: getResource ( $stream );
2015-08-17 17:00:26 -07:00
$this -> assertInternalType ( 'resource' , $r );
fclose ( $r );
}
}