1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
<?php
namespace GuzzleHttp\Tests\Psr7;
use GuzzleHttp\Psr7;
use GuzzleHttp\Psr7\FnStream;
/**
* @covers GuzzleHttp\Psr7\FnStream
*/
class FnStreamTest extends BaseTest
{
public function testThrowsWhenNotImplemented()
{
$this->expectExceptionGuzzle('BadMethodCallException', 'seek() is not implemented in the FnStream');
(new FnStream([]))->seek(1);
}
public function testProxiesToFunction()
{
$s = new FnStream([
'read' => function ($len) {
$this->assertSame(3, $len);
return 'foo';
}
]);
$this->assertSame('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);
$this->assertTrue(true); // strict mode requires an assertion
}
public function testDecoratesStream()
{
$a = Psr7\Utils::streamFor('foo');
$b = FnStream::decorate($a, []);
$this->assertSame(3, $b->getSize());
$this->assertSame($b->isWritable(), true);
$this->assertSame($b->isReadable(), true);
$this->assertSame($b->isSeekable(), true);
$this->assertSame($b->read(3), 'foo');
$this->assertSame($b->tell(), 3);
$this->assertSame($a->tell(), 3);
$this->assertSame('', $a->read(1));
$this->assertSame($b->eof(), true);
$this->assertSame($a->eof(), true);
$b->seek(0);
$this->assertSame('foo', (string) $b);
$b->seek(0);
$this->assertSame('foo', $b->getContents());
$this->assertSame($a->getMetadata(), $b->getMetadata());
$b->seek(0, SEEK_END);
$b->write('bar');
$this->assertSame('foobar', (string) $b);
$this->assertInternalTypeGuzzle('resource', $b->detach());
$b->close();
}
public function testDecoratesWithCustomizations()
{
$called = false;
$a = Psr7\Utils::streamFor('foo');
$b = FnStream::decorate($a, [
'read' => function ($len) use (&$called, $a) {
$called = true;
return $a->read($len);
}
]);
$this->assertSame('foo', $b->read(3));
$this->assertTrue($called);
}
public function testDoNotAllowUnserialization()
{
$a = new FnStream([]);
$b = serialize($a);
$this->expectExceptionGuzzle('\LogicException', 'FnStream should never be unserialized');
unserialize($b);
}
}
|