Send encoded file to Restful service

I would like to test an API test test for uploading files.

I am trying to run:

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

and I would like it to behave the same way as the user sent the example.jpgfile to the input file with the name file, but it does not seem to work that way. I get:

[PHPUnit_Framework_ExceptionWrapper] The downloaded file must be an array or an instance of UploadedFile.

Is it possible to upload a file using a REST plugin in code generation? The documentation is very limited, and it’s hard to say how to do it.

I also test the API using the plugin Postmanbefore Google Chrome, and I can upload the file without problems using this plugin.

+4
5

, , UploadedFile .

:

$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]);
+2

, Symfony UploadedFile. , $_FILES. , :

$I->sendPOST(
    '/my-awesome-api',
    [
        'sample-field' => 'sample-value',
    ],
    [
        'myFile' => [
            'name' => 'myFile.jpg',
            'type' => 'image/jpeg',
            'error' => UPLOAD_ERR_OK,
            'size' => filesize(codecept_data_dir('myFile.jpg')),
            'tmp_name' => codecept_data_dir('myFile.jpg'),
        ]
    ]
);

, - ( , ​​ )

+9

['file' => 'example.jpg'] , .

$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);

$I->sendPOST($this->endpoint, $postData, ['file' =>  $path . $filename]);
+1

,

:

$uploadedResume= $_FILES['resume_uploader'];
$outPut = [];

        if (isset($uploadedResume) && empty($uploadedResume['error'])) {
            $uploadDirectory = 'uploads/users/' . $userId . '/documents/';
            if (!is_dir($uploadDirectory)) {
                @mkdir($uploadDirectory, 0777, true);
            }

            $ext = explode('.', basename($uploadedResume['name']));
            $targetPath = $uploadDirectory . md5(uniqid()) . '.' . end($ext);

            if (move_uploaded_file($uploadedResume['tmp_name'], $targetPath)) {
                $outPut[] = ['success' => 'success', 'uploaded_path' => $targetPath];
            }
        }
return json_encode($output);

: P

:

 //resume.pdf is copied in to tests/_data directory
$I->sendPOST('/student/resume', [], ['resume_uploader' => codecept_data_dir('resume.pdf') ]);
0

@ Yaronius , :

$I->haveHttpHeader('Content-Type', 'multipart/form-data');

0

All Articles