带 vfsStream 的文件系统迭代器

FilesystemIterator with vfsStream

我正在使用(学习)vfsStream 在包含 23,000 个项目的目录树上测试文件系统操作。

这是我在 PHPUnit 测试中尝试做的事情:

    public function testMockFS() {
        $buffer = (array) json_decode(file_get_contents(__DIR__ . '/resources/structure.json'));
        $root = vfsStream::setup('/',null,$buffer);

        $it = new FilesystemIterator($root);
        foreach ($it as $fileInfo) {
            echo $fileInfo->getFilename() . "\n";
    }

}

注意:我知道这不会测试任何东西。现在,我只是想让迭代工作,所以我想在开始编写测试之前将目录结构打印到屏幕上。

structure.json 是来自生产的转储,其中包含文件系统的完整复制(没有文件内容,这与我们正在处理的内容无关)。

vfsStream::setup() 似乎工作正常。没有错误。

如何使用 FilesystemIterator 迭代 vfsSystem?我觉得要么 1) $root 应该 return FilesystemIterator 可以使用的 vfs 中的一个目录,要么 b) 我应该传递 "vfs::/" 作为参数,但是 FilesystemIterator 不知道什么 "vfs::"事情是。

我确信我正在使这比它必须的更难。提前致谢。

我明白了。你必须使用 vfsStream::url('/path/to/directory/you/want');

public function testMockFS() {
    $buffer = (array) json_decode(file_get_contents(__DIR__ . '/resources/structure.json'));
    $root = vfsStream::setup('/',null,$buffer);

    $it = new FilesystemIterator(vfsStream::url('/'));
    foreach ($it as $fileInfo) {
        echo $fileInfo->getFilename() . "\n";
}

}