dustin10 / VichUploaderBundle

A simple Symfony bundle to ease file uploads with ORM entities and ODM documents.
MIT License
1.85k stars 519 forks source link

Disable namer for unit test #1467

Closed danielpeci closed 1 month ago

danielpeci commented 1 month ago
Q A
Bundle version 2.4.0
Symfony version 7.1
PHP version 8.1

Support Question

I’m currently working on a unit test for file uploads. The test is functioning correctly, but there’s an issue when the upload occurs: VichUploader automatically renames the file from logo.png to something like logo-66f5060e...png. This causes the test to fail on following runs, as logo.png no longer exists. I’ve tried creating a custom Vich namer to prevent this behavior, but encountered the following error: The "App\Namer\TestNamer.media" service or alias has been removed or inlined when the container was compiled. You should either make it public, or stop using the container directly and use dependency injection instead.

Is there any known solution for this?

Unit Test:

private function uploadMedia(): void
{
    $uploadedFile = new UploadedFile(
        __DIR__.'/Fixtures/logo.png',
        'logo.png'
    );

    $this->client->request('POST', '/api/medias',
        [
            'headers' => ['Content-Type' => 'multipart/form-data'],
            'extra' => [
                'files' => [
                    'file' => $uploadedFile
                ]
            ]
        ]
    );
}

vich settings:

when@test:
    vich_uploader:
        db_driver: orm
        metadata:
            type: attribute
        mappings:
            media:
                uri_prefix: /media
                upload_destination: '%kernel.project_dir%/tests/Api/Media/Fixtures'

I also tried to use property namer. But couldn't find name property vich settings: when@test: vich_uploader: db_driver: orm metadata: type: attribute mappings: media: uri_prefix: /media upload_destination: '%kernel.project_dir%/tests/Api/Media/Fixtures' namer: service: Vich\UploaderBundle\Naming\PropertyNamer options: { property: 'name' }

garak commented 1 month ago

Did you try to make your namer service public?

danielpeci commented 1 month ago

It worked. Thank you!