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\LimitStream ;
use GuzzleHttp\Psr7\PumpStream ;
use GuzzleHttp\Psr7 ;
2015-08-17 17:00:26 -07:00
class PumpStreamTest extends \PHPUnit_Framework_TestCase
{
public function testHasMetadataAndSize ()
{
$p = new PumpStream ( function () {}, [
'metadata' => [ 'foo' => 'bar' ],
'size' => 100
]);
$this -> assertEquals ( 'bar' , $p -> getMetadata ( 'foo' ));
$this -> assertEquals ([ 'foo' => 'bar' ], $p -> getMetadata ());
$this -> assertEquals ( 100 , $p -> getSize ());
}
public function testCanReadFromCallable ()
{
2015-08-27 12:03:05 -07:00
$p = Psr7\stream_for ( function ( $size ) {
2015-08-17 17:00:26 -07:00
return 'a' ;
});
$this -> assertEquals ( 'a' , $p -> read ( 1 ));
$this -> assertEquals ( 1 , $p -> tell ());
$this -> assertEquals ( 'aaaaa' , $p -> read ( 5 ));
$this -> assertEquals ( 6 , $p -> tell ());
}
public function testStoresExcessDataInBuffer ()
{
$called = [];
2015-08-27 12:03:05 -07:00
$p = Psr7\stream_for ( function ( $size ) use ( & $called ) {
2015-08-17 17:00:26 -07:00
$called [] = $size ;
return 'abcdef' ;
});
$this -> assertEquals ( 'a' , $p -> read ( 1 ));
$this -> assertEquals ( 'b' , $p -> read ( 1 ));
$this -> assertEquals ( 'cdef' , $p -> read ( 4 ));
$this -> assertEquals ( 'abcdefabc' , $p -> read ( 9 ));
$this -> assertEquals ([ 1 , 9 , 3 ], $called );
}
public function testInifiniteStreamWrappedInLimitStream ()
{
2015-08-27 12:03:05 -07:00
$p = Psr7\stream_for ( function () { return 'a' ; });
2015-08-17 17:00:26 -07:00
$s = new LimitStream ( $p , 5 );
$this -> assertEquals ( 'aaaaa' , ( string ) $s );
}
public function testDescribesCapabilities ()
{
2015-08-27 12:03:05 -07:00
$p = Psr7\stream_for ( function () {});
2015-08-17 17:00:26 -07:00
$this -> assertTrue ( $p -> isReadable ());
$this -> assertFalse ( $p -> isSeekable ());
$this -> assertFalse ( $p -> isWritable ());
$this -> assertNull ( $p -> getSize ());
$this -> assertEquals ( '' , $p -> getContents ());
$this -> assertEquals ( '' , ( string ) $p );
$p -> close ();
$this -> assertEquals ( '' , $p -> read ( 10 ));
$this -> assertTrue ( $p -> eof ());
2015-08-27 12:03:05 -07:00
try {
$this -> assertFalse ( $p -> write ( 'aa' ));
$this -> fail ();
} catch ( \RuntimeException $e ) {}
2015-08-17 17:00:26 -07:00
}
}