我想写 UnitTests,用 vfs 模拟文件系统。
我的测试如下所示:
<?php
use org\bovigo\vfs\vfsStreamContent;
use PHPUnit\Framework\TestCase;
use org\bovigo\vfs\vfsStream;
class MyClassTest extends TestCase {
private $vfs;
function setUp() {
ini_set("allow_url_fopen", true);
$this->vfs = vfsStream::setup('root', 777);
$sampledataDir = vfsStream::newDirectory('sampledata', 777)->at($this->vfs);
vfsStream::copyFromFileSystem(realpath(__DIR__ . '/../sampledata'), $sampledataDir);
$dataDir = vfsStream::newDirectory('data', 777)->at($this->vfs);
}
function tearDown() {
unset($this->vfs);
}
function testSetup() {
file_put_contents($this->vfs->url() . "/data/newFile.html", "stuff");
}
}
file_put_contents 命令作为示例插入此处,它应该来自我要测试的类。不幸的是,我收到一个错误:
file_put_contents(vfs://root/data/newFile.html): failed to open stream: "org\bovigo\vfs\vfsStreamWrapper::stream_open" call failed
为什么?我究竟做错了什么?
来自 composer.json
"require-dev": {
"mikey179/vfsstream": "^1.6",
"phpunit/phpunit": "^6.5.0"
}
附加信息:
php --version
PHP 7.3.11-1~deb10u1 (cli) (built: Oct 26 2019 14:14:18) ( NTS )