From 9b8676a200778c5e46e0ed48ad18949602523995 Mon Sep 17 00:00:00 2001 From: Alec Ritson Date: Thu, 12 Sep 2024 14:26:39 +0100 Subject: [PATCH 1/7] Fix extended model resolving --- .../src/Base/Traits/HasModelExtending.php | 31 +++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/packages/core/src/Base/Traits/HasModelExtending.php b/packages/core/src/Base/Traits/HasModelExtending.php index c0a0412d94..3561376eba 100644 --- a/packages/core/src/Base/Traits/HasModelExtending.php +++ b/packages/core/src/Base/Traits/HasModelExtending.php @@ -5,23 +5,48 @@ use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Support\Arr; +use Illuminate\Support\Str; +use Lunar\Base\BaseModel; use Lunar\Facades\ModelManifest; trait HasModelExtending { public function newModelQuery(): Builder { - $realClass = static::modelClass(); + $concreteClass = static::modelClass(); + $parentClass = get_parent_class($concreteClass); // If they are both the same class i.e. they haven't changed // then just call the parent method. - if ($this instanceof $realClass) { + if ($parentClass == BaseModel::class || $this instanceof $concreteClass) { return parent::newModelQuery(); } + $newModel = new $concreteClass; + $newModel->id = $this->id; + + foreach (array_keys($this->getAttributes()) as $key) { + $newModel->setAttribute($key, $this->{$key}); + } + return $this->newEloquentBuilder( $this->newBaseQueryBuilder() - )->setModel(new $realClass($this->toArray())); + )->setModel($newModel); + } + + public function getForeignKey(): string + { + $parentClass = get_parent_class($this); + + return $parentClass == BaseModel::class ? parent::getForeignKey() : Str::snake(class_basename($parentClass)).'_'.$this->getKeyName(); + + } + + public function getTable() + { + $parentClass = get_parent_class($this); + + return $parentClass == BaseModel::class ? parent::getTable() : (new $parentClass)->table; } public static function __callStatic($method, $parameters) From 4ec94f469d38b2f8d0b60bebaf1f41115904f4c9 Mon Sep 17 00:00:00 2001 From: Alec Ritson Date: Thu, 12 Sep 2024 14:26:47 +0100 Subject: [PATCH 2/7] Tests --- tests/core/Stubs/Models/CustomOrder.php | 7 +++++++ tests/core/Stubs/Models/Order.php | 7 +++++++ tests/core/Unit/Actions/Carts/CreateOrderTest.php | 11 ++++++++--- tests/core/Unit/Base/Traits/HasModelExtendingTest.php | 7 +++++++ 4 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 tests/core/Stubs/Models/CustomOrder.php create mode 100644 tests/core/Stubs/Models/Order.php diff --git a/tests/core/Stubs/Models/CustomOrder.php b/tests/core/Stubs/Models/CustomOrder.php new file mode 100644 index 0000000000..87164a14fc --- /dev/null +++ b/tests/core/Stubs/Models/CustomOrder.php @@ -0,0 +1,7 @@ +create([ 'default' => true, ]); @@ -223,15 +228,15 @@ function can_update_draft_order() ->and($order->shippingAddress)->toBeInstanceOf(OrderAddress::class) ->and($order->billingAddress)->toBeInstanceOf(OrderAddress::class); - $this->assertDatabaseHas((new Order)->getTable(), $datacheck); - $this->assertDatabaseHas((new OrderLine)->getTable(), [ + assertDatabaseHas((new Order)->getTable(), $datacheck); + assertDatabaseHas((new OrderLine)->getTable(), [ 'identifier' => $shippingOption->getIdentifier(), ]); $order->save(); $containsCurrency = str_contains($order->fresh()->getRawOriginal('tax_breakdown'), '"currency"'); expect($containsCurrency)->toBeFalse(); -}); +})->group('wah'); test('can create order with customer', function () { CustomerGroup::factory()->create([ diff --git a/tests/core/Unit/Base/Traits/HasModelExtendingTest.php b/tests/core/Unit/Base/Traits/HasModelExtendingTest.php index 413a276eb9..ae2481a1dd 100644 --- a/tests/core/Unit/Base/Traits/HasModelExtendingTest.php +++ b/tests/core/Unit/Base/Traits/HasModelExtendingTest.php @@ -38,6 +38,13 @@ function () { expect($sizeOption->sizes)->toHaveCount(1); }); +test('extended model returns correct table name', function () { + expect((new \Lunar\Tests\Core\Stubs\Models\CustomOrder())->getTable()) + ->toBe( + (new \Lunar\Models\Order())->getTable() + ); +}); + test('can forward static method calls to extended model', function () { /** @see \Lunar\Tests\Core\Stubs\Models\ProductOption::getSizesStatic() */ $newStaticMethod = ProductOption::getSizesStatic(); From 43de966f8c69c3cbfb949c3be97312db3e12acda Mon Sep 17 00:00:00 2001 From: alecritson Date: Thu, 12 Sep 2024 13:29:33 +0000 Subject: [PATCH 3/7] chore: fix code style --- tests/core/Stubs/Models/CustomOrder.php | 4 +--- tests/core/Stubs/Models/Order.php | 4 +--- tests/core/Unit/Actions/Carts/CreateOrderTest.php | 1 + tests/core/Unit/Base/Traits/HasModelExtendingTest.php | 4 ++-- 4 files changed, 5 insertions(+), 8 deletions(-) diff --git a/tests/core/Stubs/Models/CustomOrder.php b/tests/core/Stubs/Models/CustomOrder.php index 87164a14fc..7cf7db04ce 100644 --- a/tests/core/Stubs/Models/CustomOrder.php +++ b/tests/core/Stubs/Models/CustomOrder.php @@ -2,6 +2,4 @@ namespace Lunar\Tests\Core\Stubs\Models; -class CustomOrder extends \Lunar\Models\Order -{ -} +class CustomOrder extends \Lunar\Models\Order {} diff --git a/tests/core/Stubs/Models/Order.php b/tests/core/Stubs/Models/Order.php index a4c9779a5a..f6497b8250 100644 --- a/tests/core/Stubs/Models/Order.php +++ b/tests/core/Stubs/Models/Order.php @@ -2,6 +2,4 @@ namespace Lunar\Tests\Core\Stubs\Models; -class Order extends \Lunar\Models\Order -{ -} +class Order extends \Lunar\Models\Order {} diff --git a/tests/core/Unit/Actions/Carts/CreateOrderTest.php b/tests/core/Unit/Actions/Carts/CreateOrderTest.php index 5841c7f439..b075875210 100644 --- a/tests/core/Unit/Actions/Carts/CreateOrderTest.php +++ b/tests/core/Unit/Actions/Carts/CreateOrderTest.php @@ -20,6 +20,7 @@ use Lunar\Models\ProductVariant; use Lunar\Models\TaxClass; use Lunar\Models\TaxRateAmount; + use function Pest\Laravel\{assertDatabaseHas}; uses(\Illuminate\Foundation\Testing\RefreshDatabase::class); diff --git a/tests/core/Unit/Base/Traits/HasModelExtendingTest.php b/tests/core/Unit/Base/Traits/HasModelExtendingTest.php index ae2481a1dd..fed014d621 100644 --- a/tests/core/Unit/Base/Traits/HasModelExtendingTest.php +++ b/tests/core/Unit/Base/Traits/HasModelExtendingTest.php @@ -39,9 +39,9 @@ function () { }); test('extended model returns correct table name', function () { - expect((new \Lunar\Tests\Core\Stubs\Models\CustomOrder())->getTable()) + expect((new \Lunar\Tests\Core\Stubs\Models\CustomOrder)->getTable()) ->toBe( - (new \Lunar\Models\Order())->getTable() + (new \Lunar\Models\Order)->getTable() ); }); From 590e5a750ff9a5e4d615bbe0277d5ca060de0125 Mon Sep 17 00:00:00 2001 From: Alec Ritson Date: Thu, 12 Sep 2024 14:45:10 +0100 Subject: [PATCH 4/7] Update CreateOrderTest.php --- tests/core/Unit/Actions/Carts/CreateOrderTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/core/Unit/Actions/Carts/CreateOrderTest.php b/tests/core/Unit/Actions/Carts/CreateOrderTest.php index 5841c7f439..8ae7c5cb98 100644 --- a/tests/core/Unit/Actions/Carts/CreateOrderTest.php +++ b/tests/core/Unit/Actions/Carts/CreateOrderTest.php @@ -236,7 +236,7 @@ function can_update_draft_order() $order->save(); $containsCurrency = str_contains($order->fresh()->getRawOriginal('tax_breakdown'), '"currency"'); expect($containsCurrency)->toBeFalse(); -})->group('wah'); +}); test('can create order with customer', function () { CustomerGroup::factory()->create([ From cb6c457f9dd939cafa325ab573453d71ac3f6ee7 Mon Sep 17 00:00:00 2001 From: Alec Ritson Date: Mon, 16 Sep 2024 08:36:39 +0100 Subject: [PATCH 5/7] Update HasModelExtending.php --- .../src/Base/Traits/HasModelExtending.php | 37 +++++++++++++++---- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/packages/core/src/Base/Traits/HasModelExtending.php b/packages/core/src/Base/Traits/HasModelExtending.php index 3561376eba..892a243c31 100644 --- a/packages/core/src/Base/Traits/HasModelExtending.php +++ b/packages/core/src/Base/Traits/HasModelExtending.php @@ -3,6 +3,7 @@ namespace Lunar\Base\Traits; use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Support\Arr; use Illuminate\Support\Str; @@ -22,16 +23,36 @@ public function newModelQuery(): Builder return parent::newModelQuery(); } - $newModel = new $concreteClass; - $newModel->id = $this->id; - - foreach (array_keys($this->getAttributes()) as $key) { - $newModel->setAttribute($key, $this->{$key}); - } - return $this->newEloquentBuilder( $this->newBaseQueryBuilder() - )->setModel($newModel); + )->setModel( + static::withoutEvents( + fn () => $this->replicateInto($concreteClass) + ) + ); + } + + public function replicateInto($newClass) + { + $defaults = array_values(array_filter([ + $this->getKeyName(), + $this->getCreatedAtColumn(), + $this->getUpdatedAtColumn(), + ...$this->uniqueIds(), + 'laravel_through_key', + ])); + + $attributes = Arr::except( + $this->getAttributes(), $defaults + ); + + return tap(new $newClass, function ($instance) use ($attributes) : Model { + $instance->setRawAttributes($attributes); + + $instance->setRelations($this->relations); + + return $instance; + }); } public function getForeignKey(): string From a3fb640bb43e0ecac5da7d325434500f62112f44 Mon Sep 17 00:00:00 2001 From: alecritson Date: Mon, 16 Sep 2024 07:39:21 +0000 Subject: [PATCH 6/7] chore: fix code style --- packages/core/src/Base/Traits/HasModelExtending.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/Base/Traits/HasModelExtending.php b/packages/core/src/Base/Traits/HasModelExtending.php index 892a243c31..6a92ba56c3 100644 --- a/packages/core/src/Base/Traits/HasModelExtending.php +++ b/packages/core/src/Base/Traits/HasModelExtending.php @@ -46,7 +46,7 @@ public function replicateInto($newClass) $this->getAttributes(), $defaults ); - return tap(new $newClass, function ($instance) use ($attributes) : Model { + return tap(new $newClass, function ($instance) use ($attributes): Model { $instance->setRawAttributes($attributes); $instance->setRelations($this->relations); From 43dbae84d58770bdbeabf39a1a0ad43a504c02cc Mon Sep 17 00:00:00 2001 From: alecritson Date: Tue, 17 Sep 2024 08:33:27 +0000 Subject: [PATCH 7/7] chore: fix code style --- packages/core/src/Base/Traits/HasModelExtending.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/core/src/Base/Traits/HasModelExtending.php b/packages/core/src/Base/Traits/HasModelExtending.php index 6ecfffe38f..a8e0f2a689 100644 --- a/packages/core/src/Base/Traits/HasModelExtending.php +++ b/packages/core/src/Base/Traits/HasModelExtending.php @@ -96,10 +96,11 @@ public static function modelClass(): string /** * Returns the model alias registered in the model relation morph map. */ - public static function morphName():string{ + public static function morphName(): string + { return (new (static::modelClass()))->getMorphClass(); } - + public function getMorphClass(): string { $morphMap = Relation::morphMap();