6 changed files with 157 additions and 20 deletions
-
77app/Cart/CartCollection.php
-
11app/Cart/CashDraft.php
-
11app/Cart/CreditDraft.php
-
8app/Cart/TransactionDraft.php
-
50tests/Unit/CartCollectionTest.php
-
20tests/Unit/ExampleTest.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(); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,11 @@ |
|||
<?php |
|||
|
|||
namespace App\Cart; |
|||
|
|||
/** |
|||
* Cash Draft |
|||
*/ |
|||
class CashDraft implements TransactionDraft |
|||
{ |
|||
public $draftKey; |
|||
} |
|||
@ -0,0 +1,11 @@ |
|||
<?php |
|||
|
|||
namespace App\Cart; |
|||
|
|||
/** |
|||
* Credit Draft |
|||
*/ |
|||
class CreditDraft implements TransactionDraft |
|||
{ |
|||
public $draftKey; |
|||
} |
|||
@ -0,0 +1,8 @@ |
|||
<?php |
|||
|
|||
namespace App\Cart; |
|||
|
|||
/** |
|||
* Transaction Draft Interface |
|||
*/ |
|||
interface TransactionDraft {} |
|||
@ -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()); |
|||
} |
|||
} |
|||
@ -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); |
|||
} |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue