如何使用laravel和phpunit测试文件上传?

问题描述 投票:17回答:6

我正在尝试在我的laravel控制器上运行此功能测试。我想测试图像处理,但这样做我想伪造图像上传。我该怎么做呢?我在网上找到了一些例子,但似乎没有一个适合我。这就是我所拥有的:

public function testResizeMethod()
{
    $this->prepareCleanDB();

    $this->_createAccessableCompany();

    $local_file = __DIR__ . '/test-files/large-avatar.jpg';

    $uploadedFile = new Symfony\Component\HttpFoundation\File\UploadedFile(
        $local_file,
        'large-avatar.jpg',
        'image/jpeg',
        null,
        null,
        true
    );


    $values =  array(
        'company_id' => $this->company->id
    );

    $response = $this->action(
        'POST',
        'FileStorageController@store',
        $values,
        ['file' => $uploadedFile]
    );

    $readable_response = $this->getReadableResponseObject($response);
}

但是控制器没有通过这个检查:

elseif (!Input::hasFile('file'))
{
    return Response::error('No file uploaded');
}

所以文件不能正确传递。我该怎么做?


更新

基于max.lanin的建议,我也尝试过:

public function setUp()
{
    // Tried with parent::setUp() here and at the end
    // parent::setUp();
    $local_file = __DIR__ . '/test-files/large-avatar.jpg';

    print($local_file);

    $_FILES = array(
        'file' => array (
            'tmp_name' => $local_file,
            'name' => 'large-avatar.jpg',
            'type' => 'image/jpeg',
            'size' => 335057,
            'error' => 0,
        ),
        'image' => array (
            'tmp_name' => $local_file,
            'name' => 'large-avatar.jpg',
            'type' => 'image/jpeg',
            'size' => 335057,
            'error' => 0,
        ),
    );

    parent::setUp();
}

但没有成功。使用的文件存在且大小正确。

php laravel phpunit
6个回答
10
投票

Docs for CrawlerTrait.html#method_action读到:

参数 string $ method string $ action array $ wildcards 数组$参数 数组$ cookies 数组$文件 数组$ server string $ content

所以我假设正确的呼叫应该是

$response = $this->action(
    'POST',
    'FileStorageController@store',
    [],
    $values,
    [],
    ['file' => $uploadedFile]
);

除非它需要非空的通配符和cookie。

作为旁注,它绝不是单元测试。


6
投票

对于任何绊倒这个问题的人,你现在可以这样做:

    $response = $this->postJson('/product-import', [
        'file' => new \Illuminate\Http\UploadedFile(resource_path('test-files/large-avatar.jpg'), 'large-avatar.jpg', null, null, null, true),
    ]);

5
投票

使用phpunit,您可以使用attach()方法将文件附加到表单。

来自lumen docs的示例:

public function testPhotoCanBeUploaded()
{
    $this->visit('/upload')
         ->name('File Name', 'name')
         ->attach($absolutePathToFile, 'photo')
         ->press('Upload')
         ->see('Upload Successful!');
}

3
投票

以下是如何使用自定义文件进行测试的完整示例。我需要这个来解析具有已知格式的CSV文件,因此我的文件必须具有精确的格式和内容。如果您只需要图像或随机大小的文件,请使用$ file-> fake-> image()或create()方法。那些与Laravel捆绑在一起。

namespace Tests\Feature;

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

class PanelistImportTest extends TestCase
{
    /** @test */
    public function user_should_be_able_to_upload_csv_file()
    {
        // If your route requires authenticated user
        $user = Factory('App\User')->create();
        $this->actingAs($user);

        // Fake any disk here
        Storage::fake('local');

        $filePath='/tmp/randomstring.csv';

        // Create file
        file_put_contents($filePath, "HeaderA,HeaderB,HeaderC\n");

        $this->postJson('/upload', [
            'file' => new UploadedFile($filePath,'test.csv', null, null, null, true),
        ])->assertStatus(200);

        Storage::disk('local')->assertExists('test.csv');
    }
}

这是控制器:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Storage;

class UploadController extends Controller
{
    public function save(Request $request)
    {
        $file = $request->file('file');

        Storage::disk('local')->putFileAs('', $file, $file->getClientOriginalName());

        return response([
            'message' => 'uploaded'
        ], 200);
    }
}

2
投票

首先导入必要的东西

use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;

然后制作一个假文件上传。

Storage::fake('local');
$file = UploadedFile::fake()->create('file.pdf');

然后创建一个JSON数据来传递文件。例

$parameters =[
            'institute'=>'Allen Peter Institute',
            'total_marks'=>'100',
            'aggregate_marks'=>'78',
            'percentage'=>'78',
            'year'=>'2002',
            'qualification_document'=>$file,
        ];

然后将数据发送到您的API。

$user = User::where('email','[email protected]')->first();

$response = $this->json('post', 'api/user', $parameters, $this->headers($user));

$response->assertStatus(200);

我希望它能奏效。


1
投票

将类似的setUp()方法添加到您的测试用例中:

protected function setUp()
{
    parent::setUp();

    $_FILES = array(
        'image'    =>  array(
            'name'      =>  'test.jpg',
            'tmp_name'  =>  __DIR__ . '/_files/phpunit-test.jpg',
            'type'      =>  'image/jpeg',
            'size'      =>  499,
            'error'     =>  0
        )
    );
}

这会欺骗你的$ _FILES全局,让Laravel认为上传了一些内容。

© www.soinside.com 2019 - 2024. All rights reserved.