我尝试模拟文件创建但没有任何成功。该文件是在函数中硬创建的,没有依赖关系,只有文件名作为参数。
有没有办法从 vfs 文件系统覆盖真实的文件系统?我有一个模拟此类的解决方案File
,但我更喜欢保留原始解决方案。
测试类:
class FileWorkflow
{
public function moveAttachmentsFiles(string $filename): void
{
$file = new File(sys_get_temp_dir() . '/' . $filename);
return;
}
}
单元测试:
class FileWorkflowTest extends TestCase
{
private FileWorkflow $fileWorkflow;
/** @var ObjectProphecy | File */
private ObjectProphecy $file;
protected function setUp(): void
{
$this->file = $this->prophesize(File::class);
$this->fileWorkflow = new FileWorkflow();
$tmp = vfsStream::newDirectory('/tmp')
->at(vfsStream::setup('/'))
;
$file = vfsStream::newFile('unit-1')
->at($tmp)
;
// file exists in vfs filesystem
}
public function testMoveAttachmentsFiles(/*array $attachments*/): void
{
$this->file
->getUuid()
->shouldBeCalled()
->willReturn('unit-1', 'unit-2', 'unit-3');
$this->fileWorkflow->moveAttachmentsFiles([$this->file->reveal()], '');
// Error "file does not exists" because tested function code is not opening file from vfs filesystem.
}
}