问题
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