From f445d848b524facf9014d419241d24e5ed9a2086 Mon Sep 17 00:00:00 2001 From: Jakub Theimer <5587309+theimerj@users.noreply.github.com> Date: Wed, 8 May 2024 17:30:04 +0200 Subject: [PATCH 1/3] add test purchasable --- .../tests/Stubs/DataTypes/TestPurchasable.php | 139 ++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 packages/core/tests/Stubs/DataTypes/TestPurchasable.php 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 e7eb6d8fa4bd168a1da60c595cb205879721c2a3 Mon Sep 17 00:00:00 2001 From: Jakub Theimer <5587309+theimerj@users.noreply.github.com> Date: Wed, 8 May 2024 17:30:31 +0200 Subject: [PATCH 2/3] make check more general by checking if class implements purchasable contract --- packages/core/src/Observers/OrderLineObserver.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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); } } From 92b2616c5d46b25b0bbfa0d7ca0fca3b5bc29b4c Mon Sep 17 00:00:00 2001 From: Jakub Theimer <5587309+theimerj@users.noreply.github.com> Date: Wed, 8 May 2024 17:30:53 +0200 Subject: [PATCH 3/3] test that purchasable non-eloquent classes can be added to order --- .../core/tests/Unit/Models/OrderLineTest.php | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/packages/core/tests/Unit/Models/OrderLineTest.php b/packages/core/tests/Unit/Models/OrderLineTest.php index 627452e129..1589cc492a 100644 --- a/packages/core/tests/Unit/Models/OrderLineTest.php +++ b/packages/core/tests/Unit/Models/OrderLineTest.php @@ -3,6 +3,8 @@ namespace Lunar\Tests\Unit\Models; use Illuminate\Foundation\Testing\RefreshDatabase; +use Lunar\DataTypes\Price; +use Lunar\DataTypes\ShippingOption; use Lunar\Exceptions\NonPurchasableItemException; use Lunar\Models\CartLine; use Lunar\Models\Channel; @@ -10,6 +12,8 @@ use Lunar\Models\Order; use Lunar\Models\OrderLine; use Lunar\Models\ProductVariant; +use Lunar\Models\TaxClass; +use Lunar\Tests\Stubs\DataTypes\TestPurchasable; use Lunar\Tests\TestCase; /** @@ -91,4 +95,72 @@ public function only_purchasables_can_be_added_to_an_order() $this->assertDatabaseMissing((new CartLine())->getTable(), $data); } + + /** @test */ + public function purchasable_non_eloquent_models_can_be_added_to_an_order() + { + $order = Order::factory()->create(); + + $currency = Currency::factory()->create([ + 'default' => true, + ]); + + $taxClass = TaxClass::factory()->create(); + + $shippingOption = new ShippingOption( + name: 'Basic Delivery', + description: 'Basic Delivery', + identifier: 'BASDEL', + price: new Price(500, $currency, 1), + taxClass: $taxClass + ); + + $data = [ + 'order_id' => $order->id, + 'quantity' => 1, + 'type' => $shippingOption->getType(), + 'purchasable_type' => ShippingOption::class, + 'purchasable_id' => $shippingOption->getIdentifier(), + 'unit_price' => $shippingOption->getPrice()->value, + 'unit_quantity' => $shippingOption->getUnitQuantity(), + ]; + + $orderLine = OrderLine::factory()->create($data); + + $this->assertDatabaseHas( + (new OrderLine())->getTable(), + $data + ); + + $this->assertEquals(5.0, $orderLine->unit_price->decimal); + $this->assertEquals(5.0, $orderLine->unit_price->unitDecimal); + + $testPurchasable = new TestPurchasable( + name: 'Test Purchasable', + description: 'Test Purchasable', + identifier: 'TESTPUR', + price: new 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); + + $this->assertDatabaseHas( + (new OrderLine())->getTable(), + $data + ); + + $this->assertEquals(6.5, $orderLine->unit_price->decimal); + $this->assertEquals(6.5, $orderLine->unit_price->unitDecimal); + } }