Skip to content

Commit

Permalink
Merge pull request #14 from Crell/stream-open
Browse files Browse the repository at this point in the history
Open stream for reading when appropriate.
  • Loading branch information
elazar committed Aug 15, 2024
2 parents 13bf51e + 2d3a396 commit a98c791
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/StreamWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ public function stream_open(
if (strpbrk($mode, 'waxc') !== false) {
$this->stream_write('');
$this->stream_flush();
} elseif (strpbrk($mode, 'r') !== false) {
$this->openRead();
}
return true;
}
Expand Down
33 changes: 25 additions & 8 deletions tests/StreamWrapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
use Monolog\Logger;
use Psr\Log\LoggerInterface;

function createFile(string $uri): void
{
$result = touch($uri);
expect($result)->toBeTrue();
expect(file_exists($uri))->toBeTrue();
}

beforeEach(function () {
$serviceLocator = new ServiceLocator();
ServiceLocator::setInstance($serviceLocator);
Expand Down Expand Up @@ -45,8 +52,7 @@
});

it('can copy an empty file', function () {
$success = touch('fly://src');
expect($success)->toBe(true);
createFile('fly://src');

$success = copy('fly://src', 'fly://dst');
expect($success)->toBe(true);
Expand Down Expand Up @@ -98,9 +104,7 @@
});

it('can rename an existing file', function () {
$result = touch('fly://foo');
expect($result)->toBeTrue();
expect(file_exists('fly://foo'))->toBeTrue();
createFile('fly://foo');
$result = rename('fly://foo', 'fly://bar');
expect($result)->toBeTrue();
clearstatcache();
Expand All @@ -123,6 +127,8 @@
});

it('can acquire multiple shared locks', function () {
createFile('fly://foo');

$stream1 = fopen('fly://foo', 'r');
$result = flock($stream1, LOCK_SH);
expect($result)->toBeTrue();
Expand Down Expand Up @@ -152,6 +158,8 @@
});

it('cannot acquire an exclusive lock with existing locks', function () {
createFile('fly://foo');

$stream1 = fopen('fly://foo', 'r');
$result = flock($stream1, LOCK_SH);
expect($result)->toBeTrue();
Expand All @@ -166,9 +174,7 @@
});

it('does not support operations to change owner, group, or access', function () {
$result = touch('fly://foo');
expect($result)->toBeTrue();
expect(file_exists('fly://foo'))->toBeTrue();
createFile('fly://foo');

$result = chmod('fly://foo', 0755);
expect($result)->toBeFalse();
Expand Down Expand Up @@ -281,3 +287,14 @@

expect($actual)->toBe($expected);
});

it('can stat a file', function () {
$result = file_put_contents('fly://foo', 'foobar');
expect(file_exists('fly://foo'))->toBeTrue();

$stream = fopen('fly://foo', 'r');
$metadata = \stream_get_meta_data($stream);
expect($metadata['uri'])->toEqual('fly://foo');
fclose($stream);
;
});

0 comments on commit a98c791

Please sign in to comment.