diff --git a/app/Cart/TransactionDraft.php b/app/Cart/TransactionDraft.php index 831da8a..9424110 100644 --- a/app/Cart/TransactionDraft.php +++ b/app/Cart/TransactionDraft.php @@ -48,6 +48,11 @@ abstract class TransactionDraft return $this->items()->sum('subtotal'); } + public function getTotalQty() + { + return $this->items()->sum('qty'); + } + public function updateItem($itemKey, $newItemData) { if (!isset($this->items[$itemKey])) diff --git a/app/Http/Controllers/CartController.php b/app/Http/Controllers/CartController.php index 0c4f308..102b254 100644 --- a/app/Http/Controllers/CartController.php +++ b/app/Http/Controllers/CartController.php @@ -34,4 +34,22 @@ class CartController extends Controller return redirect()->route('cart.index', $item->draftKey); } + + public function removeDraftItem(Request $request, $draftKey) + { + $this->cart->removeItemFromDraft($draftKey, $request->item_index); + return redirect()->route('cart.index', $draftKey); + } + + public function remove(Request $request) + { + $this->cart->removeDraft($request->draft_key); + return redirect()->route('cart.index'); + } + + public function destroy() + { + $this->cart->destroy(); + return redirect()->route('cart.index'); + } } diff --git a/routes/web.php b/routes/web.php index 349ec67..935af31 100644 --- a/routes/web.php +++ b/routes/web.php @@ -20,3 +20,6 @@ Auth::routes(); Route::get('/home', 'HomeController@index')->name('home'); Route::post('cart/add-draft/{product}', 'CartController@add')->name('cart.add'); Route::post('cart/add-draft-item/{draftKey}/{product}', 'CartController@addDraftItem')->name('cart.add-draft-item'); +Route::delete('cart/remove-draft-item/{draftKey}', 'CartController@removeDraftItem')->name('cart.remove-draft-item'); +Route::delete('cart/remove', 'CartController@remove')->name('cart.remove'); +Route::delete('cart/destroy', 'CartController@destroy')->name('cart.destroy'); diff --git a/tests/Feature/Cart/CartControllerTest.php b/tests/Feature/Cart/CartControllerTest.php index f371e09..229fc13 100644 --- a/tests/Feature/Cart/CartControllerTest.php +++ b/tests/Feature/Cart/CartControllerTest.php @@ -5,6 +5,7 @@ namespace Tests\Feature\Cart; use App\Cart\CartCollection; use App\Cart\CashDraft; use App\Cart\CreditDraft; +use App\Cart\Item; use App\Product; use Illuminate\Foundation\Testing\DatabaseMigrations; use Tests\TestCase; @@ -53,4 +54,69 @@ class CartControllerTest extends TestCase $this->assertTrue($cashDraft instanceof CashDraft); $this->assertEquals(2200, $cashDraft->getTotal()); } + + /** @test */ + public function user_can_remove_item_product_from_a_transaction_draft() + { + $this->loginAsUser(); + + $cart = new CartCollection; + $cashDraft = new CashDraft; + $product = factory(Product::class)->create(['cash_price' => 1100], ['credit_price' => 1000]); + $item = new Item($product, 2); + + $cashDraft->addItem($item); + $cart->add($cashDraft); + + $this->assertEquals(2, $cashDraft->getTotalQty()); + $this->assertEquals(2200, $cashDraft->getTotal()); + + // Add Product as CashDraft item + $response = $this->delete(route('cart.remove-draft-item', [$cashDraft->draftKey]), [ + 'item_index' => 0 + ]); + + $this->assertEquals(0, $cashDraft->getTotalQty()); + $this->assertCount(0, $cashDraft->items()); + $this->assertEquals(0, $cashDraft->getTotal()); + } + + /** @test */ + public function user_can_remove_a_transaction_draft_from_cart() + { + $this->loginAsUser(); + + $cart = new CartCollection; + $cashDraft = new CashDraft; + $cart->add($cashDraft); + + $this->assertFalse($cart->isEmpty()); + $this->assertEquals(1, $cart->count()); + + // Add Product as CashDraft item + $response = $this->delete(route('cart.remove'), [ + 'draft_key' => $cashDraft->draftKey + ]); + + $this->assertTrue($cart->isEmpty()); + } + + /** @test */ + public function user_can_destroy_cart() + { + $this->loginAsUser(); + + $cart = new CartCollection; + $cashDraft = new CashDraft; + $cart->add($cashDraft); + $cart->add($cashDraft); + + $this->assertFalse($cart->isEmpty()); + $this->assertEquals(2, $cart->count()); + + // Add Product as CashDraft item + $response = $this->delete(route('cart.destroy')); + + $this->assertTrue($cart->isEmpty()); + } } \ No newline at end of file