add unit tests, refactoring

This commit is contained in:
Steffen Mahler 2020-02-04 11:18:40 +01:00
parent cb5e3a47f6
commit 7c661efd88
12 changed files with 164 additions and 35 deletions

View File

@ -347,9 +347,9 @@
@php ($taxRates = Webkul\Tax\Helpers\Tax::getTaxRatesWithAmount($order, true))
@foreach ($taxRates as $taxRate => $baseTaxAmount)
<tr {{ $loop->last ? 'class=border' : ''}}>
<td id="taxrate-{{ $taxRate }}">{{ __('admin::app.sales.orders.tax') }} {{ $taxRate }} %</td>
<td id="taxrate-{{ core()->taxRateAsIdentifier($taxRate) }}">{{ __('admin::app.sales.orders.tax') }} {{ $taxRate }} %</td>
<td>-</td>
<td id="basetaxamount-{{ $taxRate }}">{{ core()->formatBasePrice($baseTaxAmount) }}</td>
<td id="basetaxamount-{{ core()->taxRateAsIdentifier($taxRate) }}">{{ core()->formatBasePrice($baseTaxAmount) }}</td>
</tr>
@endforeach

View File

@ -933,4 +933,15 @@ class Core
return $instance[$className] = app($className);
}
/**
* Returns a string as selector part for identifying elements in views
* @param float $taxRate
*
* @return string
*/
public static function taxRateAsIdentifier(float $taxRate): string
{
return str_replace('.', '_', (string)$taxRate);
}
}

View File

@ -20,8 +20,8 @@
@if ($cart->base_tax_total)
@foreach (Webkul\Tax\Helpers\Tax::getTaxRatesWithAmount($cart, true) as $taxRate => $baseTaxAmount )
<div class="item-detail">
<label id="taxrate-{{ $taxRate }}">{{ __('shop::app.checkout.total.tax') }} {{ $taxRate }} %</label>
<label class="right" id="basetaxamount-{{ $taxRate }}">{{ core()->currency($baseTaxAmount) }}</label>
<label id="taxrate-{{ core()->taxRateAsIdentifier($taxRate) }}">{{ __('shop::app.checkout.total.tax') }} {{ $taxRate }} %</label>
<label class="right" id="basetaxamount-{{ core()->taxRateAsIdentifier($taxRate) }}">{{ core()->currency($baseTaxAmount) }}</label>
</div>
@endforeach
@endif

View File

@ -163,8 +163,8 @@
@foreach (Webkul\Tax\Helpers\Tax::getTaxRatesWithAmount($order, true) as $taxRate => $baseTaxAmount )
<div>
<span id="taxrate-{{ $taxRate }}">{{ __('shop::app.mail.order.tax') }} {{ $taxRate }} %</span>
<span id="basetaxamount-{{ $taxRate }}" style="float: right;">
<span id="taxrate-{{ core()->taxRateAsIdentifier($taxRate) }}">{{ __('shop::app.mail.order.tax') }} {{ $taxRate }} %</span>
<span id="basetaxamount-{{ core()->taxRateAsIdentifier($taxRate) }}" style="float: right;">
{{ core()->formatBasePrice($baseTaxAmount) }}
</span>
</div>

View File

@ -158,8 +158,8 @@
@foreach (Webkul\Tax\Helpers\Tax::getTaxRatesWithAmount($refund, false) as $taxRate => $taxAmount)
<div>
<span id="taxrate-{{ $taxRate }}">{{ __('shop::app.mail.order.tax') }} {{ $taxRate }} %</span>
<span id="taxamount-{{ $taxRate }}" style="float: right;">
<span id="taxrate-{{ core()->taxRateAsIdentifier($taxRate) }}">{{ __('shop::app.mail.order.tax') }} {{ $taxRate }} %</span>
<span id="taxamount-{{ core()->taxRateAsIdentifier($taxRate) }}" style="float: right;">
{{ core()->formatPrice($taxAmount, $order->order_currency_code) }}
</span>
</div>

