首页 > 解决方案 > phpunit test,写一个文件上传和覆盖的测试

问题描述

我的 PHP 应用程序中有一个方法可以上传文件,如果 mime 类型是图像,我们希望将该图像转换为 PDF,该功能正在运行,但我想为它编写一个单元测试,我有一个部分测试,但它只测试原始文件的上传和存储,它不测试最终的转换。谁能向我解释我将如何去做,我有点迷茫,我对 TDD 还很陌生。

$path = $request->file('certificate')->store('verification');
$newPath = \Storage::disk('local')->path($path);

if($request->file('certificate')->getMimeType() === "image") {
   $pdf = new Fpdf();
   $pdf->AddPage();
   $pdf->Image($newPath, 0, 0, -300);
   $newPath = \Storage::disk('local')->path('verification/' . $newPath->hashName() . '.pdf');
   $pdf->Output('F', $newPath);
    }

当前测试:

public function testVerificationUpload()
{
    Storage::fake('local');

    $file = UploadedFile::fake()->image('avatar.jpg');

    $response = $this->post('/verify', ['certificate' => $file]);

    // Assert the file was stored...
    Storage::disk('local')->assertExists('verification/' . $file->hashName());
}

测试仅断言文件已上传,如何断言 PDF 也已创建?

标签: phplaravelphpunit

解决方案


推荐阅读