Browse Source

Added TransactionDraft and Product item on CartController

pull/1/head
Nafies Luthfi 9 years ago
parent
commit
d733f9f3e4
  1. 1
      app/Cart/CartCollection.php
  2. 37
      app/Http/Controllers/CartController.php
  3. 10
      database/factories/ModelFactory.php
  4. 3
      database/migrations/2017_04_09_013901_create_products_table.php
  5. 2
      routes/web.php
  6. 5
      tests/BrowserKitTestCase.php
  7. 56
      tests/Feature/Cart/CartControllerTest.php
  8. 9
      tests/TestCase.php

1
app/Cart/CartCollection.php

@ -105,6 +105,7 @@ class CartCollection
$content[$draftKey]->addItem($item); $content[$draftKey]->addItem($item);
$this->session->put($this->instance, $content); $this->session->put($this->instance, $content);
return $item->product;
} }
public function updateDraftItem($draftKey, $itemKey, $newItemData) public function updateDraftItem($draftKey, $itemKey, $newItemData)

37
app/Http/Controllers/CartController.php

@ -0,0 +1,37 @@
<?php
namespace App\Http\Controllers;
use App\Cart\CartCollection;
use App\Cart\CashDraft;
use App\Cart\CreditDraft;
use App\Cart\Item;
use App\Product;
use Illuminate\Http\Request;
class CartController extends Controller
{
private $cart;
public function __construct()
{
$this->cart = new CartCollection;
}
public function add(Request $request, $type)
{
if ($type == 1)
$this->cart->add(new CashDraft);
else
$this->cart->add(new CreditDraft);
return redirect()->route('cart.index', $item->draftKey);
}
public function addDraftItem(Request $request, $draftKey, Product $product)
{
$item = new Item($product, $request->qty);
$this->cart->addItemToDraft($draftKey, $item);
return redirect()->route('cart.index', $item->draftKey);
}
}

10
database/factories/ModelFactory.php

@ -21,3 +21,13 @@ $factory->define(App\User::class, function (Faker\Generator $faker) {
'remember_token' => str_random(10), 'remember_token' => str_random(10),
]; ];
}); });
/** @var \Illuminate\Database\Eloquent\Factory $factory */
$factory->define(App\Product::class, function (Faker\Generator $faker) {
return [
'name' => $faker->name,
'cash_price' => 2000,
'credit_price' => 1000,
];
});

3
database/migrations/2017_04_09_013901_create_products_table.php

@ -15,6 +15,9 @@ class CreateProductsTable extends Migration
{ {
Schema::create('products', function (Blueprint $table) { Schema::create('products', function (Blueprint $table) {
$table->increments('id'); $table->increments('id');
$table->string('name');
$table->unsignedInteger('cash_price');
$table->unsignedInteger('credit_price');
$table->timestamps(); $table->timestamps();
}); });
} }

2
routes/web.php

@ -18,3 +18,5 @@ Route::get('/', function () {
Auth::routes(); Auth::routes();
Route::get('/home', 'HomeController@index')->name('home'); Route::get('/home', 'HomeController@index')->name('home');
Route::post('cart/add-draft/{product}', 'CartController@add')->name('cart.add');
Route::post('cart/add-draft-item/{draftKey}/{product}', 'CartController@addDraftItem')->name('cart.add-draft-item');

5
tests/BrowserKitTestCase.php

@ -2,11 +2,6 @@
namespace Tests; namespace Tests;
use App\Entities\Customers\Customer;
use App\Entities\Invoices\Invoice;
use App\Entities\Receipts\Receipt;
use App\Entities\Users\User;
abstract class BrowserKitTestCase extends \Laravel\BrowserKitTesting\TestCase abstract class BrowserKitTestCase extends \Laravel\BrowserKitTesting\TestCase
{ {
use CreatesApplication; use CreatesApplication;

56
tests/Feature/Cart/CartControllerTest.php

@ -0,0 +1,56 @@
<?php
namespace Tests\Feature\Cart;
use App\Cart\CartCollection;
use App\Cart\CashDraft;
use App\Cart\CreditDraft;
use App\Product;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Tests\TestCase;
class CartControllerTest extends TestCase
{
use DatabaseMigrations;
/** @test */
public function user_can_add_new_draft_into_cart()
{
$this->loginAsUser();
$cart = new CartCollection;
$response = $this->post(route('cart.add', 1));
$response = $this->post(route('cart.add', 2));
$response->assertSessionHas('transactions.drafts');
$cashDraft = $cart->content()->first();
$this->assertTrue($cashDraft instanceof CashDraft);
$creditDraft = $cart->content()->last();
$this->assertTrue($creditDraft instanceof CreditDraft);
}
/** @test */
public function user_can_add_item_product_into_cart_draft()
{
$this->loginAsUser();
$cart = new CartCollection;
$draft = new CashDraft;
$cart->add($draft);
// Add Product to database
$product = factory(Product::class)->create(['cash_price' => 1100], ['credit_price' => 1000]);
$itemQty = 2;
// Add Product as CashDraft item
$response = $this->post(route('cart.add-draft-item', [$draft->draftKey, $product->id]), [
'qty' => $itemQty
]);
$cashDraft = $cart->content()->first();
$this->assertTrue($cashDraft instanceof CashDraft);
$this->assertEquals(2200, $cashDraft->getTotal());
}
}

9
tests/TestCase.php

@ -2,9 +2,18 @@
namespace Tests; namespace Tests;
use App\User;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase; use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase abstract class TestCase extends BaseTestCase
{ {
use CreatesApplication; use CreatesApplication;
protected function loginAsUser()
{
$user = factory(User::class)->create();
$this->actingAs($user);
return $user;
}
} }
Loading…
Cancel
Save