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 ;
use GuzzleHttp\Psr7\FnStream ;
use GuzzleHttp\Psr7\Stream ;
use GuzzleHttp\Psr7\LimitStream ;
use GuzzleHttp\Psr7\NoSeekStream ;
2015-08-17 17:00:26 -07:00
/**
2015-08-27 12:03:05 -07:00
* @ covers GuzzleHttp\Psr7\LimitStream
2015-08-17 17:00:26 -07:00
*/
class LimitStreamTest extends \PHPUnit_Framework_TestCase
{
/** @var LimitStream */
protected $body ;
/** @var Stream */
protected $decorated ;
public function setUp ()
{
2015-08-27 12:03:05 -07:00
$this -> decorated = Psr7\stream_for ( fopen ( __FILE__ , 'r' ));
2015-08-17 17:00:26 -07:00
$this -> body = new LimitStream ( $this -> decorated , 10 , 3 );
}
public function testReturnsSubset ()
{
2015-08-27 12:03:05 -07:00
$body = new LimitStream ( Psr7\stream_for ( 'foo' ), - 1 , 1 );
2015-08-17 17:00:26 -07:00
$this -> assertEquals ( 'oo' , ( string ) $body );
$this -> assertTrue ( $body -> eof ());
$body -> seek ( 0 );
$this -> assertFalse ( $body -> eof ());
$this -> assertEquals ( 'oo' , $body -> read ( 100 ));
2015-08-27 12:03:05 -07:00
$this -> assertSame ( '' , $body -> read ( 1 ));
2015-08-17 17:00:26 -07:00
$this -> assertTrue ( $body -> eof ());
}
public function testReturnsSubsetWhenCastToString ()
{
2015-08-27 12:03:05 -07:00
$body = Psr7\stream_for ( 'foo_baz_bar' );
2015-08-17 17:00:26 -07:00
$limited = new LimitStream ( $body , 3 , 4 );
$this -> assertEquals ( 'baz' , ( string ) $limited );
}
2015-08-27 12:03:05 -07:00
/**
* @ expectedException \RuntimeException
* @ expectedExceptionMessage Unable to seek to stream position 10 with whence 0
*/
public function testEnsuresPositionCanBeekSeekedTo ()
{
new LimitStream ( Psr7\stream_for ( '' ), 0 , 10 );
}
2015-08-17 17:00:26 -07:00
public function testReturnsSubsetOfEmptyBodyWhenCastToString ()
{
2015-08-27 12:03:05 -07:00
$body = Psr7\stream_for ( '01234567891234' );
2015-08-17 17:00:26 -07:00
$limited = new LimitStream ( $body , 0 , 10 );
$this -> assertEquals ( '' , ( string ) $limited );
}
2015-08-27 12:03:05 -07:00
public function testReturnsSpecificSubsetOBodyWhenCastToString ()
{
$body = Psr7\stream_for ( '0123456789abcdef' );
$limited = new LimitStream ( $body , 3 , 10 );
$this -> assertEquals ( 'abc' , ( string ) $limited );
}
2015-08-17 17:00:26 -07:00
public function testSeeksWhenConstructed ()
{
$this -> assertEquals ( 0 , $this -> body -> tell ());
$this -> assertEquals ( 3 , $this -> decorated -> tell ());
}
public function testAllowsBoundedSeek ()
{
2015-08-27 12:03:05 -07:00
$this -> body -> seek ( 100 );
2015-08-17 17:00:26 -07:00
$this -> assertEquals ( 10 , $this -> body -> tell ());
$this -> assertEquals ( 13 , $this -> decorated -> tell ());
2015-08-27 12:03:05 -07:00
$this -> body -> seek ( 0 );
2015-08-17 17:00:26 -07:00
$this -> assertEquals ( 0 , $this -> body -> tell ());
$this -> assertEquals ( 3 , $this -> decorated -> tell ());
2015-08-27 12:03:05 -07:00
try {
$this -> body -> seek ( - 10 );
$this -> fail ();
} catch ( \RuntimeException $e ) {}
2015-08-17 17:00:26 -07:00
$this -> assertEquals ( 0 , $this -> body -> tell ());
$this -> assertEquals ( 3 , $this -> decorated -> tell ());
2015-08-27 12:03:05 -07:00
$this -> body -> seek ( 5 );
2015-08-17 17:00:26 -07:00
$this -> assertEquals ( 5 , $this -> body -> tell ());
$this -> assertEquals ( 8 , $this -> decorated -> tell ());
2015-08-27 12:03:05 -07:00
// Fail
try {
$this -> body -> seek ( 1000 , SEEK_END );
$this -> fail ();
} catch ( \RuntimeException $e ) {}
2015-08-17 17:00:26 -07:00
}
public function testReadsOnlySubsetOfData ()
{
$data = $this -> body -> read ( 100 );
$this -> assertEquals ( 10 , strlen ( $data ));
2015-08-27 12:03:05 -07:00
$this -> assertSame ( '' , $this -> body -> read ( 1000 ));
2015-08-17 17:00:26 -07:00
$this -> body -> setOffset ( 10 );
$newData = $this -> body -> read ( 100 );
$this -> assertEquals ( 10 , strlen ( $newData ));
$this -> assertNotSame ( $data , $newData );
}
/**
2015-08-27 12:03:05 -07:00
* @ expectedException \RuntimeException
* @ expectedExceptionMessage Could not seek to stream offset 2
2015-08-17 17:00:26 -07:00
*/
public function testThrowsWhenCurrentGreaterThanOffsetSeek ()
{
2015-08-27 12:03:05 -07:00
$a = Psr7\stream_for ( 'foo_bar' );
2015-08-17 17:00:26 -07:00
$b = new NoSeekStream ( $a );
$c = new LimitStream ( $b );
$a -> getContents ();
$c -> setOffset ( 2 );
}
2015-08-27 12:03:05 -07:00
public function testCanGetContentsWithoutSeeking ()
{
$a = Psr7\stream_for ( 'foo_bar' );
$b = new NoSeekStream ( $a );
$c = new LimitStream ( $b );
$this -> assertEquals ( 'foo_bar' , $c -> getContents ());
}
2015-08-17 17:00:26 -07:00
public function testClaimsConsumedWhenReadLimitIsReached ()
{
$this -> assertFalse ( $this -> body -> eof ());
$this -> body -> read ( 1000 );
$this -> assertTrue ( $this -> body -> eof ());
}
public function testContentLengthIsBounded ()
{
$this -> assertEquals ( 10 , $this -> body -> getSize ());
}
public function testGetContentsIsBasedOnSubset ()
{
2015-08-27 12:03:05 -07:00
$body = new LimitStream ( Psr7\stream_for ( 'foobazbar' ), 3 , 3 );
2015-08-17 17:00:26 -07:00
$this -> assertEquals ( 'baz' , $body -> getContents ());
}
public function testReturnsNullIfSizeCannotBeDetermined ()
{
$a = new FnStream ([
'getSize' => function () { return null ; },
'tell' => function () { return 0 ; },
]);
$b = new LimitStream ( $a );
$this -> assertNull ( $b -> getSize ());
}
public function testLengthLessOffsetWhenNoLimitSize ()
{
2015-08-27 12:03:05 -07:00
$a = Psr7\stream_for ( 'foo_bar' );
2015-08-17 17:00:26 -07:00
$b = new LimitStream ( $a , - 1 , 4 );
$this -> assertEquals ( 3 , $b -> getSize ());
}
}