Skip to content

Serialization of 'Symfony\Component\HttpFoundation\File\UploadedFile' is not allowed #19572

Closed
@PlamenVasilev

Description

@PlamenVasilev

I'm trying to write white unit test to test my API with file uploads.

I'm following the docs about this using basic client request, not crawler.

The unit test is:

class RecordsControllerTest extends WebTestCase {

private $client;

public function __construct() {
    parent::__construct();
    $this->client = self::createClient();
    $this->client->insulate();
}

public function testApiPostUpload($params){
    $fileToUpload = realpath(__DIR__.'/../../resources/mpthreetest.mp3');
    $file = new UploadedFile(
        $fileToUpload,
        'mpthreetest.mp3',
        MimeTypeGuesser::getInstance()->guess($fileToUpload),
        filesize($fileToUpload)
    );
    $this->client->request('POST', '/records/'.$params['createdRecordId'].'/upload', array(), array('file' => $file) );

    $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
}
}

When I execute the test I receive an error:

Exception: Serialization of 'Symfony\Component\HttpFoundation\File\UploadedFile' is not allowed

/path/to/project/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Client.php:165
/path/to/project/vendor/symfony/symfony/src/Symfony/Component/BrowserKit/Client.php:348
/path/to/project/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Client.php:143
/path/to/project/vendor/symfony/symfony/src/Symfony/Component/BrowserKit/Client.php:313
/path/to/project/src/Bundle/Tests/Functional/Controller/RecordsControllerTest.php:182

Posting files is not working as it is described in the docs.

Any ideas?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions