diff --git a/packages/Webkul/Core/src/Helpers/Laravel5Helper.php b/packages/Webkul/Core/src/Helpers/Laravel5Helper.php index c1889cc06..31d7476ef 100644 --- a/packages/Webkul/Core/src/Helpers/Laravel5Helper.php +++ b/packages/Webkul/Core/src/Helpers/Laravel5Helper.php @@ -312,7 +312,7 @@ class Laravel5Helper extends Laravel5 'special_price_from' => null, 'special_price_to' => null, 'special_price' => null, - 'price' => $faker->randomFloat(4, 1, 1000), + 'price' => $faker->randomFloat(2, 1, 1000), 'weight' => '1.00', // necessary for shipping 'brand' => AttributeOption::firstWhere('attribute_id', $brand->id)->id, ]; diff --git a/tests/functional/Shop/CartTaxesCest.php b/tests/functional/Shop/CartTaxesCest.php index a0ead79cc..af1c338a1 100644 --- a/tests/functional/Shop/CartTaxesCest.php +++ b/tests/functional/Shop/CartTaxesCest.php @@ -16,6 +16,9 @@ class CartTaxesCest { public $country; + private const CART_TOTAL_PRECISION = 2; + private const TAX_AMOUNT_PRECISION = 2; + function _before(FunctionalTester $I) { $this->country = strtoupper(Config::get('app.default_country')) ?? 'DE'; @@ -62,7 +65,7 @@ class CartTaxesCest $product2 = $I->haveProduct(Laravel5Helper::SIMPLE_PRODUCT, $config2); $prod1Quantity = $I->fake()->numberBetween(9, 30); - // quantity of product1 should be not even + // quantity of product1 should be odd if ($prod1Quantity % 2 !== 0) { $prod1Quantity -= 1; } @@ -79,10 +82,15 @@ class CartTaxesCest 'quantity' => 1, ]); + $expectedTaxAmount1 = round( + round(1 * $product1->price, self::CART_TOTAL_PRECISION) + * $tax1->tax_rate / 100, + self::TAX_AMOUNT_PRECISION + ); + $I->amOnPage('/checkout/cart'); $I->see('Tax ' . $tax1->tax_rate . ' %', '#taxrate-' . core()->taxRateAsIdentifier($tax1->tax_rate)); - $I->see( - core()->currency(round($product1->price * $tax1->tax_rate / 100, 2)), + $I->see(core()->currency($expectedTaxAmount1), '#basetaxamount-' . core()->taxRateAsIdentifier($tax1->tax_rate) ); @@ -92,10 +100,15 @@ class CartTaxesCest 'quantity' => $prod1Quantity, ]); + $expectedTaxAmount1 = round( + round((1 + $prod1Quantity) * $product1->price, self::CART_TOTAL_PRECISION) + * $tax1->tax_rate / 100, + self::TAX_AMOUNT_PRECISION + ); + $I->amOnPage('/checkout/cart'); $I->see('Tax ' . $tax1->tax_rate . ' %', '#taxrate-' . core()->taxRateAsIdentifier($tax1->tax_rate)); - $I->see( - core()->currency(round(($prod1Quantity + 1) * $product1->price * $tax1->tax_rate / 100, 2)), + $I->see(core()->currency($expectedTaxAmount1), '#basetaxamount-' . core()->taxRateAsIdentifier($tax1->tax_rate) ); @@ -105,38 +118,40 @@ class CartTaxesCest 'quantity' => $prod2Quantity, ]); + $expectedTaxAmount2 = round( + round($prod2Quantity * $product2->price, self::CART_TOTAL_PRECISION) + * $tax2->tax_rate / 100, + self::TAX_AMOUNT_PRECISION + ); + $I->amOnPage('/checkout/cart'); $I->see('Tax ' . $tax1->tax_rate . ' %', '#taxrate-' . core()->taxRateAsIdentifier($tax1->tax_rate)); - $taxAmount1 = round(($prod1Quantity + 1) * $product1->price * $tax1->tax_rate / 100, 2); - $I->see(core()->currency($taxAmount1), '#basetaxamount-' . core()->taxRateAsIdentifier($tax1->tax_rate)); + $I->see(core()->currency($expectedTaxAmount1), '#basetaxamount-' . core()->taxRateAsIdentifier($tax1->tax_rate)); $I->see('Tax ' . $tax2->tax_rate . ' %', '#taxrate-' . core()->taxRateAsIdentifier($tax2->tax_rate)); - $taxAmount2 = round($prod2Quantity * $product2->price * $tax2->tax_rate / 100, 2); - $I->see(core()->currency($taxAmount2), '#basetaxamount-' . core()->taxRateAsIdentifier($tax2->tax_rate)); + $I->see(core()->currency($expectedTaxAmount2), '#basetaxamount-' . core()->taxRateAsIdentifier($tax2->tax_rate)); $cart = Cart::getCart(); $I->assertEquals(2, $cart->items_count); $I->assertEquals((float)($prod1Quantity + 1 + $prod2Quantity), $cart->items_qty); - $I->assertEquals($taxAmount1 + $taxAmount2, $cart->tax_total); + $I->assertEquals(round($expectedTaxAmount1 + $expectedTaxAmount2, self::TAX_AMOUNT_PRECISION), $cart->tax_total); Cart::removeItem($cart->items[1]->id); $I->amOnPage('/checkout/cart'); $I->amOnPage('/checkout/cart'); $I->see('Tax ' . $tax1->tax_rate . ' %', '#taxrate-' . core()->taxRateAsIdentifier($tax1->tax_rate)); - $taxAmount1 = round(($prod1Quantity + 1) * $product1->price * $tax1->tax_rate / 100, 2); - $I->see(core()->currency($taxAmount1), '#basetaxamount-' . core()->taxRateAsIdentifier($tax1->tax_rate)); + $I->see(core()->currency($expectedTaxAmount1), '#basetaxamount-' . core()->taxRateAsIdentifier($tax1->tax_rate)); $I->dontSee('Tax ' . $tax2->tax_rate . ' %', '#taxrate-' . core()->taxRateAsIdentifier($tax2->tax_rate)); - $taxAmount2 = round($prod2Quantity * $product2->price * $tax2->tax_rate / 100, 2); - $I->dontSee(core()->currency($taxAmount2), '#basetaxamount-' . core()->taxRateAsIdentifier($tax2->tax_rate)); + $I->dontSee(core()->currency($expectedTaxAmount2), '#basetaxamount-' . core()->taxRateAsIdentifier($tax2->tax_rate)); $cart = Cart::getCart(); $I->assertEquals(1, $cart->items_count); $I->assertEquals((float)($prod1Quantity + 1), $cart->items_qty); - $I->assertEquals($taxAmount1, $cart->tax_total); + $I->assertEquals($expectedTaxAmount1, $cart->tax_total); } public function checkCartWithMultipleZipRangeBasedTaxes(FunctionalTester $I): void @@ -233,6 +248,28 @@ class CartTaxesCest 'quantity' => 1, ]); + $expectedTaxAmount11 = round( + round(1 * $product1->price, self::CART_TOTAL_PRECISION) + * $tax11->tax_rate / 100, + self::TAX_AMOUNT_PRECISION + ); + $expectedTaxAmount12 = round( + round(1 * $product1->price, self::CART_TOTAL_PRECISION) + * $tax12->tax_rate / 100, + self::TAX_AMOUNT_PRECISION + ); + + $expectedTaxAmount21 = round( + round(1 * $product2->price, self::CART_TOTAL_PRECISION) + * $tax21->tax_rate / 100, + self::TAX_AMOUNT_PRECISION + ); + $expectedTaxAmount22 = round( + round(1 * $product2->price, self::CART_TOTAL_PRECISION) + * $tax22->tax_rate / 100, + self::TAX_AMOUNT_PRECISION + ); + Cart::saveCustomerAddress( [ 'billing' => [ @@ -256,26 +293,22 @@ class CartTaxesCest $I->amOnPage('/checkout/cart'); $I->see('Tax ' . $tax11->tax_rate . ' %', '#taxrate-' . core()->taxRateAsIdentifier($tax11->tax_rate)); - $I->see( - core()->currency(round($product1->price * $tax11->tax_rate / 100, 2)), + $I->see(core()->currency($expectedTaxAmount11), '#basetaxamount-' . core()->taxRateAsIdentifier($tax11->tax_rate) ); $I->dontSee('Tax ' . $tax12->tax_rate . ' %', '#taxrate-' . core()->taxRateAsIdentifier($tax12->tax_rate)); - $I->dontSee( - core()->currency(round($product1->price * $tax12->tax_rate / 100, 2)), + $I->dontSee(core()->currency($expectedTaxAmount12), '#basetaxamount-' . core()->taxRateAsIdentifier($tax12->tax_rate) ); $I->dontSee('Tax ' . $tax21->tax_rate . ' %', '#taxrate-' . core()->taxRateAsIdentifier($tax21->tax_rate)); - $I->dontSee( - core()->currency(round($product2->price * $tax21->tax_rate / 100, 2)), + $I->dontSee(core()->currency($expectedTaxAmount21), '#basetaxamount-' . core()->taxRateAsIdentifier($tax21->tax_rate) ); $I->dontSee('Tax ' . $tax22->tax_rate . ' %', '#taxrate-' . core()->taxRateAsIdentifier($tax22->tax_rate)); - $I->dontSee( - core()->currency(round($product2->price * $tax22->tax_rate / 100, 2)), + $I->dontSee(core()->currency($expectedTaxAmount22), '#basetaxamount-' . core()->taxRateAsIdentifier($tax22->tax_rate) ); @@ -288,34 +321,27 @@ class CartTaxesCest $I->amOnPage('/checkout/cart'); $I->see('Tax ' . $tax11->tax_rate . ' %', '#taxrate-' . core()->taxRateAsIdentifier($tax11->tax_rate)); - $I->see( - core()->currency(round($product1->price * $tax11->tax_rate / 100, 2)), + $I->see(core()->currency($expectedTaxAmount11), '#basetaxamount-' . core()->taxRateAsIdentifier($tax11->tax_rate) ); $I->dontSee('Tax ' . $tax12->tax_rate . ' %', '#taxrate-' . core()->taxRateAsIdentifier($tax12->tax_rate)); - $I->dontSee( - core()->currency(round($product1->price * $tax12->tax_rate / 100, 2)), + $I->dontSee(core()->currency($expectedTaxAmount12), '#basetaxamount-' . core()->taxRateAsIdentifier($tax12->tax_rate) ); $I->see('Tax ' . $tax21->tax_rate . ' %', '#taxrate-' . core()->taxRateAsIdentifier($tax21->tax_rate)); - $I->see( - core()->currency(round($product2->price * $tax21->tax_rate / 100, 2)), + $I->see(core()->currency($expectedTaxAmount21), '#basetaxamount-' . core()->taxRateAsIdentifier($tax21->tax_rate) ); $I->dontSee('Tax ' . $tax22->tax_rate . ' %', '#taxrate-' . core()->taxRateAsIdentifier($tax22->tax_rate)); - $I->dontSee( - core()->currency(round($product2->price * $tax22->tax_rate / 100, 2)), + $I->dontSee(core()->currency($expectedTaxAmount22), '#basetaxamount-' . core()->taxRateAsIdentifier($tax22->tax_rate) ); - $taxAmount1 = round($product1->price * $tax11->tax_rate / 100, 2); - $I->see(core()->currency($taxAmount1), '#basetaxamount-' . core()->taxRateAsIdentifier($tax11->tax_rate)); - - $taxAmount2 = round($product2->price * $tax21->tax_rate / 100, 2); - $I->see(core()->currency($taxAmount2), '#basetaxamount-' . core()->taxRateAsIdentifier($tax21->tax_rate)); + $I->see(core()->currency($expectedTaxAmount11), '#basetaxamount-' . core()->taxRateAsIdentifier($tax11->tax_rate)); + $I->see(core()->currency($expectedTaxAmount21), '#basetaxamount-' . core()->taxRateAsIdentifier($tax21->tax_rate)); $I->wantToTest('customer address with postcode in range of 50000 - 89999'); @@ -349,20 +375,17 @@ class CartTaxesCest $I->amOnPage('/checkout/cart'); $I->dontSee('Tax ' . $tax11->tax_rate . ' %', '#taxrate-' . core()->taxRateAsIdentifier($tax11->tax_rate)); - $I->dontSee( - core()->currency(round($product1->price * $tax11->tax_rate / 100, 2)), + $I->dontSee(core()->currency($expectedTaxAmount11), '#basetaxamount-' . core()->taxRateAsIdentifier($tax11->tax_rate) ); $I->see('Tax ' . $tax12->tax_rate . ' %', '#taxrate-' . core()->taxRateAsIdentifier($tax12->tax_rate)); - $I->see( - core()->currency(round($product1->price * $tax12->tax_rate / 100, 2)), + $I->see(core()->currency($expectedTaxAmount12), '#basetaxamount-' . core()->taxRateAsIdentifier($tax12->tax_rate) ); $I->dontSee('Tax ' . $tax21->tax_rate . ' %', '#taxrate-' . core()->taxRateAsIdentifier($tax21->tax_rate)); - $I->dontSee( - core()->currency(round($product2->price * $tax21->tax_rate / 100, 2)), + $I->dontSee(core()->currency($expectedTaxAmount21), '#basetaxamount-' . core()->taxRateAsIdentifier($tax21->tax_rate) ); @@ -372,11 +395,8 @@ class CartTaxesCest '#basetaxamount-' . core()->taxRateAsIdentifier($tax22->tax_rate) ); - $taxAmount1 = round($product1->price * $tax12->tax_rate / 100, 2); - $I->see(core()->currency($taxAmount1), '#basetaxamount-' . core()->taxRateAsIdentifier($tax12->tax_rate)); - - $taxAmount2 = round($product2->price * $tax22->tax_rate / 100, 2); - $I->see(core()->currency($taxAmount2), '#basetaxamount-' . core()->taxRateAsIdentifier($tax22->tax_rate)); + $I->see(core()->currency($expectedTaxAmount12), '#basetaxamount-' . core()->taxRateAsIdentifier($tax12->tax_rate)); + $I->see(core()->currency($expectedTaxAmount22), '#basetaxamount-' . core()->taxRateAsIdentifier($tax22->tax_rate)); $I->wantToTest('customer address with postcode in range of 90000 - 99000'); $I->wanttoTest('as we dont have any taxes in this zip range'); @@ -410,26 +430,22 @@ class CartTaxesCest $I->amOnPage('/checkout/cart'); $I->dontSee('Tax ' . $tax11->tax_rate . ' %', '#taxrate-' . core()->taxRateAsIdentifier($tax11->tax_rate)); - $I->dontSee( - core()->currency(round($product1->price * $tax11->tax_rate / 100, 2)), + $I->dontSee(core()->currency($expectedTaxAmount11), '#basetaxamount-' . core()->taxRateAsIdentifier($tax11->tax_rate) ); $I->dontSee('Tax ' . $tax12->tax_rate . ' %', '#taxrate-' . core()->taxRateAsIdentifier($tax12->tax_rate)); - $I->dontSee( - core()->currency(round($product1->price * $tax12->tax_rate / 100, 2)), + $I->dontSee(core()->currency($expectedTaxAmount12), '#basetaxamount-' . core()->taxRateAsIdentifier($tax12->tax_rate) ); $I->dontSee('Tax ' . $tax21->tax_rate . ' %', '#taxrate-' . core()->taxRateAsIdentifier($tax21->tax_rate)); - $I->dontSee( - core()->currency(round($product2->price * $tax21->tax_rate / 100, 2)), + $I->dontSee(core()->currency($expectedTaxAmount21), '#basetaxamount-' . core()->taxRateAsIdentifier($tax21->tax_rate) ); $I->dontSee('Tax ' . $tax22->tax_rate . ' %', '#taxrate-' . core()->taxRateAsIdentifier($tax22->tax_rate)); - $I->dontSee( - core()->currency(round($product2->price * $tax22->tax_rate / 100, 2)), + $I->dontSee(core()->currency($expectedTaxAmount22), '#basetaxamount-' . core()->taxRateAsIdentifier($tax22->tax_rate) ); } diff --git a/tests/unit/Tax/Helpers/TaxCest.php b/tests/unit/Tax/Helpers/TaxCest.php index 9e1c5be7e..4dd4c843a 100644 --- a/tests/unit/Tax/Helpers/TaxCest.php +++ b/tests/unit/Tax/Helpers/TaxCest.php @@ -2,7 +2,6 @@ namespace Tests\Unit\Tax\Helpers; -use Faker\Factory; use Illuminate\Support\Facades\Config; use UnitTester; use Webkul\Tax\Models\TaxCategory; @@ -14,6 +13,12 @@ class TaxCest { public $scenario; + private const PRODUCT1_QTY = 11; + private const PRODUCT2_QTY = 7; + + private const CART_TOTAL_PRECISION = 2; + private const TAX_AMOUNT_PRECISION = 2; + public function _before(UnitTester $I) { $country = strtoupper(Config::get('app.default_country')) ?? 'DE'; @@ -59,30 +64,35 @@ class TaxCest Cart::addProduct($product1->id, [ '_token' => session('_token'), 'product_id' => $product1->id, - 'quantity' => 11, + 'quantity' => self::PRODUCT1_QTY, ]); Cart::addProduct($product2->id, [ '_token' => session('_token'), 'product_id' => $product2->id, - 'quantity' => 7, + 'quantity' => self::PRODUCT2_QTY, ]); + $expectedTaxAmount1 = round( + round(self::PRODUCT1_QTY * $product1->price, self::CART_TOTAL_PRECISION) + * $tax1->tax_rate / 100, + self::TAX_AMOUNT_PRECISION + ); + + $expectedTaxAmount2 = round( + round(self::PRODUCT2_QTY * $product2->price, self::CART_TOTAL_PRECISION) + * $tax2->tax_rate / 100, + self::TAX_AMOUNT_PRECISION + ); $this->scenario = [ - 'object' => Cart::getCart(), + 'cart' => Cart::getCart(), 'expectedTaxRates' => [ - (string)round((float)$tax1->tax_rate, 4) - => round(11 * $product1->price * $tax1->tax_rate / 100, 2), - - (string)round((float)$tax2->tax_rate, 4) - => round(7 * $product2->price * $tax2->tax_rate / 100, 2), + (string)round((float)$tax1->tax_rate, 4) => $expectedTaxAmount1, + (string)round((float)$tax2->tax_rate, 4) => $expectedTaxAmount2, ], 'expectedTaxTotal' => - round( - round(11 * $product1->price * $tax1->tax_rate / 100, 2) - + round(7 * $product2->price * $tax2->tax_rate / 100, 2) - , 2), + round($expectedTaxAmount1 + $expectedTaxAmount2, self::TAX_AMOUNT_PRECISION), ]; } @@ -91,7 +101,7 @@ class TaxCest $result = $I->executeFunction( \Webkul\Tax\Helpers\Tax::class, 'getTaxRatesWithAmount', - [$this->scenario['object'], false] + [$this->scenario['cart'], false] ); foreach ($result as $taxRate => $taxAmount) { @@ -105,7 +115,7 @@ class TaxCest $result = $I->executeFunction( \Webkul\Tax\Helpers\Tax::class, 'getTaxTotal', - [$this->scenario['object'], false] + [$this->scenario['cart'], false] ); $I->assertEquals($this->scenario['expectedTaxTotal'], $result);