Browse Source

Added updateDraftAttributes by draftKey and with stubbed draft attributes

pull/1/head
Nafies Luthfi 9 years ago
parent
commit
de6e5e0e88
  1. 40
      app/Cart/CartCollection.php
  2. 2
      app/Cart/CashDraft.php
  3. 2
      app/Cart/CreditDraft.php
  4. 18
      app/Cart/TransactionDraft.php
  5. 48
      tests/Unit/CartCollectionTest.php

40
app/Cart/CartCollection.php

@ -52,6 +52,19 @@ class CartCollection
return null;
}
public function updateDraftAttributes($draftKey, $draftAttributes)
{
$content = $this->getContent();
foreach ($draftAttributes as $attribute => $value) {
$content[$draftKey]->{$attribute} = $value;
}
$this->session->put($this->instance, $content);
return $content[$draftKey];
}
public function removeDraft($draftKey)
{
$content = $this->getContent();
@ -85,39 +98,28 @@ class CartCollection
$this->session->remove($this->instance);
}
public function addItemToCart($draftKey, $item)
public function addProductToDraft($draftKey, $item)
{
$content = $this->getContent();
$content[$draftKey]->addItem($item);
$content[$draftKey] = $this->updateCart($content[$draftKey]);
$content[$draftKey]->addProduct($item);
$content[$draftKey] = $this->updateDraft($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)
public function updateDraftProduct($draftKey, $itemKey, $newProductData)
{
$content = $this->getContent();
$content[$draftKey]->updateItem($itemKey, $newItemData);
$content[$draftKey] = $this->updateCart($content[$draftKey]);
$content[$draftKey]->updateProduct($itemKey, $newProductData);
$content[$draftKey] = $this->updateDraft($content[$draftKey]);
$this->session->put($this->instance, $content);
}
public function removeItemFromCart($draftKey, $itemKey)
public function removeProductFromDraft($draftKey, $itemKey)
{
$content = $this->getContent();
$content[$draftKey]->removeItem($itemKey);
$content[$draftKey]->removeProduct($itemKey);
$this->session->put($this->instance, $content);
}

2
app/Cart/CashDraft.php

@ -5,7 +5,7 @@ namespace App\Cart;
/**
* Cash Draft
*/
class CashDraft implements TransactionDraft
class CashDraft extends TransactionDraft
{
public $draftKey;
}

2
app/Cart/CreditDraft.php

@ -5,7 +5,7 @@ namespace App\Cart;
/**
* Credit Draft
*/
class CreditDraft implements TransactionDraft
class CreditDraft extends TransactionDraft
{
public $draftKey;
}

18
app/Cart/TransactionDraft.php

@ -5,4 +5,20 @@ namespace App\Cart;
/**
* Transaction Draft Interface
*/
interface TransactionDraft {}
abstract class TransactionDraft
{
public function toArray()
{
return [
'invoice_no' => 2,
'date' => 1,
'items' => [],
'total' => 0,
'payment' => 0,
'customer' => 0,
'status_id' => 0,
'creator_id' => 0,
'remark' => '',
];
}
}

48
tests/Unit/CartCollectionTest.php

@ -97,4 +97,52 @@ class CartCollectionTest extends TestCase
$this->assertCount(0, $cart->content());
$this->assertTrue($cart->isEmpty());
}
/** @test */
public function it_has_content_keys()
{
$cart = new CartCollection;
$cashDraft = new CashDraft;
$creditDraft = new CreditDraft;
$cart->add($cashDraft);
$cart->add($creditDraft);
$this->assertCount(2, $cart->keys());
$cart->removeDraft($cart->content()->keys()->last());
$this->assertCount(1, $cart->keys());
}
/** @test */
public function it_can_update_a_draft_attributes()
{
$cart = new CartCollection;
$draft = $cart->add(new CashDraft);
$this->assertCount(1, $cart->content());
$newDraftAttribute = [
'invoice_no' => 2,
'date' => 1,
'items' => [],
'total' => 0,
'payment' => 0,
'customer' => 0,
'status_id' => 0,
'creator_id' => 0,
'remark' => 0,
];
$cart->updateDraftAttributes($draft->draftKey, $newDraftAttribute);
$this->assertArrayHasKey('invoice_no', $draft->toArray());
$this->assertArrayHasKey('date', $draft->toArray());
$this->assertArrayHasKey('items', $draft->toArray());
$this->assertArrayHasKey('total', $draft->toArray());
$this->assertArrayHasKey('payment', $draft->toArray());
$this->assertArrayHasKey('customer', $draft->toArray());
$this->assertArrayHasKey('status_id', $draft->toArray());
$this->assertArrayHasKey('creator_id', $draft->toArray());
$this->assertArrayHasKey('remark', $draft->toArray());
}
}
Loading…
Cancel
Save