定制 constup/vfs-testable 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

constup/vfs-testable

最新稳定版本:1.0.2

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

Supporting development

buymeacoffee.com ko-fi.com

统计信息

  • 总下载量: 3
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 7
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-04-27

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固