shopware - Shopware 6:如何将 shopware 单元测试输出从夹具路由到原始文件夹?
问题描述
我正在为从公共目录下的下载文件夹中获取文件的功能构建单元测试。但在单元测试中,我使用夹具作为测试文件路径。我如何在我的常用功能 beetween 夹具和原始 pub 目录中模拟目录?
解决方案
看看这个核心测试:\Shopware\CI\Test\Service\ReleasePrepareServiceTest::setUp
他们使用以下代码来模拟文件夹内容:
public function setUp(): void
{
$this->artifactsFilesystem = new Filesystem(new MemoryAdapter());
[...]
$this->artifactsFilesystem->put('install.zip', random_bytes(1024 * 1024 * 2 + 11));
$this->artifactsFilesystem->put('install.tar.xz', random_bytes(1024 + 11));
$this->artifactsFilesystem->put('update.zip', random_bytes(1024 * 1024 + 13));
}
单元测试在这里创建一些内存文件系统,并用一些随机数据的文件填充它。
我通过检查Filesystem
Shopware 的测试文件夹中的类的用法发现了这一点。
该文件系统可以注入到您正在测试的服务或代码中。
例如:
public function setUp(): void
{
$this->myMockFileSystem = new Filesystem(new MemoryAdapter());
$this->myMockFileSystem->put('file_we_need_in_the_test.pdf', random_bytes(1024 * 1024 * 2 + 11));
}
public function testSomething(): void
{
$service = new MyService($this->myMockFilesystem);
$this->assertEquals('some result', $service->doSomething());
}