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

问题描述 投票:3回答:1

我进行了一项测试,声称可以上传图像。这是代码...

// Test

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

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

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

$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,但我会这样做,但问题是每次运行测试时,它将文件上传到存储目录中。我不想要的。我只需要伪造上传内容而不是真实的内容。

任何解决方案?

提前感谢:)

laravel laravel-5 tdd intervention
1个回答
0
投票

您需要使用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());
© www.soinside.com 2019 - 2024. All rights reserved.