From 68a1d16e926589d13512e6feffc34eac0ae3071f Mon Sep 17 00:00:00 2001 From: Steffen Mahler Date: Fri, 1 May 2020 09:09:57 +0200 Subject: [PATCH] fix randomly failing tax cest --- tests/unit/Tax/Helpers/TaxCest.php | 40 +++++++++++++++++++----------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/tests/unit/Tax/Helpers/TaxCest.php b/tests/unit/Tax/Helpers/TaxCest.php index 7a29e5e3a..8cd1e9793 100644 --- a/tests/unit/Tax/Helpers/TaxCest.php +++ b/tests/unit/Tax/Helpers/TaxCest.php @@ -5,6 +5,7 @@ namespace Tests\Unit\Tax\Helpers; use Faker\Factory; use Illuminate\Support\Facades\Config; use UnitTester; +use Webkul\Tax\Helpers\Tax; use Webkul\Tax\Models\TaxCategory; use Webkul\Tax\Models\TaxMap; use Webkul\Tax\Models\TaxRate; @@ -14,6 +15,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 +66,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, \Webkul\Tax\Helpers\Tax::TAX_PRECISION) - => round(11 * $product1->price * $tax1->tax_rate / 100, 4), - - (string)round((float)$tax2->tax_rate, \Webkul\Tax\Helpers\Tax::TAX_PRECISION) - => round(7 * $product2->price * $tax2->tax_rate / 100, 4), + (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 +103,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 +117,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);