mirror of
https://github.com/shufflingpixels/php-io.git
synced 2026-06-16 05:04:59 +02:00
Initial commit
This commit is contained in:
commit
c677cfccd4
23 changed files with 5553 additions and 0 deletions
68
tests/Unit/ResourceTest.php
Normal file
68
tests/Unit/ResourceTest.php
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
<?php
|
||||
|
||||
use Shufflingpixels\IO\Exception\IOException;
|
||||
use Shufflingpixels\IO\Resource;
|
||||
|
||||
it('reads, writes, seeks and reports stream state', function () {
|
||||
$handle = fopen('php://temp', 'r+');
|
||||
fwrite($handle, 'hello');
|
||||
rewind($handle);
|
||||
|
||||
$stream = new class($handle, true, true, true) extends Resource {
|
||||
public function __construct($resource, bool $seekable, bool $readable, bool $writeable)
|
||||
{
|
||||
parent::__construct($resource, $seekable, $readable, $writeable);
|
||||
}
|
||||
};
|
||||
|
||||
expect($stream->length())->toBe(5)
|
||||
->and($stream->tell())->toBe(0)
|
||||
->and($stream->isSeekable())->toBeTrue()
|
||||
->and($stream->isReadable())->toBeTrue()
|
||||
->and($stream->isWriteable())->toBeTrue()
|
||||
->and($stream->read(2))->toBe('he');
|
||||
|
||||
$stream->seek(0);
|
||||
expect($stream->write('H'))->toBe(1);
|
||||
|
||||
$stream->seek(0);
|
||||
expect($stream->read(5))->toBe('Hello')
|
||||
->and($stream->length())->toBe(5)
|
||||
->and($stream->eof())->toBeFalse();
|
||||
|
||||
$stream->read(1);
|
||||
expect($stream->eof())->toBeTrue();
|
||||
|
||||
$stream->close();
|
||||
});
|
||||
|
||||
it('throws when seeking or getting length on non-seekable stream', function () {
|
||||
$handle = fopen('php://temp', 'r+');
|
||||
|
||||
$stream = new class($handle, false, true, false) extends Resource {
|
||||
public function __construct($resource, bool $seekable, bool $readable, bool $writeable)
|
||||
{
|
||||
parent::__construct($resource, $seekable, $readable, $writeable);
|
||||
}
|
||||
};
|
||||
|
||||
expect(fn () => $stream->length())->toThrow(IOException::class)
|
||||
->and(fn () => $stream->seek(0))->toThrow(IOException::class);
|
||||
|
||||
$stream->close();
|
||||
});
|
||||
|
||||
it('throws when writing to a non-writeable stream', function () {
|
||||
$handle = fopen('php://temp', 'r+');
|
||||
|
||||
$stream = new class($handle, true, true, false) extends Resource {
|
||||
public function __construct($resource, bool $seekable, bool $readable, bool $writeable)
|
||||
{
|
||||
parent::__construct($resource, $seekable, $readable, $writeable);
|
||||
}
|
||||
};
|
||||
|
||||
expect(fn () => $stream->write('x'))->toThrow(IOException::class, 'not writeable');
|
||||
|
||||
$stream->close();
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue