diff --git a/packages/Webkul/Checkout/src/Cart.php b/packages/Webkul/Checkout/src/Cart.php index 28021ecde..5da54ae3b 100755 --- a/packages/Webkul/Checkout/src/Cart.php +++ b/packages/Webkul/Checkout/src/Cart.php @@ -16,17 +16,6 @@ use Webkul\Customer\Repositories\CustomerAddressRepository; use Illuminate\Support\Facades\Event; use Illuminate\Support\Arr; -class addressHelper -{ - public $country; - public $postcode; - - function __construct() - { - $this->country = config('app.default_country'); - } -} - /** * Facades handler for all the methods to be implemented in Cart. * @@ -755,9 +744,6 @@ class Cart return false; } -// if (! $cart->shipping_address && ! $cart->billing_address) -// return; - foreach ($cart->items()->get() as $item) { $taxCategory = $this->taxCategoryRepository->find($item->product->tax_category_id); @@ -772,7 +758,15 @@ class Cart } if ($address === null) { - $address = new addressHelper(); + $address = new class() { + public $country; + public $postcode; + + function __construct() + { + $this->country = config('app.default_country'); + } + }; } $taxRates = $taxCategory->tax_rates()->where([ diff --git a/packages/Webkul/Tax/src/Helpers/Tax.php b/packages/Webkul/Tax/src/Helpers/Tax.php index e7bd81eee..fac5dc7ca 100644 --- a/packages/Webkul/Tax/src/Helpers/Tax.php +++ b/packages/Webkul/Tax/src/Helpers/Tax.php @@ -19,11 +19,10 @@ class Tax foreach ($that->items as $item) { $taxRate = (string)round((float)$item->tax_percent, self::TAX_PRECISION); - if (array_key_exists($taxRate, $taxes)) { - $taxes[$taxRate] += $asBase ? $item->base_tax_amount : $item->tax_amount; - } else { - $taxes += [$taxRate => $asBase ? $item->base_tax_amount : $item->tax_amount]; + if (!array_key_exists($taxRate, $taxes)) { + $taxes[$taxRate] = 0; } + $taxes[$taxRate] += $asBase ? $item->base_tax_amount : $item->tax_amount; } return $taxes; diff --git a/packages/Webkul/Tax/src/Providers/TaxServiceProvider.php b/packages/Webkul/Tax/src/Providers/TaxServiceProvider.php index 047ec84b4..886962155 100755 --- a/packages/Webkul/Tax/src/Providers/TaxServiceProvider.php +++ b/packages/Webkul/Tax/src/Providers/TaxServiceProvider.php @@ -23,17 +23,6 @@ class TaxServiceProvider extends ServiceProvider */ public function register() { - $this->registerEloquentFactoriesFrom(__DIR__ . '/../Database/Factories'); - } - - /** - * Register factories. - * - * @param string $path - * @return void - */ - protected function registerEloquentFactoriesFrom($path): void - { - $this->app->make(EloquentFactory::class)->load($path); + $this->loadFactoriesFrom(__DIR__ . '/../Database/Factories'); } } \ No newline at end of file diff --git a/tests/functional/Shop/CartCest.php b/tests/functional/Shop/CartCest.php index 8381defa2..2a6ab3a7b 100644 --- a/tests/functional/Shop/CartCest.php +++ b/tests/functional/Shop/CartCest.php @@ -20,25 +20,23 @@ class CartCest function _before(FunctionalTester $I) { - $this->faker = Factory::create(); + $this->country = Config::get('app.default_country'); $this->tax1 = $I->have(TaxRate::class, [ - //'tax_rate' => 7.00, 'country' => $this->country ]); - $taxCategorie1 = $I->have(TaxCategory::class, []); + $taxCategorie1 = $I->have(TaxCategory::class); $I->have(TaxMap::class, [ 'tax_rate_id' => $this->tax1->id, 'tax_category_id' => $taxCategorie1->id ]); $this->tax2 = $I->have(TaxRate::class, [ - //'tax_rate' => 19.00, 'country' => $this->country ]); - $taxCategorie2 = $I->have(TaxCategory::class, []); + $taxCategorie2 = $I->have(TaxCategory::class); $I->have(TaxMap::class, [ 'tax_rate_id' => $this->tax2->id, 'tax_category_id' => $taxCategorie2->id @@ -67,13 +65,14 @@ class CartCest public function checkCartWithMultipleTaxRates(FunctionalTester $I) { - - $prod1Quantity = $this->faker->numberBetween(9, 30); + $prod1Quantity = $I->fake()->numberBetween(9, 30); + // quantity of product1 should be not even if ($prod1Quantity % 2 !== 0) { $prod1Quantity -= 1; } - $prod2Quantity = $this->faker->numberBetween(9, 30); + $prod2Quantity = $I->fake()->numberBetween(9, 30); + // quantity of product2 should be even if ($prod2Quantity % 2 == 0) { $prod2Quantity -= 1; } @@ -86,8 +85,10 @@ class CartCest $I->amOnPage('/checkout/cart'); $I->see('Tax ' . $this->tax1->tax_rate . ' %', '#taxrate-' . core()->taxRateAsIdentifier($this->tax1->tax_rate)); - $I->see(core()->currency(round($this->product1->price * $this->tax1->tax_rate / 100, 2)), - '#basetaxamount-' . core()->taxRateAsIdentifier($this->tax1->tax_rate)); + $I->see( + core()->currency(round($this->product1->price * $this->tax1->tax_rate / 100, 2)), + '#basetaxamount-' . core()->taxRateAsIdentifier($this->tax1->tax_rate) + ); Cart::addProduct($this->product1->id, [ '_token' => session('_token'), @@ -97,8 +98,10 @@ class CartCest $I->amOnPage('/checkout/cart'); $I->see('Tax ' . $this->tax1->tax_rate . ' %', '#taxrate-' . core()->taxRateAsIdentifier($this->tax1->tax_rate)); - $I->see(core()->currency(round(($prod1Quantity + 1) * $this->product1->price * $this->tax1->tax_rate / 100, 2)), - '#basetaxamount-' . core()->taxRateAsIdentifier($this->tax1->tax_rate)); + $I->see( + core()->currency(round(($prod1Quantity + 1) * $this->product1->price * $this->tax1->tax_rate / 100, 2)), + '#basetaxamount-' . core()->taxRateAsIdentifier($this->tax1->tax_rate) + ); Cart::addProduct($this->product2->id, [ '_token' => session('_token'), diff --git a/tests/unit/Core/CoreCest.php b/tests/unit/Core/CoreCest.php index 25189b907..acb996f79 100644 --- a/tests/unit/Core/CoreCest.php +++ b/tests/unit/Core/CoreCest.php @@ -2,13 +2,31 @@ namespace Tests\Unit\Core; +use Codeception\Example; use UnitTester; class CoreCest { - public function testTaxRateAsIdentifier(UnitTester $I) + /** + * @param \UnitTester $I + * + * @param \Codeception\Example $scenario + * + * @throws \Exception + * @dataProvider getTaxRateScenarios + * + */ + public function testTaxRateAsIdentifier(UnitTester $I, Example $scenario): void { - $scenarios = [ + $I->assertEquals( + $scenario['expected'], + $I->executeFunction(\Webkul\Core\Core::class, 'taxRateAsIdentifier', [$scenario['input']]) + ); + } + + protected function getTaxRateScenarios(): array + { + return [ [ 'input' => 0, 'expected' => '0', @@ -18,17 +36,13 @@ class CoreCest 'expected' => '0_01', ], [ - 'input' => .12, + 'input' => .12, 'expected' => '0_12', ], [ - 'input' => 1234.5678, + 'input' => 1234.5678, 'expected' => '1234_5678', ], ]; - - foreach ($scenarios as $scenario) { - $I->assertEquals($scenario['expected'], $I->executeFunction(\Webkul\Core\Core::class, 'taxRateAsIdentifier', [$scenario['input']])); - } } } diff --git a/tests/unit/Tax/Helpers/TaxCest.php b/tests/unit/Tax/Helpers/TaxCest.php index 5d987aacf..e1d7d1461 100644 --- a/tests/unit/Tax/Helpers/TaxCest.php +++ b/tests/unit/Tax/Helpers/TaxCest.php @@ -16,14 +16,12 @@ class TaxCest public function _before(UnitTester $I) { - $faker = Factory::create(); - $country = Config::get('app.default_country'); $tax1 = $I->have(TaxRate::class, [ 'country' => $country, ]); - $taxCategorie1 = $I->have(TaxCategory::class, []); + $taxCategorie1 = $I->have(TaxCategory::class); $I->have(TaxMap::class, [ 'tax_rate_id' => $tax1->id, 'tax_category_id' => $taxCategorie1->id, @@ -32,7 +30,7 @@ class TaxCest $tax2 = $I->have(TaxRate::class, [ 'country' => $country, ]); - $taxCategorie2 = $I->have(TaxCategory::class, []); + $taxCategorie2 = $I->have(TaxCategory::class); $I->have(TaxMap::class, [ 'tax_rate_id' => $tax2->id, 'tax_category_id' => $taxCategorie2->id, @@ -90,8 +88,11 @@ class TaxCest public function testGetTaxRatesWithAmount(UnitTester $I) { - $result = $I->executeFunction(\Webkul\Tax\Helpers\Tax::class, 'getTaxRatesWithAmount', - [$this->scenario['object'], false]); + $result = $I->executeFunction( + \Webkul\Tax\Helpers\Tax::class, + 'getTaxRatesWithAmount', + [$this->scenario['object'], false] + ); foreach ($result as $taxRate => $taxAmount) { $I->assertTrue(array_key_exists($taxRate, $result)); @@ -101,8 +102,11 @@ class TaxCest public function testGetTaxTotal(UnitTester $I) { - $result = $I->executeFunction(\Webkul\Tax\Helpers\Tax::class, 'getTaxTotal', - [$this->scenario['object'], false]); + $result = $I->executeFunction( + \Webkul\Tax\Helpers\Tax::class, + 'getTaxTotal', + [$this->scenario['object'], false] + ); $I->assertEquals($this->scenario['expectedTaxTotal'], $result); }