流明5.5 JSON API文件上传测试与UploadedFile中断 [英] Lumen 5.5 JSON API File Upload test breaks with UploadedFile

查看:312
本文介绍了流明5.5 JSON API文件上传测试与UploadedFile中断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用流明构建一个文件上传JSON API,并试图编写phpunit API测试。

我遇到的问题是只要我尝试用像 [file=>构建的真实图像来模拟文件上载。新的UploadedFile(TestUtils :: tempPath('test.jpg'),'test.jpg',image\jpeg,100,null,true)] 并通过<$ c $发送c> $ this-> json('POST','/ chunk',$ data); ,
我不能对上传的文件做任何事情,因为它是一个数组,我得到错误调用成员函数move()在数组中...



奇怪的是,如果我使用 UploadedFile :: fake() - > image('test.jpg')它破坏了我的请求,我发送的其他数据在控制器中不可用。

如果我在我的控制器中的dd文件对象,它显示数组(0){}。我认为这与通过json('POST')传输有关,因为使用UploadedFileObject的单元测试可以直接成功运行。



如果我dd完整的请求,它显示:

  [ 请求] => 
object(Symfony \ Component \HttpFoundation\ParameterBag)#52(1){
[parameters:protected] =>
array(5){
[chunkType] =>
string(4)flow
[flowIdentifier] =>
string(13)Testing123Bnu
[flowChunkNumber] =>
int(1)
[flowTotalChunks] =>
int(2)
[file] =>
array(0){
}
}
}

...

[files] => ;
object(Symfony \ Component \HttpFoundation\FileBag)#71(1){
[parameters:protected] =>
array(0){
}
}

...

[content:protected] =>字符串(103){(otherData ..),file:{}}

I不知道如何绕过这个或测试一个文件上传在这种情况下,并已经寻找小时。 UploadedFile构造函数中的symfony测试模式不起作用。

这是我的测试:

