file_put_contents 进入 vfsstream 引发错误“无法打开流:”
file_put_contents into vfsstream raises error `failed to open stream:`
我想编写单元测试,用 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命令作为示例,它应该来自我要测试的class。不幸的是我收到一个错误:
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 )
我自己发现的:我存错了url,忘记了root-dir
更改它以达到目的。有时最琐碎的事情是最讨厌的。
file_put_contents($this->vfs->url() . "/root/data/newFile.html", "stuff");
我想编写单元测试,用 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命令作为示例,它应该来自我要测试的class。不幸的是我收到一个错误:
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 )
我自己发现的:我存错了url,忘记了root-dir
更改它以达到目的。有时最琐碎的事情是最讨厌的。
file_put_contents($this->vfs->url() . "/root/data/newFile.html", "stuff");