Browse Source

Added CartCollection, CashDraft, and CreditDraft class

pull/1/head
Nafies Luthfi 9 years ago
parent
commit
6c34a0cd73
  1. 77
      app/Cart/CartCollection.php
  2. 11
      app/Cart/CashDraft.php
  3. 11
      app/Cart/CreditDraft.php
  4. 8
      app/Cart/TransactionDraft.php
  5. 50
      tests/Unit/CartCollectionTest.php
  6. 20
      tests/Unit/ExampleTest.php

77
app/Cart/CartCollection.php

@ -0,0 +1,77 @@
<?php
namespace App\Cart;
use Illuminate\Support\Collection;
/**
* Cart Collection Class
*/
class CartCollection
{
private $instance;
private $session;
public function __construct()
{
$this->session = session();
$this->instance('drafts');
}
public function instance($instance = null)
{
$instance = $instance ?: 'drafts';
$this->instance = sprintf('%s.%s', 'transactions', $instance);
return $this;
}
public function currentInstance()
{
return str_replace('transactions.', '', $this->instance);
}
public function add(TransactionDraft $draft)
{
$content = $this->getContent();
$draft->draftKey = str_random(10);
$content->put($draft->draftKey, $draft);
$this->session->put($this->instance, $content);
return $draft;
}
public function content()
{
if (is_null($this->session->get($this->instance))) {
return collect([]);
}
return $this->session->get($this->instance);
}
protected function getContent()
{
$content = $this->session->has($this->instance) ? $this->session->get($this->instance) : collect([]);
return $content;
}
public function count()
{
return $this->getContent()->count();
}
public function isEmpty()
{
return $this->count() == 0;
}
public function hasContent()
{
return !$this->isEmpty();
}
}

11
app/Cart/CashDraft.php

@ -0,0 +1,11 @@
<?php
namespace App\Cart;
/**
* Cash Draft
*/
class CashDraft implements TransactionDraft
{
public $draftKey;
}

11
app/Cart/CreditDraft.php

@ -0,0 +1,11 @@
<?php
namespace App\Cart;
/**
* Credit Draft
*/
class CreditDraft implements TransactionDraft
{
public $draftKey;
}

8
app/Cart/TransactionDraft.php

@ -0,0 +1,8 @@
<?php
namespace App\Cart;
/**
* Transaction Draft Interface
*/
interface TransactionDraft {}

50
tests/Unit/CartCollectionTest.php

@ -0,0 +1,50 @@
<?php
namespace Tests\Unit;
use App\Cart\CartCollection;
use App\Cart\CashDraft;
use App\Cart\CreditDraft;
use Tests\TestCase;
class CartCollectionTest extends TestCase
{
/** @test */
public function it_has_a_default_instance()
{
$cart = new CartCollection;
$this->assertEquals('drafts', $cart->currentInstance());
}
/** @test */
public function it_can_have_multiple_instances()
{
$cart = new CartCollection;
$cashDraft = new CashDraft;
$creditDraft = new CreditDraft;
$cart->add($cashDraft);
$cart->add($creditDraft);
$cart->instance('wishlist')->add($cashDraft);
$cart->instance('wishlist')->add($creditDraft);
$this->assertCount(2, $cart->instance('drafts')->content());
$this->assertCount(2, $cart->instance('wishlist')->content());
}
/** @test */
public function cart_collection_consist_of_transacion_draft_class()
{
$cart = new CartCollection;
$cashDraft = new CashDraft;
$creditDraft = new CreditDraft;
$cart->add($cashDraft);
$cart->add($creditDraft);
$this->assertCount(2, $cart->content());
$this->assertTrue($cart->hasContent());
}
}

20
tests/Unit/ExampleTest.php

@ -1,20 +0,0 @@
<?php
namespace Tests\Unit;
use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ExampleTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function testBasicTest()
{
$this->assertTrue(true);
}
}
Loading…
Cancel
Save