From a5c49e4b7683c1326a0807eb682a5ad300254167 Mon Sep 17 00:00:00 2001 From: Steffen Mahler Date: Fri, 24 Jan 2020 07:15:49 +0100 Subject: [PATCH] button 'update cart' will only be shown if corresponding products are in cart --- .gitignore | 1 + packages/Webkul/Checkout/src/Cart.php | 19 ++++++ packages/Webkul/Checkout/src/Models/Cart.php | 16 +++++ .../src/Http/Controllers/CartController.php | 19 +++++- .../views/checkout/cart/index.blade.php | 4 +- public/mix-manifest.json | 3 +- tests/_support/UnitTester.php | 49 +++++++++++++++ tests/functional/Checkout/Cart/CartCest.php | 13 ++++ .../Cart/Controllers/CartControllerCest.php | 36 +++++++++++ .../Checkout/Cart/Models/CartModelCest.php | 59 +++++++++++++++++++ 10 files changed, 215 insertions(+), 4 deletions(-) create mode 100644 tests/functional/Checkout/Cart/CartCest.php create mode 100644 tests/unit/Checkout/Cart/Controllers/CartControllerCest.php create mode 100644 tests/unit/Checkout/Cart/Models/CartModelCest.php diff --git a/.gitignore b/.gitignore index fa1eccb1e..129eca994 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,4 @@ yarn.lock .php_cs.cache storage/ storage/*.key +/docker-compose-collection/ diff --git a/packages/Webkul/Checkout/src/Cart.php b/packages/Webkul/Checkout/src/Cart.php index 506cecb0c..3f22ad2c0 100755 --- a/packages/Webkul/Checkout/src/Cart.php +++ b/packages/Webkul/Checkout/src/Cart.php @@ -320,6 +320,25 @@ class Cart { return true; } + /** + * Clear cart + * @return bool + */ + public function clear(): bool + { + if (! $cart = $this->getCart()) { + return false; + } + + $this->cartRepository->delete($cart->id); + + if (session()->has('cart')) { + session()->forget('cart'); + } + + return true; + } + /** * This function handles when guest has some of cart products and then logs in. * diff --git a/packages/Webkul/Checkout/src/Models/Cart.php b/packages/Webkul/Checkout/src/Models/Cart.php index 4f17e688c..177ac2758 100755 --- a/packages/Webkul/Checkout/src/Models/Cart.php +++ b/packages/Webkul/Checkout/src/Models/Cart.php @@ -130,4 +130,20 @@ class Cart extends Model implements CartContract return false; } + + /** + * Returns true if cart contains one or many products with quantity box. + * f.e. simple, configurable, virtual + * @return bool + */ + public function hasProductsWithQuantityBox(): bool + { + $result = true; + foreach ($this->items as $item) { + if ($item->product->getTypeInstance()->showQuantityBox() === true) { + $result = false; + } + } + return $result; + } } \ No newline at end of file diff --git a/packages/Webkul/Shop/src/Http/Controllers/CartController.php b/packages/Webkul/Shop/src/Http/Controllers/CartController.php index 374695d40..fd5e314aa 100755 --- a/packages/Webkul/Shop/src/Http/Controllers/CartController.php +++ b/packages/Webkul/Shop/src/Http/Controllers/CartController.php @@ -80,9 +80,12 @@ class CartController extends Controller try { $result = Cart::addProduct($id, request()->all()); - if (is_array($result) && isset($result['warning'])) { + if ($this->onWarningAddingToCart($result)) { session()->flash('warning', $result['warning']); - } else { + return redirect()->back(); + } + + if ($result instanceof Cart) { session()->flash('success', trans('shop::app.checkout.cart.item.success')); if ($customer = auth()->guard('customer')->user()) @@ -203,4 +206,16 @@ class CartController extends Controller 'message' => trans('shop::app.checkout.total.remove-coupon') ]); } + + /** + * Returns true, if result of adding product to cart + * is an array and contains a key "warning" + * + * @param $result + * + * @return bool + */ + private function onWarningAddingToCart($result): bool { + return is_array($result) && isset($result['warning']); + } } \ No newline at end of file diff --git a/packages/Webkul/Shop/src/Resources/views/checkout/cart/index.blade.php b/packages/Webkul/Shop/src/Resources/views/checkout/cart/index.blade.php index 85b83c6c8..368e08867 100755 --- a/packages/Webkul/Shop/src/Resources/views/checkout/cart/index.blade.php +++ b/packages/Webkul/Shop/src/Resources/views/checkout/cart/index.blade.php @@ -108,9 +108,11 @@ {{ __('shop::app.checkout.cart.continue-shopping') }}
- + @endif @if (! cart()->hasError()) diff --git a/public/mix-manifest.json b/public/mix-manifest.json index 0d5d4a41c..2d6011713 100755 --- a/public/mix-manifest.json +++ b/public/mix-manifest.json @@ -1,3 +1,4 @@ { - "/js/app.js": "/js/app.js" + "/js/app.js": "/js/app.js", + "/css/app.css": "/css/app.css" } diff --git a/tests/_support/UnitTester.php b/tests/_support/UnitTester.php index e19544a51..5e3dccc40 100644 --- a/tests/_support/UnitTester.php +++ b/tests/_support/UnitTester.php @@ -1,5 +1,6 @@ comment('I execute function "' + . $functionName + . '" of class "' + . (is_object($className) ? get_class($className) : $className) + . '" with ' + . count($methodParams) + . ' method-params, ' + . count($constructParams) + . ' constuctor-params and ' + . count($mocks) + . ' mocked class-methods/params' + ); + $class = new \ReflectionClass($className); + $method = $class->getMethod($functionName); + $method->setAccessible(true); + if (is_object($className)) { + $reflectedClass = $className; + } elseif (empty($constructParams)) { + $reflectedClass = Stub::make($className, $mocks); + } else { + $reflectedClass = Stub::construct($className, $constructParams, $mocks); + } + + return $method->invokeArgs($reflectedClass, $methodParams); + } } diff --git a/tests/functional/Checkout/Cart/CartCest.php b/tests/functional/Checkout/Cart/CartCest.php new file mode 100644 index 000000000..d785db783 --- /dev/null +++ b/tests/functional/Checkout/Cart/CartCest.php @@ -0,0 +1,13 @@ + ['key' => 'value', 'warning' => 'Hello World. Something went wrong.'], + 'expected' => true, + ], + [ + 'result' => ['key' => 'value'], + 'expected' => false, + ], + [ + 'result' => new Cart(), + 'expected' => false, + ], + ]; + + foreach ($scenarios as $scenario) { + $I->assertEquals($scenario['expected'], $I->executeFunction(CartController::class, 'onWarningAddingToCart', [$scenario['result']])); + } + } +} \ No newline at end of file diff --git a/tests/unit/Checkout/Cart/Models/CartModelCest.php b/tests/unit/Checkout/Cart/Models/CartModelCest.php new file mode 100644 index 000000000..e917cc1db --- /dev/null +++ b/tests/unit/Checkout/Cart/Models/CartModelCest.php @@ -0,0 +1,59 @@ +productWithQuantityBox = $I->haveProduct(); + $this->productWithoutQuantityBox = $I->haveProduct(); + } + + public function testHasProductsWithQuantityBox(UnitTester $I) + { + $this->createMixedCart(); + $I->assertTrue($I->executeFunction(CartModel::class, 'hasProductsWithQuantityBox')); + + $this->createCardWithProductsWithQuantityBox(); + $I->assertTrue($I->executeFunction(CartModel::class, 'hasProductsWithQuantityBox')); + + $this->createCardWithProductsWithQuantityBox(); + $I->assertFalse($I->executeFunction(CartModel::class, 'hasProductsWithQuantityBox')); + } + + private function createMixedCart() + { + $this->clearCart(); + + Cart::addProduct($this->productWithQuantityBox->id); + Cart::addProduct($this->productWithoutQuantityBox->id); + } + + private function createCardWithProductsWithQuantityBox() + { + $this->clearCart(); + Cart::addProduct($this->productWithQuantityBox->id); + } + + private function createCardWithoutProductsWithQuantityBox() + { + $this->clearCart(); + Cart::addProduct($this->productWithoutQuantityBox); + } + + private function clearCart() + { + Cart::clear(); + } +} \ No newline at end of file