View File

@ -158,8 +158,8 @@
@foreach (Webkul\Tax\Helpers\Tax::getTaxRatesWithAmount($order, false) as $taxRate => $taxAmount )
<div>
<span id="taxrate-{{ $taxRate }}">{{ __('shop::app.mail.order.tax') }} {{ $taxRate }} %</span>
<span id="taxamount-{{ $taxRate }}" style="float: right;">
<span id="taxrate-{{ core()->taxRateAsIdentifier($taxRate) }}">{{ __('shop::app.mail.order.tax') }} {{ $taxRate }} %</span>
<span id="taxamount-{{ core()->taxRateAsIdentifier($taxRate) }}" style="float: right;">
{{ core()->formatPrice($taxAmount, $order->order_currency_code) }}
</span>
</div>

View File

@ -159,8 +159,8 @@
@foreach (Webkul\Tax\Helpers\Tax::getTaxRatesWithAmount($order, false) as $taxRate => $taxAmount )
<div>
<span id="taxrate-{{ $taxRate }}">{{ __('shop::app.mail.order.cancel.tax') }} {{ $taxRate }} %</span>
<span id="taxamount-{{ $taxRate }}" style="float: right;">
<span id="taxrate-{{ core()->taxRateAsIdentifier($taxRate) }}">{{ __('shop::app.mail.order.cancel.tax') }} {{ $taxRate }} %</span>
<span id="taxamount-{{ core()->taxRateAsIdentifier($taxRate) }}" style="float: right;">
{{ core()->formatPrice($taxAmount, $order->order_currency_code) }}
</span>
</div>

View File

@ -29,6 +29,13 @@ class Tax
return $taxes;
}
/**
* Returns the total tax amount
* @param object $that
* @param bool $asBase
*
* @return float
*/
public static function getTaxTotal(object $that, bool $asBase = false): float
{
$taxes = self::getTaxRatesWithAmount($that, $asBase);

View File

@ -1,5 +1,6 @@
<?php
use Codeception\Stub;
/**
* Inherited Methods
@ -15,12 +16,60 @@
* @method void pause()
*
* @SuppressWarnings(PHPMD)
*/
*/
class UnitTester extends \Codeception\Actor
{
use _generated\UnitTesterActions;
/**
* Define custom actions here
*/
}
/**
* Define custom actions here
*/
/**
* execute any function of a class (also private/protected) and return its return
*
* @param string|object $className name of the class (FQCN) or an instance of it
* @param string $functionName name of the function which will be executed
* @param array $methodParams params the function will be executed with
* @param array $constructParams params which will be called in constructor. Will be ignored if $className
* is already an instance of an object.
* @param array $mocks mock/stub overrides of methods and properties. Will be ignored if $className is
* already an instance of an object.
*
* @return mixed
* @throws \Exception
*/
public function executeFunction(
$className,
string $functionName,
array $methodParams = [],
array $constructParams = [],
array $mocks = []
) {
$I = $this;
$I->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);
}
}

View File

