mirror of
https://github.com/shufflingpixels/php-io.git
synced 2026-10-17 13:36:46 +02:00
Adopt Go-style minimal interfaces for streams
Replace PSR-7 StreamInterface with a set of small, composable interfaces (ReaderInterface, WriterInterface, SeekerInterface and their combinations). BinaryReader now depends only on ReaderInterface, BinaryWriter on WriterInterface, making both easy to satisfy with any byte source or sink. Buffer and Resource are simplified — no capability flags, no detach/close lifecycle, no PSR-7 metadata methods. LimitedResource is replaced by LimitedReader, a lightweight reader that enforces a byte count limit. BinaryReader::read() is renamed to readExact() for clarity. Tests updated throughout to match the new APIs.
This commit is contained in:
parent
a27c0a87ea
commit
07ec405512
25 changed files with 451 additions and 1168 deletions
|
|
@ -5,14 +5,12 @@ use Shufflingpixels\IO\Buffer;
|
|||
it('reads, seeks and tracks position', function () {
|
||||
$buffer = new Buffer('abcdef');
|
||||
|
||||
expect($buffer->getSize())->toBe(6)
|
||||
expect($buffer->length())->toBe(6)
|
||||
->and($buffer->tell())->toBe(0)
|
||||
->and($buffer->remaining())->toBe(6)
|
||||
->and($buffer->eof())->toBeFalse();
|
||||
|
||||
expect($buffer->read(2))->toBe('ab')
|
||||
->and($buffer->tell())->toBe(2)
|
||||
->and($buffer->remaining())->toBe(4);
|
||||
->and($buffer->tell())->toBe(2);
|
||||
|
||||
$buffer->seek(-1, SEEK_END);
|
||||
|
||||
|
|
@ -50,6 +48,13 @@ it('returns available bytes when reading beyond end of stream', function () {
|
|||
->and($buffer->eof())->toBeTrue();
|
||||
});
|
||||
|
||||
it('returns false when reading at end of stream', function () {
|
||||
$buffer = new Buffer('a');
|
||||
$buffer->read(1);
|
||||
|
||||
expect($buffer->read(1))->toBeFalse();
|
||||
});
|
||||
|
||||
it('writes at current position and updates contents', function () {
|
||||
$buffer = new Buffer('abcdef');
|
||||
$buffer->seek(2);
|
||||
|
|
@ -67,14 +72,15 @@ it('writes nothing for empty payload', function () {
|
|||
|
||||
expect($buffer->write(''))->toBe(0)
|
||||
->and($buffer->tell())->toBe(0)
|
||||
->and($buffer->getSize())->toBe(3);
|
||||
->and($buffer->length())->toBe(3);
|
||||
});
|
||||
|
||||
it('returns remaining contents and advances cursor', function () {
|
||||
it('reads remaining bytes from cursor to end', function () {
|
||||
$buffer = new Buffer('abcdef');
|
||||
$buffer->seek(2);
|
||||
|
||||
expect($buffer->getContents())->toBe('cdef')
|
||||
->and($buffer->tell())->toBe(6)
|
||||
->and($buffer->getContents())->toBe('');
|
||||
$remaining = $buffer->read($buffer->length() - $buffer->tell());
|
||||
|
||||
expect($remaining)->toBe('cdef')
|
||||
->and($buffer->eof())->toBeTrue();
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue