|
1 | 1 | <?php |
2 | 2 |
|
| 3 | +use Binafy\LaravelCart\Events\LaravelCartDecreaseQuantityEvent; |
| 4 | +use Binafy\LaravelCart\Events\LaravelCartIncreaseQuantityEvent; |
3 | 5 | use Binafy\LaravelCart\Models\Cart; |
4 | 6 | use Binafy\LaravelCart\Models\CartItem; |
5 | 7 | use Illuminate\Foundation\Testing\RefreshDatabase; |
|
14 | 16 | uses(RefreshDatabase::class); |
15 | 17 |
|
16 | 18 | test('can increase quantity of the item in cart', function () { |
| 19 | + Event::fake(); |
| 20 | + |
17 | 21 | $user = User:: query()-> create([ 'name' => 'Milwad', 'email' => '[email protected]']); |
18 | 22 | $product = Product::query()->create(['title' => 'Product 1']); |
19 | 23 |
|
|
35 | 39 | $cart->increaseQuantity($product, 2); |
36 | 40 |
|
37 | 41 | assertDatabaseHas('cart_items', ['quantity' => 3]); |
| 42 | + |
| 43 | + // Event Assertion |
| 44 | + Event::assertDispatched(LaravelCartIncreaseQuantityEvent::class); |
38 | 45 | }); |
39 | 46 |
|
40 | 47 | test('can decrease quantity of the item in cart', function () { |
| 48 | + Event::fake(); |
| 49 | + |
41 | 50 | $user = User:: query()-> create([ 'name' => 'Milwad', 'email' => '[email protected]']); |
42 | 51 | $product = Product::query()->create(['title' => 'Product 1']); |
43 | 52 |
|
|
59 | 68 | $cart->decreaseQuantity($product, 2); |
60 | 69 |
|
61 | 70 | assertDatabaseHas('cart_items', ['quantity' => 1]); |
| 71 | + |
| 72 | + // Event Assertion |
| 73 | + Event::assertDispatched(LaravelCartDecreaseQuantityEvent::class); |
62 | 74 | }); |
63 | 75 |
|
64 | 76 | test('can not increase quantity of the item in cart when item not found', function () { |
| 77 | + Event::fake(); |
| 78 | + |
65 | 79 | $user = User:: query()-> create([ 'name' => 'Milwad', 'email' => '[email protected]']); |
66 | 80 | $product1 = Product::query()->create(['title' => 'Product 1']); |
67 | 81 |
|
|
84 | 98 | $cart->increaseQuantity($product2, 2); |
85 | 99 |
|
86 | 100 | assertDatabaseHas('cart_items', ['quantity' => 1]); |
| 101 | + |
| 102 | + // Event Assertion |
| 103 | + Event::assertNotDispatched(LaravelCartIncreaseQuantityEvent::class); |
87 | 104 | })->expectExceptionMessage('The item not found'); |
88 | 105 |
|
89 | 106 | test('can not decrease quantity of the item in cart when item not found', function () { |
| 107 | + Event::fake(); |
| 108 | + |
90 | 109 | $user = User:: query()-> create([ 'name' => 'Milwad', 'email' => '[email protected]']); |
91 | 110 | $product1 = Product::query()->create(['title' => 'Product 1']); |
92 | 111 |
|
|
109 | 128 | $cart->decreaseQuantity($product2, 2); |
110 | 129 |
|
111 | 130 | assertDatabaseHas('cart_items', ['quantity' => 3]); |
| 131 | + |
| 132 | + // Event Assertion |
| 133 | + Event::assertNotDispatched(LaravelCartDecreaseQuantityEvent::class); |
112 | 134 | })->expectExceptionMessage('The item not found'); |
0 commit comments