From f2c5a5646edcc55d9515ea73ea96b4c17e2aeefe Mon Sep 17 00:00:00 2001 From: Niels Vanpachtenbeke Date: Tue, 27 Oct 2020 19:41:49 +0100 Subject: [PATCH] File namer#1784 (#2114) * support a custom file namer for responsive images * Fix styling * apply code review suggestions * let conversions use a new file namer class * add documentation * cleanup last conversion file namer parts * apply code review suggestions * split up conversion & responsive file namer * remove get prefix on FileNamer methods * Update naming-generated-files.md * Update naming-generated-files.md Co-authored-by: Nielsvanpach Co-authored-by: Freek Van der Herten --- CHANGELOG.md | 3 +- UPGRADING.md | 1 + config/media-library.php | 5 +- docs/advanced-usage/naming-generated-files.md | 55 +++++++++++++++++++ .../naming-conversion-files.md | 32 ----------- docs/installation-setup.md | 5 +- src/Conversions/Conversion.php | 11 ++-- src/Conversions/ConversionFileNamer.php | 17 ------ .../DefaultConversionFileNamer.php | 15 ----- .../ResponsiveImageGenerator.php | 35 ++++++++---- src/Support/FileNamer/DefaultFileNamer.php | 20 +++++++ src/Support/FileNamer/FileNamer.php | 23 ++++++++ tests/Conversions/ConversionFileNamerTest.php | 14 +++-- .../ResponsiveImageFileNamerTest.php | 17 ++++++ .../ResponsiveImageGeneratorFileNamerTest.php | 17 ++++++ .../ResponsiveImageGeneratorTest.php | 40 +++++++------- .../ResponsiveImages/ResponsiveImageTest.php | 38 +++++++------ tests/TestSupport/TestFileNamer.php | 23 ++++++++ .../testfiles/TestConversionFileNamer.php | 17 ------ 19 files changed, 243 insertions(+), 145 deletions(-) create mode 100644 docs/advanced-usage/naming-generated-files.md delete mode 100644 docs/converting-images/naming-conversion-files.md delete mode 100644 src/Conversions/ConversionFileNamer.php delete mode 100644 src/Conversions/DefaultConversionFileNamer.php create mode 100644 src/Support/FileNamer/DefaultFileNamer.php create mode 100644 src/Support/FileNamer/FileNamer.php create mode 100644 tests/ResponsiveImages/ResponsiveImageFileNamerTest.php create mode 100644 tests/ResponsiveImages/ResponsiveImageGeneratorFileNamerTest.php create mode 100644 tests/TestSupport/TestFileNamer.php delete mode 100644 tests/TestSupport/testfiles/TestConversionFileNamer.php diff --git a/CHANGELOG.md b/CHANGELOG.md index d0c53a1c3..2f4fdd2c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,8 @@ All notable changes to `laravel-medialibrary` will be documented in this file ## 9.0.0 - Unreleased - names of the generated conversions will now be put in a dedicated `generated_conversions` on media -- adds support for media library pro +- add support for media library pro +- responsive image files can now be named using the `file_namer` key in the `media-library` config file (#2114) ## 8.10.1 - 2020-10-05 diff --git a/UPGRADING.md b/UPGRADING.md index 88ca8e18f..ce336f217 100644 --- a/UPGRADING.md +++ b/UPGRADING.md @@ -5,6 +5,7 @@ Because there are many breaking changes an upgrade is not that easy. There are m ## From v8 to v9 - add a column `generated_conversions` to the `media` table. If you are using Media Library Pro you used copy the values you now have in the `generated_conversions` key of the `custom_properties` column to `generated_conversions` +- rename `conversion_file_namer` key in the `media-library` config to `file_namer`. This will support both the conversions and responsive images from now on. ## From v7 to v8 diff --git a/config/media-library.php b/config/media-library.php index d974d9ae5..188cd4ee3 100644 --- a/config/media-library.php +++ b/config/media-library.php @@ -85,10 +85,9 @@ 'default_loading_attribute_value' => null, /* - * This is the class that is responsible for naming conversion files. By default, - * it will use the filename of the original and concatenate the conversion name to it. + * This is the class that is responsible for naming generated files. */ - 'conversion_file_namer' => Spatie\MediaLibrary\Conversions\DefaultConversionFileNamer::class, + 'file_namer' => Spatie\MediaLibrary\Support\FileNamer\DefaultFileNamer::class, /* * The class that contains the strategy for determining a media file's path. diff --git a/docs/advanced-usage/naming-generated-files.md b/docs/advanced-usage/naming-generated-files.md new file mode 100644 index 000000000..dbaf8f676 --- /dev/null +++ b/docs/advanced-usage/naming-generated-files.md @@ -0,0 +1,55 @@ +--- +title: Naming generated files +weight: 11 +--- + +### Naming conversion files + +By default, all conversion files will be named in this format: + +``` +{original-file-name-without-extension}-{name-of-the-conversion}.{extension} +``` + +Should you want to name your conversion file using another format, +then you can specify the class name of your own `FileNamer` in the `file_namer` key +of the `media-library.php` config file. + +The only requirements is that your class extends `Spatie\MediaLibrary\Support\FileNamer`. +In your class you should implement 2 methods: +1. `conversionFileName` should return the media file name combined with the conversion name +2. `responsiveFileName` should return the media file name + +Here is the implementation of `Spatie\MediaLibrary\Support\FileNamer\DefaultFileNamer` + +```php +namespace Spatie\MediaLibrary\Support\FileNamer; + +use Spatie\MediaLibrary\Conversions\Conversion; + +class DefaultFileNamer extends FileNamer +{ + public function conversionFileName(string $fileName, Conversion $conversion): string + { + $strippedFileName = pathinfo($fileName, PATHINFO_FILENAME); + + return "{$strippedFileName}-{$conversion->getName()}"; + } + + public function responsiveFileName(string $fileName): string + { + return pathinfo($fileName, PATHINFO_FILENAME); + } +} +``` + +### Naming responsive image files + +By default, all responsive image files will be named in this format: + +``` +{original-file-name-without-extension}___{name-of-the-conversion}_{width}_{height}.{extension} +``` + +Just like the conversion file names, you can use another format for naming your files +by using your own `FileNamer` class. It is only possible to prefix the name, because other parts are needed in processing responsive images. diff --git a/docs/converting-images/naming-conversion-files.md b/docs/converting-images/naming-conversion-files.md deleted file mode 100644 index 19c0ca389..000000000 --- a/docs/converting-images/naming-conversion-files.md +++ /dev/null @@ -1,32 +0,0 @@ ---- -title: Naming conversion files -weight: 4 ---- - -By default, all conversion files will be named in this format: - -``` -{original-file-name-without-extension}-{name-of-the-conversion}.{extension} -``` - -Should you want to name your conversion file using another format, than you can specify the class name of your own `ConversionFileNamer` in the `conversion_file_namer` key of the `media-library.php` config file. - -The only requirement is that your class extends `Spatie\MediaLibrary\Conversion\ConversionFileNamer`. In your class you should implement the `getFileName` method that returns the name of the file without the extension. - -Here the implementation of `Spatie\MediaLibrary\Conversion\DefaultConversionFileNamer` - -```php -namespace Spatie\MediaLibrary\Conversions; - -use Spatie\MediaLibrary\MediaCollections\Models\Media; - -class DefaultConversionFileNamer extends ConversionFileNamer -{ - public function getFileName(Conversion $conversion, Media $media): string - { - $fileName = pathinfo($media->file_name, PATHINFO_FILENAME); - - return "{$fileName}-{$conversion->getName()}"; - } -} -``` diff --git a/docs/installation-setup.md b/docs/installation-setup.md index b3a215c21..531eab25f 100644 --- a/docs/installation-setup.md +++ b/docs/installation-setup.md @@ -128,10 +128,9 @@ return [ 'default_loading_attribute_value' => null, /* - * This is the class that is responsible for naming conversion files. By default, - * it will use the filename of the original and concatenate the conversion name to it. + * This is the class that is responsible for naming generated files. */ - 'conversion_file_namer' => \Spatie\MediaLibrary\Conversions\DefaultConversionFileNamer::class, + 'file_namer' => Spatie\MediaLibrary\Support\FileNamer\DefaultFileNamer::class, /* * The class that contains the strategy for determining a media file's path. diff --git a/src/Conversions/Conversion.php b/src/Conversions/Conversion.php index 4d03e2302..32480f07f 100644 --- a/src/Conversions/Conversion.php +++ b/src/Conversions/Conversion.php @@ -5,13 +5,14 @@ use BadMethodCallException; use Spatie\Image\Manipulations; use Spatie\MediaLibrary\MediaCollections\Models\Media; +use Spatie\MediaLibrary\Support\FileNamer\FileNamer; /** @mixin \Spatie\Image\Manipulations */ class Conversion { protected string $name = ''; - protected ConversionFileNamer $conversionFileNamer; + protected FileNamer $fileNamer; protected float $extractVideoFrameAtSecond = 0; @@ -37,7 +38,7 @@ public function __construct(string $name) ->optimize(config('media-library.image_optimizers')) ->format(Manipulations::FORMAT_JPG); - $this->conversionFileNamer = app(config('media-library.conversion_file_namer')); + $this->fileNamer = app(config('media-library.file_namer')); $this->loadingAttributeValue = config('media-library.default_loading_attribute_value'); @@ -217,8 +218,10 @@ public function getResultExtension(string $originalFileExtension = ''): string public function getConversionFile(Media $media): string { - $fileName = $this->conversionFileNamer->getFileName($this, $media); - $extension = $this->conversionFileNamer->getExtension($this, $media); + $fileName = $this->fileNamer->conversionFileName($media->file_name, $this); + + $fileExtension = $this->fileNamer->extensionFromBaseImage($media->file_name); + $extension = $this->getResultExtension($fileExtension) ?: $fileExtension; return "{$fileName}.{$extension}"; } diff --git a/src/Conversions/ConversionFileNamer.php b/src/Conversions/ConversionFileNamer.php deleted file mode 100644 index 45badd9a7..000000000 --- a/src/Conversions/ConversionFileNamer.php +++ /dev/null @@ -1,17 +0,0 @@ -file_name, PATHINFO_EXTENSION); - - return $conversion->getResultExtension($fileExtension) ?: $fileExtension; - } -} diff --git a/src/Conversions/DefaultConversionFileNamer.php b/src/Conversions/DefaultConversionFileNamer.php deleted file mode 100644 index 34bb1bfce..000000000 --- a/src/Conversions/DefaultConversionFileNamer.php +++ /dev/null @@ -1,15 +0,0 @@ -file_name, PATHINFO_FILENAME); - - return "{$fileName}-{$conversion->getName()}"; - } -} diff --git a/src/ResponsiveImages/ResponsiveImageGenerator.php b/src/ResponsiveImages/ResponsiveImageGenerator.php index 39e89ccb5..4935da9f2 100644 --- a/src/ResponsiveImages/ResponsiveImageGenerator.php +++ b/src/ResponsiveImages/ResponsiveImageGenerator.php @@ -11,6 +11,7 @@ use Spatie\MediaLibrary\ResponsiveImages\TinyPlaceholderGenerator\TinyPlaceholderGenerator; use Spatie\MediaLibrary\ResponsiveImages\WidthCalculator\WidthCalculator; use Spatie\MediaLibrary\Support\File; +use Spatie\MediaLibrary\Support\FileNamer\FileNamer; use Spatie\MediaLibrary\Support\ImageFactory; use Spatie\MediaLibrary\Support\TemporaryDirectory; use Spatie\TemporaryDirectory\TemporaryDirectory as BaseTemporaryDirectory; @@ -25,6 +26,8 @@ class ResponsiveImageGenerator protected const DEFAULT_CONVERSION_QUALITY = 90; + protected FileNamer $fileNamer; + public function __construct( Filesystem $filesystem, WidthCalculator $widthCalculator, @@ -35,6 +38,8 @@ public function __construct( $this->widthCalculator = $widthCalculator; $this->tinyPlaceholderGenerator = $tinyPlaceholderGenerator; + + $this->fileNamer = app(config('media-library.file_namer')); } public function generateResponsiveImages(Media $media): void @@ -87,11 +92,8 @@ public function generateResponsiveImage( BaseTemporaryDirectory $temporaryDirectory, int $conversionQuality = self::DEFAULT_CONVERSION_QUALITY ): void { - $responsiveImagePath = $this->appendToFileName( - $media->file_name, - "___{$conversionName}_{$targetWidth}", - $baseImage - ); + $extension = $this->fileNamer->extensionFromBaseImage($baseImage); + $responsiveImagePath = $this->fileNamer->temporaryFileName($media, $extension); $tempDestination = $temporaryDirectory->path($responsiveImagePath); @@ -103,15 +105,22 @@ public function generateResponsiveImage( $responsiveImageHeight = ImageFactory::load($tempDestination)->getHeight(); - $finalImageFileName = $this->appendToFileName($responsiveImagePath, "_{$responsiveImageHeight}"); + // Users can customize the name like they want, but we expect the last part in a certain format + $fileName = $this->addPropertiesToFileName( + $responsiveImagePath, + $conversionName, + $targetWidth, + $responsiveImageHeight, + $extension + ); - $finalResponsiveImagePath = $temporaryDirectory->path($finalImageFileName); + $responsiveImagePath = $temporaryDirectory->path($fileName); - rename($tempDestination, $finalResponsiveImagePath); + rename($tempDestination, $responsiveImagePath); - $this->filesystem->copyToMediaLibrary($finalResponsiveImagePath, $media, 'responsiveImages'); + $this->filesystem->copyToMediaLibrary($responsiveImagePath, $media, 'responsiveImages'); - ResponsiveImage::register($media, $finalImageFileName, $conversionName); + ResponsiveImage::register($media, $fileName, $conversionName); } public function generateTinyJpg( @@ -177,4 +186,10 @@ protected function cleanResponsiveImages(Media $media, string $conversionName = return $media; } + + protected function addPropertiesToFileName(string $fileName, string $conversionName, int $width, int $height, string $extension): string + { + $fileName = pathinfo($fileName, PATHINFO_FILENAME); + return "{$fileName}___{$conversionName}_{$width}_{$height}.{$extension}"; + } } diff --git a/src/Support/FileNamer/DefaultFileNamer.php b/src/Support/FileNamer/DefaultFileNamer.php new file mode 100644 index 000000000..02bac581c --- /dev/null +++ b/src/Support/FileNamer/DefaultFileNamer.php @@ -0,0 +1,20 @@ +getName()}"; + } + + public function responsiveFileName(string $fileName): string + { + return pathinfo($fileName, PATHINFO_FILENAME); + } +} diff --git a/src/Support/FileNamer/FileNamer.php b/src/Support/FileNamer/FileNamer.php new file mode 100644 index 000000000..25d520488 --- /dev/null +++ b/src/Support/FileNamer/FileNamer.php @@ -0,0 +1,23 @@ +responsiveFileName($media->file_name)}.{$extension}"; + } + + public function extensionFromBaseImage(string $baseImage): string + { + return pathinfo($baseImage, PATHINFO_EXTENSION); + } +} diff --git a/tests/Conversions/ConversionFileNamerTest.php b/tests/Conversions/ConversionFileNamerTest.php index 91649cb46..6d2d1c27a 100644 --- a/tests/Conversions/ConversionFileNamerTest.php +++ b/tests/Conversions/ConversionFileNamerTest.php @@ -3,25 +3,27 @@ namespace Spatie\MediaLibrary\Tests\Conversions; use Spatie\MediaLibrary\Tests\TestCase; -use Spatie\MediaLibrary\Tests\TestSupport\testfiles\TestConversionFileNamer; +use Spatie\MediaLibrary\Tests\TestSupport\TestFileNamer; class ConversionFileNamerTest extends TestCase { + public string $fileName = "prefix_test_suffix"; + /** @test */ - public function it_can_use_a_custom_conversion_file_namer() + public function it_can_use_a_custom_file_namer() { - config()->set('media-library.conversion_file_namer', TestConversionFileNamer::class); + config()->set("media-library.file_namer", TestFileNamer::class); $this ->testModelWithConversion ->addMedia($this->getTestJpg()) ->toMediaCollection(); - $path = $this->testModelWithConversion->refresh()->getFirstMediaPath('default', 'thumb'); + $path = $this->testModelWithConversion->refresh()->getFirstMediaPath("default", "thumb"); - $this->assertStringEndsWith('test---thumb.jpg', $path); + $this->assertStringEndsWith("{$this->fileName}---thumb.jpg", $path); $this->assertFileExists($path); - $this->assertEquals('/media/1/conversions/test---thumb.jpg', $this->testModelWithConversion->getFirstMediaUrl('default', 'thumb')); + $this->assertEquals("/media/1/conversions/{$this->fileName}---thumb.jpg", $this->testModelWithConversion->getFirstMediaUrl("default", "thumb")); } } diff --git a/tests/ResponsiveImages/ResponsiveImageFileNamerTest.php b/tests/ResponsiveImages/ResponsiveImageFileNamerTest.php new file mode 100644 index 000000000..5d8b16374 --- /dev/null +++ b/tests/ResponsiveImages/ResponsiveImageFileNamerTest.php @@ -0,0 +1,17 @@ +set("media-library.file_namer", TestFileNamer::class); + + $this->fileName = "prefix_test_suffix"; + } +} diff --git a/tests/ResponsiveImages/ResponsiveImageGeneratorFileNamerTest.php b/tests/ResponsiveImages/ResponsiveImageGeneratorFileNamerTest.php new file mode 100644 index 000000000..d89dce928 --- /dev/null +++ b/tests/ResponsiveImages/ResponsiveImageGeneratorFileNamerTest.php @@ -0,0 +1,17 @@ +set("media-library.file_namer", TestFileNamer::class); + + $this->fileName = "prefix_test_suffix"; + } +} diff --git a/tests/ResponsiveImages/ResponsiveImageGeneratorTest.php b/tests/ResponsiveImages/ResponsiveImageGeneratorTest.php index 71820aeeb..9505ef1f7 100644 --- a/tests/ResponsiveImages/ResponsiveImageGeneratorTest.php +++ b/tests/ResponsiveImages/ResponsiveImageGeneratorTest.php @@ -8,6 +8,8 @@ class ResponsiveImageGeneratorTest extends TestCase { + public string $fileName = "test"; + /** @test */ public function it_can_generate_responsive_images() { @@ -16,9 +18,9 @@ public function it_can_generate_responsive_images() ->withResponsiveImages() ->toMediaCollection(); - $this->assertFileExists($this->getTempDirectory('media/1/responsive-images/test___media_library_original_237_195.jpg')); - $this->assertFileExists($this->getTempDirectory('media/1/responsive-images/test___media_library_original_284_233.jpg')); - $this->assertFileExists($this->getTempDirectory('media/1/responsive-images/test___media_library_original_340_280.jpg')); + $this->assertFileExists($this->getTempDirectory("media/1/responsive-images/{$this->fileName}___media_library_original_237_195.jpg")); + $this->assertFileExists($this->getTempDirectory("media/1/responsive-images/{$this->fileName}___media_library_original_284_233.jpg")); + $this->assertFileExists($this->getTempDirectory("media/1/responsive-images/{$this->fileName}___media_library_original_340_280.jpg")); } /** @test */ @@ -29,11 +31,11 @@ public function it_will_generate_responsive_images_if_withResponsiveImagesIf_ret ->withResponsiveImagesIf(fn () => true) ->toMediaCollection(); - $this->assertFileExists($this->getTempDirectory('media/1/responsive-images/test___media_library_original_237_195.jpg')); - $this->assertFileExists($this->getTempDirectory('media/1/responsive-images/test___media_library_original_284_233.jpg')); - $this->assertFileExists($this->getTempDirectory('media/1/responsive-images/test___media_library_original_340_280.jpg')); + $this->assertFileExists($this->getTempDirectory("media/1/responsive-images/{$this->fileName}___media_library_original_237_195.jpg")); + $this->assertFileExists($this->getTempDirectory("media/1/responsive-images/{$this->fileName}___media_library_original_284_233.jpg")); + $this->assertFileExists($this->getTempDirectory("media/1/responsive-images/{$this->fileName}___media_library_original_340_280.jpg")); } - + /** @test */ public function it_will_not_generate_responsive_images_if_withResponsiveImagesIf_returns_false() { @@ -42,7 +44,7 @@ public function it_will_not_generate_responsive_images_if_withResponsiveImagesIf ->withResponsiveImagesIf(fn () => false) ->toMediaCollection(); - $this->assertFileDoesNotExist($this->getTempDirectory('media/1/responsive-images/test___media_library_original_237_195.jpg')); + $this->assertFileDoesNotExist($this->getTempDirectory("media/1/responsive-images/{$this->fileName}___media_library_original_237_195.jpg")); } /** @test */ @@ -53,7 +55,7 @@ public function its_conversions_can_have_responsive_images() ->withResponsiveImages() ->toMediaCollection(); - $this->assertFileExists($this->getTempDirectory('media/1/responsive-images/test___thumb_50_41.jpg')); + $this->assertFileExists($this->getTempDirectory("media/1/responsive-images/{$this->fileName}___thumb_50_41.jpg")); } /** @test */ @@ -64,7 +66,7 @@ public function its_conversions_can_have_responsive_images_and_change_format() ->withResponsiveImages() ->toMediaCollection(); - $this->assertFileExists($this->getTempDirectory('media/1/responsive-images/test___pngtojpg_700_883.jpg')); + $this->assertFileExists($this->getTempDirectory("media/1/responsive-images/{$this->fileName}___pngtojpg_700_883.jpg")); } /** @test */ @@ -84,32 +86,32 @@ public function it_triggers_an_event_when_the_responsive_images_are_generated() public function it_cleans_the_responsive_images_urls_from_the_db_before_regeneration() { $media = $this->testModelWithResponsiveImages - ->addMedia($this->getTestFilesDirectory('test.jpg')) + ->addMedia($this->getTestFilesDirectory("test.jpg")) ->withResponsiveImages() ->toMediaCollection(); - $this->assertCount(1, $media->fresh()->responsive_images['thumb']['urls']); + $this->assertCount(1, $media->fresh()->responsive_images["thumb"]["urls"]); - $this->artisan('media-library:regenerate'); - $this->assertCount(1, $media->fresh()->responsive_images['thumb']['urls']); + $this->artisan("media-library:regenerate"); + $this->assertCount(1, $media->fresh()->responsive_images["thumb"]["urls"]); } /** @test */ public function it_will_add_responsive_image_entries_when_there_were_none_when_regenerating() { $media = $this->testModelWithResponsiveImages - ->addMedia($this->getTestFilesDirectory('test.jpg')) + ->addMedia($this->getTestFilesDirectory("test.jpg")) ->withResponsiveImages() ->toMediaCollection(); // remove all responsive image db entries $responsiveImages = $media->responsive_images; - $responsiveImages['thumb']['urls'] = []; + $responsiveImages["thumb"]["urls"] = []; $media->responsive_images = $responsiveImages; $media->save(); - $this->assertCount(0, $media->fresh()->responsive_images['thumb']['urls']); + $this->assertCount(0, $media->fresh()->responsive_images["thumb"]["urls"]); - $this->artisan('media-library:regenerate'); - $this->assertCount(1, $media->fresh()->responsive_images['thumb']['urls']); + $this->artisan("media-library:regenerate"); + $this->assertCount(1, $media->fresh()->responsive_images["thumb"]["urls"]); } } diff --git a/tests/ResponsiveImages/ResponsiveImageTest.php b/tests/ResponsiveImages/ResponsiveImageTest.php index 9a0a11d46..0463514c1 100644 --- a/tests/ResponsiveImages/ResponsiveImageTest.php +++ b/tests/ResponsiveImages/ResponsiveImageTest.php @@ -6,6 +6,8 @@ class ResponsiveImageTest extends TestCase { + public string $fileName = 'test'; + /** @test */ public function a_media_instance_can_get_responsive_image_urls() { @@ -18,16 +20,16 @@ public function a_media_instance_can_get_responsive_image_urls() $media = $this->testModelWithResponsiveImages->getFirstMedia(); $this->assertEquals([ - 'http://localhost/media/1/responsive-images/test___media_library_original_340_280.jpg', - 'http://localhost/media/1/responsive-images/test___media_library_original_284_233.jpg', - 'http://localhost/media/1/responsive-images/test___media_library_original_237_195.jpg', + "http://localhost/media/1/responsive-images/{$this->fileName}___media_library_original_340_280.jpg", + "http://localhost/media/1/responsive-images/{$this->fileName}___media_library_original_284_233.jpg", + "http://localhost/media/1/responsive-images/{$this->fileName}___media_library_original_237_195.jpg", ], $media->getResponsiveImageUrls()); $this->assertEquals([ - 'http://localhost/media/1/responsive-images/test___thumb_50_41.jpg', - ], $media->getResponsiveImageUrls('thumb')); + "http://localhost/media/1/responsive-images/{$this->fileName}___thumb_50_41.jpg", + ], $media->getResponsiveImageUrls("thumb")); - $this->assertEquals([], $media->getResponsiveImageUrls('non-existing-conversion')); + $this->assertEquals([], $media->getResponsiveImageUrls("non-existing-conversion")); } /** @test */ @@ -41,16 +43,16 @@ public function a_media_instance_can_generate_the_contents_of_scrset() $media = $this->testModelWithResponsiveImages->getFirstMedia(); $this->assertStringContainsString( - 'http://localhost/media/1/responsive-images/test___media_library_original_340_280.jpg 340w, http://localhost/media/1/responsive-images/test___media_library_original_284_233.jpg 284w, http://localhost/media/1/responsive-images/test___media_library_original_237_195.jpg 237w', + "http://localhost/media/1/responsive-images/{$this->fileName}___media_library_original_340_280.jpg 340w, http://localhost/media/1/responsive-images/{$this->fileName}___media_library_original_284_233.jpg 284w, http://localhost/media/1/responsive-images/{$this->fileName}___media_library_original_237_195.jpg 237w", $media->getSrcset() ); - $this->assertStringContainsString('data:image/svg+xml;base64', $media->getSrcset()); + $this->assertStringContainsString("data:image/svg+xml;base64", $media->getSrcset()); $this->assertStringContainsString( - 'http://localhost/media/1/responsive-images/test___thumb_50_41.jpg 50w', - $media->getSrcset('thumb') + "http://localhost/media/1/responsive-images/{$this->fileName}___thumb_50_41.jpg 50w", + $media->getSrcset("thumb") ); - $this->assertStringContainsString('data:image/svg+xml;base64,', $media->getSrcset('thumb')); + $this->assertStringContainsString("data:image/svg+xml;base64,", $media->getSrcset("thumb")); } /** @test */ @@ -65,7 +67,7 @@ public function a_responsive_image_can_return_some_properties() $responsiveImage = $media->responsiveImages()->files->first(); - $this->assertEquals('media_library_original', $responsiveImage->generatedFor()); + $this->assertEquals("media_library_original", $responsiveImage->generatedFor()); $this->assertEquals(340, $responsiveImage->width()); @@ -78,10 +80,10 @@ public function responsive_image_generation_respects_the_conversion_quality_sett $this->testModelWithResponsiveImages ->addMedia($this->getTestJpg()) ->preservingOriginal() - ->toMediaCollection('default'); + ->toMediaCollection("default"); - $standardQualityResponsiveConversion = $this->getTempDirectory('media/1/responsive-images/test___standardQuality_340_280.jpg'); - $lowerQualityResponsiveConversion = $this->getTempDirectory('media/1/responsive-images/test___lowerQuality_340_280.jpg'); + $standardQualityResponsiveConversion = $this->getTempDirectory("media/1/responsive-images/{$this->fileName}___standardQuality_340_280.jpg"); + $lowerQualityResponsiveConversion = $this->getTempDirectory("media/1/responsive-images/{$this->fileName}___lowerQuality_340_280.jpg"); $this->assertLessThan(filesize($standardQualityResponsiveConversion), filesize($lowerQualityResponsiveConversion)); } @@ -92,13 +94,13 @@ public function a_media_instance_can_get_responsive_image_urls_with_conversions_ $this->testModelWithResponsiveImages ->addMedia($this->getTestJpg()) ->withResponsiveImages() - ->storingConversionsOnDisk('secondMediaDisk') + ->storingConversionsOnDisk("secondMediaDisk") ->toMediaCollection(); $media = $this->testModelWithResponsiveImages->getFirstMedia(); $this->assertEquals([ - 'http://localhost/media2/1/responsive-images/test___thumb_50_41.jpg', - ], $media->getResponsiveImageUrls('thumb')); + "http://localhost/media2/1/responsive-images/{$this->fileName}___thumb_50_41.jpg", + ], $media->getResponsiveImageUrls("thumb")); } } diff --git a/tests/TestSupport/TestFileNamer.php b/tests/TestSupport/TestFileNamer.php new file mode 100644 index 000000000..45e91b2be --- /dev/null +++ b/tests/TestSupport/TestFileNamer.php @@ -0,0 +1,23 @@ +getName()}"; + } +} diff --git a/tests/TestSupport/testfiles/TestConversionFileNamer.php b/tests/TestSupport/testfiles/TestConversionFileNamer.php deleted file mode 100644 index c22190f67..000000000 --- a/tests/TestSupport/testfiles/TestConversionFileNamer.php +++ /dev/null @@ -1,17 +0,0 @@ -file_name, PATHINFO_FILENAME); - - return "{$fileName}---{$conversion->getName()}"; - } -}