From 7d73e604d14c1adef1ce391a6163dcee24c70d76 Mon Sep 17 00:00:00 2001 From: Larry Garfield Date: Thu, 15 Aug 2024 07:45:40 -0500 Subject: [PATCH 1/3] Open stream for reading when appropriate. --- src/StreamWrapper.php | 2 ++ tests/StreamWrapperTest.php | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/src/StreamWrapper.php b/src/StreamWrapper.php index aa19e8a..377d29c 100644 --- a/src/StreamWrapper.php +++ b/src/StreamWrapper.php @@ -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; } diff --git a/tests/StreamWrapperTest.php b/tests/StreamWrapperTest.php index d9657f7..0f5500d 100644 --- a/tests/StreamWrapperTest.php +++ b/tests/StreamWrapperTest.php @@ -281,3 +281,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); + ; +}); From 0a183174a5388af99c0b08596016da1120013d5a Mon Sep 17 00:00:00 2001 From: elazar Date: Thu, 15 Aug 2024 14:16:25 -0500 Subject: [PATCH 2/3] Refactor duplicated file creation logic in StreamWrapperTest into a helper function --- tests/StreamWrapperTest.php | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/tests/StreamWrapperTest.php b/tests/StreamWrapperTest.php index 0f5500d..4a8c8fd 100644 --- a/tests/StreamWrapperTest.php +++ b/tests/StreamWrapperTest.php @@ -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); @@ -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); @@ -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(); @@ -166,9 +170,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(); From 2d3a39676d65c24a0edefe03b9d4f39fc09c77e1 Mon Sep 17 00:00:00 2001 From: elazar Date: Thu, 15 Aug 2024 14:16:51 -0500 Subject: [PATCH 3/3] Add missing file creation to lock cases in StreamWrapperTest --- tests/StreamWrapperTest.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/StreamWrapperTest.php b/tests/StreamWrapperTest.php index 4a8c8fd..72791ff 100644 --- a/tests/StreamWrapperTest.php +++ b/tests/StreamWrapperTest.php @@ -127,6 +127,8 @@ function createFile(string $uri): void }); it('can acquire multiple shared locks', function () { + createFile('fly://foo'); + $stream1 = fopen('fly://foo', 'r'); $result = flock($stream1, LOCK_SH); expect($result)->toBeTrue(); @@ -156,6 +158,8 @@ function createFile(string $uri): void }); 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();