相关疑难解决方法(0)

Laravel 5.2 | 测试UploadedFile在Post之后错过$ test值.错误?

更新2016/04/26 11:30 GMT + 2解决方法

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

解决方法是暂时使用Laravel 5.2.14.

更新2016/04/26 11:00 GMT + 2

Laravel自己的UploadedFile没有传递$ test参数.查看这些资源:

我知道,还有另一个问题:如何在Laravel 5.2中测试文件上传,但标记的答案对我不起作用.

测试用例

我创建了一个Symfony的UploadedFile类的实例,我设置$testtrue.我将文件发布到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', [], …
Run Code Online (Sandbox Code Playgroud)

phpunit file-upload laravel laravel-5 laravel-5.2

22
推荐指数
1
解决办法
3816
查看次数

在lumen 5.5中测试文件上传

我正在使用 Lumen 5.5,并且编写了上传文件的简单应用程序。

我写了这样的测试(按照教程)

<?php

class UploadImageTest extends TestCase
{

    Use DatabaseMigrations;

    public function testUploadingImageSuccessfully()
    {
        $this->json('POST', '/images', [
            'image' => UploadedFile::fake()->image('image.jpg')
        ])->assertResponseOk()
    }

}
Run Code Online (Sandbox Code Playgroud)

问题是在我的控制器中,$request->file('image')返回 null。

<?php

use Illuminate\Http\Request;

class UploadController extends Controller
{

    public function upload(Request $request)
    {
        if ($request->file('image')) { // always return null
            return "File is uploaded!";
        }

        return "File is not uploaded!";
    }

}
Run Code Online (Sandbox Code Playgroud)

我检查了其他问题(例如这个问题)并尝试了给定的解决方案,但没有成功!

php testing unit-testing lumen

3
推荐指数
1
解决办法
2155
查看次数

Laravel phpunit 测试 maatwebsite/excel 导入

我创建了一个 maatwebsite/excel 导入例程,我想测试它。

maatwebsite/excel 测试页面除了伪造之外没有向我提供任何其他信息。但我需要上传我的真实 Excel 文件,因为我想验证 Excel 文件中的数据是否已正确处理。

这是我的上传输入字段和用于点击端点的相应按钮/import

<form action="/import" method="post" enctype="multipart/form-data">
   @csrf
   <div class="form-group">
       <input type="file" class="form-control-file file-path" name="fileToUpload">
   </div>
   <button type="submit" class="btn btn-primary">Import File</button>
</form>
Run Code Online (Sandbox Code Playgroud)

在视图的控制器端,将处理并导入上传的文件。

...

public function store(Request $request) {
        $request->validate([
            'fileToUpload' => 'required|file|max:4096|mimes:xls,xlsx',
        ]);

        // start the import
        Excel::import(new SheetNavigator, request()->file('fileToUpload'));
...

Run Code Online (Sandbox Code Playgroud)

需要导入的文件位于我的测试环境中:

/tests
  /files
    /myexcel.xlsx
Run Code Online (Sandbox Code Playgroud)
public function test_user_can_import_file() {

        Excel::fake();

        $datafile = new UploadedFile(
            base_path('tests/files/myfile.xlsx'),
            'myfile.xlsx',
            'xlsx',
            13071,
            true);

        $res = $this->post('/import', [
            'fileToUpload' => $datafile
        ]);

        // asserting, that everything …
Run Code Online (Sandbox Code Playgroud)

testing phpunit laravel maatwebsite-excel

0
推荐指数
1
解决办法
4293
查看次数