Skip to content

Commit 0a22392

Browse files
authored
Merge pull request #20 from binafy/add-event-listener
[1.x] Add Event
2 parents 271ffb5 + b956ea6 commit 0a22392

10 files changed

+197
-1
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
- [Delete All Items From Cart](#delete-all-items-from-cart)
2828
- [Increase Quantity](#increase-quantity)
2929
- [Decrease Quantity](#decrease-quantity)
30+
- [Available Events](#available-events)
3031
- [Contributors](#contributors)
3132
- [Security](#security)
3233
- [Changelog](#changelog)
@@ -312,6 +313,19 @@ If you may to decrease the quantity of item in cart, you can use `decreaseQuanti
312313
$cart->decreaseQuantity(item: $item, quantity: 2); // By default quantity is 1
313314
```
314315

316+
<a name="available-events"></a>
317+
### Available Events
318+
319+
The `Laravel Cart` have some events that you can listen to these event and doing something:
320+
321+
| Events | Description |
322+
|------------------------------------|------------------------------------------------------------------|
323+
| `LaravelCartStoreItemEvent` | When you store an item in cart, this event fired |
324+
| `LaravelCartRemoveItemEvent` | When you remove an item from cart, this event fired |
325+
| `LaravelCartEmptyEvent` | When you remove all items from cart, this event fired |
326+
| `LaravelCartIncreaseQuantityEvent` | When you increase the quantity of item in cart, this event fired |
327+
| `LaravelCartDecreaseQuantityEvent` | When you decrease the quantity of item in cart, this event fired |
328+
315329
<a name="contributors"></a>
316330
## Contributors
317331

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Binafy\LaravelCart\Events;
4+
5+
use Illuminate\Broadcasting\InteractsWithSockets;
6+
use Illuminate\Foundation\Events\Dispatchable;
7+
use Illuminate\Queue\SerializesModels;
8+
9+
class LaravelCartDecreaseQuantityEvent
10+
{
11+
use Dispatchable, InteractsWithSockets, SerializesModels;
12+
13+
/**
14+
* Create a new event instance.
15+
*/
16+
public function __construct()
17+
{
18+
//
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Binafy\LaravelCart\Events;
4+
5+
use Illuminate\Broadcasting\InteractsWithSockets;
6+
use Illuminate\Foundation\Events\Dispatchable;
7+
use Illuminate\Queue\SerializesModels;
8+
9+
class LaravelCartEmptyEvent
10+
{
11+
use Dispatchable, InteractsWithSockets, SerializesModels;
12+
13+
/**
14+
* Create a new event instance.
15+
*/
16+
public function __construct()
17+
{
18+
//
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Binafy\LaravelCart\Events;
4+
5+
use Illuminate\Broadcasting\InteractsWithSockets;
6+
use Illuminate\Foundation\Events\Dispatchable;
7+
use Illuminate\Queue\SerializesModels;
8+
9+
class LaravelCartIncreaseQuantityEvent
10+
{
11+
use Dispatchable, InteractsWithSockets, SerializesModels;
12+
13+
/**
14+
* Create a new event instance.
15+
*/
16+
public function __construct()
17+
{
18+
//
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Binafy\LaravelCart\Events;
4+
5+
use Illuminate\Broadcasting\InteractsWithSockets;
6+
use Illuminate\Foundation\Events\Dispatchable;
7+
use Illuminate\Queue\SerializesModels;
8+
9+
class LaravelCartRemoveItemEvent
10+
{
11+
use Dispatchable, InteractsWithSockets, SerializesModels;
12+
13+
/**
14+
* Create a new event instance.
15+
*/
16+
public function __construct()
17+
{
18+
//
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Binafy\LaravelCart\Events;
4+
5+
use Illuminate\Broadcasting\InteractsWithSockets;
6+
use Illuminate\Foundation\Events\Dispatchable;
7+
use Illuminate\Queue\SerializesModels;
8+
9+
class LaravelCartStoreItemEvent
10+
{
11+
use Dispatchable, InteractsWithSockets, SerializesModels;
12+
13+
/**
14+
* Create a new event instance.
15+
*/
16+
public function __construct()
17+
{
18+
//
19+
}
20+
}

src/Models/Cart.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
namespace Binafy\LaravelCart\Models;
44

55
use Binafy\LaravelCart\Cartable;
6+
use Binafy\LaravelCart\Events\LaravelCartDecreaseQuantityEvent;
7+
use Binafy\LaravelCart\Events\LaravelCartEmptyEvent;
8+
use Binafy\LaravelCart\Events\LaravelCartIncreaseQuantityEvent;
9+
use Binafy\LaravelCart\Events\LaravelCartRemoveItemEvent;
10+
use Binafy\LaravelCart\Events\LaravelCartStoreItemEvent;
611
use Illuminate\Database\Eloquent\Builder;
712
use Illuminate\Database\Eloquent\Model;
813

@@ -69,6 +74,9 @@ public function scopeFirstOrCreateWithStoreItems(
6974

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

77+
// Dispatch Event
78+
LaravelCartStoreItemEvent::dispatch();
79+
7280
return $query;
7381
}
7482

@@ -122,6 +130,9 @@ public function storeItem(Model|array $item): static
122130
]);
123131
}
124132

133+
// Dispatch Event
134+
LaravelCartStoreItemEvent::dispatch();
135+
125136
return $this;
126137
}
127138

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

150+
// Dispatch Event
151+
LaravelCartRemoveItemEvent::dispatch();
152+
139153
return $this;
140154
}
141155

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

163+
// Dispatch Event
164+
LaravelCartEmptyEvent::dispatch();
165+
149166
return $this;
150167
}
151168

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

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

181+
// Dispatch Event
182+
LaravelCartIncreaseQuantityEvent::dispatch($item);
183+
164184
return $this;
165185
}
166186

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

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

199+
// Dispatch Event
200+
LaravelCartDecreaseQuantityEvent::dispatch($item);
201+
179202
return $this;
180203
}
181204
}

tests/Feature/Models/CartDeleteTest.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
<?php
22

3+
use Binafy\LaravelCart\Events\LaravelCartEmptyEvent;
4+
use Binafy\LaravelCart\Events\LaravelCartRemoveItemEvent;
5+
use Binafy\LaravelCart\Events\LaravelCartStoreItemEvent;
36
use Binafy\LaravelCart\Models\Cart;
47
use Illuminate\Foundation\Testing\RefreshDatabase;
8+
use Illuminate\Support\Facades\Event;
59
use Tests\SetUp\Models\Product;
610
use Tests\SetUp\Models\User;
711

@@ -12,7 +16,9 @@
1216
*/
1317
uses(RefreshDatabase::class);
1418

15-
test('can remove an item from the cart', function () {
19+
test('can remove an item from the cart', closure: function () {
20+
Event::fake();
21+
1622
$user = User::query()->create(['name' => 'Milwad', 'email' => '[email protected]']);
1723
$product1 = Product::query()->create(['title' => 'Product 1']);
1824
$product2 = Product::query()->create(['title' => 'Product 2']);
@@ -54,9 +60,15 @@
5460

5561
$cart->removeItem($product1);
5662
assertDatabaseCount('cart_items', 2);
63+
64+
// Event Assertions
65+
Event::assertDispatched(LaravelCartStoreItemEvent::class);
66+
Event::assertDispatched(LaravelCartRemoveItemEvent::class);
5767
});
5868

5969
test('can empty the cart', function () {
70+
Event::fake();
71+
6072
$user = User::query()->create(['name' => 'Milwad', 'email' => '[email protected]']);
6173
$product1 = Product::query()->create(['title' => 'Product 1']);
6274
$product2 = Product::query()->create(['title' => 'Product 2']);
@@ -91,4 +103,8 @@
91103
// Remove all items from cart
92104
$cart->emptyCart();
93105
assertDatabaseCount('cart_items', 0);
106+
107+
// Event Assertions
108+
Event::assertDispatched(LaravelCartStoreItemEvent::class);
109+
Event::assertDispatched(LaravelCartEmptyEvent::class);
94110
});

tests/Feature/Models/CartStoreTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use Binafy\LaravelCart\Events\LaravelCartStoreItemEvent;
34
use Binafy\LaravelCart\Models\Cart;
45
use Binafy\LaravelCart\Models\CartItem;
56
use Illuminate\Foundation\Testing\RefreshDatabase;
@@ -79,6 +80,8 @@
7980
});
8081

8182
test('can store product in cart with firstOrCreateWithItems scope', function () {
83+
Event::fake();
84+
8285
$user = User::query()->create(['name' => 'Milwad', 'email' => '[email protected]']);
8386
$product = Product::query()->create(['title' => 'Product 1']);
8487

@@ -92,9 +95,14 @@
9295
'itemable_type' => $product::class,
9396
'quantity' => 1,
9497
]);
98+
99+
// Event Assertion
100+
Event::assertDispatched(LaravelCartStoreItemEvent::class);
95101
});
96102

97103
test('can store product in cart with firstOrCreateWithItems scope when user sign-in', function () {
104+
Event::fake();
105+
98106
$user = User::query()->create(['name' => 'Milwad', 'email' => '[email protected]']);
99107
$product = Product::query()->create(['title' => 'Product 1']);
100108

@@ -110,9 +118,14 @@
110118
'itemable_type' => $product::class,
111119
'quantity' => 1,
112120
]);
121+
122+
// Event Assertion
123+
Event::assertDispatched(LaravelCartStoreItemEvent::class);
113124
});
114125

115126
test('can store multiple products in cart', function () {
127+
Event::fake();
128+
116129
$user = User::query()->create(['name' => 'Milwad', 'email' => '[email protected]']);
117130
$product1 = Product::query()->create(['title' => 'Product 1']);
118131
$product2 = Product::query()->create(['title' => 'Product 1']);
@@ -144,6 +157,9 @@
144157
'itemable_type' => $product1::class,
145158
'quantity' => 2,
146159
]);
160+
161+
// Event Assertion
162+
Event::assertDispatchedTimes(LaravelCartStoreItemEvent::class, 3);
147163
});
148164

