From 5bb349dcf27282a3c4ae797ca17ae406fb9ace40 Mon Sep 17 00:00:00 2001 From: Lazizbek Ergashev Date: Thu, 26 Nov 2026 18:10:43 +0500 Subject: [PATCH] fix(tia): forward ReadCapture calls to wrappers that use __call (#2078) * fix(tia): forward ReadCapture calls to wrappers that use __call * fix(tia): open through a __call wrapper too --------- Co-authored-by: Punyapal Shah <53343069+MrPunyapal@users.noreply.github.com> --- src/Plugins/Tia/ReadCapture.php | 56 ++++++++---- tests/.snapshots/success.txt | 3 +- .../Tia/CallForwardingFileWrapper.php | 90 +++++++++++++++++++ tests/Unit/Plugins/Tia/ReadCapture.php | 35 ++++++++ tests/Visual/Parallel.php | 2 +- 5 files changed, 165 insertions(+), 21 deletions(-) create mode 100644 tests/Fixtures/Tia/CallForwardingFileWrapper.php diff --git a/src/Plugins/Tia/ReadCapture.php b/src/Plugins/Tia/ReadCapture.php index 7380f981..e363f5fb 100644 --- a/src/Plugins/Tia/ReadCapture.php +++ b/src/Plugins/Tia/ReadCapture.php @@ -127,16 +127,22 @@ final class ReadCapture public function dir_closedir(): bool { - closedir($this->resource()); + $this->forward('dir_closedir', function (mixed $resource): bool { + closedir($resource); + + return true; + }); return true; } - public function dir_opendir(string $path): bool + public function dir_opendir(string $path, int $options): bool { - $handle = $this->withOriginalWrapper(fn () => opendir($path, $this->context)); + $handle = $this->withOriginalWrapper(fn (): mixed => self::$previousWrapper === null + ? opendir($path, $this->context) + : $this->openDirectoryThroughPreviousWrapper(self::$previousWrapper, $path, $options)); - if (! is_resource($handle)) { + if ($handle === false) { return false; } @@ -147,14 +153,18 @@ final class ReadCapture public function dir_readdir(): string|false { - return readdir($this->resource()); + $entry = $this->forward('dir_readdir', fn (mixed $resource): string|false => readdir($resource)); + + return is_string($entry) ? $entry : false; } public function dir_rewinddir(): bool { - rewinddir($this->resource()); + return $this->forward('dir_rewinddir', function (mixed $resource): bool { + rewinddir($resource); - return true; + return true; + }) === true; } public function mkdir(string $path, int $mode, int $options): bool @@ -320,7 +330,25 @@ final class ReadCapture $wrapper->context = $this->context; } - if (! method_exists($wrapper, 'stream_open') || $wrapper->stream_open($path, $mode, $options, $openedPath) !== true) { + if (! is_callable([$wrapper, 'stream_open']) || $wrapper->stream_open($path, $mode, $options, $openedPath) !== true) { + return false; + } + + return $wrapper; + } + + /** + * @param class-string $wrapperClass + */ + private function openDirectoryThroughPreviousWrapper(string $wrapperClass, string $path, int $options): object|false + { + $wrapper = new $wrapperClass; + + if (property_exists($wrapper, 'context')) { + $wrapper->context = $this->context; + } + + if (! is_callable([$wrapper, 'dir_opendir']) || $wrapper->dir_opendir($path, $options) !== true) { return false; } @@ -336,16 +364,6 @@ final class ReadCapture return str_contains($mode, 'r') || str_contains($mode, '+'); } - /** - * @return resource - */ - private function resource(): mixed - { - assert(is_resource($this->handle)); - - return $this->handle; - } - /** * @param Closure(resource): mixed $native */ @@ -355,7 +373,7 @@ final class ReadCapture return $native($this->handle); } - if (! method_exists($this->handle, $method)) { + if (! is_callable([$this->handle, $method])) { return false; } diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 8a79c0a1..e5ce6cbf 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -2147,6 +2147,7 @@ ✓ a wrapper already registered for file:// → it opens reads through it and still links them ✓ a wrapper already registered for file:// → it keeps the include flag for the files a test includes ✓ a wrapper already registered for file:// → it is registered again after the test + ✓ a __call wrapper already registered for file:// → it passes reads, includes and directory listings through it ✓ withoutReadCapture() → it does not link a file matching an excluded glob ✓ withoutReadCapture() → it still links a file outside the excluded globs ✓ withoutReadCapture() → it keeps the file wrapper registered while globs are excluded @@ -2680,4 +2681,4 @@ ✓ pass with dataset with ('my-datas-set-value') ✓ within describe → pass with dataset with ('my-datas-set-value') - Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 39 skipped, 1969 passed (4458 assertions) \ No newline at end of file + Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 39 skipped, 1970 passed (4460 assertions) \ No newline at end of file diff --git a/tests/Fixtures/Tia/CallForwardingFileWrapper.php b/tests/Fixtures/Tia/CallForwardingFileWrapper.php new file mode 100644 index 00000000..fbe3cfee --- /dev/null +++ b/tests/Fixtures/Tia/CallForwardingFileWrapper.php @@ -0,0 +1,90 @@ +native(fn (): mixed => @fopen($path, $mode, ($options & STREAM_USE_PATH) !== 0)); + + if ($resource === false) { + return false; + } + + $this->resource = $resource; + + return true; + } + + public function dir_opendir(string $path): bool + { + $resource = $this->native(fn (): mixed => @opendir($path)); + + if ($resource === false) { + return false; + } + + $this->resource = $resource; + + return true; + } + + /** + * @param array $arguments + */ + public function __call(string $method, array $arguments): mixed + { + return match ($method) { + 'stream_read' => fread($this->resource, max(1, (int) $arguments[0])), + 'stream_write' => fwrite($this->resource, (string) $arguments[0]), + 'stream_eof' => feof($this->resource), + 'stream_close' => fclose($this->resource), + 'stream_stat' => fstat($this->resource), + 'dir_readdir' => readdir($this->resource), + 'dir_closedir' => closedir($this->resource) ?? true, + 'url_stat' => $this->native(fn (): mixed => @stat((string) $arguments[0])), + 'unlink' => $this->native(fn (): bool => @unlink((string) $arguments[0])), + default => false, + }; + } + + private function native(Closure $callback): mixed + { + self::disable(); + + try { + return $callback(); + } finally { + self::enable(); + } + } +} diff --git a/tests/Unit/Plugins/Tia/ReadCapture.php b/tests/Unit/Plugins/Tia/ReadCapture.php index a2e7d829..90e4bb83 100644 --- a/tests/Unit/Plugins/Tia/ReadCapture.php +++ b/tests/Unit/Plugins/Tia/ReadCapture.php @@ -7,6 +7,7 @@ use Pest\Plugins\Tia\ReadCapture; use Pest\Plugins\Tia\Recorder; use Pest\Plugins\Tia\WatchPatterns; use Pest\Support\Container; +use Tests\Fixtures\Tia\CallForwardingFileWrapper; use Tests\Fixtures\Tia\LoggingFileWrapper; /** @@ -403,6 +404,40 @@ describe('a wrapper already registered for file://', function (): void { }); }); +describe('a __call wrapper already registered for file://', function (): void { + beforeEach(function (): void { + CallForwardingFileWrapper::enable(); + }); + + afterEach(function (): void { + CallForwardingFileWrapper::disable(); + }); + + it('passes reads, includes and directory listings through it', function (): void { + $source = (string) realpath(dirname(__DIR__, 4).'/src/Plugins/Tia/ReadCapture.php'); + $path = __DIR__.'/read-capture-'.bin2hex(random_bytes(8)).'.php'; + file_put_contents($path, "toBe([ + 'read' => true, + 'include' => 1, + 'scandir' => true, + ])->and($links)->toBe([$source]); + }); +}); + describe('withoutReadCapture()', function (): void { beforeEach(function (): void { Container::getInstance()->add(WatchPatterns::class, new WatchPatterns); diff --git a/tests/Visual/Parallel.php b/tests/Visual/Parallel.php index e522f00b..51a766d7 100644 --- a/tests/Visual/Parallel.php +++ b/tests/Visual/Parallel.php @@ -21,7 +21,7 @@ test('parallel', function () use ($run): void { $output = implode("\n", array_slice(explode("\n", (string) $output), -10)); $profileOutput = $run('tests/Fixtures/Suites/SuccessOnly.php', '--profile'); - $expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 31 skipped, 1842 passed (3933 assertions)'; + $expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 31 skipped, 1843 passed (3935 assertions)'; expect($output) ->toContain("Tests: {$expected}")