Создание макета файла (созданного с жесткой зависимостью)
Я безуспешно пытаюсь смоделировать создание файла. Файл жестко создан в функции, и нет никакой зависимости, только имя файла в качестве аргумента.
Есть ли способ переопределить реальную файловую систему из файловой системы vfs? У меня есть решение, которое насмехается над этим классом
Протестированный класс:
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.
}
}