149165
test('get correct price with calculated quantity', function () {
@@ -184,6 +200,8 @@
184200
});
185201

186202
test('can not store product in cart when item is not instance of cartable', function () {
203+
Event::fake();
204+
187205
$user = User::query()->create(['name' => 'Milwad', 'email' => '[email protected]']);
188206
$user2 = User::query()->create(['name' => 'Binafy', 'email' => '[email protected]']);
189207

@@ -202,4 +220,7 @@
202220
'itemable_type' => $user2::class,
203221
'quantity' => 2,
204222
]);
223+
224+
// Event Assertion
225+
Event::assertNotDispatched(LaravelCartStoreItemEvent::class);
205226
})->expectExceptionMessage('The item must be an instance of Cartable');

tests/Feature/Models/CartUpdateQuantityTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
use Binafy\LaravelCart\Events\LaravelCartDecreaseQuantityEvent;
4+
use Binafy\LaravelCart\Events\LaravelCartIncreaseQuantityEvent;
35
use Binafy\LaravelCart\Models\Cart;
46
use Binafy\LaravelCart\Models\CartItem;
57
use Illuminate\Foundation\Testing\RefreshDatabase;
@@ -14,6 +16,8 @@
1416
uses(RefreshDatabase::class);
1517

1618
test('can increase quantity of the item in cart', function () {
19+
Event::fake();
20+
1721
$user = User::query()->create(['name' => 'Milwad', 'email' => '[email protected]']);
1822
$product = Product::query()->create(['title' => 'Product 1']);
1923

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

3741
assertDatabaseHas('cart_items', ['quantity' => 3]);
42+
43+
// Event Assertion
44+
Event::assertDispatched(LaravelCartIncreaseQuantityEvent::class);
3845
});
3946

