sarga/tests/unit/Tax/Helpers/TaxCest.php

114 lines
3.5 KiB
PHP
Raw Normal View History

2020-02-04 10:18:40 +00:00
<?php
namespace Tests\Unit\Tax\Helpers;
2020-02-04 12:27:47 +00:00
use Faker\Factory;
use Illuminate\Support\Facades\Config;
2020-02-04 10:18:40 +00:00
use UnitTester;
2020-02-04 12:27:47 +00:00
use Webkul\Tax\Models\TaxCategory;
use Webkul\Tax\Models\TaxMap;
use Webkul\Tax\Models\TaxRate;
use Cart;
2020-02-04 10:18:40 +00:00
class TaxCest
{
2020-02-04 12:27:47 +00:00
public $scenario;
public function _before(UnitTester $I)
{
2020-02-04 16:41:05 +00:00
$country = strtoupper(Config::get('app.default_country')) ?? 'DE';
2020-02-04 12:27:47 +00:00
$tax1 = $I->have(TaxRate::class, [
'country' => $country,
]);
2020-02-04 13:31:18 +00:00
$taxCategorie1 = $I->have(TaxCategory::class);
2020-02-04 12:27:47 +00:00
$I->have(TaxMap::class, [
'tax_rate_id' => $tax1->id,
'tax_category_id' => $taxCategorie1->id,
]);
$tax2 = $I->have(TaxRate::class, [
'country' => $country,
]);
2020-02-04 13:31:18 +00:00
$taxCategorie2 = $I->have(TaxCategory::class);
2020-02-04 12:27:47 +00:00
$I->have(TaxMap::class, [
'tax_rate_id' => $tax2->id,
'tax_category_id' => $taxCategorie2->id,
]);
$config1 = [
'productInventory' => ['qty' => 100],
'attributeValues' => [
'status' => true,
'new' => 1,
'tax_category_id' => $taxCategorie1->id,
],
];
2020-02-04 17:13:09 +00:00
$product1 = $I->haveProduct(\Webkul\Core\Helpers\Laravel5Helper::SIMPLE_PRODUCT, $config1);
2020-02-04 12:27:47 +00:00
$config2 = [
'productInventory' => ['qty' => 100],
'attributeValues' => [
'status' => true,
'new' => 1,
'tax_category_id' => $taxCategorie2->id,
],
];
2020-02-04 17:13:09 +00:00
$product2 = $I->haveProduct(\Webkul\Core\Helpers\Laravel5Helper::SIMPLE_PRODUCT, $config2);
2020-02-04 12:27:47 +00:00
Cart::addProduct($product1->id, [
'_token' => session('_token'),
'product_id' => $product1->id,
'quantity' => 11,
]);
Cart::addProduct($product2->id, [
'_token' => session('_token'),
'product_id' => $product2->id,
'quantity' => 7,
]);
$this->scenario = [
'object' => 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),
],
'expectedTaxTotal' =>
round(
round(11 * $product1->price * $tax1->tax_rate / 100, 2)
+ round(7 * $product2->price * $tax2->tax_rate / 100, 2)
, 2),
];
}
2020-02-04 10:18:40 +00:00
public function testGetTaxRatesWithAmount(UnitTester $I)
{
2020-02-04 13:31:18 +00:00
$result = $I->executeFunction(
\Webkul\Tax\Helpers\Tax::class,
'getTaxRatesWithAmount',
[$this->scenario['object'], false]
);
2020-02-04 12:27:47 +00:00
foreach ($result as $taxRate => $taxAmount) {
$I->assertTrue(array_key_exists($taxRate, $result));
$I->assertEquals($this->scenario['expectedTaxRates'][$taxRate], $taxAmount);
}
2020-02-04 10:18:40 +00:00
}
public function testGetTaxTotal(UnitTester $I)
{
2020-02-04 13:31:18 +00:00
$result = $I->executeFunction(
\Webkul\Tax\Helpers\Tax::class,
'getTaxTotal',
[$this->scenario['object'], false]
);
2020-02-04 12:27:47 +00:00
$I->assertEquals($this->scenario['expectedTaxTotal'], $result);
2020-02-04 10:18:40 +00:00
}
}