refactoring

This commit is contained in:
Steffen Mahler 2020-02-04 14:31:18 +01:00
parent 721091cf32
commit a61446f1aa
6 changed files with 62 additions and 59 deletions

View File

@ -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([

View File

@ -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;

View File

@ -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');
}
}

View File

@ -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'),

View File

@ -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']]));
}
}
}

View File

@ -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);
}