diff --git a/src/Events/LaravelCartDecreaseQuantityEvent.php b/src/Events/LaravelCartDecreaseQuantityEvent.php new file mode 100644 index 0000000..aa7b9ab --- /dev/null +++ b/src/Events/LaravelCartDecreaseQuantityEvent.php @@ -0,0 +1,36 @@ + + */ + public function broadcastOn(): array + { + return [ + new PrivateChannel('channel-name'), + ]; + } +} diff --git a/src/Events/LaravelCartEmptyEvent.php b/src/Events/LaravelCartEmptyEvent.php new file mode 100644 index 0000000..6fc8240 --- /dev/null +++ b/src/Events/LaravelCartEmptyEvent.php @@ -0,0 +1,36 @@ + + */ + public function broadcastOn(): array + { + return [ + new PrivateChannel('channel-name'), + ]; + } +} diff --git a/src/Events/LaravelCartIncreaseQuantityEvent.php b/src/Events/LaravelCartIncreaseQuantityEvent.php new file mode 100644 index 0000000..1dc6cfc --- /dev/null +++ b/src/Events/LaravelCartIncreaseQuantityEvent.php @@ -0,0 +1,36 @@ + + */ + public function broadcastOn(): array + { + return [ + new PrivateChannel('channel-name'), + ]; + } +} diff --git a/src/Events/LaravelCartRemoveItemEvent.php b/src/Events/LaravelCartRemoveItemEvent.php new file mode 100644 index 0000000..3f8d8fa --- /dev/null +++ b/src/Events/LaravelCartRemoveItemEvent.php @@ -0,0 +1,36 @@ + + */ + public function broadcastOn(): array + { + return [ + new PrivateChannel('channel-name'), + ]; + } +} diff --git a/src/Events/LaravelCartStoreItemEvent.php b/src/Events/LaravelCartStoreItemEvent.php new file mode 100644 index 0000000..79ebf62 --- /dev/null +++ b/src/Events/LaravelCartStoreItemEvent.php @@ -0,0 +1,36 @@ + + */ + public function broadcastOn(): array + { + return [ + new PrivateChannel('channel-name'), + ]; + } +} diff --git a/src/Models/Cart.php b/src/Models/Cart.php index d5f179a..97e804f 100644 --- a/src/Models/Cart.php +++ b/src/Models/Cart.php @@ -2,7 +2,12 @@ namespace Binafy\LaravelCart\Models; +use Binafy\LaravelCart\Events\LaravelCartStoreItemEvent; use Binafy\LaravelCart\Cartable; +use Binafy\LaravelCart\Events\LaravelCartDecreaseQuantityEvent; +use Binafy\LaravelCart\Events\LaravelCartEmptyEvent; +use Binafy\LaravelCart\Events\LaravelCartIncreaseQuantityEvent; +use Binafy\LaravelCart\Events\LaravelCartRemoveItemEvent; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; @@ -122,6 +127,9 @@ public function storeItem(Model|array $item): static ]); } + // Dispatch Event + LaravelCartStoreItemEvent::dispatch(); + return $this; } @@ -136,6 +144,9 @@ public function removeItem(Model $item): static $itemToDelete->delete(); } + // Dispatch Event + LaravelCartRemoveItemEvent::dispatch(); + return $this; } @@ -146,6 +157,9 @@ public function emptyCart(): static { $this->items()->delete(); + // Dispatch Event + LaravelCartEmptyEvent::dispatch(); + return $this; } @@ -161,6 +175,9 @@ public function increaseQuantity(Model $item, int $quantity = 1): static $item->increment('quantity', $quantity); + // Dispatch Event + LaravelCartIncreaseQuantityEvent::dispatch($item); + return $this; } @@ -176,6 +193,9 @@ public function decreaseQuantity(Model $item, int $quantity = 1): static $item->decrement('quantity', $quantity); + // Dispatch Event + LaravelCartDecreaseQuantityEvent::dispatch($item); + return $this; } } diff --git a/tests/Feature/Models/CartDeleteTest.php b/tests/Feature/Models/CartDeleteTest.php index d63be1e..3220a7d 100644 --- a/tests/Feature/Models/CartDeleteTest.php +++ b/tests/Feature/Models/CartDeleteTest.php @@ -1,7 +1,11 @@ create(['name' => 'Milwad', 'email' => 'milwad.dev@gmail.comd']); $product1 = Product::query()->create(['title' => 'Product 1']); $product2 = Product::query()->create(['title' => 'Product 2']); @@ -54,9 +60,15 @@ $cart->removeItem($product1); assertDatabaseCount('cart_items', 2); + + // Event Assertions + Event::assertDispatched(LaravelCartStoreItemEvent::class); + Event::assertDispatched(LaravelCartRemoveItemEvent::class); }); test('can empty the cart', function () { + Event::fake(); + $user = User::query()->create(['name' => 'Milwad', 'email' => 'milwad.dev@gmail.comd']); $product1 = Product::query()->create(['title' => 'Product 1']); $product2 = Product::query()->create(['title' => 'Product 2']); @@ -91,4 +103,8 @@ // Remove all items from cart $cart->emptyCart(); assertDatabaseCount('cart_items', 0); + + // Event Assertions + Event::assertDispatched(LaravelCartStoreItemEvent::class); + Event::assertDispatched(LaravelCartEmptyEvent::class); });