From 959bbe172d7fc67e3cc524cb1a96430b6bdaa15d Mon Sep 17 00:00:00 2001 From: Sander Van Damme Date: Thu, 21 Sep 2023 09:06:59 +0000 Subject: [PATCH] Fix tests for new formatter --- tests/Mixins/UploadedFileMixinTest.php | 20 +++++++++-------- tests/TestFormats/TestHero.php | 8 +++++++ tests/TestFormats/TestHeroWebp.php | 8 +++++++ tests/TestModels/TestModel.php | 6 +---- .../Collections/FormatsCollectionTest.php | 17 +++++++------- .../Unit/Conversions/LocalConversionTest.php | 22 ++++++------------- tests/Unit/Models/AttachmentTest.php | 6 ++--- 7 files changed, 46 insertions(+), 41 deletions(-) diff --git a/tests/Mixins/UploadedFileMixinTest.php b/tests/Mixins/UploadedFileMixinTest.php index 7335314..3e6f46c 100644 --- a/tests/Mixins/UploadedFileMixinTest.php +++ b/tests/Mixins/UploadedFileMixinTest.php @@ -2,6 +2,8 @@ use Codedor\MediaLibrary\Jobs\GenerateAttachmentFormat; use Codedor\MediaLibrary\Models\Attachment; +use Codedor\MediaLibrary\Tests\TestFormats\TestHero; +use Codedor\MediaLibrary\Tests\TestFormats\TestHeroWebp; use Codedor\MediaLibrary\Tests\TestModels\TestModel; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Http\UploadedFile; @@ -17,9 +19,9 @@ Queue::fake(); Storage::fake('public'); - \Codedor\MediaLibrary\Facades\Formats::registerForModels([ - TestModel::class, - Attachment::class, + \Codedor\MediaLibrary\Facades\Formats::register([ + TestHero::class, + TestHeroWebp::class, ]); $file = UploadedFile::fake()->image('test.jpg', 100, 100); @@ -37,9 +39,9 @@ Queue::fake(); Storage::fake('public'); - \Codedor\MediaLibrary\Facades\Formats::registerForModels([ - TestModel::class, - Attachment::class, + \Codedor\MediaLibrary\Facades\Formats::register([ + TestHero::class, + TestHeroWebp::class, ]); assertDatabaseCount(Attachment::class, 0); @@ -72,9 +74,9 @@ Queue::fake(); $disk = 'local'; - \Codedor\MediaLibrary\Facades\Formats::registerForModels([ - TestModel::class, - Attachment::class, + \Codedor\MediaLibrary\Facades\Formats::register([ + TestHero::class, + TestHeroWebp::class, ]); Storage::fake($disk); diff --git a/tests/TestFormats/TestHero.php b/tests/TestFormats/TestHero.php index b96985d..5d3d2c6 100644 --- a/tests/TestFormats/TestHero.php +++ b/tests/TestFormats/TestHero.php @@ -3,6 +3,7 @@ namespace Codedor\MediaLibrary\Tests\TestFormats; use Codedor\MediaLibrary\Formats\Format; +use Codedor\MediaLibrary\Tests\TestModels\TestModel; use Spatie\Image\Manipulations; class TestHero extends Format @@ -15,4 +16,11 @@ public function definition(): Manipulations ->fit(Manipulations::FIT_CROP, 100, 100) ->sepia(); } + + public function registerModelsForFormatter(): void + { + $this->registerFor(TestModel::class, [ + 'test_id', + ]); + } } diff --git a/tests/TestFormats/TestHeroWebp.php b/tests/TestFormats/TestHeroWebp.php index c85e740..de53905 100644 --- a/tests/TestFormats/TestHeroWebp.php +++ b/tests/TestFormats/TestHeroWebp.php @@ -3,6 +3,7 @@ namespace Codedor\MediaLibrary\Tests\TestFormats; use Codedor\MediaLibrary\Formats\Format; +use Codedor\MediaLibrary\Tests\TestModels\TestModel; use Spatie\Image\Manipulations; class TestHeroWebp extends Format @@ -16,4 +17,11 @@ public function definition(): Manipulations ->format(Manipulations::FORMAT_WEBP) ->sepia(); } + + public function registerModelsForFormatter(): void + { + $this->registerFor(TestModel::class, [ + 'test_id', + ]); + } } diff --git a/tests/TestModels/TestModel.php b/tests/TestModels/TestModel.php index fb67e8b..e8d6bfe 100644 --- a/tests/TestModels/TestModel.php +++ b/tests/TestModels/TestModel.php @@ -10,9 +10,5 @@ class TestModel extends Model implements HasFormats { - public static function getFormats(Collection $formats): Collection - { - return $formats->add(TestHero::make('test_id')) - ->add(TestHeroWebp::make('test_id')); - } + } diff --git a/tests/Unit/Collections/FormatsCollectionTest.php b/tests/Unit/Collections/FormatsCollectionTest.php index 15318dd..d85e147 100644 --- a/tests/Unit/Collections/FormatsCollectionTest.php +++ b/tests/Unit/Collections/FormatsCollectionTest.php @@ -1,22 +1,22 @@ toBeInstanceOf(\Codedor\MediaLibrary\Collections\Formats::class) ->toHaveKey(TestModel::class) ->first() - ->toHaveCount(2) ->first() - ->first() - ->toBeInstanceOf(TestHero::class); + ->toBeInstanceOf(Format::class); }); it('returns format if format is registered', function () { - Formats::registerForModel(TestModel::class); + Formats::register([TestHero::class, TestHeroWebp::class]); expect(Formats::exists('test-hero')) ->toBeInstanceOf(TestHero::class); @@ -26,17 +26,16 @@ }); it('returns null if format is not registered', function () { - Formats::registerForModel(TestModel::class); + Formats::register([TestHero::class, TestHeroWebp::class]); expect(Formats::exists('format-does-not-exist')) ->toBeNull(); }); it('returns collection with kebab keys', function () { - Formats::registerForModel(TestModel::class); + Formats::register([TestHero::class, TestHeroWebp::class]); expect(Formats::mapToKebab()) ->toHaveKey('test-hero') - ->toHaveKey('test-hero-webp') - ->toHaveCount(2); + ->toHaveKey('test-hero-webp'); }); diff --git a/tests/Unit/Conversions/LocalConversionTest.php b/tests/Unit/Conversions/LocalConversionTest.php index 81a7168..3d70856 100644 --- a/tests/Unit/Conversions/LocalConversionTest.php +++ b/tests/Unit/Conversions/LocalConversionTest.php @@ -3,6 +3,8 @@ use Codedor\MediaLibrary\Conversions\LocalConversion; use Codedor\MediaLibrary\Facades\Formats; use Codedor\MediaLibrary\Models\Attachment; +use Codedor\MediaLibrary\Tests\TestFormats\TestHero; +use Codedor\MediaLibrary\Tests\TestFormats\TestHeroWebp; use Codedor\MediaLibrary\Tests\TestModels\TestModel; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Support\Facades\File; @@ -14,9 +16,7 @@ uses(RefreshDatabase::class); it('skips generation if attachment is not an image', function () { - Formats::registerForModels([ - TestModel::class, - ]); + Formats::register([TestHero::class, TestHeroWebp::class]); $attachment = createAttachment([ 'type' => 'not-an-image', @@ -31,9 +31,7 @@ }); it('skips generation if attachment is a gif', function () { - Formats::registerForModels([ - TestModel::class, - ]); + Formats::register([TestHero::class, TestHeroWebp::class]); $attachment = createAttachment([ 'type' => 'image', @@ -49,9 +47,7 @@ it('skips generation if force is false and format image exists', function () { Storage::fake('public'); - Formats::registerForModels([ - TestModel::class, - ]); + Formats::register([TestHero::class, TestHeroWebp::class]); $attachment = createAttachment([ 'type' => 'image', @@ -79,9 +75,7 @@ ->andReturn('sdf'); }); - Formats::registerForModels([ - TestModel::class, - ]); + Formats::register([TestHero::class, TestHeroWebp::class]); /** @var Attachment $attachment */ $attachment = createAttachment([ @@ -116,9 +110,7 @@ ->andReturn('sdf'); }); - Formats::registerForModels([ - TestModel::class, - ]); + Formats::register([TestHero::class, TestHeroWebp::class]); /** @var Attachment $attachment */ $attachment = createAttachment([ diff --git a/tests/Unit/Models/AttachmentTest.php b/tests/Unit/Models/AttachmentTest.php index c13b6cc..bf7cf75 100644 --- a/tests/Unit/Models/AttachmentTest.php +++ b/tests/Unit/Models/AttachmentTest.php @@ -2,6 +2,8 @@ use Codedor\MediaLibrary\Facades\Formats; use Codedor\MediaLibrary\Models\Attachment; +use Codedor\MediaLibrary\Tests\TestFormats\TestHero; +use Codedor\MediaLibrary\Tests\TestFormats\TestHeroWebp; use Codedor\MediaLibrary\Tests\TestModels\TestModel; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Support\Facades\File; @@ -30,9 +32,7 @@ }); it('returns the right url for a format', function () { - Formats::registerForModels([ - TestModel::class, - ]); + Formats::register([TestHero::class, TestHeroWebp::class]); /** @var Attachment $attachment */ $attachment = createAttachment([