feat: add cart:debug command

This commit is contained in:
Joel Male
2026-05-27 21:44:47 +10:00
parent 15d4397968
commit c2b2563cf1
2 changed files with 138 additions and 0 deletions
+67
View File
@@ -0,0 +1,67 @@
<?php
declare(strict_types=1);
namespace Wearepixel\Cart\Commands;
use Illuminate\Console\Command;
use Wearepixel\Cart\Cart;
class DebugCommand extends Command
{
protected $signature = 'cart:debug';
protected $description = 'Dump the current cart state. Only runs outside production.';
public function handle(): int
{
if (app()->isProduction()) {
$this->error('cart:debug is not available in production.');
return self::FAILURE;
}
$cart = app('cart');
$data = static::debugData($cart);
$this->info('Cart Debug');
$this->line('');
$this->line(" Driver: <comment>{$data['driver']}</comment>");
$this->line(" Items: <comment>{$data['item_count']}</comment>");
$this->line(" Total Quantity: <comment>{$data['total_quantity']}</comment>");
$this->line(" Subtotal: <comment>{$data['subtotal']}</comment>");
$this->line(" Total: <comment>{$data['total']}</comment>");
if (! empty($data['conditions'])) {
$this->line(" Conditions: <comment>" . implode(', ', $data['conditions']) . '</comment>');
} else {
$this->line(' Conditions: <comment>none</comment>');
}
if (! empty($data['items'])) {
$this->line('');
$this->table(['ID', 'Name', 'Price', 'Qty'], $data['items']);
}
return self::SUCCESS;
}
public static function debugData(Cart $cart): array
{
$items = $cart->getContent();
return [
'driver' => $cart->getDriver()->getDriverName(),
'item_count' => $items->count(),
'total_quantity' => (int) $cart->getTotalQuantity(),
'subtotal' => (float) $cart->getSubTotal(false),
'total' => (float) $cart->getTotal(),
'conditions' => $cart->getConditions()->map(fn($c) => $c->getName())->values()->toArray(),
'items' => $items->map(fn($item) => [
$item->id,
$item->name,
$item->price,
$item->quantity,
])->values()->toArray(),
];
}
}
+71
View File
@@ -0,0 +1,71 @@
<?php
use Wearepixel\Cart\Cart;
use Wearepixel\Cart\Commands\DebugCommand;
use Wearepixel\Cart\Drivers\NullDriver;
function makeDebugCart(): Cart
{
$events = Mockery::mock('Illuminate\Contracts\Events\Dispatcher');
$events->shouldReceive('dispatch');
return new Cart(
new NullDriver('debug-session'),
$events,
'cart',
require __DIR__ . '/../../Helpers/ConfigMock.php',
);
}
afterEach(fn() => Mockery::close());
describe('DebugCommand::debugData()', function () {
test('returns driver name', function () {
$cart = makeDebugCart();
$data = DebugCommand::debugData($cart);
expect($data['driver'])->toBe('null');
});
test('returns item count', function () {
$cart = makeDebugCart();
$cart->add(1, 'Widget', 10.00, 1);
$data = DebugCommand::debugData($cart);
expect($data['item_count'])->toBe(1);
});
test('returns total quantity', function () {
$cart = makeDebugCart();
$cart->add(1, 'Widget', 10.00, 3);
$data = DebugCommand::debugData($cart);
expect($data['total_quantity'])->toBe(3);
});
test('returns subtotal', function () {
$cart = makeDebugCart();
$cart->add(1, 'Widget', 25.00, 2);
$data = DebugCommand::debugData($cart);
expect($data['subtotal'])->toBe(50.00);
});
test('returns total', function () {
$cart = makeDebugCart();
$cart->add(1, 'Widget', 25.00, 2);
$data = DebugCommand::debugData($cart);
expect($data['total'])->toBe(50.00);
});
test('returns empty items array when cart is empty', function () {
$cart = makeDebugCart();
$data = DebugCommand::debugData($cart);
expect($data['items'])->toBeArray()->toBeEmpty();
});
test('returns condition names', function () {
$cart = makeDebugCart();
$cart->condition(new \Wearepixel\Cart\CartCondition([
'name' => 'GST', 'type' => 'tax', 'value' => '10%', 'target' => 'subtotal',
]));
$data = DebugCommand::debugData($cart);
expect($data['conditions'])->toContain('GST');
});
});