在项目开发过程中,特别是涉及到文件操作时,编写可靠的单元测试至关重要。Symfony Filesystem 组件提供了强大的文件系统操作能力,但对于测试来说,仍然存在一些痛点,比如临时目录的管理、文件的清理等等。
fidry/filesystem
fidry/filesystem
FileSystem
FileSystem
FS
FS
FileSystemTestCase
使用 Composer 安装
fidry/filesystem
<pre class="brush:php;toolbar:false;">composer require fidry/filesystem
下面是一个使用
FileSystemTestCase
<pre class="brush:php;toolbar:false;"><?php declare(strict_types=1);
namespace App;
use Fidry\FileSystem\FS;
use Fidry\FileSystem\Test\FileSystemTestCase;
use PHPUnit\Framework\Attributes\CoversClass;
use Symfony\Component\Finder\Finder;
use function getenv;
use function is_string;
final class MyAppFileSystemTest extends FileSystemTestCase
{
public static function getTmpDirNamespace(): string
{
// This is to make it thread safe with Infection. If you are not using
// infection or do not need thread safety, this can return a constant
// string, e.g. your project/library name.
$threadId = getenv('TEST_TOKEN');
if (!is_string($threadId)) {
$threadId = '';
}
return 'MyApp'.$threadId;
}
public function test_it_works(): void
{
// This file is dumped into a temporary directory. Here
// something like '/private/var/folders/p3/lkw0cgjj2fq0656q_9rd0mk80000gn/T/MyApp/MyAppFileSystemTest10000'
// on OSX.
FS::dumpFile('file1', '');
$files = Finder::create()
->files()
->in($this->tmp);
self::assertSame(['file1'], $this->normalizePaths($files));
}
}在这个例子中,
FileSystemTestCase
FS::dumpFile()
fidry/filesystem
fidry/filesystem
以上就是轻松搞定文件操作测试!fidry/filesystem让你的SymfonyFilesystem更强大的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号