如何使用Laravel伪造图像上传进行干预图像包的测试 [英] How to fake image upload for testing with Intervention image package using Laravel

查看:56
本文介绍了如何使用Laravel伪造图像上传进行干预图像包的测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a test asserting that images can be uploaded. Here is the code...

// Test

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

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

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

Then in controller i am doing something simpler..

$file->store('images', 'public');

并声明几件事.它就像魅力.

And asserting couple of things. and it works like charm.

但是现在我需要使用Intervention图像包来调整图像的大小.为此,我有以下代码:

But now i need to resize the image using Intervention image package. for that i have a following code:

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

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

And in case if directory does not existing i am checking first this and creating one -

if (!Storage::exists('app/public/images/')) {
        Storage::makeDirectory('public/images/', 666, true, true);
         }

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

Now Test should be green and i will but the issue is that every time i run tests it upload a file into storage directory. Which i don't want. I just need to fake the uploading and not real one.

任何解决方案?

先谢谢您了:)

推荐答案

您需要使用 Storage 外观存储文件. Storage :: putAs 不起作用,因为它不接受干预图像类.但是,您可以使用 Storage :: put :

You need to store your file using the Storage facade. Storage::putAs does not work because it does not accept intervention image class. However you can you use 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伪造图像上传进行干预图像包的测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