Dev*_*DOS 6 php phpunit unit-testing symfony vfs-stream
我想使用 vfsstream
class MyClass{
public function createFile($dirPath)
{
$name = time() . "-RT";
$file = $dirPath . '/' . $name . '.tmp';
fopen($file, "w+");
if (file_exists($file)) {
return $name . '.tmp';
} else {
return '';
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试测试文件创建时:
$filename = $myClass->createFile(vfsStream::url('/var/www/app/web/exported/folder'));
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
无法打开流:“org\bovigo\vfs\vfsStreamWrapper::stream_open”调用失败 fopen(vfs://var/www/app/web/exported/folder)
我看到这个问题在谈论模拟文件系统,但它没有关于文件创建的信息。vfsstream 是否支持使用 fopen 函数创建文件?如何测试文件创建?
尝试使用设置创建 vsf 流,例如:
$root = vfsStream::setup('root');
$filename = $myClass->createFile($root->url());
Run Code Online (Sandbox Code Playgroud)
希望这有帮助
作为工作示例:
/**
* @test
* @group unit
*/
public function itShouldBeTested()
{
$myClass = new MyClass();
$root = vfsStream::setup('root');
$filename = $myClass->createFile($root->url());
$this->assertNotNull($filename);
var_dump($filename);
}
Run Code Online (Sandbox Code Playgroud)
这将产生
(dev) bash-4.4$ phpunit -c app --filter=itShouldBeTested PHPUnit 4.8.26 由 Sebastian Bergmann 和贡献者编写。
.string(17) "1498555092-RT.tmp"
时间:13.11秒,内存:200.00MB
OK(1 个测试,1 个断言)
| 归档时间: |
|
| 查看次数: |
1800 次 |
| 最近记录: |