-
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 #16 from binafy/add-methods-for-increase-decrease-…
…quantity [1.x] Add methods for increase decrease quantity
- Loading branch information
Showing
5 changed files
with
168 additions
and
6 deletions.
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
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
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,112 @@ | ||
<?php | ||
|
||
use Binafy\LaravelCart\Models\Cart; | ||
use Binafy\LaravelCart\Models\CartItem; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Tests\SetUp\Models\Product; | ||
use Tests\SetUp\Models\User; | ||
|
||
use function Pest\Laravel\assertDatabaseHas; | ||
|
||
/* | ||
* Use `RefreshDatabase` for delete migration data for each test. | ||
*/ | ||
uses(RefreshDatabase::class); | ||
|
||
test('can increase quantity of the item in cart', function () { | ||
$user = User::query()->create(['name' => 'Milwad', 'email' => '[email protected]']); | ||
$product = Product::query()->create(['title' => 'Product 1']); | ||
|
||
// Create cart | ||
$cart = Cart::query()->firstOrCreate(['user_id' => $user->id]); | ||
|
||
// Store item to cart | ||
$cartItem = new CartItem([ | ||
'itemable_id' => $product->id, | ||
'itemable_type' => $product::class, | ||
'quantity' => 1, | ||
]); | ||
|
||
$cart->items()->save($cartItem); | ||
|
||
assertDatabaseHas('cart_items', ['quantity' => 1]); | ||
|
||
// Increase quantity | ||
$cart->increaseQuantity($product, 2); | ||
|
||
assertDatabaseHas('cart_items', ['quantity' => 3]); | ||
}); | ||
|
||
test('can decrease quantity of the item in cart', function () { | ||
$user = User::query()->create(['name' => 'Milwad', 'email' => '[email protected]']); | ||
$product = Product::query()->create(['title' => 'Product 1']); | ||
|
||
// Create cart | ||
$cart = Cart::query()->firstOrCreate(['user_id' => $user->id]); | ||
|
||
// Store item to cart | ||
$cartItem = new CartItem([ | ||
'itemable_id' => $product->id, | ||
'itemable_type' => $product::class, | ||
'quantity' => 3, | ||
]); | ||
|
||
$cart->items()->save($cartItem); | ||
|
||
assertDatabaseHas('cart_items', ['quantity' => 3]); | ||
|
||
// Increase quantity | ||
$cart->decreaseQuantity($product, 2); | ||
|
||
assertDatabaseHas('cart_items', ['quantity' => 1]); | ||
}); | ||
|
||
test('can not increase quantity of the item in cart when item not found', function () { | ||
$user = User::query()->create(['name' => 'Milwad', 'email' => '[email protected]']); | ||
$product1 = Product::query()->create(['title' => 'Product 1']); | ||
|
||
// Create cart | ||
$cart = Cart::query()->firstOrCreate(['user_id' => $user->id]); | ||
|
||
// Store item to cart | ||
$cartItem = new CartItem([ | ||
'itemable_id' => $product1->id, | ||
'itemable_type' => $product1::class, | ||
'quantity' => 1, | ||
]); | ||
|
||
$cart->items()->save($cartItem); | ||
|
||
assertDatabaseHas('cart_items', ['quantity' => 1]); | ||
|
||
// Increase quantity | ||
$product2 = Product::query()->create(['title' => 'Product 2']); | ||
$cart->increaseQuantity($product2, 2); | ||
|
||
assertDatabaseHas('cart_items', ['quantity' => 1]); | ||
})->expectExceptionMessage('The item not found'); | ||
|
||
test('can not decrease quantity of the item in cart when item not found', function () { | ||
$user = User::query()->create(['name' => 'Milwad', 'email' => '[email protected]']); | ||
$product1 = Product::query()->create(['title' => 'Product 1']); | ||
|
||
// Create cart | ||
$cart = Cart::query()->firstOrCreate(['user_id' => $user->id]); | ||
|
||
// Store item to cart | ||
$cartItem = new CartItem([ | ||
'itemable_id' => $product1->id, | ||
'itemable_type' => $product1::class, | ||
'quantity' => 3, | ||
]); | ||
|
||
$cart->items()->save($cartItem); | ||
|
||
assertDatabaseHas('cart_items', ['quantity' => 3]); | ||
|
||
// Increase quantity | ||
$product2 = Product::query()->create(['title' => 'Product 2']); | ||
$cart->decreaseQuantity($product2, 2); | ||
|
||
assertDatabaseHas('cart_items', ['quantity' => 3]); | ||
})->expectExceptionMessage('The item not found'); |