【问题标题】:How to test in PHPUnit; a CSV File Upload using an API written in Laravel?如何在 PHPUnit 中进行测试;使用 Laravel 编写的 API 上传 CSV 文件?
【发布时间】:2016-10-01 18:31:09
【问题描述】:

我想通过 Laravel API 上传一个 CSV 文件,然后用 PHPUnit 测试上传。

我在 Controller 中的 store() 函数和 testCreate() 函数基本上是什么样子的。

这是我目前得到的:

<?php

use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\WithoutMiddleware;

class ProspectListControllerTest extends TestCase
{

use WithoutMiddleware, DatabaseTransactions;

public function testCreate()
{

    $file = new Symfony\Component\HttpFoundation\File\UploadedFile(storage_path('/crm/data/test-file.csv'), 'test-file.csv', 'text/plain', 446, null, true);

    $this->call('POST', '/api/lists-imports/', [], [], ['csv_file' => $file]);
    $this->dump()->assertResponseOk();
  }
}

控制器方法如下:

<?php

namespace App\Http\Controllers;

use App\ListImport;
use Illuminate\Http\Request;

class ListImportController extends Controller
{
public $model = ListImport::class;

public function store(Request $request, ListImport $importList)
{
    $request->file('importFile')->move(public_path('storage.crm.data'), $request->file('importFile')->getClientOriginalName());

    $importList->importFile = public_path('storage.crm.data') . '/' . $request->file('importFile')->getClientOriginalName();

    $importList->save();

}
}

任何帮助将不胜感激:)

【问题讨论】:

  • 在 testCreate 函数中,您可以将 csv 文件模拟为字符串,而不是上传本身不是吗?我正在处理我在测试用例中制作的 xml 文件
  • store() 的单元测试应该涵盖文件是否已存储。更好的测试是assertStringEqualsFile()。第一个参数应该是$importList-&gt;importFile,第二个参数应该是$file。此测试将涵盖的不仅仅是响应状态。

标签: php unit-testing laravel csv phpunit


【解决方案1】:

这是我的功能测试 Laravel 6 及更高版本的示例(“上传”是我的存储驱动程序):

    use Illuminate\Http\UploadedFile;

    Storage::fake('uploads');

    $header = 'Header 1,Header 2,Header 3';
    $row1 = 'value 1,value 2,value 3';
    $row2 = 'value 1,value 2,value 3';

    $content = implode("\n", [$header, $row1, $row2]);

    $inputs = [
        'csv_file' =>
            UploadedFile::
                fake()->
                createWithContent(
                    'test.csv',
                    $content
                )
    ];


    $response = $this->postJson(
        'file-upload',
        $inputs

    );

    $response->assertOk();

【讨论】:

  • createWithontent 仅适用于 Laravel 6 及以上版本
  • 太棒了! createWithContent() 方法:在 Laravel v.6 文档中没有提及,但它被定义在 Illuminate\Http\Testing\FileFactory 类中。
【解决方案2】:

在 Laravel 中有一种测试文件上传的新方法。 https://laravel-news.com/testing-file-uploads-with-laravel

是这样的:(来自文档)

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

    $response = $this->json('POST', '/avatar', [
        'avatar' => UploadedFile::fake()->image('avatar.jpg')
    ]);

    // Assert the file was stored...
    Storage::disk('avatars')->assertExists('avatar.jpg');

    // Assert a file does not exist...
    Storage::disk('avatars')->assertMissing('missing.jpg');
}

【讨论】:

    猜你喜欢
    • 2016-01-21
    • 2015-01-24
    • 2016-06-04
    • 2019-06-10
    • 2011-03-25
    • 2021-09-14
    • 2015-09-01
    • 2017-04-14
    • 1970-01-01
    相关资源
    最近更新 更多