我有一个测试断言可以上传图像。这是代码...
// Test
$file = UploadedFile::fake()->image('image_one.jpg');
Storage::fake('public');
$response = $this->post('/api/images', [
'images' => $file
]);
然后在 Controller 中我正在做一些更简单的事情..
$file->store('images', 'public');
并断言几件事。它就像魅力一样。
但现在我需要使用干预图像包调整图像大小。为此,我有以下代码:
Image::make($file)
->resize(1200, null)
->save(storage_path('app/public/images/' . $file->hashName()));
如果目录不存在,我首先检查这个并创建一个 -
if (!Storage::exists('app/public/images/')) {
Storage::makeDirectory('public/images/', 666, true, true);
}
现在测试应该是
green
我会,但问题是每次我运行测试时,它都会将文件上传到存储目录中。我不想要的。我只需要伪造上传而不是真实上传。任何解决方案?
提前致谢 :)
最佳答案
您需要使用 Storage
存储您的文件正面。 Storage::putAs
不起作用,因为它不接受干预图像类。但是你可以使用 Storage::put
:
$file = UploadedFile::fake()->image('image_one.jpg');
Storage::fake('public');
// Somewhere in your controller
$image = Image::make($file)
->resize(1200, null)
->encode('jpg', 80);
Storage::disk('public')->put('images/' . $file->hashName(), $image);
// back in your test
Storage::disk('public')->assertExists('images/' . $file->hashName());
关于laravel - 如何使用 Laravel 伪造图像上传以使用干预图像包进行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59592101/