Skip to content

Commit

Permalink
Fix tests for new formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
AngryMoustache committed Sep 21, 2023
1 parent fe30d13 commit 959bbe1
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 41 deletions.
20 changes: 11 additions & 9 deletions tests/Mixins/UploadedFileMixinTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions tests/TestFormats/TestHero.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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',
]);
}
}
8 changes: 8 additions & 0 deletions tests/TestFormats/TestHeroWebp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -16,4 +17,11 @@ public function definition(): Manipulations
->format(Manipulations::FORMAT_WEBP)
->sepia();
}

public function registerModelsForFormatter(): void
{
$this->registerFor(TestModel::class, [
'test_id',
]);
}
}
6 changes: 1 addition & 5 deletions tests/TestModels/TestModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}

}
17 changes: 8 additions & 9 deletions tests/Unit/Collections/FormatsCollectionTest.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
<?php

use Codedor\MediaLibrary\Facades\Formats;
use Codedor\MediaLibrary\Formats\Format;
use Codedor\MediaLibrary\Tests\TestFormats\TestHero;
use Codedor\MediaLibrary\Tests\TestFormats\TestHeroWebp;
use Codedor\MediaLibrary\Tests\TestModels\TestModel;

it('registers model', function () {
expect(Formats::registerForModel(TestModel::class))
expect(Formats::register([TestHero::class]))
->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);
Expand All @@ -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');
});
22 changes: 7 additions & 15 deletions tests/Unit/Conversions/LocalConversionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand Down Expand Up @@ -79,9 +75,7 @@
->andReturn('sdf');
});

Formats::registerForModels([
TestModel::class,
]);
Formats::register([TestHero::class, TestHeroWebp::class]);

/** @var Attachment $attachment */
$attachment = createAttachment([
Expand Down Expand Up @@ -116,9 +110,7 @@
->andReturn('sdf');
});

Formats::registerForModels([
TestModel::class,
]);
Formats::register([TestHero::class, TestHeroWebp::class]);

/** @var Attachment $attachment */
$attachment = createAttachment([
Expand Down
6 changes: 3 additions & 3 deletions tests/Unit/Models/AttachmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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([
Expand Down

0 comments on commit 959bbe1

Please sign in to comment.