Slim 3 method getUploadedFiles() returns an empty array

不想你离开。 提交于 2019-12-13 03:37:20

问题


When trying to mock a test with a test file I get stuck. Basically the withUploadedFiles() method wants me to work with an array. As you can see down below, I try to var_dump to see if it has worked but in every case the array is empty. I wonder why this is? If I only var_dump my array with the name $uploadedFiles it shows me the content.

Do you see where my mistake is?

<?php

use Slim\Http\UploadedFile;

class FileControllerTest extends PHPUnit_Framework_TestCase
{
    // ...

    public function testUserCanCreateAFileSuccessfullyWithStatusCode200()
    {
        // ...

        $uploadedFile = new UploadedFile(
            $directory . '/File_1.txt',
            'File_1.txt',
            'text/plain',
            filesize($directory . '/File_1.txt'),
            0
        );

        $uploadedFiles = array();
        $uploadedFiles["file"] = $uploadedFile;

        // ...

        $request = \Slim\Http\Request::createFromEnvironment($environment);
        $request->withUploadedFiles($uploadedFiles);    

        var_dump($request->getUploadedFiles());         // array(0) { }

        // ...
    }
}

回答1:


As requests are immutable, you always create a new instance when you manipulate it...

 $request = $request->withUploadedFiles($uploadedFiles); 


来源:https://stackoverflow.com/questions/47369641/slim-3-method-getuploadedfiles-returns-an-empty-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!