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>
This commit is contained in:
Lazizbek Ergashev
2026-11-26 18:10:43 +05:00
committed by GitHub
parent 1f60dc8f9a
commit 5bb349dcf2
5 changed files with 165 additions and 21 deletions
+37 -19
View File
@@ -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;
}
+2 -1
View File
@@ -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)
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 39 skipped, 1970 passed (4460 assertions)
@@ -0,0 +1,90 @@
<?php
declare(strict_types=1);
namespace Tests\Fixtures\Tia;
use Closure;
/**
* @internal
*/
final class CallForwardingFileWrapper
{
/**
* @var resource|null
*/
public mixed $context = null;
/**
* @var resource
*/
private mixed $resource;
public static function enable(): void
{
stream_wrapper_unregister('file');
stream_wrapper_register('file', self::class);
}
public static function disable(): void
{
stream_wrapper_restore('file');
}
public function stream_open(string $path, string $mode, int $options): bool
{
$resource = $this->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<int, mixed> $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();
}
}
}
+35
View File
@@ -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, "<?php\n\nreturn 1;\n");
$results = [];
try {
$links = tiaReadCaptureLinks(function () use ($source, $path, &$results): void {
$results['read'] = file_get_contents($source) !== '';
$results['include'] = require $path;
$results['scandir'] = in_array(basename($path), (array) scandir(__DIR__), true);
});
} finally {
@unlink($path);
}
expect($results)->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);
+1 -1
View File
@@ -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}")