mirror of
https://github.com/shufflingpixels/php-io.git
synced 2026-06-16 05:04:59 +02:00
stream: adopt PSR-7 StreamInterface
This commit is contained in:
parent
8d8690f77b
commit
89525294de
12 changed files with 250 additions and 152 deletions
|
|
@ -1,13 +1,11 @@
|
|||
<?php
|
||||
|
||||
use Shufflingpixels\IO\Buffer;
|
||||
use Shufflingpixels\IO\Exception\EndOfStreamException;
|
||||
use Shufflingpixels\IO\SeekMode;
|
||||
|
||||
it('reads, seeks and tracks position', function () {
|
||||
$buffer = new Buffer('abcdef');
|
||||
|
||||
expect($buffer->length())->toBe(6)
|
||||
expect($buffer->getSize())->toBe(6)
|
||||
->and($buffer->tell())->toBe(0)
|
||||
->and($buffer->remaining())->toBe(6)
|
||||
->and($buffer->eof())->toBeFalse();
|
||||
|
|
@ -16,7 +14,7 @@ it('reads, seeks and tracks position', function () {
|
|||
->and($buffer->tell())->toBe(2)
|
||||
->and($buffer->remaining())->toBe(4);
|
||||
|
||||
$buffer->seek(-1, SeekMode::END);
|
||||
$buffer->seek(-1, SEEK_END);
|
||||
|
||||
expect($buffer->tell())->toBe(5)
|
||||
->and($buffer->read(1))->toBe('f')
|
||||
|
|
@ -27,7 +25,7 @@ it('supports relative seek modes', function () {
|
|||
$buffer = new Buffer('abcdef');
|
||||
|
||||
$buffer->seek(3);
|
||||
$buffer->seek(-2, SeekMode::CUR);
|
||||
$buffer->seek(-2, SEEK_CUR);
|
||||
|
||||
expect($buffer->tell())->toBe(1)
|
||||
->and($buffer->read(2))->toBe('bc');
|
||||
|
|
@ -45,10 +43,11 @@ it('throws for negative read length', function () {
|
|||
expect(fn () => $buffer->read(-1))->toThrow(InvalidArgumentException::class);
|
||||
});
|
||||
|
||||
it('throws when reading beyond end of stream', function () {
|
||||
it('returns available bytes when reading beyond end of stream', function () {
|
||||
$buffer = new Buffer('abc');
|
||||
|
||||
expect(fn () => $buffer->read(4))->toThrow(EndOfStreamException::class);
|
||||
expect($buffer->read(4))->toBe('abc')
|
||||
->and($buffer->eof())->toBeTrue();
|
||||
});
|
||||
|
||||
it('writes at current position and updates contents', function () {
|
||||
|
|
@ -68,5 +67,14 @@ it('writes nothing for empty payload', function () {
|
|||
|
||||
expect($buffer->write(''))->toBe(0)
|
||||
->and($buffer->tell())->toBe(0)
|
||||
->and($buffer->length())->toBe(3);
|
||||
->and($buffer->getSize())->toBe(3);
|
||||
});
|
||||
|
||||
it('returns remaining contents and advances cursor', function () {
|
||||
$buffer = new Buffer('abcdef');
|
||||
$buffer->seek(2);
|
||||
|
||||
expect($buffer->getContents())->toBe('cdef')
|
||||
->and($buffer->tell())->toBe(6)
|
||||
->and($buffer->getContents())->toBe('');
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue