如何使用 Laravel 伪造图像上传以使用干预图像包进行测试

Tru*_*ode 6 tdd laravel laravel-5 intervention

我有一个测试断言可以上传图像。这是代码...

// Test

$file = UploadedFile::fake()->image('image_one.jpg');
Storage::fake('public');

$response = $this->post('/api/images', [
'images' => $file
]);
Run Code Online (Sandbox Code Playgroud)

然后在控制器中我正在做一些更简单的事情..

$file->store('images', 'public');
Run Code Online (Sandbox Code Playgroud)

并断言几件事。它就像魅力一样。

但现在我需要使用干预图像包调整图像大小。为此,我有以下代码:

 Image::make($file)
        ->resize(1200, null)
        ->save(storage_path('app/public/images/' . $file->hashName()));
Run Code Online (Sandbox Code Playgroud)

如果目录不存在,我首先检查这个并创建一个 -

if (!Storage::exists('app/public/images/')) {
        Storage::makeDirectory('public/images/', 666, true, true);
         }
Run Code Online (Sandbox Code Playgroud)

现在测试应该是green,我会,但问题是每次我运行测试时,它都会将文件上传到存储目录中。我不想要的。我只需要伪造上传而不是真实上传。

任何解决方案?

提前致谢 :)

Ada*_*dam 2

您需要使用外观来存储文件StorageStorage::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());
Run Code Online (Sandbox Code Playgroud)