Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: add remark for custom model definition #1966

Merged
merged 5 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions packages/core/config/media.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
return [

'definitions' => [
Lunar\Models\Asset::class => StandardMediaDefinitions::class,
Lunar\Models\Brand::class => StandardMediaDefinitions::class,
Lunar\Models\Collection::class => StandardMediaDefinitions::class,
Lunar\Models\Product::class => StandardMediaDefinitions::class,
Lunar\Models\ProductOption::class => StandardMediaDefinitions::class,
Lunar\Models\ProductOptionValue::class => StandardMediaDefinitions::class,
'asset' => StandardMediaDefinitions::class,
'brand' => StandardMediaDefinitions::class,
'collection' => StandardMediaDefinitions::class,
'product' => StandardMediaDefinitions::class,
'product-option' => StandardMediaDefinitions::class,
'product-option-value' => StandardMediaDefinitions::class,
],

'collection' => 'images',
Expand Down
7 changes: 6 additions & 1 deletion packages/core/src/Base/Traits/HasMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ protected function getDefinitionClass()
{
$conversionClasses = config('lunar.media.definitions', []);

return $conversionClasses[static::class] ?? StandardMediaDefinitions::class;
$alias = \Illuminate\Support\Str::snake(class_basename(static::class));

return $conversionClasses[$alias]
?? $conversionClasses[static::class] // fallback for published config
?? $conversionClasses[get_parent_class(static::class)] // fallback use parent class
?? StandardMediaDefinitions::class;
}
}
7 changes: 7 additions & 0 deletions tests/core/Stubs/TestStandardMediaDefinitions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Lunar\Tests\Core\Stubs;

use Lunar\Base\StandardMediaDefinitions;

class TestStandardMediaDefinitions extends StandardMediaDefinitions {}
29 changes: 28 additions & 1 deletion tests/core/Unit/Traits/HasMediaTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

uses(\Lunar\Tests\Core\TestCase::class);
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Config;
use Lunar\Base\StandardMediaDefinitions;
use Lunar\Models\Product;
use Lunar\Tests\Core\Stubs\TestStandardMediaDefinitions;

uses(\Illuminate\Foundation\Testing\RefreshDatabase::class);

Expand All @@ -12,7 +14,7 @@

expect($definitions)->toHaveCount(6);

expect($definitions[Product::class])->toEqual(StandardMediaDefinitions::class);
expect($definitions['product'])->toEqual(StandardMediaDefinitions::class);

$file = UploadedFile::fake()->image('avatar.jpg');

Expand All @@ -28,6 +30,31 @@
expect($image->hasGeneratedConversion('zoom'))->toBeTrue();
});

test('custom conversions are loaded', function () {
Config::set('lunar.media.definitions', [
'product' => TestStandardMediaDefinitions::class,
]);

$product = invade(new Product);

expect($product->getDefinitionClass())->toEqual(TestStandardMediaDefinitions::class);
});

test('custom conversions are loaded for extended model', function () {
\Lunar\Facades\ModelManifest::replace(
\Lunar\Models\Contracts\Product::class,
\Lunar\Tests\Core\Stubs\Models\Product::class
);

Config::set('lunar.media.definitions', [
'product' => TestStandardMediaDefinitions::class,
]);

$product = invade(app(\Lunar\Models\Contracts\Product::class));

expect($product->getDefinitionClass())->toEqual(TestStandardMediaDefinitions::class);
});

test('images can have fallback url', function () {
$testImageUrl = 'https://picsum.photos/200';
config()->set('lunar.media.fallback.url', $testImageUrl);
Expand Down