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