4047
test('can decrease quantity of the item in cart', function () {
48+
Event::fake();
49+
4150
$user = User::query()->create(['name' => 'Milwad', 'email' => '[email protected]']);
4251
$product = Product::query()->create(['title' => 'Product 1']);
4352

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

6170
assertDatabaseHas('cart_items', ['quantity' => 1]);
71+
72+
// Event Assertion
73+
Event::assertDispatched(LaravelCartDecreaseQuantityEvent::class);
6274
});
6375

6476
test('can not increase quantity of the item in cart when item not found', function () {
77+
Event::fake();
78+
6579
$user = User::query()->create(['name' => 'Milwad', 'email' => '[email protected]']);
6680
$product1 = Product::query()->create(['title' => 'Product 1']);
6781

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

86100
assertDatabaseHas('cart_items', ['quantity' => 1]);
101+
102+
// Event Assertion
103+
Event::assertNotDispatched(LaravelCartIncreaseQuantityEvent::class);
87104
})->expectExceptionMessage('The item not found');
88105

89106
test('can not decrease quantity of the item in cart when item not found', function () {
107+
Event::fake();
108+
90109
$user = User::query()->create(['name' => 'Milwad', 'email' => '[email protected]']);
91110
$product1 = Product::query()->create(['title' => 'Product 1']);
92111

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

111130
assertDatabaseHas('cart_items', ['quantity' => 3]);
131+
132+
// Event Assertion
133+
Event::assertNotDispatched(LaravelCartDecreaseQuantityEvent::class);
112134
})->expectExceptionMessage('The item not found');

0 commit comments

Comments
 (0)