Update 2016/04/26 11:30 GMT+2 Workaround

从Laravel 5.2.15开始,$test参数被删除,但是没有明确的原因,因为symfony的UploadedFile仍然有$test参数.

A workaround is to use Laravel 5.2.14 provisionally.

Update 2016/04/26 11:00 GMT+2

Laravel自己的UploadedFile不传递$test参数.请参阅以下资源:

我知道,还有一个问题:How to test file upload in Laravel 5.2,但标记的答案对我不适用.

Test Case

我创建了Symfony的UploadedFile类的一个实例,并将$test设置为true.我把文件发到file/upload.

class FileControllerTest extends TestCase
{
    use \Illuminate\Foundation\Testing\DatabaseTransactions;

    private $file;

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

        $this->file = new Symfony\Component\HttpFoundation\File\UploadedFile(
            public_path() . '/examples/example.jpg',
            'example.jpg',
            'image/jpeg',
            filesize(public_path() . '/examples/example.jpg'),
            null,
            true // for $test
        );
    }

    /** @test */
    public function it_uploads_a_valid_file()
    {
        var_dump($this->file); // $test = true
        $this->call('POST', 'file/upload', [], [], ['file' => $this->file],
            ['accept' => 'application/json']);

        $this->assertResponseOk();
    }
}

Controller

namespace App\Http\Controllers;

class FileController extends Controller
{
    public function upload(Request $request)
    {
        var_dump($request->file('file')); // $test = false

        return [];
    }
}

Problem

  • 要发布的文件的参数为true表示$test
  • The posted file arrives in upload()
  • $request->file('file')包含正确的参数,但是

    $test is false

It seems the argument $test is not past by the post call. Is this a bug?

推荐答案

Explanation

This is really interesting thing. You've noticed already a lot of when creating this post (if anyone has the problem should read it carefully).

在这commit中,正如您已经提到的,删除了$testing个参数,简化了类代码,删除了反射,得到了testing个属性值Symfony\Component\HttpFoundation\File\UploadedFile.

现在,棘手的事情是,根据您正在测试的内容,您可能没有注意到更改,所有操作都可能正常工作,但在某些情况下不会,您也不知道为什么.

例如,一切都可能正常工作——文件将毫无问题地上传,但如果您将instance mimes规则添加到请求类中,如下所示:

'logo' => ['mimes:jpeg,png'],

它不会告诉您文件有无效的MIME(这是因为在内部,它还会验证文件是否真的上传过,如果是测试,实际上它与真正的上传不同).

解决方案是再次查看真正更改为提交的是什么,以及方法看起来是什么样子.在this file中,上传文件的实例返回如下:

 return $file instanceof static ? $file : new static(
            $file->getRealPath(), $file->getClientOriginalName(), $file->getClientMimeType(),
            $file->getClientSize(), $file->getError()
        );

so in case if the file is instance of this class it will return this instance unmodified, otherwise it will create now object without passing $testing argument to class constructor.

Solution

所以为了解决这个问题,当测试文件上传到你should not use

\Symfony\Component\HttpFoundation\File\UploadedFile

class anymore. You should now use

\Illuminate\Http\UploadedFile

为了在测试文件上传时不出现任何奇怪的问题(当然,您仍然应该将true作为$testing参数传递给这个对象构造函数,但现在它将在以后使用,不会出现任何问题)

Laravel相关问答推荐

无法在终端cPanel中打开输入文件:artisan

拉威尔望远镜没有显示请求细节

Laravel 通过 API 嵌套 url 发布

无法在 Laravel 项目中安装 @vitejs/plugin-vue

自从 Laravel 使用 Vite 更新后,我无法运行npm run dev

找不到 Laravel 5 类日志(log)

验证错误后的 Laravel 自定义重定向

RuntimeException 未安装 Zip PHP 扩展

Laravel + Plupload 上传到 S3 响应以进行预检无效 - CORS

Laravel Blade:@stop VS @show VS @endsection VS @append

Laravel Eloquent,仅 Select 存在关系的行

覆盖 HTTP 标头的默认设置 (X-FRAME-OPTIONS)

验证规则 required_if 与其他条件(Laravel 5.4)

合并两个 Eloquent 集合并删除所有重复项.

如何从 laravel 中的现有数据库创建迁移

Blade 中的条件扩展

Carbon解析到 ISO8601

如何在 Eloquent Orm 中实现自引用(parent_id)模型

Laravel 删除集合中的第一项

Laravel Eloquent 多对多查询 whereIn