Marcin Nabiałek Marcin Nabiałek - 10 months ago 159
reST (reStructuredText) Question

Send file to Restful service in codeception

I would like to test restful API test for file uploading.

I try to run:

$I->sendPOST($this->endpoint, $postData, ['file' => 'example.jpg']);

and I would like it to behave the same as user sent
file in file input with name
but it doesn't seem to work this way. I'm getting:

[PHPUnit_Framework_ExceptionWrapper] An uploaded file must be an array or an instance of UploadedFile.

Is it possible to upload file using REST plugin in codeception? Documentation is very limited and it's hard to say how to do it.

I'm also testing API using
plugin to
Google Chrome
and I can upload file without a problem using this plugin.

Answer Source

After testing it seems to make it work we need to use UploadedFile object as file.

For example:

$path = codecept_data_dir();
$filename = 'example-image.jpg';

// copy original test file to have at the same place after test
copy($path . 'example.jpg', $path . $filename);

$mime = 'image/jpeg';

$uploadedFile = new \Symfony\Component\HttpFoundation\File\UploadedFile($path . $filename, $filename, $mime,
    filesize($path . $filename));

$I->sendPOST($this->endpoint, $postData, ['file' => $uploadedFile]);