【问题标题】:How to fake image upload for testing with Intervention image package using Laravel如何使用 Laravel 使用干预图像包伪造图像上传以进行测试
【发布时间】:2020-04-22 19:35:23
【问题描述】:

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

// 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】:

    您需要使用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());
    

    【讨论】:

      猜你喜欢
      • 2019-04-25
      • 1970-01-01
      • 2023-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-15
      • 2021-02-13
      相关资源
      最近更新 更多