Skip to content

Commit

Permalink
Handle cart line update validation (#1718)
Browse files Browse the repository at this point in the history
  • Loading branch information
alecritson committed Apr 30, 2024
1 parent c8bb70b commit 503e489
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/core/src/Validation/CartLine/CartLineQuantity.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ public function validate(): bool
{
$quantity = $this->parameters['quantity'] ?? 0;
$purchasable = $this->parameters['purchasable'] ?? null;
$cartLineId = $this->parameters['cartLineId'] ?? null;
$cart = $this->parameters['cart'] ?? null;

if ($cartLineId && ! $purchasable && $cart) {
$purchasable = $cart->lines->first(
fn ($cartLine) => $cartLine->id == $cartLineId
)?->purchasable;
}

if ($quantity < 1) {
$this->fail(
Expand Down
29 changes: 29 additions & 0 deletions tests/core/Unit/Validation/CartLine/CartLineQuantityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,32 @@
'increment' => 14,
],
]);

test('can validate from cart line id', function () {
$currency = Currency::factory()->create();

$cart = Cart::factory()->create([
'currency_id' => $currency->id,
]);

$purchasable = \Lunar\Models\ProductVariant::factory()->create([
'quantity_increment' => 25,
]);

$cart->lines()->create([
'purchasable_type' => \Lunar\Models\ProductVariant::class,
'purchasable_id' => $purchasable->id,
'quantity' => 50,
]);

$validator = (new CartLineQuantity)->using(
cart: $cart,
purchasable: null,
cartLineId: $cart->lines()->first()->id,
quantity: 26,
meta: []
);

expect(fn () => $validator->validate())
->toThrow(CartException::class);
});

0 comments on commit 503e489

Please sign in to comment.