$ p $ public function testChunkUpload_FlowChunk()
{
$ data = [(otherData)
file=>新的UploadedFile(TestUtils :: tempPath('test.jpg'),'test.jpg','image\jpeg',100,null,true)
// UploadedFile :: fake() - > image ('test.jpg')
];

$ this-> assertFileExists(TestUtils :: tempPath('test.jpg'));

$ this-> json('POST','/ chunk',$ data);

$ this-> assertEquals(200,$ this-> response-> status());
$ this-> assertEquals('application / json',$ this-> response-> headers-> get('Content-Type'));



$ b $ p
$ b

这里是我的控制器中的相关方法:

  public function receiveChunk(Request $ request){
if(!$ request-> has('chunkType')){
返回响应() - > json([无效块类型],400);

$ data = $ this-> chunkNormalizer-> normalizeInput($ request-> all());
$ chunk = $ this-> chunkFactory-> createChunk((otherData)
$ data ['fileHandle']);

$ this-> fileAssembler-> processChunk($ chunk);

return response() - > json([Success],200);
}

在processChunk中发生错误:

  public function processChunk(FileChunk $ chunk){
...
$ chunkName =...。 .chunk;

$ fileHandle = $ chunk-> getFileHandle();
$ fileHandle-> move($ this-> assemblePath,$ chunkName);



$ b $ p
$ b $ p


<解决方案编辑:奇怪的是,在laravel文档中使用json方法而不是调用以及。这可能是一个错误?还是只是一个流明不兼容问题?
(Laravel Docs)



我已经想出了如何让测试运行。我关于如何通过JS供应商库上传文件的假设是错误的。实际上,文件块是通过XmlHttpRequestmultipart / formdata而不是JSON来上传的。

我理解错误(JSON编码),但不是源。了解它使用XmlHttpRequest后,修复很简单。

工作测试:
$ b

  public function testChunkUpload_FlowChunk()
{
$ data = [(otherData)
file=>新的UploadedFile(TestUtils :: tempPath('test.jpg'),'test.jpg','image\jpeg',100,null,true)
// UploadedFile :: fake() - > image ('test.jpg')
];

$ this-> assertFileExists(TestUtils :: tempPath('test.jpg'));

$ this-> call('POST','/ chunk',$ data);

$ this-> assertEquals(200,$ this-> response-> status());
$ this-> assertEquals('application / json',$ this-> response-> headers-> get('Content-Type'));

$ / code>

我还不确定为什么使用UploadedFile :: fake (xyz.jpg)有不同的效果,现在测试运行两种方法。

I am building a file upload JSON API with Lumen and trying to write phpunit API tests.

The problem I am having though is that as soon as I try to simulate a file upload with a real image constructed like ["file" => new UploadedFile(TestUtils::tempPath('test.jpg'), 'test.jpg', "image\jpeg", 100, null, true)] and sending it via $this->json('POST', '/chunk', $data);, I can't do anything with the uploaded file because it is an array and I get the error Call to a member function move() on array in ...

Oddly enough if I use UploadedFile::fake()->image('test.jpg') it mangles my request and the other data I send with it isn't available in the controller.

If I dd the file object in my controller, it shows an "array(0){}". I am thinking it has something to do with the transfer via json('POST'), as unit tests that use an UploadedFileObject directly run successfully.

If I dd the entire request, it shows:

["request"]=>
  object(Symfony\Component\HttpFoundation\ParameterBag)#52 (1) {
    ["parameters":protected]=>
    array(5) {
      ["chunkType"]=>
      string(4) "flow"
      ["flowIdentifier"]=>
      string(13) "Testing123Bnu"
      ["flowChunkNumber"]=>
      int(1)
      ["flowTotalChunks"]=>
      int(2)
      ["file"]=>
      array(0) {
      }
    }
  }

 ...

["files"]=>
object(Symfony\Component\HttpFoundation\FileBag)#71 (1) {
    ["parameters":protected]=>
    array(0) {
    }
  }

...

["content":protected]=> string(103) "{(otherData..), "file":{}}"

I have no idea how to circumvent this or test a file upload in this situation and have been searching for hours. The symfony test mode in the UploadedFile constructor has no effect.

Here is my test:

    public function testChunkUpload_FlowChunk()
    {    
        $data = [   (otherData)
                    "file"              => new UploadedFile(TestUtils::tempPath('test.jpg'), 'test.jpg', "image\jpeg", 100, null, true)
                    //UploadedFile::fake()->image('test.jpg')
                ];

        $this->assertFileExists(TestUtils::tempPath('test.jpg'));

        $this->json('POST', '/chunk', $data);

        $this->assertEquals(200, $this->response->status());
        $this->assertEquals('application/json', $this->response->headers->get('Content-Type'));
    }

And here is the relevant method in my controller:

public function receiveChunk(Request $request){
    if (! $request->has('chunkType')) {
        return response()->json(["Invalid Chunk Type"], 400);
    }
    $data = $this->chunkNormalizer->normalizeInput($request->all());
    $chunk = $this->chunkFactory->createChunk(  (otherData)
                                                $data['fileHandle']);

    $this->fileAssembler->processChunk($chunk);

    return response()->json(["Success"], 200);    
}

And the error occurs in processChunk:

public function processChunk(FileChunk $chunk){
    ...
    $chunkName  = "..." . ".chunk";

    $fileHandle = $chunk->getFileHandle();
    $fileHandle->move($this->assemblePath, $chunkName);
}

Any ideas would be greatly appreciated!

解决方案

EDIT: Strangely enough, in the laravel docs the "json" method is used instead of "call" as well. Could this be an error? Or just a Lumen incompatability issue? (Laravel Docs)

I have figured out how to get the test to run. My assumptions about how the file is uploaded through the JS vendor library were wrong. In fact, the file chunks are uploaded with a XmlHttpRequest "multipart/formdata" and NOT as JSON.

I understood the error (JSON encoding), but not the source. After understanding that it uses an XmlHttpRequest, the fix was simple.

Working test:

public function testChunkUpload_FlowChunk()
{    
    $data = [   (otherData)
                "file"              => new UploadedFile(TestUtils::tempPath('test.jpg'), 'test.jpg', "image\jpeg", 100, null, true)
                //UploadedFile::fake()->image('test.jpg')
            ];

    $this->assertFileExists(TestUtils::tempPath('test.jpg'));

    $this->call('POST', '/chunk', $data);

    $this->assertEquals(200, $this->response->status());
    $this->assertEquals('application/json', $this->response->headers->get('Content-Type'));
}

I am still unsure about why using UploadedFile::fake()->image("xyz.jpg") had a different effect, now the test runs with both methods.

这篇关于流明5.5 JSON API文件上传测试与UploadedFile中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