Browse Source

Added get single draft, remove draft and destroy cart (remove all content)

pull/1/head
Nafies Luthfi 9 years ago
parent
commit
9c97438981
  1. 63
      app/Cart/CartCollection.php
  2. 52
      tests/Unit/CartCollectionTest.php

63
app/Cart/CartCollection.php

@ -43,6 +43,22 @@ class CartCollection
return $draft; return $draft;
} }
public function get($draftKey)
{
$content = $this->getContent();
if (isset($content[$draftKey]))
return $content[$draftKey];
return null;
}
public function removeDraft($draftKey)
{
$content = $this->getContent();
$content->pull($draftKey);
$this->session->put($this->instance, $content);
}
public function content() public function content()
{ {
if (is_null($this->session->get($this->instance))) { if (is_null($this->session->get($this->instance))) {
@ -59,6 +75,53 @@ class CartCollection
return $content; return $content;
} }
public function keys()
{
return $this->getContent()->keys();
}
public function destroy()
{
$this->session->remove($this->instance);
}
public function addItemToCart($draftKey, $item)
{
$content = $this->getContent();
$content[$draftKey]->addItem($item);
$content[$draftKey] = $this->updateCart($content[$draftKey]);
$this->session->put($this->instance, $content);
}
public function updateCart(Cart $draft)
{
$draft->charged_weight = $draft->getChargedWeight();
$draft->items_count = $draft->itemsCount();
$draft->pcs_count = $draft->itemsCount();
$draft->base_charge = $draft->getCharge();
$draft->subtotal = $draft->getCharge();
$draft->total = $draft->getCharge();
return $draft;
}
public function updateCartItem($draftKey, $itemKey, $newItemData)
{
$content = $this->getContent();
$content[$draftKey]->updateItem($itemKey, $newItemData);
$content[$draftKey] = $this->updateCart($content[$draftKey]);
$this->session->put($this->instance, $content);
}
public function removeItemFromCart($draftKey, $itemKey)
{
$content = $this->getContent();
$content[$draftKey]->removeItem($itemKey);
$this->session->put($this->instance, $content);
}
public function count() public function count()
{ {
return $this->getContent()->count(); return $this->getContent()->count();

52
tests/Unit/CartCollectionTest.php

@ -35,7 +35,7 @@ class CartCollectionTest extends TestCase
} }
/** @test */ /** @test */
public function cart_collection_consist_of_transacion_draft_class()
public function cart_collection_consist_of_transacion_draft()
{ {
$cart = new CartCollection; $cart = new CartCollection;
$cashDraft = new CashDraft; $cashDraft = new CashDraft;
@ -47,4 +47,54 @@ class CartCollectionTest extends TestCase
$this->assertCount(2, $cart->content()); $this->assertCount(2, $cart->content());
$this->assertTrue($cart->hasContent()); $this->assertTrue($cart->hasContent());
} }
/** @test */
public function it_can_get_a_draft_by_key()
{
$draft = new CashDraft;
$cart = new CartCollection;
$cart->add($draft);
$gottenDraft = $cart->get($draft->draftKey);
$invalidDraft = $cart->get('random_key');
$this->assertEquals($draft, $gottenDraft);
$this->assertNull($invalidDraft);
}
/** @test */
public function it_can_remove_draft_from_draft_collection()
{
$cart = new CartCollection;
$cashDraft = new CashDraft;
$creditDraft = new CreditDraft;
$cart->add($cashDraft);
$cart->add($creditDraft);
$this->assertCount(2, $cart->content());
$cart->removeDraft($cart->content()->keys()->last());
$this->assertCount(1, $cart->content());
}
/** @test */
public function it_can_be_empty_out()
{
$cart = new CartCollection;
$cashDraft = new CashDraft;
$creditDraft = new CreditDraft;
$cart->add($cashDraft);
$cart->add($cashDraft);
$cart->add($cashDraft);
$cart->add($creditDraft);
$cart->add($creditDraft);
$this->assertCount(5, $cart->content());
$cart->destroy();
$this->assertCount(0, $cart->content());
$this->assertTrue($cart->isEmpty());
}
} }
Loading…
Cancel
Save