@ -4,6 +4,7 @@ namespace Tests\Functional\Cart;
use FunctionalTester;
use Faker\Factory;
use Illuminate\Support\Facades\Config;
use Webkul\Tax\Models\TaxMap;
use Webkul\Tax\Models\TaxRate;
use Webkul\Tax\Models\TaxCategory;
@ -21,15 +22,27 @@ class CartCest
{
$this->faker = Factory::create();
$this->country = 'DE'; //$this->faker->countryCode;
$this->country = Config::get('app.default_country');
$this->tax1 = $I->have(TaxRate::class, ['tax_rate' => 7.00, 'country' => $this->country]);
$this->tax1 = $I->have(TaxRate::class, [
//'tax_rate' => 7.00,
'country' => $this->country
]);
$taxCategorie1 = $I->have(TaxCategory::class, []);
$I->have(TaxMap::class, ['tax_rate_id' => $this->tax1->id, 'tax_category_id' => $taxCategorie1->id]);
$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]);
$this->tax2 = $I->have(TaxRate::class, [
//'tax_rate' => 19.00,
'country' => $this->country
]);
$taxCategorie2 = $I->have(TaxCategory::class, []);
$I->have(TaxMap::class, ['tax_rate_id' => $this->tax2->id, 'tax_category_id' => $taxCategorie2->id]);
$I->have(TaxMap::class, [
'tax_rate_id' => $this->tax2->id,
'tax_category_id' => $taxCategorie2->id
]);
$config1 = [
'productInventory' => ['qty' => 100],
@ -54,7 +67,6 @@ class CartCest
public function checkCartWithMultipleTaxRates(FunctionalTester $I)
{
$I->setConfigData(['default_country' => $this->country]);
$prod1Quantity = $this->faker->numberBetween(9, 30);
if ($prod1Quantity % 2 !== 0) {
@ -73,9 +85,9 @@ class CartCest
]);
$I->amOnPage('/checkout/cart');
$I->see('Tax ' . $this->tax1->tax_rate . ' %', '#taxrate-' . $this->tax1->tax_rate);
$I->see(round($this->product1->price * $this->tax1->tax_rate / 100, 2),
'#basetaxamount-' . $this->tax1->tax_rate);
$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));
Cart::addProduct($this->product1->id, [
'_token' => session('_token'),
@ -84,9 +96,9 @@ class CartCest
]);
$I->amOnPage('/checkout/cart');
$I->see('Tax ' . $this->tax1->tax_rate . ' %', '#taxrate-' . $this->tax1->tax_rate);
$I->see(round(($prod1Quantity + 1) * $this->product1->price * $this->tax1->tax_rate / 100, 2),
'#basetaxamount-' . $this->tax1->tax_rate);
$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));
Cart::addProduct($this->product2->id, [
'_token' => session('_token'),
@ -95,20 +107,18 @@ class CartCest
]);
$I->amOnPage('/checkout/cart');
$I->see('Tax ' . $this->tax1->tax_rate . ' %', '#taxrate-' . $this->tax1->tax_rate);
$I->see('Tax ' . $this->tax1->tax_rate . ' %', '#taxrate-' . core()->taxRateAsIdentifier($this->tax1->tax_rate));
$taxAmount1 = round(($prod1Quantity + 1) * $this->product1->price * $this->tax1->tax_rate / 100, 2);
$I->see(core()->currency($taxAmount1),'#basetaxamount-' . $this->tax1->tax_rate);
$I->see(core()->currency($taxAmount1),'#basetaxamount-' . core()->taxRateAsIdentifier($this->tax1->tax_rate));
$I->see('Tax ' . $this->tax2->tax_rate . ' %', '#taxrate-' . $this->tax2->tax_rate);
$I->see('Tax ' . $this->tax2->tax_rate . ' %', '#taxrate-' . core()->taxRateAsIdentifier($this->tax2->tax_rate));
$taxAmount2 = round($prod2Quantity * $this->product2->price * $this->tax2->tax_rate / 100, 2);
$I->see(core()->currency($taxAmount2),'#basetaxamount-' . $this->tax2->tax_rate);
$I->see(core()->currency($taxAmount2),'#basetaxamount-' . core()->taxRateAsIdentifier($this->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);
}
}

View File

@ -0,0 +1,34 @@
<?php
namespace Tests\Unit\Core;
use UnitTester;
class CoreCest
{
public function testTaxRateAsIdentifier(UnitTester $I)
{
$scenarios = [
[
'input' => 0,
'expected' => '0',
],
[
'input' => 0.01,
'expected' => '0_01',
],
[
'input' => .12,
'expected' => '0_12',
],
[
'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

@ -0,0 +1,18 @@
<?php
namespace Tests\Unit\Tax\Helpers;
use UnitTester;
class TaxCest
{
public function testGetTaxRatesWithAmount(UnitTester $I)
{
$I->assertTrue(false);
}
public function testGetTaxTotal(UnitTester $I)
{
$I->assertTrue(false);
}
}