7 changed files with 227 additions and 8 deletions
-
15app/Cart/CartCollection.php
-
45app/Cart/Item.php
-
38app/Cart/TransactionDraft.php
-
18app/Product.php
-
31database/migrations/2017_04_09_013901_create_products_table.php
-
52tests/Unit/CartCollectionTest.php
-
36tests/Unit/Integration/ProductTest.php
@ -0,0 +1,45 @@ |
|||
<?php |
|||
|
|||
namespace App\Cart; |
|||
|
|||
use App\Product; |
|||
|
|||
/** |
|||
* Draft Item class |
|||
*/ |
|||
class Item |
|||
{ |
|||
public $id; |
|||
public $product; |
|||
public $name; |
|||
public $price; |
|||
public $qty; |
|||
public $item_discount = 0; |
|||
public $item_discount_subtotal = 0; |
|||
public $subtotal; |
|||
|
|||
public function __construct(Product $product, $qty) |
|||
{ |
|||
$this->id = $product->id; |
|||
$this->product = $product; |
|||
$this->qty = $qty; |
|||
$this->price = $product->getPrice(); |
|||
$this->subtotal = $product->getPrice() * $qty; |
|||
} |
|||
|
|||
public function updateAttribute(array $newItemData) |
|||
{ |
|||
if (isset($newItemData['qty'])) { |
|||
$this->qty = $newItemData['qty']; |
|||
$this->subtotal = $this->price * $this->qty; |
|||
} |
|||
|
|||
if (isset($newItemData['item_discount'])) { |
|||
$this->item_discount = $newItemData['item_discount']; |
|||
$this->item_discount_subtotal = $this->item_discount * $this->qty; |
|||
$this->subtotal = $this->subtotal - $this->item_discount_subtotal; |
|||
} |
|||
|
|||
return $this; |
|||
} |
|||
} |
|||
@ -0,0 +1,18 @@ |
|||
<?php |
|||
|
|||
namespace App; |
|||
|
|||
use Illuminate\Database\Eloquent\Model; |
|||
|
|||
class Product extends Model |
|||
{ |
|||
protected $fillable = ['cash_price','credit_price']; |
|||
|
|||
public function getPrice($type = 'cash') |
|||
{ |
|||
if ($type == 'credit') |
|||
return $this->credit_price; |
|||
|
|||
return $this->cash_price; |
|||
} |
|||
} |
|||
@ -0,0 +1,31 @@ |
|||
<?php |
|||
|
|||
use Illuminate\Support\Facades\Schema; |
|||
use Illuminate\Database\Schema\Blueprint; |
|||
use Illuminate\Database\Migrations\Migration; |
|||
|
|||
class CreateProductsTable extends Migration |
|||
{ |
|||
/** |
|||
* Run the migrations. |
|||
* |
|||
* @return void |
|||
*/ |
|||
public function up() |
|||
{ |
|||
Schema::create('products', function (Blueprint $table) { |
|||
$table->increments('id'); |
|||
$table->timestamps(); |
|||
}); |
|||
} |
|||
|
|||
/** |
|||
* Reverse the migrations. |
|||
* |
|||
* @return void |
|||
*/ |
|||
public function down() |
|||
{ |
|||
Schema::dropIfExists('products'); |
|||
} |
|||
} |
|||
@ -0,0 +1,36 @@ |
|||
<?php |
|||
|
|||
namespace Tests\Unit\Integration; |
|||
|
|||
use App\Product; |
|||
use Illuminate\Foundation\Testing\DatabaseMigrations; |
|||
use Illuminate\Foundation\Testing\DatabaseTransactions; |
|||
use Tests\TestCase; |
|||
|
|||
class ProductTest extends TestCase |
|||
{ |
|||
/** @test */ |
|||
public function it_has_get_price_method() |
|||
{ |
|||
$product = new Product(['cash_price' => 3000]); |
|||
|
|||
$this->assertEquals(3000, $product->getPrice()); |
|||
} |
|||
|
|||
/** @test */ |
|||
public function product_get_price_method_default_to_cash_price() |
|||
{ |
|||
$product = new Product(['cash_price' => 3000]); |
|||
|
|||
$this->assertEquals($product->cash_price, $product->getPrice()); |
|||
} |
|||
|
|||
/** @test */ |
|||
public function product_get_price_can_also_returns_credit_price_of_product() |
|||
{ |
|||
$product = new Product(['cash_price' => 2000, 'credit_price' => 3000]); |
|||
|
|||
$this->assertEquals($product->credit_price, $product->getPrice('credit')); |
|||
$this->assertEquals(3000, $product->getPrice('credit')); |
|||
} |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue