diff --git a/core/lib/Thelia/TaxEngine/Calculator.php b/core/lib/Thelia/TaxEngine/Calculator.php index ec6e3c78d..66c4fcbbf 100755 --- a/core/lib/Thelia/TaxEngine/Calculator.php +++ b/core/lib/Thelia/TaxEngine/Calculator.php @@ -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); } } diff --git a/core/lib/Thelia/Tests/Rewriting/RewritingResolverTest.php b/core/lib/Thelia/Tests/Rewriting/RewritingResolverTest.php index a3ec561d2..6cd4bdf42 100755 --- a/core/lib/Thelia/Tests/Rewriting/RewritingResolverTest.php +++ b/core/lib/Thelia/Tests/Rewriting/RewritingResolverTest.php @@ -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() diff --git a/core/lib/Thelia/Tests/TaxEngine/CalculatorTest.php b/core/lib/Thelia/Tests/TaxEngine/CalculatorTest.php new file mode 100755 index 000000000..fac6ca643 --- /dev/null +++ b/core/lib/Thelia/Tests/TaxEngine/CalculatorTest.php @@ -0,0 +1,169 @@ +. */ +/* */ +/*************************************************************************************/ + +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 + * + */ +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); + } +}