api-platform / docs

API Platform documentation
https://api-platform.com/docs/
165 stars 1.08k forks source link

DOC - File upload tests - Why the file is removed on each test? #1459

Open Snowbaha opened 2 years ago

Snowbaha commented 2 years ago

Hello, I am following this part of the documentation to test my upload: https://api-platform.com/docs/core/file-upload/#testing

But each time the file is uploaded, it's removed from my fixtures (fixtures/files/image.png). Why? Do there is a way to avoid this?

BurningDog commented 1 month ago

https://github.com/symfony/symfony/blob/7.2/src/Symfony/Component/HttpFoundation/File/UploadedFile.php#L181 has a move() function which moves the original file to the destination location.

It seems that to get around this we need to duplicate the original file in the test, then upload the duplicated file. Like so:

    public function testCreateAMediaObject(): void
    {
        // The file "image.jpg" is the folder fixtures which is in the project dir
        $originalFilePath = __DIR__.'/../fixtures/image.jpg';
        // Duplicate file path
        $duplicateFilePath = __DIR__.'/../fixtures/image_test.jpg';

        // Copy the original file to create a duplicate
        if (!copy($originalFilePath, $duplicateFilePath)) {
            throw new \Exception("Failed to copy $originalFilePath to $duplicateFilePath");
        }

        $file = new UploadedFile(__DIR__.'/../fixtures/image_test.jpg', 'image_test.jpg');

        // etc