Skip to content

Commit

Permalink
Merge pull request #20 from binafy/add-event-listener
Browse files Browse the repository at this point in the history
[1.x] Add Event
  • Loading branch information
milwad-dev authored Jul 20, 2024
2 parents 271ffb5 + b956ea6 commit 0a22392
Show file tree
Hide file tree
Showing 10 changed files with 197 additions and 1 deletion.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
- [Delete All Items From Cart](#delete-all-items-from-cart)
- [Increase Quantity](#increase-quantity)
- [Decrease Quantity](#decrease-quantity)
- [Available Events](#available-events)
- [Contributors](#contributors)
- [Security](#security)
- [Changelog](#changelog)
Expand Down Expand Up @@ -312,6 +313,19 @@ If you may to decrease the quantity of item in cart, you can use `decreaseQuanti
$cart->decreaseQuantity(item: $item, quantity: 2); // By default quantity is 1
```

<a name="available-events"></a>
### Available Events

The `Laravel Cart` have some events that you can listen to these event and doing something:

| Events | Description |
|------------------------------------|------------------------------------------------------------------|
| `LaravelCartStoreItemEvent` | When you store an item in cart, this event fired |
| `LaravelCartRemoveItemEvent` | When you remove an item from cart, this event fired |
| `LaravelCartEmptyEvent` | When you remove all items from cart, this event fired |
| `LaravelCartIncreaseQuantityEvent` | When you increase the quantity of item in cart, this event fired |
| `LaravelCartDecreaseQuantityEvent` | When you decrease the quantity of item in cart, this event fired |

<a name="contributors"></a>
## Contributors

Expand Down
20 changes: 20 additions & 0 deletions src/Events/LaravelCartDecreaseQuantityEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Binafy\LaravelCart\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class LaravelCartDecreaseQuantityEvent
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
* Create a new event instance.
*/
public function __construct()
{
//
}
}
20 changes: 20 additions & 0 deletions src/Events/LaravelCartEmptyEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Binafy\LaravelCart\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class LaravelCartEmptyEvent
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
* Create a new event instance.
*/
public function __construct()
{
//
}
}
20 changes: 20 additions & 0 deletions src/Events/LaravelCartIncreaseQuantityEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Binafy\LaravelCart\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class LaravelCartIncreaseQuantityEvent
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
* Create a new event instance.
*/
public function __construct()
{
//
}
}
20 changes: 20 additions & 0 deletions src/Events/LaravelCartRemoveItemEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Binafy\LaravelCart\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class LaravelCartRemoveItemEvent
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
* Create a new event instance.
*/
public function __construct()
{
//
}
}
20 changes: 20 additions & 0 deletions src/Events/LaravelCartStoreItemEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Binafy\LaravelCart\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class LaravelCartStoreItemEvent
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
* Create a new event instance.
*/
public function __construct()
{
//
}
}
23 changes: 23 additions & 0 deletions src/Models/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
namespace Binafy\LaravelCart\Models;

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 Binafy\LaravelCart\Events\LaravelCartStoreItemEvent;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

Expand Down Expand Up @@ -69,6 +74,9 @@ public function scopeFirstOrCreateWithStoreItems(

$cart->items()->save($cartItem);

// Dispatch Event
LaravelCartStoreItemEvent::dispatch();

return $query;
}

Expand Down Expand Up @@ -122,6 +130,9 @@ public function storeItem(Model|array $item): static
]);
}

// Dispatch Event
LaravelCartStoreItemEvent::dispatch();

return $this;
}

Expand All @@ -136,6 +147,9 @@ public function removeItem(Model $item): static
$itemToDelete->delete();
}

// Dispatch Event
LaravelCartRemoveItemEvent::dispatch();

return $this;
}

Expand All @@ -146,6 +160,9 @@ public function emptyCart(): static
{
$this->items()->delete();

// Dispatch Event
LaravelCartEmptyEvent::dispatch();

return $this;
}

Expand All @@ -161,6 +178,9 @@ public function increaseQuantity(Model $item, int $quantity = 1): static

$item->increment('quantity', $quantity);

// Dispatch Event
LaravelCartIncreaseQuantityEvent::dispatch($item);

return $this;
}

Expand All @@ -176,6 +196,9 @@ public function decreaseQuantity(Model $item, int $quantity = 1): static

$item->decrement('quantity', $quantity);

// Dispatch Event
LaravelCartDecreaseQuantityEvent::dispatch($item);

return $this;
}
}
18 changes: 17 additions & 1 deletion tests/Feature/Models/CartDeleteTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<?php

use Binafy\LaravelCart\Events\LaravelCartEmptyEvent;
use Binafy\LaravelCart\Events\LaravelCartRemoveItemEvent;
use Binafy\LaravelCart\Events\LaravelCartStoreItemEvent;
use Binafy\LaravelCart\Models\Cart;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Event;
use Tests\SetUp\Models\Product;
use Tests\SetUp\Models\User;

Expand All @@ -12,7 +16,9 @@
*/
uses(RefreshDatabase::class);

