From 395ee98022874eb821fd7b595d5cfeef2c2c905d Mon Sep 17 00:00:00 2001 From: Jakub Theimer <5587309+theimerj@users.noreply.github.com> Date: Mon, 19 Aug 2024 12:27:51 +0200 Subject: [PATCH 1/3] Fix order line observer (#1732) --- .../core/src/Observers/OrderLineObserver.php | 4 +- .../tests/Stubs/DataTypes/TestPurchasable.php | 139 ++++++++++++++++++ 2 files changed, 141 insertions(+), 2 deletions(-) create mode 100644 packages/core/tests/Stubs/DataTypes/TestPurchasable.php diff --git a/packages/core/src/Observers/OrderLineObserver.php b/packages/core/src/Observers/OrderLineObserver.php index 91d3fbe700..7486adc21b 100644 --- a/packages/core/src/Observers/OrderLineObserver.php +++ b/packages/core/src/Observers/OrderLineObserver.php @@ -15,7 +15,7 @@ class OrderLineObserver */ public function creating(OrderLine $orderLine) { - if ($orderLine->type != 'shipping' && ! $orderLine->purchasable instanceof Purchasable) { + if (! in_array(Purchasable::class, class_implements($orderLine->purchasable_type, true))) { throw new NonPurchasableItemException($orderLine->purchasable_type); } } @@ -27,7 +27,7 @@ public function creating(OrderLine $orderLine) */ public function updating(OrderLine $orderLine) { - if ($orderLine->type != 'shipping' && ! $orderLine->purchasable instanceof Purchasable) { + if (! in_array(Purchasable::class, class_implements($orderLine->purchasable_type, true))) { throw new NonPurchasableItemException($orderLine->purchasable_type); } } diff --git a/packages/core/tests/Stubs/DataTypes/TestPurchasable.php b/packages/core/tests/Stubs/DataTypes/TestPurchasable.php new file mode 100644 index 0000000000..d572b65337 --- /dev/null +++ b/packages/core/tests/Stubs/DataTypes/TestPurchasable.php @@ -0,0 +1,139 @@ +price; + } + + /** + * Get prices for the purchasable item. + */ + public function getPrices(): Collection + { + return collect([ + $this->price, + ]); + } + + /** + * Return the purchasable unit quantity. + */ + public function getUnitQuantity(): int + { + return 1; + } + + /** + * Return the purchasable tax class. + */ + public function getTaxClass(): TaxClass + { + return $this->taxClass; + } + + /** + * Return the purchasable tax reference. + * + * @return string|null + */ + public function getTaxReference() + { + return $this->taxReference; + } + + /** + * Return what type of purchasable this is, i.e. physical,digital,shipping. + * + * @return string + */ + public function getType() + { + return 'test-purchsable'; + } + + /** + * Return the name for the purchasable. + * + * @return string + */ + public function getName() + { + return $this->name; + } + + /** + * Return the description for the purchasable. + * + * @return string + */ + public function getDescription() + { + return $this->description; + } + + /** + * Return the option for this purchasable. + * + * @return string|null + */ + public function getOption() + { + return $this->option; + } + + /** + * Return a unique string which identifies the purchasable item. + * + * @return string + */ + public function getIdentifier() + { + return $this->identifier; + } + + /** + * Returns whether the purchasable item is shippable. + * + * @return bool + */ + public function isShippable() + { + return false; + } + + /** + * {@inheritDoc} + */ + public function getThumbnail() + { + return null; + } +} From 2f82f252882e853b6443e01e0d64737b089998f5 Mon Sep 17 00:00:00 2001 From: Alec Ritson Date: Mon, 19 Aug 2024 11:38:13 +0100 Subject: [PATCH 2/3] Fix up tests --- .../core/Stubs}/TestPurchasable.php | 18 ++++- tests/core/Unit/Models/OrderLineTest.php | 70 +++++++++++++++++++ 2 files changed, 87 insertions(+), 1 deletion(-) rename {packages/core/tests/Stubs/DataTypes => tests/core/Stubs}/TestPurchasable.php (86%) diff --git a/packages/core/tests/Stubs/DataTypes/TestPurchasable.php b/tests/core/Stubs/TestPurchasable.php similarity index 86% rename from packages/core/tests/Stubs/DataTypes/TestPurchasable.php rename to tests/core/Stubs/TestPurchasable.php index d572b65337..bc3d3d9c04 100644 --- a/packages/core/tests/Stubs/DataTypes/TestPurchasable.php +++ b/tests/core/Stubs/TestPurchasable.php @@ -1,6 +1,6 @@ assertDatabaseMissing((new CartLine)->getTable(), $data); }); + +test('non eloquent models can be added to an order', function () { + $order = Order::factory()->create(); + + $currency = Currency::factory()->create([ + 'default' => true, + ]); + + $taxClass = \Lunar\Models\TaxClass::factory()->create(); + + $shippingOption = new \Lunar\DataTypes\ShippingOption( + name: 'Basic Delivery', + description: 'Basic Delivery', + identifier: 'BASDEL', + price: new \Lunar\DataTypes\Price(500, $currency, 1), + taxClass: $taxClass + ); + + $data = [ + 'order_id' => $order->id, + 'quantity' => 1, + 'type' => $shippingOption->getType(), + 'purchasable_type' => \Lunar\DataTypes\ShippingOption::class, + 'purchasable_id' => $shippingOption->getIdentifier(), + 'unit_price' => $shippingOption->getPrice()->value, + 'unit_quantity' => $shippingOption->getUnitQuantity(), + ]; + + $orderLine = OrderLine::factory()->create($data); + + assertDatabaseHas( + (new OrderLine())->getTable(), + $data + ); + + expect($orderLine->unit_price->decimal)->toEqual(5.0) + ->and($orderLine->unit_price->unitDecimal)->toEqual(5.0); + + $testPurchasable = new TestPurchasable( + name: 'Test Purchasable', + description: 'Test Purchasable', + identifier: 'TESTPUR', + price: new \Lunar\DataTypes\Price(650, $currency, 1), + taxClass: $taxClass + ); + + $data = [ + 'order_id' => $order->id, + 'quantity' => 1, + 'type' => $testPurchasable->getType(), + 'purchasable_type' => TestPurchasable::class, + 'purchasable_id' => $testPurchasable->getIdentifier(), + 'unit_price' => $testPurchasable->getPrice()->value, + 'unit_quantity' => $testPurchasable->getUnitQuantity(), + ]; + + $orderLine = OrderLine::factory()->create($data); + + assertDatabaseHas( + (new OrderLine())->getTable(), + $data + ); + + expect($orderLine->unit_price->decimal)->toEqual(6.5) + ->and($orderLine->unit_price->unitDecimal) + ->toEqual(6.5); +}); From e6ddd4f23208c07de5e71aad0e83df064cb0a16c Mon Sep 17 00:00:00 2001 From: alecritson Date: Mon, 19 Aug 2024 10:40:45 +0000 Subject: [PATCH 3/3] chore: fix code style --- tests/core/Unit/Models/OrderLineTest.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/core/Unit/Models/OrderLineTest.php b/tests/core/Unit/Models/OrderLineTest.php index ec7814c258..f7ea038b16 100644 --- a/tests/core/Unit/Models/OrderLineTest.php +++ b/tests/core/Unit/Models/OrderLineTest.php @@ -10,6 +10,7 @@ use Lunar\Models\OrderLine; use Lunar\Models\ProductVariant; use Lunar\Tests\Core\Stubs\TestPurchasable; + use function Pest\Laravel\assertDatabaseHas; uses(\Illuminate\Foundation\Testing\RefreshDatabase::class); @@ -111,7 +112,7 @@ $orderLine = OrderLine::factory()->create($data); assertDatabaseHas( - (new OrderLine())->getTable(), + (new OrderLine)->getTable(), $data ); @@ -139,7 +140,7 @@ $orderLine = OrderLine::factory()->create($data); assertDatabaseHas( - (new OrderLine())->getTable(), + (new OrderLine)->getTable(), $data );