gpt4 book ai didi

php - 存储中的模拟文件以在 Laravel 中下载

转载 作者:行者123 更新时间:2023-12-03 15:44:56 25 4
gpt4 key购买 nike

有没有办法使用 Laravel 模拟文件 Storage::fake()方法?

我用过 https://laravel.com/docs/5.7/mocking#storage-fake作为我测试的基础,它适用于上传。但是我的下载测试很难看,因为我每次都必须先通过模拟上传运行我的上传路线 UploadedFile::fake()->image('avatar.jpg') .有没有办法跳过那部分并模拟文件直接存在于假存储系统中?

public function testAvatarUpload()
{
Storage::fake('avatars');

// This is the call I would like to change into a mocked existing uploaded file
$uploadResponse = $this->json('POST', '/avatar', [
'avatar' => UploadedFile::fake()->image('avatar.jpg')
]);

// Download the first avatar
$response = $this->get('/download/avatar/1');

$response->assertStatus(200);
}

最佳答案

我可能来晚了。但想帮助访问此问题的其他人提供实现它的想法。

这是一个带有一些断言的示例。

<?php

namespace Tests\Feature\Upload;

use Illuminate\Http\File;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;

class SampleDownloadTest extends TestCase
{
/**
* @test
*/
public function uploaded_file_downloads_correctly()
{
//keep a sample file inside projectroot/resources/files folder
//create a file from it
$exampleFile = new File(resource_path('files/test-file.png'))
//copy that file to projectroot/storage/app/uploads folder
Storage::putFileAs('/uploads', $exampleFile, 'test-file.png');

//make request to file download url to get file
$response = $this->get("/files/file/download/url");

//check whethe response was ok
$response->assertOk();
$response->assertHeader('Content-Type', 'image/png')
//check whether file exists in path
Storage::assertExists('/uploads/test-file.png');
//do some more assertions.....
//after test delete the file from storage path
Storage::delete('uploads/test-file.png');
//check whether file was deleted
Storage::assertMissing('/uploads/test-file.png');
}
}

关于php - 存储中的模拟文件以在 Laravel 中下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53925082/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com