Merge branch 'tax'

This commit is contained in:
Etienne Roudeix
2013-09-09 17:44:26 +02:00
3 changed files with 183 additions and 15 deletions

View File

@@ -69,28 +69,27 @@ class Calculator
public function getTaxAmount($amount)
{
if(false === filter_var($amount, FILTER_VALIDATE_FLOAT)) {
throw new TaxEngineException('BAD AMOUNT FORMAT', TaxEngineException::BAD_AMOUNT_FORMAT);
}
if(null === $this->taxRulesGroupedCollection) {
throw new TaxEngineException('Tax rules collection is empty in Calculator::getTaxAmount', TaxEngineException::UNDEFINED_TAX_RULES_COLLECTION);
}
$taxRateAmount = 0;
foreach($this->taxRulesGroupedCollection as $taxRule) {
$taxRateAmount += $taxRule->getTaxRuleRateSum();
}
return $amount * $taxRateAmount * 0.01;
}
public function getTaxedPrice($amount)
{
if(false === filter_var($amount, FILTER_VALIDATE_FLOAT)) {
throw new TaxEngineException('BAD AMOUNT FORMAT', TaxEngineException::BAD_AMOUNT_FORMAT);
}
$totalTaxAmount = 0;
foreach($this->taxRulesGroupedCollection as $taxRule) {
$rateSum = $taxRule->getTaxRuleRateSum();
$taxAmount = $amount * $rateSum * 0.01;
$totalTaxAmount += $taxAmount;
$amount += $taxAmount;
}
return $totalTaxAmount;
}
public function getTaxedPrice($amount)
{
return $amount + $this->getTaxAmount($amount);
}
}

View File

@@ -61,7 +61,7 @@ class RewritingResolverTest extends \PHPUnit_Framework_TestCase
$resolver = new RewritingResolver();
$method = $this->getMethod('getOtherParameters');
$actual = $method->invoke($resolver);
$method->invoke($resolver);
}
public function testGetOtherParameters()

View File

@@ -0,0 +1,169 @@
<?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\Tests\TaxEngine;
use Propel\Runtime\Collection\ObjectCollection;
use Thelia\Model\Country;
use Thelia\Model\CountryQuery;
use Thelia\Model\Product;
use Thelia\Model\ProductQuery;
use Thelia\Model\Tax;
use Thelia\TaxEngine\Calculator;
/**
*
* @author Etienne Roudeix <eroudeix@openstudio.fr>
*
*/
class CalculatorTest extends \PHPUnit_Framework_TestCase
{
protected function getMethod($name)
{
$class = new \ReflectionClass('\Thelia\TaxEngine\Calculator');
$method = $class->getMethod($name);
$method->setAccessible(true);
return $method;
}
protected function getProperty($name)
{
$class = new \ReflectionClass('\Thelia\TaxEngine\Calculator');
$property = $class->getProperty($name);
$property->setAccessible(true);
return $property;
}
/**
* @expectedException \Thelia\Exception\TaxEngineException
* @expectedExceptionCode 501
*/
public function testLoadEmptyProductException()
{
$calculator = new Calculator();
$calculator->load(new Product(), CountryQuery::create()->findOne());
}
/**
* @expectedException \Thelia\Exception\TaxEngineException
* @expectedExceptionCode 502
*/
public function testLoadEmptyCountryException()
{
$calculator = new Calculator();
$calculator->load(ProductQuery::create()->findOne(), new Country());
}
public function testLoad()
{
$productQuery = ProductQuery::create()->findOneById(1);
$countryQuery = CountryQuery::create()->findOneById(64);
$calculator = new Calculator();
$taxRuleQuery = $this->getMock('\Thelia\Model\TaxRuleQuery', array('getTaxCalculatorGroupedCollection'));
$taxRuleQuery->expects($this->once())
->method('getTaxCalculatorGroupedCollection')
->with($productQuery, $countryQuery)
->will($this->returnValue('foo'));
$rewritingUrlQuery = $this->getProperty('taxRuleQuery');
$rewritingUrlQuery->setValue($calculator, $taxRuleQuery);
$calculator->load($productQuery, $countryQuery);
$this->assertEquals(
$productQuery,
$this->getProperty('product')->getValue($calculator)
);
$this->assertEquals(
$countryQuery,
$this->getProperty('country')->getValue($calculator)
);
$this->assertEquals(
'foo',
$this->getProperty('taxRulesGroupedCollection')->getValue($calculator)
);
}
/**
* @expectedException \Thelia\Exception\TaxEngineException
* @expectedExceptionCode 503
*/
public function testGetTaxAmountBadTaxRulesCollection()
{
$calculator = new Calculator();
$calculator->getTaxAmount(500);
}
/**
* @expectedException \Thelia\Exception\TaxEngineException
* @expectedExceptionCode 601
*/
public function testGetTaxAmountBadAmount()
{
$taxRulesGroupedCollection = new ObjectCollection();
$calculator = new Calculator();
$rewritingUrlQuery = $this->getProperty('taxRulesGroupedCollection');
$rewritingUrlQuery->setValue($calculator, $taxRulesGroupedCollection);
$calculator->getTaxAmount('foo');
}
public function testGetTaxAmountAndGetTaxedPrice()
{
$taxRulesGroupedCollection = new ObjectCollection();
$taxRulesGroupedCollection->setModel('\Thelia\Model\Tax');
$tax = new Tax();
$tax->setVirtualColumn('taxRateSum', 10);
$taxRulesGroupedCollection->append($tax);
$tax = new Tax();
$tax->setVirtualColumn('taxRateSum', 8);
$taxRulesGroupedCollection->append($tax);
$calculator = new Calculator();
$rewritingUrlQuery = $this->getProperty('taxRulesGroupedCollection');
$rewritingUrlQuery->setValue($calculator, $taxRulesGroupedCollection);
$taxAmount = $calculator->getTaxAmount(500);
$taxedPrice = $calculator->getTaxedPrice(500);
/*
* expect :
* tax 1 = 500*0.10 = 50 // amout with tax 1 : 550
* tax 2 = 550*0.08 = 44 // amout with tax 2 : 594
* total tax amount = 94
*/
$this->assertEquals(94, $taxAmount);
$this->assertEquals(594, $taxedPrice);
}
}