constup/vfs-testable
Composer 安装命令:
composer require constup/vfs-testable
包简介
Interface and implementation of PHP's built-in functions that do not support vfsStream.
README 文档
README
Description
Interface and implementation of PHP’s built-in functions that do not support vfsStream.
vfsStream works by registering a stream wrapper (vfs://). Any function that bypasses PHP’s stream layer and calls the
OS directly will fail or return wrong results. This library provides a wrapper around affected functions for easier unit
testing.
Installation
Constup\VfsTestable is supposed to be used in your production code as a wrapper around affected functions. It must be
installed as your project's dependency (not dev dependency).
composer require constup/vfs-testable
Use
Instead of calling an unsupported built-in function directly, use the wrapper from this library.
Original code:
<?php
declare(strict_types=1);
namespace Constup\VfsTestableExamples;
readonly class MyClass
{
public function myMethod(string $path): bool {
$_path = realpath($path);
return file_exists($_path . DIRECTORY_SEPARATOR . 'some_file.txt');
}
}
With VfsTestable:
<?php
declare(strict_types=1);
namespace Constup\VfsTestableExamples;
use Constup\VfsTestable\VfsTestableInterface;
readonly class MyClassVfsTestable
{
public function __construct(
private VfsTestableInterface $vfsTestable
) {}
public function myMethod(string $path): bool {
$_path = $this->vfsTestable->realpath($path);
return file_exists($_path . DIRECTORY_SEPARATOR . 'some_file.txt');
}
}
You can then mock it using PHP Unit:
<?php
declare(strict_types = 1);
namespace Constup\VfsTestable\Tests;
use Constup\VfsTestable\VfsTestableInterface;
use Constup\VfsTestableExamples\MyClassVfsTestable;
use org\bovigo\vfs\vfsStream;
use PHPUnit\Framework\TestCase;
class MyClassVfsTestableTest extends TestCase
{
/**
* This test shows that we are still using `vfsStream` file system for other functions, like `file_exists()` while
* mocking `realpath()` using `VfsTestableInterface`.
*/
public function test_myMethod_vfsStream_FileExists(): void
{
$root = vfsStream::setup('root', null, [
'some' => [
'path' => [
'some_file.txt' => 'some content',
],
],
]);
$path = vfsStream::url('root/some/path');
$realpathMockResult = $root->url() . '/some/path';
$vfsTestableMock = $this->createMock(VfsTestableInterface::class);
$vfsTestableMock
->expects($this->once())
->method('realpath')
->with($path)
->willReturn($realpathMockResult);
$myClassVfsTestable = new MyClassVfsTestable($vfsTestableMock);
$this->assertTrue($myClassVfsTestable->myMethod($path));
}
}
If you want to run or debug the example, clone this repository and take a look at the examples directory
and its tests. Note that tests are excluded from the composer library and are not
available in your vendor directory.
License
MIT License
- License file: LICENSE.txt
- Online version: https://opensource.org/license/mit
Supporting development
constup/vfs-testable 适用场景与选型建议
constup/vfs-testable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 04 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 constup/vfs-testable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 constup/vfs-testable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 29
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-04-27
