Skip to content

Commit

Permalink
Port PR #1732 to 1.0 (#1911)
Browse files Browse the repository at this point in the history
  • Loading branch information
alecritson authored Aug 19, 2024
1 parent 8205935 commit e03dfb7
Show file tree
Hide file tree
Showing 3 changed files with 228 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/core/src/Observers/OrderLineObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand All @@ -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);
}
}
Expand Down
155 changes: 155 additions & 0 deletions tests/core/Stubs/TestPurchasable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?php

namespace Lunar\Tests\Core\Stubs;

use Illuminate\Support\Collection;
use Lunar\Base\Purchasable;
use Lunar\DataTypes\Price;
use Lunar\Models\TaxClass;

class TestPurchasable implements Purchasable
{
public function __construct(
public $name,
public $description,
public $identifier,
public Price $price,
public TaxClass $taxClass,
public $taxReference = null,
public $option = null,
public bool $collect = false,
public $meta = null
) {
// ..
}

/**
* Get the price for the purchasable item.
*
* @return \Lunar\DataTypes\Price
*/
public function getPrice()
{
return $this->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;
}

/**
* Return whether the purchasable can be fulfilled at a given quantity
*/
public function canBeFulfilledAtQuantity(int $quantity): bool
{
return true;
}

/**
* Returns the total inventory the purchasable has available
*/
public function getTotalInventory(): int
{
return 999;
}
}
71 changes: 71 additions & 0 deletions tests/core/Unit/Models/OrderLineTest.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
<?php

uses(\Lunar\Tests\Core\TestCase::class);

use Lunar\Exceptions\NonPurchasableItemException;
use Lunar\Models\CartLine;
use Lunar\Models\Channel;
use Lunar\Models\Currency;
use Lunar\Models\Order;
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);

Expand Down Expand Up @@ -77,3 +81,70 @@

$this->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);
});

0 comments on commit e03dfb7

Please sign in to comment.