-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from binafy/add-event-listener
[1.x] Add Event
- Loading branch information
Showing
10 changed files
with
197 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
{ | ||
// | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
{ | ||
// | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
{ | ||
// | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
{ | ||
// | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
{ | ||
// | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
|
@@ -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' => '[email protected]']); | ||
$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' => '[email protected]']); | ||
$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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
@@ -79,6 +80,8 @@ | |
}); | ||
|
||
test('can store product in cart with firstOrCreateWithItems scope', function () { | ||
Event::fake(); | ||
|
||
$user = User::query()->create(['name' => 'Milwad', 'email' => '[email protected]']); | ||
$product = Product::query()->create(['title' => 'Product 1']); | ||
|
||
|
@@ -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' => '[email protected]']); | ||
$product = Product::query()->create(['title' => 'Product 1']); | ||
|
||
|
@@ -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' => '[email protected]']); | ||
$product1 = Product::query()->create(['title' => 'Product 1']); | ||
$product2 = Product::query()->create(['title' => 'Product 1']); | ||
|
@@ -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 () { | ||
|
@@ -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' => '[email protected]']); | ||
$user2 = User::query()->create(['name' => 'Binafy', 'email' => '[email protected]']); | ||
|
||
|
@@ -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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
@@ -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' => '[email protected]']); | ||
$product = Product::query()->create(['title' => 'Product 1']); | ||
|
||
|
@@ -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' => '[email protected]']); | ||
$product = Product::query()->create(['title' => 'Product 1']); | ||
|
||
|
@@ -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' => '[email protected]']); | ||
$product1 = Product::query()->create(['title' => 'Product 1']); | ||
|
||
|
@@ -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' => '[email protected]']); | ||
$product1 = Product::query()->create(['title' => 'Product 1']); | ||
|
||
|
@@ -109,4 +128,7 @@ | |
$cart->decreaseQuantity($product2, 2); | ||
|
||
assertDatabaseHas('cart_items', ['quantity' => 3]); | ||
|
||
// Event Assertion | ||
Event::assertNotDispatched(LaravelCartDecreaseQuantityEvent::class); | ||
})->expectExceptionMessage('The item not found'); |