/vendor/guzzlehttp/streams/tests/AppendStreamTest.php |
@@ -0,0 +1,162 @@ |
<?php |
namespace GuzzleHttp\Tests\Stream; |
|
use GuzzleHttp\Stream\AppendStream; |
use GuzzleHttp\Stream\Stream; |
|
class AppendStreamTest extends \PHPUnit_Framework_TestCase |
{ |
/** |
* @expectedException \InvalidArgumentException |
* @expectedExceptionMessage Each stream must be readable |
*/ |
public function testValidatesStreamsAreReadable() |
{ |
$a = new AppendStream(); |
$s = $this->getMockBuilder('GuzzleHttp\Stream\StreamInterface') |
->setMethods(['isReadable']) |
->getMockForAbstractClass(); |
$s->expects($this->once()) |
->method('isReadable') |
->will($this->returnValue(false)); |
$a->addStream($s); |
} |
|
public function testValidatesSeekType() |
{ |
$a = new AppendStream(); |
$this->assertFalse($a->seek(100, SEEK_CUR)); |
} |
|
public function testTriesToRewindOnSeek() |
{ |
$a = new AppendStream(); |
$s = $this->getMockBuilder('GuzzleHttp\Stream\StreamInterface') |
->setMethods(['isReadable', 'seek', 'isSeekable']) |
->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()) |
->method('seek') |
->will($this->returnValue(false)); |
$a->addStream($s); |
$this->assertFalse($a->seek(10)); |
} |
|
public function testSeeksToPositionByReading() |
{ |
$a = new AppendStream([ |
Stream::factory('foo'), |
Stream::factory('bar'), |
Stream::factory('baz'), |
]); |
|
$this->assertTrue($a->seek(3)); |
$this->assertEquals(3, $a->tell()); |
$this->assertEquals('bar', $a->read(3)); |
$a->seek(6); |
$this->assertEquals(6, $a->tell()); |
$this->assertEquals('baz', $a->read(3)); |
} |
|
public function testDetachesEachStream() |
{ |
$s1 = Stream::factory('foo'); |
$s2 = Stream::factory('foo'); |
$a = new AppendStream([$s1, $s2]); |
$this->assertSame('foofoo', (string) $a); |
$a->detach(); |
$this->assertSame('', (string) $a); |
$this->assertSame(0, $a->getSize()); |
} |
|
public function testClosesEachStream() |
{ |
$s1 = Stream::factory('foo'); |
$a = new AppendStream([$s1]); |
$a->close(); |
$this->assertSame('', (string) $a); |
} |
|
public function testIsNotWritable() |
{ |
$a = new AppendStream([Stream::factory('foo')]); |
$this->assertFalse($a->isWritable()); |
$this->assertTrue($a->isSeekable()); |
$this->assertTrue($a->isReadable()); |
$this->assertFalse($a->write('foo')); |
} |
|
public function testDoesNotNeedStreams() |
{ |
$a = new AppendStream(); |
$this->assertEquals('', (string) $a); |
} |
|
public function testCanReadFromMultipleStreams() |
{ |
$a = new AppendStream([ |
Stream::factory('foo'), |
Stream::factory('bar'), |
Stream::factory('baz'), |
]); |
$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)); |
$this->assertTrue($a->eof()); |
$this->assertSame(9, $a->tell()); |
$this->assertEquals('foobarbaz', (string) $a); |
} |
|
public function testCanDetermineSizeFromMultipleStreams() |
{ |
$a = new AppendStream([ |
Stream::factory('foo'), |
Stream::factory('bar') |
]); |
$this->assertEquals(6, $a->getSize()); |
|
$s = $this->getMockBuilder('GuzzleHttp\Stream\StreamInterface') |
->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() |
{ |
$s = $this->getMockBuilder('GuzzleHttp\Stream\StreamInterface') |
->setMethods(['read', 'isReadable', 'eof']) |
->getMockForAbstractClass(); |
$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 testFlushReturnsFalse() |
{ |
$s = new AppendStream(); |
$this->assertFalse($s->flush()); |
} |
} |
/vendor/guzzlehttp/streams/tests/CachingStreamTest.php |
@@ -0,0 +1,137 @@ |
<?php |
namespace GuzzleHttp\Tests\Stream; |
|
use GuzzleHttp\Stream\Stream; |
use GuzzleHttp\Stream\CachingStream; |
use GuzzleHttp\Stream\Utils; |
|
/** |
* @covers GuzzleHttp\Stream\CachingStream |
*/ |
class CachingStreamTest extends \PHPUnit_Framework_TestCase |
{ |
/** @var CachingStream */ |
protected $body; |
|
/** @var Stream */ |
protected $decorated; |
|
public function setUp() |
{ |
$this->decorated = Stream::factory('testing'); |
$this->body = new CachingStream($this->decorated); |
} |
|
public function tearDown() |
{ |
$this->decorated->close(); |
$this->body->close(); |
} |
|
public function testUsesRemoteSizeIfPossible() |
{ |
$body = Stream::factory('test'); |
$caching = new CachingStream($body); |
$this->assertEquals(4, $caching->getSize()); |
} |
|
/** |
* @expectedException \RuntimeException |
* @expectedExceptionMessage Cannot seek to byte 10 |
*/ |
public function testCannotSeekPastWhatHasBeenRead() |
{ |
$this->body->seek(10); |
} |
|
public function testCannotUseSeekEnd() |
{ |
$this->assertFalse($this->body->seek(2, SEEK_END)); |
} |
|
public function testRewindUsesSeek() |
{ |
$a = Stream::factory('foo'); |
$d = $this->getMockBuilder('GuzzleHttp\Stream\CachingStream') |
->setMethods(array('seek')) |
->setConstructorArgs(array($a)) |
->getMock(); |
$d->expects($this->once()) |
->method('seek') |
->with(0) |
->will($this->returnValue(true)); |
$d->seek(0); |
} |
|
public function testCanSeekToReadBytes() |
{ |
$this->assertEquals('te', $this->body->read(2)); |
$this->body->seek(0); |
$this->assertEquals('test', $this->body->read(4)); |
$this->assertEquals(4, $this->body->tell()); |
$this->body->seek(2); |
$this->assertEquals(2, $this->body->tell()); |
$this->body->seek(2, SEEK_CUR); |
$this->assertEquals(4, $this->body->tell()); |
$this->assertEquals('ing', $this->body->read(3)); |
} |
|
public function testWritesToBufferStream() |
{ |
$this->body->read(2); |
$this->body->write('hi'); |
$this->body->seek(0); |
$this->assertEquals('tehiing', (string) $this->body); |
} |
|
public function testSkipsOverwrittenBytes() |
{ |
$decorated = Stream::factory( |
implode("\n", array_map(function ($n) { |
return str_pad($n, 4, '0', STR_PAD_LEFT); |
}, range(0, 25))), |
true |
); |
|
$body = new CachingStream($decorated); |
|
$this->assertEquals("0000\n", Utils::readline($body)); |
$this->assertEquals("0001\n", Utils::readline($body)); |
// Write over part of the body yet to be read, so skip some bytes |
$this->assertEquals(5, $body->write("TEST\n")); |
$this->assertEquals(5, $this->readAttribute($body, 'skipReadBytes')); |
// Read, which skips bytes, then reads |
$this->assertEquals("0003\n", Utils::readline($body)); |
$this->assertEquals(0, $this->readAttribute($body, 'skipReadBytes')); |
$this->assertEquals("0004\n", Utils::readline($body)); |
$this->assertEquals("0005\n", Utils::readline($body)); |
|
// Overwrite part of the cached body (so don't skip any bytes) |
$body->seek(5); |
$this->assertEquals(5, $body->write("ABCD\n")); |
$this->assertEquals(0, $this->readAttribute($body, 'skipReadBytes')); |
$this->assertEquals("TEST\n", Utils::readline($body)); |
$this->assertEquals("0003\n", Utils::readline($body)); |
$this->assertEquals("0004\n", Utils::readline($body)); |
$this->assertEquals("0005\n", Utils::readline($body)); |
$this->assertEquals("0006\n", Utils::readline($body)); |
$this->assertEquals(5, $body->write("1234\n")); |
$this->assertEquals(5, $this->readAttribute($body, 'skipReadBytes')); |
|
// Seek to 0 and ensure the overwritten bit is replaced |
$body->seek(0); |
$this->assertEquals("0000\nABCD\nTEST\n0003\n0004\n0005\n0006\n1234\n0008\n0009\n", $body->read(50)); |
|
// Ensure that casting it to a string does not include the bit that was overwritten |
$this->assertContains("0000\nABCD\nTEST\n0003\n0004\n0005\n0006\n1234\n0008\n0009\n", (string) $body); |
} |
|
public function testClosesBothStreams() |
{ |
$s = fopen('php://temp', 'r'); |
$a = Stream::factory($s); |
$d = new CachingStream($a); |
$d->close(); |
$this->assertFalse(is_resource($s)); |
} |
} |
/vendor/guzzlehttp/streams/tests/FnStreamTest.php |
@@ -0,0 +1,90 @@ |
<?php |
namespace GuzzleHttp\Tests\Stream; |
|
use GuzzleHttp\Stream\Stream; |
use GuzzleHttp\Stream\FnStream; |
|
/** |
* @covers GuzzleHttp\Stream\FnStream |
*/ |
class FnStreamTest extends \PHPUnit_Framework_TestCase |
{ |
/** |
* @expectedException \BadMethodCallException |
* @expectedExceptionMessage seek() is not implemented in the FnStream |
*/ |
public function testThrowsWhenNotImplemented() |
{ |
(new FnStream([]))->seek(1); |
} |
|
public function testProxiesToFunction() |
{ |
$s = new FnStream([ |
'read' => function ($len) { |
$this->assertEquals(3, $len); |
return 'foo'; |
} |
]); |
|
$this->assertEquals('foo', $s->read(3)); |
} |
|
public function testCanCloseOnDestruct() |
{ |
$called = false; |
$s = new FnStream([ |
'close' => function () use (&$called) { |
$called = true; |
} |
]); |
unset($s); |
$this->assertTrue($called); |
} |
|
public function testDoesNotRequireClose() |
{ |
$s = new FnStream([]); |
unset($s); |
} |
|
public function testDecoratesStream() |
{ |
$a = Stream::factory('foo'); |
$b = FnStream::decorate($a, []); |
$this->assertEquals(3, $b->getSize()); |
$this->assertEquals($b->isWritable(), true); |
$this->assertEquals($b->isReadable(), true); |
$this->assertEquals($b->isSeekable(), true); |
$this->assertEquals($b->read(3), 'foo'); |
$this->assertEquals($b->tell(), 3); |
$this->assertEquals($a->tell(), 3); |
$this->assertEquals($b->eof(), true); |
$this->assertEquals($a->eof(), true); |
$b->seek(0); |
$this->assertEquals('foo', (string) $b); |
$b->seek(0); |
$this->assertEquals('foo', $b->getContents()); |
$this->assertEquals($a->getMetadata(), $b->getMetadata()); |
$b->seek(0, SEEK_END); |
$b->write('bar'); |
$this->assertEquals('foobar', (string) $b); |
$b->flush(); |
$this->assertInternalType('resource', $b->detach()); |
$b->close(); |
} |
|
public function testDecoratesWithCustomizations() |
{ |
$called = false; |
$a = Stream::factory('foo'); |
$b = FnStream::decorate($a, [ |
'read' => function ($len) use (&$called, $a) { |
$called = true; |
return $a->read($len); |
} |
]); |
$this->assertEquals('foo', $b->read(3)); |
$this->assertTrue($called); |
} |
} |
/vendor/guzzlehttp/streams/tests/GuzzleStreamWrapperTest.php |
@@ -0,0 +1,99 @@ |
<?php |
namespace GuzzleHttp\Tests\Stream; |
|
use GuzzleHttp\Stream\GuzzleStreamWrapper; |
use GuzzleHttp\Stream\Stream; |
|
/** |
* @covers GuzzleHttp\Stream\GuzzleStreamWrapper |
*/ |
class GuzzleStreamWrapperTest extends \PHPUnit_Framework_TestCase |
{ |
public function testResource() |
{ |
$stream = Stream::factory('foo'); |
$handle = GuzzleStreamWrapper::getResource($stream); |
$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)); |
$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() |
{ |
$stream = $this->getMockBuilder('GuzzleHttp\Stream\StreamInterface') |
->setMethods(['isReadable', 'isWritable']) |
->getMockForAbstractClass(); |
$stream->expects($this->once()) |
->method('isReadable') |
->will($this->returnValue(false)); |
$stream->expects($this->once()) |
->method('isWritable') |
->will($this->returnValue(false)); |
GuzzleStreamWrapper::getResource($stream); |
} |
|
/** |
* @expectedException \PHPUnit_Framework_Error_Warning |
*/ |
public function testReturnsFalseWhenStreamDoesNotExist() |
{ |
fopen('guzzle://foo', 'r'); |
} |
|
public function testCanOpenReadonlyStream() |
{ |
$stream = $this->getMockBuilder('GuzzleHttp\Stream\StreamInterface') |
->setMethods(['isReadable', 'isWritable']) |
->getMockForAbstractClass(); |
$stream->expects($this->once()) |
->method('isReadable') |
->will($this->returnValue(false)); |
$stream->expects($this->once()) |
->method('isWritable') |
->will($this->returnValue(true)); |
$r = GuzzleStreamWrapper::getResource($stream); |
$this->assertInternalType('resource', $r); |
fclose($r); |
} |
} |
/vendor/guzzlehttp/streams/tests/LazyOpenStreamTest.php |
@@ -0,0 +1,64 @@ |
<?php |
namespace GuzzleHttp\Tests\Stream; |
|
use GuzzleHttp\Stream\LazyOpenStream; |
|
class LazyOpenStreamTest extends \PHPUnit_Framework_TestCase |
{ |
private $fname; |
|
public function setup() |
{ |
$this->fname = tempnam('/tmp', 'tfile'); |
|
if (file_exists($this->fname)) { |
unlink($this->fname); |
} |
} |
|
public function tearDown() |
{ |
if (file_exists($this->fname)) { |
unlink($this->fname); |
} |
} |
|
public function testOpensLazily() |
{ |
$l = new LazyOpenStream($this->fname, 'w+'); |
$l->write('foo'); |
$this->assertInternalType('array', $l->getMetadata()); |
$this->assertFileExists($this->fname); |
$this->assertEquals('foo', file_get_contents($this->fname)); |
$this->assertEquals('foo', (string) $l); |
} |
|
public function testProxiesToFile() |
{ |
file_put_contents($this->fname, 'foo'); |
$l = new LazyOpenStream($this->fname, 'r'); |
$this->assertEquals('foo', $l->read(4)); |
$this->assertTrue($l->eof()); |
$this->assertEquals(3, $l->tell()); |
$this->assertTrue($l->isReadable()); |
$this->assertTrue($l->isSeekable()); |
$this->assertFalse($l->isWritable()); |
$l->seek(1); |
$this->assertEquals('oo', $l->getContents()); |
$this->assertEquals('foo', (string) $l); |
$this->assertEquals(3, $l->getSize()); |
$this->assertInternalType('array', $l->getMetadata()); |
$l->close(); |
} |
|
public function testDetachesUnderlyingStream() |
{ |
file_put_contents($this->fname, 'foo'); |
$l = new LazyOpenStream($this->fname, 'r'); |
$r = $l->detach(); |
$this->assertInternalType('resource', $r); |
fseek($r, 0); |
$this->assertEquals('foo', stream_get_contents($r)); |
fclose($r); |
} |
} |
/vendor/guzzlehttp/streams/tests/LimitStreamTest.php |
@@ -0,0 +1,126 @@ |
<?php |
namespace GuzzleHttp\Tests\Http; |
|
use GuzzleHttp\Stream\FnStream; |
use GuzzleHttp\Stream\Stream; |
use GuzzleHttp\Stream\LimitStream; |
use GuzzleHttp\Stream\NoSeekStream; |
|
/** |
* @covers GuzzleHttp\Stream\LimitStream |
*/ |
class LimitStreamTest extends \PHPUnit_Framework_TestCase |
{ |
/** @var LimitStream */ |
protected $body; |
|
/** @var Stream */ |
protected $decorated; |
|
public function setUp() |
{ |
$this->decorated = Stream::factory(fopen(__FILE__, 'r')); |
$this->body = new LimitStream($this->decorated, 10, 3); |
} |
|
public function testReturnsSubset() |
{ |
$body = new LimitStream(Stream::factory('foo'), -1, 1); |
$this->assertEquals('oo', (string) $body); |
$this->assertTrue($body->eof()); |
$body->seek(0); |
$this->assertFalse($body->eof()); |
$this->assertEquals('oo', $body->read(100)); |
$this->assertTrue($body->eof()); |
} |
|
public function testReturnsSubsetWhenCastToString() |
{ |
$body = Stream::factory('foo_baz_bar'); |
$limited = new LimitStream($body, 3, 4); |
$this->assertEquals('baz', (string) $limited); |
} |
|
public function testReturnsSubsetOfEmptyBodyWhenCastToString() |
{ |
$body = Stream::factory(''); |
$limited = new LimitStream($body, 0, 10); |
$this->assertEquals('', (string) $limited); |
} |
|
public function testSeeksWhenConstructed() |
{ |
$this->assertEquals(0, $this->body->tell()); |
$this->assertEquals(3, $this->decorated->tell()); |
} |
|
public function testAllowsBoundedSeek() |
{ |
$this->body->seek(100); |
$this->assertEquals(13, $this->decorated->tell()); |
$this->body->seek(0); |
$this->assertEquals(0, $this->body->tell()); |
$this->assertEquals(3, $this->decorated->tell()); |
$this->assertEquals(false, $this->body->seek(1000, SEEK_END)); |
} |
|
public function testReadsOnlySubsetOfData() |
{ |
$data = $this->body->read(100); |
$this->assertEquals(10, strlen($data)); |
$this->assertFalse($this->body->read(1000)); |
|
$this->body->setOffset(10); |
$newData = $this->body->read(100); |
$this->assertEquals(10, strlen($newData)); |
$this->assertNotSame($data, $newData); |
} |
|
/** |
* @expectedException \GuzzleHttp\Stream\Exception\SeekException |
* @expectedExceptionMessage Could not seek the stream to position 2 |
*/ |
public function testThrowsWhenCurrentGreaterThanOffsetSeek() |
{ |
$a = Stream::factory('foo_bar'); |
$b = new NoSeekStream($a); |
$c = new LimitStream($b); |
$a->getContents(); |
$c->setOffset(2); |
} |
|
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() |
{ |
$body = new LimitStream(Stream::factory('foobazbar'), 3, 3); |
$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() |
{ |
$a = Stream::factory('foo_bar'); |
$b = new LimitStream($a, -1, 4); |
$this->assertEquals(3, $b->getSize()); |
} |
} |
/vendor/guzzlehttp/streams/tests/StreamDecoratorTraitTest.php |
@@ -0,0 +1,145 @@ |
<?php |
namespace GuzzleHttp\Tests\Stream; |
|
use GuzzleHttp\Stream\StreamInterface; |
use GuzzleHttp\Stream\Stream; |
use GuzzleHttp\Stream\StreamDecoratorTrait; |
|
class Str implements StreamInterface |
{ |
use StreamDecoratorTrait; |
} |
|
/** |
* @covers GuzzleHttp\Stream\StreamDecoratorTrait |
*/ |
class StreamDecoratorTraitTest extends \PHPUnit_Framework_TestCase |
{ |
private $a; |
private $b; |
private $c; |
|
public function setUp() |
{ |
$this->c = fopen('php://temp', 'r+'); |
fwrite($this->c, 'foo'); |
fseek($this->c, 0); |
$this->a = Stream::factory($this->c); |
$this->b = new Str($this->a); |
} |
|
public function testCatchesExceptionsWhenCastingToString() |
{ |
$s = $this->getMockBuilder('GuzzleHttp\Stream\StreamInterface') |
->setMethods(['read']) |
->getMockForAbstractClass(); |
$s->expects($this->once()) |
->method('read') |
->will($this->throwException(new \Exception('foo'))); |
$msg = ''; |
set_error_handler(function ($errNo, $str) use (&$msg) { $msg = $str; }); |
echo new Str($s); |
restore_error_handler(); |
$this->assertContains('foo', $msg); |
} |
|
public function testToString() |
{ |
$this->assertEquals('foo', (string) $this->b); |
} |
|
public function testHasSize() |
{ |
$this->assertEquals(3, $this->b->getSize()); |
$this->assertSame($this->b, $this->b->setSize(2)); |
$this->assertEquals(2, $this->b->getSize()); |
} |
|
public function testReads() |
{ |
$this->assertEquals('foo', $this->b->read(10)); |
} |
|
public function testCheckMethods() |
{ |
$this->assertEquals($this->a->isReadable(), $this->b->isReadable()); |
$this->assertEquals($this->a->isWritable(), $this->b->isWritable()); |
$this->assertEquals($this->a->isSeekable(), $this->b->isSeekable()); |
} |
|
public function testSeeksAndTells() |
{ |
$this->assertTrue($this->b->seek(1)); |
$this->assertEquals(1, $this->a->tell()); |
$this->assertEquals(1, $this->b->tell()); |
$this->assertTrue($this->b->seek(0)); |
$this->assertEquals(0, $this->a->tell()); |
$this->assertEquals(0, $this->b->tell()); |
$this->assertTrue($this->b->seek(0, SEEK_END)); |
$this->assertEquals(3, $this->a->tell()); |
$this->assertEquals(3, $this->b->tell()); |
} |
|
public function testGetsContents() |
{ |
$this->assertEquals('foo', $this->b->getContents()); |
$this->assertEquals('', $this->b->getContents()); |
$this->b->seek(1); |
$this->assertEquals('o', $this->b->getContents(1)); |
$this->assertEquals('', $this->b->getContents(0)); |
} |
|
public function testCloses() |
{ |
$this->b->close(); |
$this->assertFalse(is_resource($this->c)); |
} |
|
public function testDetaches() |
{ |
$this->b->detach(); |
$this->assertFalse($this->b->isReadable()); |
} |
|
public function testWrapsMetadata() |
{ |
$this->assertSame($this->b->getMetadata(), $this->a->getMetadata()); |
$this->assertSame($this->b->getMetadata('uri'), $this->a->getMetadata('uri')); |
} |
|
public function testWrapsWrites() |
{ |
$this->b->seek(0, SEEK_END); |
$this->b->write('foo'); |
$this->assertEquals('foofoo', (string) $this->a); |
} |
|
public function testWrapsFlush() |
{ |
$this->b->flush(); |
} |
|
/** |
* @expectedException \UnexpectedValueException |
*/ |
public function testThrowsWithInvalidGetter() |
{ |
$this->b->foo; |
} |
|
/** |
* @expectedException \BadMethodCallException |
*/ |
public function testThrowsWhenGetterNotImplemented() |
{ |
$s = new BadStream(); |
$s->stream; |
} |
} |
|
class BadStream |
{ |
use StreamDecoratorTrait; |
|
public function __construct() {} |
} |
/vendor/guzzlehttp/streams/tests/StreamTest.php |
@@ -0,0 +1,223 @@ |
<?php |
namespace GuzzleHttp\Tests\Stream; |
|
use GuzzleHttp\Stream\Stream; |
|
/** |
* @covers GuzzleHttp\Stream\Stream |
*/ |
class StreamTest extends \PHPUnit_Framework_TestCase |
{ |
/** |
* @expectedException \InvalidArgumentException |
*/ |
public function testConstructorThrowsExceptionOnInvalidArgument() |
{ |
new Stream(true); |
} |
|
public function testConstructorInitializesProperties() |
{ |
$handle = fopen('php://temp', 'r+'); |
fwrite($handle, 'data'); |
$stream = new Stream($handle); |
$this->assertTrue($stream->isReadable()); |
$this->assertTrue($stream->isWritable()); |
$this->assertTrue($stream->isSeekable()); |
$this->assertEquals('php://temp', $stream->getMetadata('uri')); |
$this->assertInternalType('array', $stream->getMetadata()); |
$this->assertEquals(4, $stream->getSize()); |
$this->assertFalse($stream->eof()); |
$stream->close(); |
} |
|
public function testStreamClosesHandleOnDestruct() |
{ |
$handle = fopen('php://temp', 'r'); |
$stream = new Stream($handle); |
unset($stream); |
$this->assertFalse(is_resource($handle)); |
} |
|
public function testConvertsToString() |
{ |
$handle = fopen('php://temp', 'w+'); |
fwrite($handle, 'data'); |
$stream = new Stream($handle); |
$this->assertEquals('data', (string) $stream); |
$this->assertEquals('data', (string) $stream); |
$stream->close(); |
} |
|
public function testGetsContents() |
{ |
$handle = fopen('php://temp', 'w+'); |
fwrite($handle, 'data'); |
$stream = new Stream($handle); |
$this->assertEquals('', $stream->getContents()); |
$stream->seek(0); |
$this->assertEquals('data', $stream->getContents()); |
$this->assertEquals('', $stream->getContents()); |
$stream->seek(0); |
$this->assertEquals('da', $stream->getContents(2)); |
$stream->close(); |
} |
|
public function testChecksEof() |
{ |
$handle = fopen('php://temp', 'w+'); |
fwrite($handle, 'data'); |
$stream = new Stream($handle); |
$this->assertFalse($stream->eof()); |
$stream->read(4); |
$this->assertTrue($stream->eof()); |
$stream->close(); |
} |
|
public function testAllowsSettingManualSize() |
{ |
$handle = fopen('php://temp', 'w+'); |
fwrite($handle, 'data'); |
$stream = new Stream($handle); |
$stream->setSize(10); |
$this->assertEquals(10, $stream->getSize()); |
$stream->close(); |
} |
|
public function testGetSize() |
{ |
$size = filesize(__FILE__); |
$handle = fopen(__FILE__, 'r'); |
$stream = new Stream($handle); |
$this->assertEquals($size, $stream->getSize()); |
// Load from cache |
$this->assertEquals($size, $stream->getSize()); |
$stream->close(); |
} |
|
public function testEnsuresSizeIsConsistent() |
{ |
$h = fopen('php://temp', 'w+'); |
$this->assertEquals(3, fwrite($h, 'foo')); |
$stream = new Stream($h); |
$this->assertEquals(3, $stream->getSize()); |
$this->assertEquals(4, $stream->write('test')); |
$this->assertEquals(7, $stream->getSize()); |
$this->assertEquals(7, $stream->getSize()); |
$stream->close(); |
} |
|
public function testProvidesStreamPosition() |
{ |
$handle = fopen('php://temp', 'w+'); |
$stream = new Stream($handle); |
$this->assertEquals(0, $stream->tell()); |
$stream->write('foo'); |
$this->assertEquals(3, $stream->tell()); |
$stream->seek(1); |
$this->assertEquals(1, $stream->tell()); |
$this->assertSame(ftell($handle), $stream->tell()); |
$stream->close(); |
} |
|
public function testKeepsPositionOfResource() |
{ |
$h = fopen(__FILE__, 'r'); |
fseek($h, 10); |
$stream = Stream::factory($h); |
$this->assertEquals(10, $stream->tell()); |
$stream->close(); |
} |
|
public function testCanDetachStream() |
{ |
$r = fopen('php://temp', 'w+'); |
$stream = new Stream($r); |
$this->assertTrue($stream->isReadable()); |
$this->assertSame($r, $stream->detach()); |
$this->assertNull($stream->detach()); |
$this->assertFalse($stream->isReadable()); |
$this->assertSame('', $stream->read(10)); |
$this->assertFalse($stream->isWritable()); |
$this->assertFalse($stream->write('foo')); |
$this->assertFalse($stream->isSeekable()); |
$this->assertFalse($stream->seek(10)); |
$this->assertFalse($stream->tell()); |
$this->assertFalse($stream->eof()); |
$this->assertNull($stream->getSize()); |
$this->assertSame('', (string) $stream); |
$this->assertSame('', $stream->getContents()); |
$stream->close(); |
} |
|
public function testCloseClearProperties() |
{ |
$handle = fopen('php://temp', 'r+'); |
$stream = new Stream($handle); |
$stream->close(); |
|
$this->assertEmpty($stream->getMetadata()); |
$this->assertFalse($stream->isSeekable()); |
$this->assertFalse($stream->isReadable()); |
$this->assertFalse($stream->isWritable()); |
$this->assertNull($stream->getSize()); |
} |
|
public function testCreatesWithFactory() |
{ |
$stream = Stream::factory('foo'); |
$this->assertInstanceOf('GuzzleHttp\Stream\Stream', $stream); |
$this->assertEquals('foo', $stream->getContents()); |
$stream->close(); |
} |
|
public function testFlushes() |
{ |
$stream = Stream::factory('foo'); |
$this->assertTrue($stream->flush()); |
$stream->close(); |
} |
|
public function testFactoryCreatesFromEmptyString() |
{ |
$s = Stream::factory(); |
$this->assertInstanceOf('GuzzleHttp\Stream\Stream', $s); |
} |
|
public function testFactoryCreatesFromResource() |
{ |
$r = fopen(__FILE__, 'r'); |
$s = Stream::factory($r); |
$this->assertInstanceOf('GuzzleHttp\Stream\Stream', $s); |
$this->assertSame(file_get_contents(__FILE__), (string) $s); |
} |
|
public function testFactoryCreatesFromObjectWithToString() |
{ |
$r = new HasToString(); |
$s = Stream::factory($r); |
$this->assertInstanceOf('GuzzleHttp\Stream\Stream', $s); |
$this->assertEquals('foo', (string) $s); |
} |
|
public function testCreatePassesThrough() |
{ |
$s = Stream::factory('foo'); |
$this->assertSame($s, Stream::factory($s)); |
} |
|
/** |
* @expectedException \InvalidArgumentException |
*/ |
public function testThrowsExceptionForUnknown() |
{ |
Stream::factory(new \stdClass()); |
} |
} |
|
class HasToString |
{ |
public function __toString() { |
return 'foo'; |
} |
} |
/vendor/guzzlehttp/streams/tests/UtilsTest.php |
@@ -0,0 +1,159 @@ |
<?php |
namespace GuzzleHttp\Tests\Stream; |
|
use GuzzleHttp\Stream\FnStream; |
use GuzzleHttp\Stream\NoSeekStream; |
use GuzzleHttp\Stream\Stream; |
use GuzzleHttp\Stream\Utils; |
|
class UtilsTest extends \PHPUnit_Framework_TestCase |
{ |
public function testCopiesToString() |
{ |
$s = Stream::factory('foobaz'); |
$this->assertEquals('foobaz', Utils::copyToString($s)); |
$s->seek(0); |
$this->assertEquals('foo', Utils::copyToString($s, 3)); |
$this->assertEquals('baz', Utils::copyToString($s, 3)); |
$this->assertEquals('', Utils::copyToString($s)); |
} |
|
public function testCopiesToStringStopsWhenReadFails() |
{ |
$s1 = Stream::factory('foobaz'); |
$s1 = FnStream::decorate($s1, ['read' => function () { return ''; }]); |
$result = Utils::copyToString($s1); |
$this->assertEquals('', $result); |
} |
|
public function testCopiesToStringStopsWhenReadFailsWithMaxLen() |
{ |
$s1 = Stream::factory('foobaz'); |
$s1 = FnStream::decorate($s1, ['read' => function () { return ''; }]); |
$result = Utils::copyToString($s1, 10); |
$this->assertEquals('', $result); |
} |
|
public function testCopiesToStream() |
{ |
$s1 = Stream::factory('foobaz'); |
$s2 = Stream::factory(''); |
Utils::copyToStream($s1, $s2); |
$this->assertEquals('foobaz', (string) $s2); |
$s2 = Stream::factory(''); |
$s1->seek(0); |
Utils::copyToStream($s1, $s2, 3); |
$this->assertEquals('foo', (string) $s2); |
Utils::copyToStream($s1, $s2, 3); |
$this->assertEquals('foobaz', (string) $s2); |
} |
|
public function testStopsCopyToStreamWhenWriteFails() |
{ |
$s1 = Stream::factory('foobaz'); |
$s2 = Stream::factory(''); |
$s2 = FnStream::decorate($s2, ['write' => function () { return 0; }]); |
Utils::copyToStream($s1, $s2); |
$this->assertEquals('', (string) $s2); |
} |
|
public function testStopsCopyToSteamWhenWriteFailsWithMaxLen() |
{ |
$s1 = Stream::factory('foobaz'); |
$s2 = Stream::factory(''); |
$s2 = FnStream::decorate($s2, ['write' => function () { return 0; }]); |
Utils::copyToStream($s1, $s2, 10); |
$this->assertEquals('', (string) $s2); |
} |
|
public function testStopsCopyToSteamWhenReadFailsWithMaxLen() |
{ |
$s1 = Stream::factory('foobaz'); |
$s1 = FnStream::decorate($s1, ['read' => function () { return ''; }]); |
$s2 = Stream::factory(''); |
Utils::copyToStream($s1, $s2, 10); |
$this->assertEquals('', (string) $s2); |
} |
|
public function testReadsLines() |
{ |
$s = Stream::factory("foo\nbaz\nbar"); |
$this->assertEquals("foo\n", Utils::readline($s)); |
$this->assertEquals("baz\n", Utils::readline($s)); |
$this->assertEquals("bar", Utils::readline($s)); |
} |
|
public function testReadsLinesUpToMaxLength() |
{ |
$s = Stream::factory("12345\n"); |
$this->assertEquals("123", Utils::readline($s, 4)); |
$this->assertEquals("45\n", Utils::readline($s)); |
} |
|
public function testReadsLineUntilFalseReturnedFromRead() |
{ |
$s = $this->getMockBuilder('GuzzleHttp\Stream\Stream') |
->setMethods(['read', 'eof']) |
->disableOriginalConstructor() |
->getMock(); |
$s->expects($this->exactly(2)) |
->method('read') |
->will($this->returnCallback(function () { |
static $c = false; |
if ($c) { |
return false; |
} |
$c = true; |
return 'h'; |
})); |
$s->expects($this->exactly(2)) |
->method('eof') |
->will($this->returnValue(false)); |
$this->assertEquals("h", Utils::readline($s)); |
} |
|
public function testCalculatesHash() |
{ |
$s = Stream::factory('foobazbar'); |
$this->assertEquals(md5('foobazbar'), Utils::hash($s, 'md5')); |
} |
|
/** |
* @expectedException \GuzzleHttp\Stream\Exception\SeekException |
*/ |
public function testCalculatesHashThrowsWhenSeekFails() |
{ |
$s = new NoSeekStream(Stream::factory('foobazbar')); |
$s->read(2); |
Utils::hash($s, 'md5'); |
} |
|
public function testCalculatesHashSeeksToOriginalPosition() |
{ |
$s = Stream::factory('foobazbar'); |
$s->seek(4); |
$this->assertEquals(md5('foobazbar'), Utils::hash($s, 'md5')); |
$this->assertEquals(4, $s->tell()); |
} |
|
public function testOpensFilesSuccessfully() |
{ |
$r = Utils::open(__FILE__, 'r'); |
$this->assertInternalType('resource', $r); |
fclose($r); |
} |
|
/** |
* @expectedException \RuntimeException |
* @expectedExceptionMessage Unable to open /path/to/does/not/exist using mode r |
*/ |
public function testThrowsExceptionNotWarning() |
{ |
Utils::open('/path/to/does/not/exist', 'r'); |
} |
|
public function testProxiesToFactory() |
{ |
$this->assertEquals('foo', (string) Utils::create('foo')); |
} |
} |