Files
pest/tests/Fixtures/Tia/LoggingFileWrapper.php
T
Jeremy Braband 38c79452c1 fix(tia): link files a test reads as text, not only files it executes (#2006)
* fix(tia): link files a test reads as text, not only files it executes

* fix(tia): report dangling symlinks as links while reads are captured

`is_link()` stats with STREAM_URL_STAT_LINK | STREAM_URL_STAT_QUIET.
The `file_exists()` precheck in `ReadCapture::url_stat()` follows the
link, so a dangling symlink returned false before reaching `lstat()`,
and `is_link()` reported false while a test recorded.

Handle STREAM_URL_STAT_LINK first and always answer it through
`lstat()`. A quiet stat of a path that is neither a link nor a file
still returns false without a warning.

The regression test covers a link to an existing file and a dangling
link.

* test(tia): skip the read-capture feature tests without a coverage driver

The fixture runs record a graph from coverage, so with CI's
`coverage: none` all five cases failed. Skip them when
`Coverage::isAvailable()` is false, as CoveragePiggyback does.

* chore: style

* Refresh read capture test snapshots

---------

Co-authored-by: Punyapal Shah <53343069+MrPunyapal@users.noreply.github.com>
Co-authored-by: Punyapal Shah <mrpunyapal@gmail.com>
2026-11-15 00:54:24 +05:30

163 lines
3.2 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Fixtures\Tia;
/**
* @internal
*/
final class LoggingFileWrapper
{
private const int STREAM_OPEN_FOR_INCLUDE = 0x00000080;
/**
* @var array<int, array{path: string, including: bool}>
*/
public static array $opened = [];
/**
* @var resource|null
*/
public mixed $context = null;
/**
* @var resource
*/
private mixed $resource;
public static function enable(): void
{
self::$opened = [];
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
{
self::disable();
try {
$resource = @fopen($path, $mode, ($options & STREAM_USE_PATH) !== 0);
} finally {
stream_wrapper_unregister('file');
stream_wrapper_register('file', self::class);
}
if ($resource === false) {
return false;
}
self::$opened[] = ['path' => $path, 'including' => ($options & self::STREAM_OPEN_FOR_INCLUDE) !== 0];
$this->resource = $resource;
return true;
}
public function stream_read(int $count): string|false
{
return fread($this->resource, max(1, $count));
}
public function stream_write(string $data): int|false
{
return fwrite($this->resource, $data);
}
public function stream_eof(): bool
{
return feof($this->resource);
}
public function stream_close(): void
{
fclose($this->resource);
}
/**
* @return array<int|string, int>|false
*/
public function stream_stat(): array|false
{
return fstat($this->resource);
}
public function stream_set_option(): bool
{
return false;
}
/**
* @return array<int|string, int>|false
*/
public function url_stat(string $path): array|false
{
self::disable();
try {
return @stat($path);
} finally {
stream_wrapper_unregister('file');
stream_wrapper_register('file', self::class);
}
}
public function dir_opendir(string $path): bool
{
self::disable();
try {
$resource = @opendir($path);
} finally {
stream_wrapper_unregister('file');
stream_wrapper_register('file', self::class);
}
if ($resource === false) {
return false;
}
$this->resource = $resource;
return true;
}
public function dir_readdir(): string|false
{
return readdir($this->resource);
}
public function dir_rewinddir(): bool
{
rewinddir($this->resource);
return true;
}
public function dir_closedir(): bool
{
closedir($this->resource);
return true;
}
public function unlink(string $path): bool
{
self::disable();
try {
return @unlink($path);
} finally {
stream_wrapper_unregister('file');
stream_wrapper_register('file', self::class);
}
}
}