Browse Source

Allowing user to add product item to a transaction draft

Added draftHasItem method on CartCollection
Added search method to transaction draft class
pull/2/head
Nafies Luthfi 9 years ago
parent
commit
c8b5e57a20
  1. 14
      app/Cart/CartCollection.php
  2. 8
      app/Cart/TransactionDraft.php
  3. 2
      app/Http/Controllers/CartController.php
  4. 6
      resources/views/cart/index.blade.php
  5. 23
      tests/Feature/TransactionEntryTest.php
  6. 42
      tests/Unit/Integration/TransactionDraftTest.php

14
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();

8
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;
}
}

2
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)

6
resources/views/cart/index.blade.php

@ -34,6 +34,12 @@
@foreach($queriedProducts as $product)
<li>{{ $product->name }}</li>
<li>{{ $draft->type == 'cash' ? $product->cash_price : $product->credit_price }}</li>
<li>
<form action="{{ route('cart.add-draft-item', [$draft->draftKey, $product->id]) }}" method="post">
<input type="number" id="qty-{{ $product->id }}" name="qty" value="1">
<input type="submit" id="add-product-{{ $product->id }}">
</form>
</li>
@endforeach
</ul>
@endif

23
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());
}
}

42
tests/Unit/Integration/TransactionDraftTest.php

@ -0,0 +1,42 @@
<?php
namespace Tests\Unit\Integration;
use App\Cart\CartCollection;
use App\Cart\CashDraft;
use App\Cart\Item;
use App\Product;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Tests\TestCase;
class TransactionDraftTest extends TestCase
{
use DatabaseMigrations;
/** @test */
public function it_can_found_an_item_in_a_draft()
{
$cart = new CartCollection();
$draft = $cart->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());
}
}
Loading…
Cancel
Save