fix tests
This commit is contained in:
@@ -197,6 +197,7 @@ class Order extends BaseAction implements EventSubscriberInterface
|
||||
$taxRuleI18n = I18n::forceI18nRetrieving($this->getSession()->getLang()->getLocale(), 'TaxRule', $product->getTaxRuleId());
|
||||
|
||||
$taxDetail = $product->getTaxRule()->getTaxDetail(
|
||||
$product,
|
||||
$taxCountry,
|
||||
$cartItem->getPrice(),
|
||||
$cartItem->getPromoPrice(),
|
||||
|
||||
@@ -40,6 +40,7 @@ class TaxEngineException extends \RuntimeException
|
||||
const UNDEFINED_REQUIREMENTS = 504;
|
||||
const UNDEFINED_REQUIREMENT_VALUE = 505;
|
||||
const UNDEFINED_TAX_RULE = 506;
|
||||
const NO_TAX_IN_TAX_RULES_COLLECTION = 507;
|
||||
|
||||
const BAD_AMOUNT_FORMAT = 601;
|
||||
|
||||
|
||||
@@ -16,14 +16,14 @@ class TaxRule extends BaseTaxRule
|
||||
*
|
||||
* @return OrderProductTaxCollection
|
||||
*/
|
||||
public function getTaxDetail(Country $country, $untaxedAmount, $untaxedPromoAmount, $askedLocale = null)
|
||||
public function getTaxDetail(Product $product, Country $country, $untaxedAmount, $untaxedPromoAmount, $askedLocale = null)
|
||||
{
|
||||
$taxCalculator = new Calculator();
|
||||
|
||||
$taxCollection = new OrderProductTaxCollection();
|
||||
$taxCalculator->loadTaxRule($this, $country)->getTaxedPrice($untaxedAmount, $taxCollection, $askedLocale);
|
||||
$taxCalculator->loadTaxRule($this, $country, $product)->getTaxedPrice($untaxedAmount, $taxCollection, $askedLocale);
|
||||
$promoTaxCollection = new OrderProductTaxCollection();
|
||||
$taxCalculator->loadTaxRule($this, $country)->getTaxedPrice($untaxedPromoAmount, $promoTaxCollection, $askedLocale);
|
||||
$taxCalculator->loadTaxRule($this, $country, $product)->getTaxedPrice($untaxedPromoAmount, $promoTaxCollection, $askedLocale);
|
||||
|
||||
foreach($taxCollection as $index => $tax) {
|
||||
$tax->setPromoAmount($promoTaxCollection->getKey($index)->getAmount());
|
||||
|
||||
@@ -76,7 +76,7 @@ class Calculator
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function loadTaxRule(TaxRule $taxRule, Country $country)
|
||||
public function loadTaxRule(TaxRule $taxRule, Country $country, Product $product)
|
||||
{
|
||||
$this->product = null;
|
||||
$this->country = null;
|
||||
@@ -88,8 +88,12 @@ class Calculator
|
||||
if($country->getId() === null) {
|
||||
throw new TaxEngineException('Country id is empty in Calculator::loadTaxRule', TaxEngineException::UNDEFINED_COUNTRY);
|
||||
}
|
||||
if($product->getId() === null) {
|
||||
throw new TaxEngineException('Product id is empty in Calculator::load', TaxEngineException::UNDEFINED_PRODUCT);
|
||||
}
|
||||
|
||||
$this->country = $country;
|
||||
$this->product = $product;
|
||||
|
||||
$this->taxRulesCollection = $this->taxRuleQuery->getTaxCalculatorCollection($taxRule, $country);
|
||||
|
||||
@@ -117,7 +121,11 @@ class Calculator
|
||||
public function getTaxedPrice($untaxedPrice, &$taxCollection = null, $askedLocale = null)
|
||||
{
|
||||
if(null === $this->taxRulesCollection) {
|
||||
throw new TaxEngineException('Tax rules collection is empty in Calculator::getTaxAmount', TaxEngineException::UNDEFINED_TAX_RULES_COLLECTION);
|
||||
throw new TaxEngineException('Tax rules collection is empty in Calculator::getTaxedPrice', TaxEngineException::UNDEFINED_TAX_RULES_COLLECTION);
|
||||
}
|
||||
|
||||
if(null === $this->product) {
|
||||
throw new TaxEngineException('Product is empty in Calculator::getTaxedPrice', TaxEngineException::UNDEFINED_PRODUCT);
|
||||
}
|
||||
|
||||
if(false === filter_var($untaxedPrice, FILTER_VALIDATE_FLOAT)) {
|
||||
@@ -143,7 +151,7 @@ class Calculator
|
||||
$currentPosition = $position;
|
||||
}
|
||||
|
||||
$taxAmount = round($taxType->calculate($taxedPrice), 2);
|
||||
$taxAmount = round($taxType->calculate($this->product, $taxedPrice), 2);
|
||||
$currentTax += $taxAmount;
|
||||
|
||||
if(null !== $taxCollection) {
|
||||
@@ -167,12 +175,20 @@ class Calculator
|
||||
throw new TaxEngineException('Tax rules collection is empty in Calculator::getTaxAmount', TaxEngineException::UNDEFINED_TAX_RULES_COLLECTION);
|
||||
}
|
||||
|
||||
if(null === $this->product) {
|
||||
throw new TaxEngineException('Product is empty in Calculator::getTaxedPrice', TaxEngineException::UNDEFINED_PRODUCT);
|
||||
}
|
||||
|
||||
if(false === filter_var($taxedPrice, FILTER_VALIDATE_FLOAT)) {
|
||||
throw new TaxEngineException('BAD AMOUNT FORMAT', TaxEngineException::BAD_AMOUNT_FORMAT);
|
||||
}
|
||||
|
||||
$taxRule = $this->taxRulesCollection->getLast();
|
||||
|
||||
if(null === $taxRule) {
|
||||
throw new TaxEngineException('Tax rules collection got no tax ', TaxEngineException::NO_TAX_IN_TAX_RULES_COLLECTION);
|
||||
}
|
||||
|
||||
$untaxedPrice = $taxedPrice;
|
||||
$currentPosition = (int)$taxRule->getTaxRuleCountryPosition();
|
||||
$currentFixTax = 0;
|
||||
@@ -192,7 +208,7 @@ class Calculator
|
||||
$currentPosition = $position;
|
||||
}
|
||||
|
||||
$currentFixTax += $taxType->fixAmountRetriever();
|
||||
$currentFixTax += $taxType->fixAmountRetriever($this->product);
|
||||
$currentTaxFactor += $taxType->pricePercentRetriever();
|
||||
|
||||
|
||||
|
||||
@@ -35,14 +35,17 @@ abstract class BaseTaxType
|
||||
{
|
||||
protected $requirements = null;
|
||||
|
||||
public abstract function calculate($untaxedPrice);
|
||||
|
||||
public abstract function pricePercentRetriever();
|
||||
|
||||
public abstract function fixAmountRetriever(Product $product);
|
||||
|
||||
public abstract function getRequirementsList();
|
||||
|
||||
public function calculate(Product $product, $untaxedPrice)
|
||||
{
|
||||
return $untaxedPrice * $this->pricePercentRetriever() + $this->fixAmountRetriever($product);
|
||||
}
|
||||
|
||||
public function loadRequirements($requirementsValues)
|
||||
{
|
||||
$this->requirements = $this->getRequirementsList();
|
||||
|
||||
@@ -26,6 +26,7 @@ use Thelia\Exception\TaxEngineException;
|
||||
use Thelia\Model\FeatureProductQuery;
|
||||
use Thelia\Model\Product;
|
||||
use Thelia\Type\FloatType;
|
||||
use Thelia\Type\ModelValidIdType;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -34,11 +35,6 @@ use Thelia\Type\FloatType;
|
||||
*/
|
||||
class FeatureFixAmountTaxType extends BaseTaxType
|
||||
{
|
||||
public function calculate($untaxedPrice)
|
||||
{
|
||||
return $this->getRequirement("amount");
|
||||
}
|
||||
|
||||
public function pricePercentRetriever()
|
||||
{
|
||||
return 0;
|
||||
@@ -46,7 +42,7 @@ class FeatureFixAmountTaxType extends BaseTaxType
|
||||
|
||||
public function fixAmountRetriever(Product $product)
|
||||
{
|
||||
$featureId = $this->getRequirement("featureId");
|
||||
$featureId = $this->getRequirement("feature");
|
||||
|
||||
$query = FeatureProductQuery::create()
|
||||
->filterByProduct($product)
|
||||
@@ -66,7 +62,7 @@ class FeatureFixAmountTaxType extends BaseTaxType
|
||||
public function getRequirementsList()
|
||||
{
|
||||
return array(
|
||||
'featureId' => new ModelType('Feature'),
|
||||
'feature' => new ModelValidIdType('Feature'),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
namespace Thelia\TaxEngine\TaxType;
|
||||
|
||||
use Thelia\Type\FloatToFloatArrayType;
|
||||
use Thelia\Type\ModelType;
|
||||
use Thelia\Type\ModelValidIdType;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -32,11 +32,6 @@ use Thelia\Type\ModelType;
|
||||
*/
|
||||
class featureSlicePercentTaxType extends BaseTaxType
|
||||
{
|
||||
public function calculate($untaxedPrice)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function pricePercentRetriever()
|
||||
{
|
||||
|
||||
@@ -50,7 +45,7 @@ class featureSlicePercentTaxType extends BaseTaxType
|
||||
public function getRequirementsList()
|
||||
{
|
||||
return array(
|
||||
'featureId' => new ModelType('Currency'),
|
||||
'featureId' => new ModelValidIdType('Currency'),
|
||||
'slices' => new FloatToFloatArrayType(),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -31,11 +31,6 @@ use Thelia\Type\FloatType;
|
||||
*/
|
||||
class FixAmountTaxType extends BaseTaxType
|
||||
{
|
||||
public function calculate($untaxedPrice)
|
||||
{
|
||||
return $this->getRequirement("amount");
|
||||
}
|
||||
|
||||
public function pricePercentRetriever()
|
||||
{
|
||||
return 0;
|
||||
|
||||
@@ -31,11 +31,6 @@ use Thelia\Type\FloatType;
|
||||
*/
|
||||
class PricePercentTaxType extends BaseTaxType
|
||||
{
|
||||
public function calculate($untaxedPrice)
|
||||
{
|
||||
return $untaxedPrice * $this->getRequirement("percent") * 0.01;
|
||||
}
|
||||
|
||||
public function pricePercentRetriever()
|
||||
{
|
||||
return ($this->getRequirement("percent") * 0.01);
|
||||
|
||||
@@ -347,7 +347,7 @@ class OrderTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
/* check tax */
|
||||
$orderProductTaxList = $orderProduct->getOrderProductTaxes();
|
||||
foreach ($cartItem->getProduct()->getTaxRule()->getTaxDetail($validDeliveryAddress->getCountry(), $cartItem->getPrice(), $cartItem->getPromoPrice()) as $index => $tax) {
|
||||
foreach ($cartItem->getProduct()->getTaxRule()->getTaxDetail($cartItem->getProduct(), $validDeliveryAddress->getCountry(), $cartItem->getPrice(), $cartItem->getPromoPrice()) as $index => $tax) {
|
||||
$orderProductTax = $orderProductTaxList[$index];
|
||||
$this->assertEquals($tax->getAmount(), $orderProductTax->getAmount());
|
||||
$this->assertEquals($tax->getPromoAmount(), $orderProductTax->getPromoAmount());
|
||||
|
||||
@@ -126,14 +126,64 @@ class CalculatorTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
$taxRulesCollection = new ObjectCollection();
|
||||
|
||||
$aProduct = ProductQuery::create()->findOne();
|
||||
if(null === $aProduct) {
|
||||
return;
|
||||
}
|
||||
|
||||
$calculator = new Calculator();
|
||||
|
||||
$rewritingUrlQuery = $this->getProperty('taxRulesCollection');
|
||||
$rewritingUrlQuery->setValue($calculator, $taxRulesCollection);
|
||||
|
||||
$product = $this->getProperty('product');
|
||||
$product->setValue($calculator, $aProduct);
|
||||
|
||||
$calculator->getTaxedPrice('foo');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Thelia\Exception\TaxEngineException
|
||||
* @expectedExceptionCode 501
|
||||
*/
|
||||
public function testGetUntaxedPriceAndGetTaxAmountFromTaxedPriceWithNoProductLoaded()
|
||||
{
|
||||
$taxRulesCollection = new ObjectCollection();
|
||||
$taxRulesCollection->setModel('\Thelia\Model\Tax');
|
||||
|
||||
$calculator = new Calculator();
|
||||
|
||||
$rewritingUrlQuery = $this->getProperty('taxRulesCollection');
|
||||
$rewritingUrlQuery->setValue($calculator, $taxRulesCollection);
|
||||
|
||||
$calculator->getTaxAmountFromTaxedPrice(600.95);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Thelia\Exception\TaxEngineException
|
||||
* @expectedExceptionCode 507
|
||||
*/
|
||||
public function testGetUntaxedPriceAndGetTaxAmountFromTaxedPriceWithEmptyTaxRuleCollection()
|
||||
{
|
||||
$taxRulesCollection = new ObjectCollection();
|
||||
$taxRulesCollection->setModel('\Thelia\Model\Tax');
|
||||
|
||||
$aProduct = ProductQuery::create()->findOne();
|
||||
if(null === $aProduct) {
|
||||
return;
|
||||
}
|
||||
|
||||
$calculator = new Calculator();
|
||||
|
||||
$rewritingUrlQuery = $this->getProperty('taxRulesCollection');
|
||||
$rewritingUrlQuery->setValue($calculator, $taxRulesCollection);
|
||||
|
||||
$product = $this->getProperty('product');
|
||||
$product->setValue($calculator, $aProduct);
|
||||
|
||||
$calculator->getTaxAmountFromTaxedPrice(600.95);
|
||||
}
|
||||
|
||||
public function testGetTaxedPriceAndGetTaxAmountFromUntaxedPrice()
|
||||
{
|
||||
$taxRulesCollection = new ObjectCollection();
|
||||
@@ -163,11 +213,19 @@ class CalculatorTest extends \PHPUnit_Framework_TestCase
|
||||
->setVirtualColumn('taxRuleCountryPosition', 3);
|
||||
$taxRulesCollection->append($tax);
|
||||
|
||||
$aProduct = ProductQuery::create()->findOne();
|
||||
if(null === $aProduct) {
|
||||
return;
|
||||
}
|
||||
|
||||
$calculator = new Calculator();
|
||||
|
||||
$rewritingUrlQuery = $this->getProperty('taxRulesCollection');
|
||||
$rewritingUrlQuery->setValue($calculator, $taxRulesCollection);
|
||||
|
||||
$product = $this->getProperty('product');
|
||||
$product->setValue($calculator, $aProduct);
|
||||
|
||||
$taxAmount = $calculator->getTaxAmountFromUntaxedPrice(500);
|
||||
$taxedPrice = $calculator->getTaxedPrice(500);
|
||||
|
||||
@@ -211,11 +269,19 @@ class CalculatorTest extends \PHPUnit_Framework_TestCase
|
||||
->setVirtualColumn('taxRuleCountryPosition', 3);
|
||||
$taxRulesCollection->append($tax);
|
||||
|
||||
$aProduct = ProductQuery::create()->findOne();
|
||||
if(null === $aProduct) {
|
||||
return;
|
||||
}
|
||||
|
||||
$calculator = new Calculator();
|
||||
|
||||
$rewritingUrlQuery = $this->getProperty('taxRulesCollection');
|
||||
$rewritingUrlQuery->setValue($calculator, $taxRulesCollection);
|
||||
|
||||
$product = $this->getProperty('product');
|
||||
$product->setValue($calculator, $aProduct);
|
||||
|
||||
$taxAmount = $calculator->getTaxAmountFromTaxedPrice(600.95);
|
||||
$untaxedPrice = $calculator->getUntaxedPrice(600.95);
|
||||
|
||||
|
||||
70
core/lib/Thelia/Type/ModelValidIdType.php
Executable file
70
core/lib/Thelia/Type/ModelValidIdType.php
Executable file
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
/*************************************************************************************/
|
||||
/* */
|
||||
/* Thelia */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : info@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* This program is free software; you can redistribute it and/or modify */
|
||||
/* it under the terms of the GNU General Public License as published by */
|
||||
/* the Free Software Foundation; either version 3 of the License */
|
||||
/* */
|
||||
/* This program is distributed in the hope that it will be useful, */
|
||||
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||
/* GNU General Public License for more details. */
|
||||
/* */
|
||||
/* You should have received a copy of the GNU General Public License */
|
||||
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
/* */
|
||||
/*************************************************************************************/
|
||||
namespace Thelia\Type;
|
||||
|
||||
use Propel\Runtime\ActiveQuery\ModelCriteria;
|
||||
use Thelia\Exception\TypeException;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Etienne Roudeix <eroudeix@openstudio.fr>
|
||||
*
|
||||
*/
|
||||
class ModelValidIdType implements TypeInterface
|
||||
{
|
||||
protected $expectedModelActiveRecordQuery = null;
|
||||
|
||||
/**
|
||||
* @param $expectedModelActiveRecord
|
||||
* @throws TypeException
|
||||
*/
|
||||
public function __construct($expectedModelActiveRecord)
|
||||
{
|
||||
$class = '\\Thelia\\Model\\' . $expectedModelActiveRecord . 'Query';
|
||||
|
||||
if (!(class_exists($class) || !new $class instanceof ModelCriteria)) {
|
||||
throw new TypeException('MODEL NOT FOUND', TypeException::MODEL_NOT_FOUND);
|
||||
}
|
||||
|
||||
$this->expectedModelActiveRecordQuery = $class;
|
||||
}
|
||||
|
||||
public function getType()
|
||||
{
|
||||
return 'Model valid Id type';
|
||||
}
|
||||
|
||||
public function isValid($value)
|
||||
{
|
||||
$queryClass = $this->expectedModelActiveRecordQuery;
|
||||
|
||||
return null !== $queryClass::create()->findPk($value);
|
||||
}
|
||||
|
||||
public function getFormattedValue($value)
|
||||
{
|
||||
$queryClass = $this->expectedModelActiveRecordQuery;
|
||||
|
||||
return $this->isValid($value) ? $queryClass::create()->findPk($value) : null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user