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\AppendStream ;
use GuzzleHttp\Psr7 ;
2015-08-17 17:00:26 -07:00
class AppendStreamTest extends \PHPUnit_Framework_TestCase
{
/**
* @ expectedException \InvalidArgumentException
* @ expectedExceptionMessage Each stream must be readable
*/
public function testValidatesStreamsAreReadable ()
{
$a = new AppendStream ();
2015-08-27 12:03:05 -07:00
$s = $this -> getMockBuilder ( 'Psr\Http\Message\StreamInterface' )
2015-08-17 17:00:26 -07:00
-> setMethods ([ 'isReadable' ])
-> getMockForAbstractClass ();
$s -> expects ( $this -> once ())
-> method ( 'isReadable' )
-> will ( $this -> returnValue ( false ));
$a -> addStream ( $s );
}
2015-08-27 12:03:05 -07:00
/**
* @ expectedException \RuntimeException
* @ expectedExceptionMessage The AppendStream can only seek with SEEK_SET
*/
2015-08-17 17:00:26 -07:00
public function testValidatesSeekType ()
{
$a = new AppendStream ();
2015-08-27 12:03:05 -07:00
$a -> seek ( 100 , SEEK_CUR );
2015-08-17 17:00:26 -07:00
}
2015-08-27 12:03:05 -07:00
/**
* @ expectedException \RuntimeException
* @ expectedExceptionMessage Unable to seek stream 0 of the AppendStream
*/
2015-08-17 17:00:26 -07:00
public function testTriesToRewindOnSeek ()
{
$a = new AppendStream ();
2015-08-27 12:03:05 -07:00
$s = $this -> getMockBuilder ( 'Psr\Http\Message\StreamInterface' )
-> setMethods ([ 'isReadable' , 'rewind' , 'isSeekable' ])
2015-08-17 17:00:26 -07:00
-> getMockForAbstractClass ();
$s -> expects ( $this -> once ())
-> method ( 'isReadable' )
-> will ( $this -> returnValue ( true ));
$s -> expects ( $this -> once ())
-> method ( 'isSeekable' )
-> will ( $this -> returnValue ( true ));
$s -> expects ( $this -> once ())
2015-08-27 12:03:05 -07:00
-> method ( 'rewind' )
-> will ( $this -> throwException ( new \RuntimeException ()));
2015-08-17 17:00:26 -07:00
$a -> addStream ( $s );
2015-08-27 12:03:05 -07:00
$a -> seek ( 10 );
2015-08-17 17:00:26 -07:00
}
public function testSeeksToPositionByReading ()
{
$a = new AppendStream ([
2015-08-27 12:03:05 -07:00
Psr7\stream_for ( 'foo' ),
Psr7\stream_for ( 'bar' ),
Psr7\stream_for ( 'baz' ),
2015-08-17 17:00:26 -07:00
]);
2015-08-27 12:03:05 -07:00
$a -> seek ( 3 );
2015-08-17 17:00:26 -07:00
$this -> assertEquals ( 3 , $a -> tell ());
$this -> assertEquals ( 'bar' , $a -> read ( 3 ));
2015-08-27 12:03:05 -07:00
2015-08-17 17:00:26 -07:00
$a -> seek ( 6 );
$this -> assertEquals ( 6 , $a -> tell ());
$this -> assertEquals ( 'baz' , $a -> read ( 3 ));
}
public function testDetachesEachStream ()
{
2015-08-27 12:03:05 -07:00
$s1 = Psr7\stream_for ( 'foo' );
$s2 = Psr7\stream_for ( 'bar' );
2015-08-17 17:00:26 -07:00
$a = new AppendStream ([ $s1 , $s2 ]);
2015-08-27 12:03:05 -07:00
$this -> assertSame ( 'foobar' , ( string ) $a );
2015-08-17 17:00:26 -07:00
$a -> detach ();
$this -> assertSame ( '' , ( string ) $a );
$this -> assertSame ( 0 , $a -> getSize ());
}
public function testClosesEachStream ()
{
2015-08-27 12:03:05 -07:00
$s1 = Psr7\stream_for ( 'foo' );
2015-08-17 17:00:26 -07:00
$a = new AppendStream ([ $s1 ]);
$a -> close ();
$this -> assertSame ( '' , ( string ) $a );
}
2015-08-27 12:03:05 -07:00
/**
* @ expectedExceptionMessage Cannot write to an AppendStream
* @ expectedException \RuntimeException
*/
2015-08-17 17:00:26 -07:00
public function testIsNotWritable ()
{
2015-08-27 12:03:05 -07:00
$a = new AppendStream ([ Psr7\stream_for ( 'foo' )]);
2015-08-17 17:00:26 -07:00
$this -> assertFalse ( $a -> isWritable ());
$this -> assertTrue ( $a -> isSeekable ());
$this -> assertTrue ( $a -> isReadable ());
2015-08-27 12:03:05 -07:00
$a -> write ( 'foo' );
2015-08-17 17:00:26 -07:00
}
public function testDoesNotNeedStreams ()
{
$a = new AppendStream ();
$this -> assertEquals ( '' , ( string ) $a );
}
public function testCanReadFromMultipleStreams ()
{
$a = new AppendStream ([
2015-08-27 12:03:05 -07:00
Psr7\stream_for ( 'foo' ),
Psr7\stream_for ( 'bar' ),
Psr7\stream_for ( 'baz' ),
2015-08-17 17:00:26 -07:00
]);
$this -> assertFalse ( $a -> eof ());
$this -> assertSame ( 0 , $a -> tell ());
$this -> assertEquals ( 'foo' , $a -> read ( 3 ));
$this -> assertEquals ( 'bar' , $a -> read ( 3 ));
$this -> assertEquals ( 'baz' , $a -> read ( 3 ));
2015-08-27 12:03:05 -07:00
$this -> assertSame ( '' , $a -> read ( 1 ));
2015-08-17 17:00:26 -07:00
$this -> assertTrue ( $a -> eof ());
$this -> assertSame ( 9 , $a -> tell ());
$this -> assertEquals ( 'foobarbaz' , ( string ) $a );
}
public function testCanDetermineSizeFromMultipleStreams ()
{
$a = new AppendStream ([
2015-08-27 12:03:05 -07:00
Psr7\stream_for ( 'foo' ),
Psr7\stream_for ( 'bar' )
2015-08-17 17:00:26 -07:00
]);
$this -> assertEquals ( 6 , $a -> getSize ());
2015-08-27 12:03:05 -07:00
$s = $this -> getMockBuilder ( 'Psr\Http\Message\StreamInterface' )
2015-08-17 17:00:26 -07:00
-> setMethods ([ 'isSeekable' , 'isReadable' ])
-> getMockForAbstractClass ();
$s -> expects ( $this -> once ())
-> method ( 'isSeekable' )
-> will ( $this -> returnValue ( null ));
$s -> expects ( $this -> once ())
-> method ( 'isReadable' )
-> will ( $this -> returnValue ( true ));
$a -> addStream ( $s );
$this -> assertNull ( $a -> getSize ());
}
public function testCatchesExceptionsWhenCastingToString ()
{
2015-08-27 12:03:05 -07:00
$s = $this -> getMockBuilder ( 'Psr\Http\Message\StreamInterface' )
-> setMethods ([ 'isSeekable' , 'read' , 'isReadable' , 'eof' ])
2015-08-17 17:00:26 -07:00
-> getMockForAbstractClass ();
2015-08-27 12:03:05 -07:00
$s -> expects ( $this -> once ())
-> method ( 'isSeekable' )
-> will ( $this -> returnValue ( true ));
2015-08-17 17:00:26 -07:00
$s -> expects ( $this -> once ())
-> method ( 'read' )
-> will ( $this -> throwException ( new \RuntimeException ( 'foo' )));
$s -> expects ( $this -> once ())
-> method ( 'isReadable' )
-> will ( $this -> returnValue ( true ));
$s -> expects ( $this -> any ())
-> method ( 'eof' )
-> will ( $this -> returnValue ( false ));
$a = new AppendStream ([ $s ]);
$this -> assertFalse ( $a -> eof ());
$this -> assertSame ( '' , ( string ) $a );
}
public function testCanDetach ()
{
$s = new AppendStream ();
$s -> detach ();
}
public function testReturnsEmptyMetadata ()
{
$s = new AppendStream ();
$this -> assertEquals ([], $s -> getMetadata ());
$this -> assertNull ( $s -> getMetadata ( 'foo' ));
}
}