dustin10/VichUploaderBundle

Disable namer for unit test

Closed this issue · 2 comments

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' }

Did you try to make your namer service public?

It worked. Thank you!