mirror of
https://github.com/wearepixel/laravel-cart.git
synced 2026-11-14 13:51:44 +00:00
feat: add HasCart trait and ShareCartWithInertia
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Wearepixel\Cart\Concerns;
|
||||
|
||||
use Wearepixel\Cart\Cart;
|
||||
|
||||
trait HasCart
|
||||
{
|
||||
public array $cartItems = [];
|
||||
|
||||
public float $cartTotal = 0.0;
|
||||
|
||||
public float $cartSubTotal = 0.0;
|
||||
|
||||
public int $cartCount = 0;
|
||||
|
||||
protected function getCart(): Cart
|
||||
{
|
||||
return app('cart');
|
||||
}
|
||||
|
||||
public function refreshCart(): void
|
||||
{
|
||||
$cart = $this->getCart();
|
||||
|
||||
$this->cartItems = $cart->getContent()->toArray();
|
||||
$this->cartSubTotal = (float) $cart->getSubTotal(false);
|
||||
$this->cartTotal = (float) $cart->getTotal();
|
||||
$this->cartCount = (int) $cart->getTotalQuantity();
|
||||
}
|
||||
|
||||
public function cartAdd(
|
||||
int|string $id,
|
||||
string $name,
|
||||
float $price,
|
||||
int $quantity = 1,
|
||||
array $attributes = [],
|
||||
): void {
|
||||
$this->getCart()->add($id, $name, $price, $quantity, $attributes);
|
||||
$this->refreshCart();
|
||||
}
|
||||
|
||||
public function cartRemove(int|string $id): void
|
||||
{
|
||||
$this->getCart()->remove($id);
|
||||
$this->refreshCart();
|
||||
}
|
||||
|
||||
public function cartUpdate(int|string $id, array $data): void
|
||||
{
|
||||
$this->getCart()->update($id, $data);
|
||||
$this->refreshCart();
|
||||
}
|
||||
|
||||
public function cartClear(): void
|
||||
{
|
||||
$this->getCart()->clear();
|
||||
$this->refreshCart();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Wearepixel\Cart\Concerns;
|
||||
|
||||
use Wearepixel\Cart\Cart;
|
||||
|
||||
class ShareCartWithInertia
|
||||
{
|
||||
public static function data(?Cart $cart = null): array
|
||||
{
|
||||
$cart ??= app('cart');
|
||||
|
||||
return [
|
||||
'items' => $cart->getContent()->toArray(),
|
||||
'subtotal' => (float) $cart->getSubTotal(false),
|
||||
'total' => (float) $cart->getTotal(),
|
||||
'count' => (int) $cart->getTotalQuantity(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
use Wearepixel\Cart\Cart;
|
||||
use Wearepixel\Cart\Concerns\HasCart;
|
||||
use Wearepixel\Cart\Drivers\SessionDriver;
|
||||
use Wearepixel\Cart\Tests\Helpers\SessionMock;
|
||||
|
||||
class FakeComponent
|
||||
{
|
||||
use HasCart;
|
||||
|
||||
public array $cartItems = [];
|
||||
public float $cartTotal = 0.0;
|
||||
public float $cartSubTotal = 0.0;
|
||||
public int $cartCount = 0;
|
||||
|
||||
public function __construct(private Cart $cart) {}
|
||||
|
||||
protected function getCart(): Cart
|
||||
{
|
||||
return $this->cart;
|
||||
}
|
||||
}
|
||||
|
||||
function makeComponent(): FakeComponent
|
||||
{
|
||||
$events = Mockery::mock('Illuminate\Contracts\Events\Dispatcher');
|
||||
$events->shouldReceive('dispatch');
|
||||
|
||||
$cart = new Cart(
|
||||
new SessionDriver(new SessionMock, 'TESTKEY'),
|
||||
$events,
|
||||
'cart',
|
||||
require __DIR__ . '/../../Helpers/ConfigMock.php',
|
||||
);
|
||||
|
||||
return new FakeComponent($cart);
|
||||
}
|
||||
|
||||
beforeEach(function () {
|
||||
$this->component = makeComponent();
|
||||
});
|
||||
|
||||
afterEach(fn() => Mockery::close());
|
||||
|
||||
describe('HasCart trait', function () {
|
||||
test('refreshCart() populates cartItems', function () {
|
||||
$this->component->cartAdd(1, 'Widget', 50.00, 2);
|
||||
|
||||
expect($this->component->cartItems)->not->toBeEmpty();
|
||||
});
|
||||
|
||||
test('cartAdd() adds an item and refreshes state', function () {
|
||||
$this->component->cartAdd(1, 'Widget', 50.00, 1);
|
||||
|
||||
expect($this->component->cartCount)->toBe(1);
|
||||
expect($this->component->cartSubTotal)->toBe(50.00);
|
||||
});
|
||||
|
||||
test('cartRemove() removes an item and refreshes state', function () {
|
||||
$this->component->cartAdd(1, 'Widget', 50.00, 1);
|
||||
$this->component->cartRemove(1);
|
||||
|
||||
expect($this->component->cartCount)->toBe(0);
|
||||
});
|
||||
|
||||
test('cartUpdate() updates an item and refreshes state', function () {
|
||||
$this->component->cartAdd(1, 'Widget', 50.00, 1);
|
||||
$this->component->cartUpdate(1, ['quantity' => ['relative' => false, 'value' => 3]]);
|
||||
|
||||
expect($this->component->cartCount)->toBe(3);
|
||||
});
|
||||
|
||||
test('cartClear() empties the cart and refreshes state', function () {
|
||||
$this->component->cartAdd(1, 'Widget', 50.00, 1);
|
||||
$this->component->cartClear();
|
||||
|
||||
expect($this->component->cartCount)->toBe(0);
|
||||
expect($this->component->cartItems)->toBeEmpty();
|
||||
});
|
||||
|
||||
test('cartTotal reflects the cart total', function () {
|
||||
$this->component->cartAdd(1, 'Widget', 100.00, 1);
|
||||
|
||||
expect($this->component->cartTotal)->toBe(100.00);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
use Wearepixel\Cart\Cart;
|
||||
use Wearepixel\Cart\Concerns\ShareCartWithInertia;
|
||||
use Wearepixel\Cart\Drivers\SessionDriver;
|
||||
use Wearepixel\Cart\Tests\Helpers\SessionMock;
|
||||
|
||||
function makeCartForInertia(): Cart
|
||||
{
|
||||
$events = Mockery::mock('Illuminate\Contracts\Events\Dispatcher');
|
||||
$events->shouldReceive('dispatch');
|
||||
|
||||
return new Cart(
|
||||
new SessionDriver(new SessionMock, 'TESTKEY'),
|
||||
$events,
|
||||
'cart',
|
||||
require __DIR__ . '/../../Helpers/ConfigMock.php',
|
||||
);
|
||||
}
|
||||
|
||||
beforeEach(function () {
|
||||
$this->cart = makeCartForInertia();
|
||||
});
|
||||
|
||||
afterEach(fn() => Mockery::close());
|
||||
|
||||
describe('ShareCartWithInertia', function () {
|
||||
test('data() returns items, total, subtotal, and count', function () {
|
||||
$this->cart->add(1, 'Widget', 50.00, 2);
|
||||
|
||||
$data = ShareCartWithInertia::data($this->cart);
|
||||
|
||||
expect($data)->toHaveKeys(['items', 'total', 'subtotal', 'count']);
|
||||
expect($data['count'])->toBe(2);
|
||||
expect($data['subtotal'])->toBe(100.00);
|
||||
expect($data['total'])->toBe(100.00);
|
||||
});
|
||||
|
||||
test('data() returns empty state when cart is empty', function () {
|
||||
$data = ShareCartWithInertia::data($this->cart);
|
||||
|
||||
expect($data['items'])->toBeEmpty();
|
||||
expect($data['count'])->toBe(0);
|
||||
});
|
||||
|
||||
test('data() items are plain arrays suitable for JSON serialization', function () {
|
||||
$this->cart->add(1, 'Widget', 50.00, 1);
|
||||
|
||||
$data = ShareCartWithInertia::data($this->cart);
|
||||
|
||||
expect($data['items'])->toBeArray();
|
||||
expect(json_encode($data))->toBeString();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user