Skip to content

Commit

Permalink
Merge branch '1.x' into lun-153-publishing-lunar-migrations-causes-an…
Browse files Browse the repository at this point in the history
…-error-on-polymorph
  • Loading branch information
alecritson authored Sep 16, 2024
2 parents e977655 + fc20edb commit ea82e76
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ public function mapVariantPermutations($fillMissing = true): void
)]
)->toArray();

$variants = $this->record->variants->load('values.option')->map(function ($variant) {
$variants = $this->record->variants->load(['basePrices.currency', 'basePrices.priceable', 'values.option'])->map(function ($variant) {
return [
'id' => $variant->id,
'sku' => $variant->sku,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/database/factories/OrderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class OrderFactory extends BaseFactory
public function definition(): array
{
$total = $this->faker->numberBetween(200, 25000);
$taxTotal = ($total - 100) * .2;
$taxTotal = intval(($total - 100) * .2);

return [
'channel_id' => Channel::factory(),
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/Base/ModelManifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public function guessModelClass(string $modelContract): string
return 'Lunar\\Models\\'.$shortName;
}

public function isLunarModel(BaseModel $model): bool
public function isLunarModel(string|BaseModel $model): bool
{
$class = (new \ReflectionClass($model));

Expand Down
18 changes: 18 additions & 0 deletions packages/core/src/Base/Traits/HasModelExtending.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Lunar\Base\Traits;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Arr;
use Lunar\Facades\ModelManifest;

Expand Down Expand Up @@ -46,6 +47,23 @@ public static function modelClass(): string
return ModelManifest::get($contractClass) ?? static::class;
}

public function getMorphClass(): string
{
$morphMap = Relation::morphMap();

if ($customModelMorphMap = array_search(static::modelClass(), $morphMap, true)) {
return $customModelMorphMap;
}

$parentClass = get_parent_class(static::class);

if (ModelManifest::isLunarModel($parentClass) && $lunarModelMorphMap = array_search($parentClass, $morphMap, true)) {
return $lunarModelMorphMap;
}

return parent::getMorphClass();
}

public static function isLunarInstance(): bool
{
return static::class == static::modelClass();
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/Facades/ModelManifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* @method static string|null get(string $interfaceClass)
* @method static string guessContractClass(string $modelClass)
* @method static string guessModelClass(string $modelContract)
* @method static bool isLunarModel(BaseModel $model)
* @method static bool isLunarModel(string|BaseModel $model)
* @method static string getTable(BaseModel $model)
* @method static void morphMap()
* @method static string getMorphMapKey(string $className)
Expand Down
12 changes: 12 additions & 0 deletions tests/core/Unit/Base/Traits/HasModelExtendingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,15 @@ function () {
expect($newStaticMethod)->toBeInstanceOf(Collection::class);
expect($newStaticMethod)->toHaveCount(3);
});

test('morph map is correct when models are extended', function () {
\Lunar\Facades\ModelManifest::replace(
\Lunar\Models\Contracts\Product::class,
\Lunar\Tests\Core\Stubs\Models\CustomProduct::class
);

expect((new \Lunar\Tests\Core\Stubs\Models\CustomProduct)->getMorphClass())
->toBe('product')
->and((new Product)->getMorphClass())
->toBe('product');
});

0 comments on commit ea82e76

Please sign in to comment.