diff --git a/src/Commands/DebugCommand.php b/src/Commands/DebugCommand.php new file mode 100644 index 0000000..5607654 --- /dev/null +++ b/src/Commands/DebugCommand.php @@ -0,0 +1,67 @@ +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: {$data['driver']}"); + $this->line(" Items: {$data['item_count']}"); + $this->line(" Total Quantity: {$data['total_quantity']}"); + $this->line(" Subtotal: {$data['subtotal']}"); + $this->line(" Total: {$data['total']}"); + + if (! empty($data['conditions'])) { + $this->line(" Conditions: " . implode(', ', $data['conditions']) . ''); + } else { + $this->line(' Conditions: none'); + } + + 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(), + ]; + } +} diff --git a/tests/Unit/Commands/DebugCommandTest.php b/tests/Unit/Commands/DebugCommandTest.php new file mode 100644 index 0000000..b43f7df --- /dev/null +++ b/tests/Unit/Commands/DebugCommandTest.php @@ -0,0 +1,71 @@ +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'); + }); +});