test('can remove an item from the cart', function () {
test('can remove an item from the cart', closure: 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']);
Expand Down Expand Up @@ -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']);
Expand Down Expand Up @@ -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);
});
21 changes: 21 additions & 0 deletions tests/Feature/Models/CartStoreTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Binafy\LaravelCart\Events\LaravelCartStoreItemEvent;
use Binafy\LaravelCart\Models\Cart;
use Binafy\LaravelCart\Models\CartItem;
use Illuminate\Foundation\Testing\RefreshDatabase;
Expand Down Expand Up @@ -79,6 +80,8 @@
});

test('can store product in cart with firstOrCreateWithItems scope', function () {
Event::fake();

$user = User::query()->create(['name' => 'Milwad', 'email' => 'milwad.dev@gmail.comd']);
$product = Product::query()->create(['title' => 'Product 1']);

Expand All @@ -92,9 +95,14 @@
'itemable_type' => $product::class,
'quantity' => 1,
]);

// Event Assertion
Event::assertDispatched(LaravelCartStoreItemEvent::class);
});

test('can store product in cart with firstOrCreateWithItems scope when user sign-in', function () {
Event::fake();

$user = User::query()->create(['name' => 'Milwad', 'email' => 'milwad.dev@gmail.comd']);
$product = Product::query()->create(['title' => 'Product 1']);

Expand All @@ -110,9 +118,14 @@
'itemable_type' => $product::class,
'quantity' => 1,
]);

// Event Assertion
Event::assertDispatched(LaravelCartStoreItemEvent::class);
});

test('can store multiple products in 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 1']);
Expand Down Expand Up @@ -144,6 +157,9 @@
'itemable_type' => $product1::class,
'quantity' => 2,
]);

// Event Assertion
Event::assertDispatchedTimes(LaravelCartStoreItemEvent::class, 3);
});

test('get correct price with calculated quantity', function () {
Expand Down Expand Up @@ -184,6 +200,8 @@
});

test('can not store product in cart when item is not instance of cartable', function () {
Event::fake();

$user = User::query()->create(['name' => 'Milwad', 'email' => 'milwad.dev@gmail.comd']);
$user2 = User::query()->create(['name' => 'Binafy', 'email' => 'binafy23@gmail.comd']);

Expand All @@ -202,4 +220,7 @@
'itemable_type' => $user2::class,
'quantity' => 2,
]);

// Event Assertion
Event::assertNotDispatched(LaravelCartStoreItemEvent::class);
})->expectExceptionMessage('The item must be an instance of Cartable');
22 changes: 22 additions & 0 deletions tests/Feature/Models/CartUpdateQuantityTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Binafy\LaravelCart\Events\LaravelCartDecreaseQuantityEvent;
use Binafy\LaravelCart\Events\LaravelCartIncreaseQuantityEvent;
use Binafy\LaravelCart\Models\Cart;
use Binafy\LaravelCart\Models\CartItem;
use Illuminate\Foundation\Testing\RefreshDatabase;
Expand All @@ -14,6 +16,8 @@
uses(RefreshDatabase::class);

test('can increase quantity of the item in cart', function () {
Event::fake();

$user = User::query()->create(['name' => 'Milwad', 'email' => 'milwad.dev@gmail.comd']);
$product = Product::query()->create(['title' => 'Product 1']);

Expand All @@ -35,9 +39,14 @@
$cart->increaseQuantity($product, 2);

assertDatabaseHas('cart_items', ['quantity' => 3]);

// Event Assertion
Event::assertDispatched(LaravelCartIncreaseQuantityEvent::class);
});

test('can decrease quantity of the item in cart', function () {
Event::fake();

$user = User::query()->create(['name' => 'Milwad', 'email' => 'milwad.dev@gmail.comd']);
$product = Product::query()->create(['title' => 'Product 1']);

Expand All @@ -59,9 +68,14 @@
$cart->decreaseQuantity($product, 2);

assertDatabaseHas('cart_items', ['quantity' => 1]);

// Event Assertion
Event::assertDispatched(LaravelCartDecreaseQuantityEvent::class);
});

test('can not increase quantity of the item in cart when item not found', function () {
Event::fake();

$user = User::query()->create(['name' => 'Milwad', 'email' => 'milwad.dev@gmail.comd']);
$product1 = Product::query()->create(['title' => 'Product 1']);

Expand All @@ -84,9 +98,14 @@
$cart->increaseQuantity($product2, 2);

assertDatabaseHas('cart_items', ['quantity' => 1]);

// Event Assertion
Event::assertNotDispatched(LaravelCartIncreaseQuantityEvent::class);
})->expectExceptionMessage('The item not found');

test('can not decrease quantity of the item in cart when item not found', function () {
Event::fake();

$user = User::query()->create(['name' => 'Milwad', 'email' => 'milwad.dev@gmail.comd']);
$product1 = Product::query()->create(['title' => 'Product 1']);

Expand All @@ -109,4 +128,7 @@
$cart->decreaseQuantity($product2, 2);

assertDatabaseHas('cart_items', ['quantity' => 3]);

// Event Assertion
Event::assertNotDispatched(LaravelCartDecreaseQuantityEvent::class);
})->expectExceptionMessage('The item not found');

0 comments on commit 0a22392

Please sign in to comment.