From d733f9f3e454892cc619472f563dbb0c560bd3f2 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Sun, 9 Apr 2017 18:21:34 +0800 Subject: [PATCH] Added TransactionDraft and Product item on CartController --- app/Cart/CartCollection.php | 1 + app/Http/Controllers/CartController.php | 37 ++++++++++++++ database/factories/ModelFactory.php | 10 ++++ .../2017_04_09_013901_create_products_table.php | 3 ++ routes/web.php | 2 + tests/BrowserKitTestCase.php | 5 -- tests/Feature/Cart/CartControllerTest.php | 56 ++++++++++++++++++++++ tests/TestCase.php | 9 ++++ 8 files changed, 118 insertions(+), 5 deletions(-) create mode 100644 app/Http/Controllers/CartController.php create mode 100644 tests/Feature/Cart/CartControllerTest.php diff --git a/app/Cart/CartCollection.php b/app/Cart/CartCollection.php index f118041..907579d 100644 --- a/app/Cart/CartCollection.php +++ b/app/Cart/CartCollection.php @@ -105,6 +105,7 @@ class CartCollection $content[$draftKey]->addItem($item); $this->session->put($this->instance, $content); + return $item->product; } public function updateDraftItem($draftKey, $itemKey, $newItemData) diff --git a/app/Http/Controllers/CartController.php b/app/Http/Controllers/CartController.php new file mode 100644 index 0000000..0c4f308 --- /dev/null +++ b/app/Http/Controllers/CartController.php @@ -0,0 +1,37 @@ +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); + } +} diff --git a/database/factories/ModelFactory.php b/database/factories/ModelFactory.php index d74465d..5e066b8 100644 --- a/database/factories/ModelFactory.php +++ b/database/factories/ModelFactory.php @@ -21,3 +21,13 @@ $factory->define(App\User::class, function (Faker\Generator $faker) { '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, + ]; +}); diff --git a/database/migrations/2017_04_09_013901_create_products_table.php b/database/migrations/2017_04_09_013901_create_products_table.php index ec0ee65..1cbdab4 100644 --- a/database/migrations/2017_04_09_013901_create_products_table.php +++ b/database/migrations/2017_04_09_013901_create_products_table.php @@ -15,6 +15,9 @@ class CreateProductsTable extends Migration { Schema::create('products', function (Blueprint $table) { $table->increments('id'); + $table->string('name'); + $table->unsignedInteger('cash_price'); + $table->unsignedInteger('credit_price'); $table->timestamps(); }); } diff --git a/routes/web.php b/routes/web.php index d8ab25e..349ec67 100644 --- a/routes/web.php +++ b/routes/web.php @@ -18,3 +18,5 @@ Route::get('/', function () { Auth::routes(); 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'); diff --git a/tests/BrowserKitTestCase.php b/tests/BrowserKitTestCase.php index b940c44..6e39516 100644 --- a/tests/BrowserKitTestCase.php +++ b/tests/BrowserKitTestCase.php @@ -2,11 +2,6 @@ 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 { use CreatesApplication; diff --git a/tests/Feature/Cart/CartControllerTest.php b/tests/Feature/Cart/CartControllerTest.php new file mode 100644 index 0000000..f371e09 --- /dev/null +++ b/tests/Feature/Cart/CartControllerTest.php @@ -0,0 +1,56 @@ +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()); + } +} \ No newline at end of file diff --git a/tests/TestCase.php b/tests/TestCase.php index 2932d4a..7582ea6 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,9 +2,18 @@ namespace Tests; +use App\User; use Illuminate\Foundation\Testing\TestCase as BaseTestCase; abstract class TestCase extends BaseTestCase { use CreatesApplication; + + protected function loginAsUser() + { + $user = factory(User::class)->create(); + $this->actingAs($user); + + return $user; + } }