php - 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 也已创建?
解决方案
推荐阅读
- javascript - 在 React 中访问嵌套的 Firestore 数据?
- postgresql - Postgresql - 创建物化视图
- .net-core - 无法写入 AOF
- python-3.x - JSONDecodeError:期望值:翻译文本时的第 1 行第 1 列(字符 0)
- python - 将每个 x 和 y 分开的点数据集转换为点列表
- selenium - 如何使用 Selenium/TestNG/Maven 运行单个测试用例(方法)而不是整个测试套件(类)
- python - threading Timer - 使用计时器'sys.exit'主线程
- activemq - 如何通过消息 ID 从 ActiveMQ 代理接收消息?
- kubernetes-helm - 如何从 Helm3 降级到 Helm2?
- php - 如何获取 php 和 mysql 中的数据库列表?