diff --git a/app/Cart/CartCollection.php b/app/Cart/CartCollection.php index d1154c9..4714561 100644 --- a/app/Cart/CartCollection.php +++ b/app/Cart/CartCollection.php @@ -2,6 +2,7 @@ namespace App\Cart; +use App\Product; use Illuminate\Support\Collection; /** @@ -80,11 +81,7 @@ class CartCollection public function content() { - if (is_null($this->session->get($this->instance))) { - return collect([]); - } - - return $this->session->get($this->instance); + return $this->getContent(); } protected function getContent() @@ -114,6 +111,13 @@ class CartCollection return $item->product; } + public function draftHasItem(TransactionDraft $draft, Product $product) + { + $item = $draft->search($product); + + return ! is_null($item); + } + public function updateDraftItem($draftKey, $itemKey, $newItemData) { $content = $this->getContent(); diff --git a/app/Cart/TransactionDraft.php b/app/Cart/TransactionDraft.php index 4e583c8..582676c 100644 --- a/app/Cart/TransactionDraft.php +++ b/app/Cart/TransactionDraft.php @@ -2,6 +2,8 @@ namespace App\Cart; +use App\Product; + /** * Transaction Draft Interface. */ @@ -73,4 +75,10 @@ abstract class TransactionDraft return $item; } + + public function search(Product $product) + { + $productItem = $this->items()->where('id', $product->id)->first(); + return $productItem; + } } diff --git a/app/Http/Controllers/CartController.php b/app/Http/Controllers/CartController.php index d678543..2af691d 100644 --- a/app/Http/Controllers/CartController.php +++ b/app/Http/Controllers/CartController.php @@ -52,7 +52,7 @@ class CartController extends Controller $item = new Item($product, $request->qty); $this->cart->addItemToDraft($draftKey, $item); - return redirect()->route('cart.index', $item->draftKey); + return back(); } public function updateDraftItem(Request $request, $draftKey) diff --git a/resources/views/cart/index.blade.php b/resources/views/cart/index.blade.php index 922f0cb..1302f70 100644 --- a/resources/views/cart/index.blade.php +++ b/resources/views/cart/index.blade.php @@ -34,6 +34,12 @@ @foreach($queriedProducts as $product)
  • {{ $product->name }}
  • {{ $draft->type == 'cash' ? $product->cash_price : $product->credit_price }}
  • +
  • +
    + + +
    +
  • @endforeach @endif diff --git a/tests/Feature/TransactionEntryTest.php b/tests/Feature/TransactionEntryTest.php index e89cae3..8b43acb 100644 --- a/tests/Feature/TransactionEntryTest.php +++ b/tests/Feature/TransactionEntryTest.php @@ -67,6 +67,29 @@ class TransactionEntryTest extends BrowserKitTestCase // See product list appears $this->see($product->name); $this->see($product->credit_price); + $this->seeElement('form', ['action' => route('cart.add-draft-item', [$draft->draftKey, $product->id])]); + $this->seeElement('input', ['id' => 'qty-' . $product->id, 'name' => 'qty']); + $this->seeElement('input', ['id' => 'add-product-' . $product->id]); $this->dontSee($product->cash_price); } + + /** @test */ + public function user_can_add_item_to_draft() + { + $product = factory(Product::class)->create(['name' => 'Testing Produk 1','cash_price' => 400,'credit_price' => 500]); + $this->loginAsUser(); + + $cart = new CartCollection(); + $draft = new CashDraft(); + $cart->add($draft); + + // Visit cart index with searched item + $this->visit(route('cart.show', [$draft->draftKey, 'query' => 'testing'])); + + $this->type(2, 'qty'); + $this->press('add-product-' . $product->id); + $this->seePageIs(route('cart.show', [$draft->draftKey, 'query' => 'testing'])); + $this->assertTrue($cart->draftHasItem($draft, $product)); + $this->assertEquals(800, $draft->getTotal()); + } } diff --git a/tests/Unit/Integration/TransactionDraftTest.php b/tests/Unit/Integration/TransactionDraftTest.php new file mode 100644 index 0000000..4e4ff95 --- /dev/null +++ b/tests/Unit/Integration/TransactionDraftTest.php @@ -0,0 +1,42 @@ +add(new CashDraft()); + $count = 2; + $product1 = factory(Product::class)->create(['cash_price' => 1000]); + $product2 = factory(Product::class)->create(['cash_price' => 2000]); + $item1 = new Item($product1, $count); + $item2 = new Item($product2, $count); + + // Add items to draft + $cart->addItemToDraft($draft->draftKey, $item1); + $cart->addItemToDraft($draft->draftKey, $item2); + + $this->assertTrue($cart->draftHasItem($draft, $product1)); + $this->assertTrue($cart->draftHasItem($draft, $product2)); + $this->assertEquals(6000, $draft->getTotal()); + + // Remove an item from draft + $cart->removeItemFromDraft($draft->draftKey, 1); + $this->assertFalse($cart->draftHasItem($draft, $product2)); + + $this->assertEquals(2000, $draft->getTotal()); + } +}