From 3a4bff634e9ee8ef17c19ce8ca372d4f2dd88396 Mon Sep 17 00:00:00 2001 From: gmorel Date: Sat, 16 Nov 2013 21:30:45 +0100 Subject: [PATCH] Adding/Refactoring unit test for ConditionEvaluator --- .../Condition/ConditionEvaluatorTest.php | 397 ++++-------------- 1 file changed, 75 insertions(+), 322 deletions(-) diff --git a/core/lib/Thelia/Tests/Condition/ConditionEvaluatorTest.php b/core/lib/Thelia/Tests/Condition/ConditionEvaluatorTest.php index dac865a7d..46f597cf2 100644 --- a/core/lib/Thelia/Tests/Condition/ConditionEvaluatorTest.php +++ b/core/lib/Thelia/Tests/Condition/ConditionEvaluatorTest.php @@ -51,328 +51,11 @@ class ConditionEvaluatorTest extends \PHPUnit_Framework_TestCase { } - public function testTestSuccess1Rules() - { - $stubTranslator = $this->getMockBuilder('\Thelia\Core\Translation\Translator') - ->disableOriginalConstructor() - ->getMock(); - - /** @var FacadeInterface $stubFacade */ - $stubFacade = $this->getMockBuilder('\Thelia\Coupon\BaseFacade') - ->disableOriginalConstructor() - ->getMock(); - - $stubFacade->expects($this->any()) - ->method('getTranslator') - ->will($this->returnValue($stubTranslator)); - $stubFacade->expects($this->any()) - ->method('getConditionEvaluator') - ->will($this->returnValue(new ConditionEvaluator())); - - $currencies = CurrencyQuery::create(); - $currencies = $currencies->find(); - $stubFacade->expects($this->any()) - ->method('getAvailableCurrencies') - ->will($this->returnValue($currencies)); - - $stubContainer = $this->getMockBuilder('\Symfony\Component\DependencyInjection\Container') - ->disableOriginalConstructor() - ->getMock(); - - $stubMatchForTotalAmountManager = $this->getMockBuilder('\Thelia\Condition\Implementation\MatchForTotalAmountManager') - ->disableOriginalConstructor() - ->getMock(); - $stubMatchForTotalAmountManager->expects($this->any()) - ->method('isMatching') - ->will($this->returnValue(true)); - - $stubContainer->expects($this->any()) - ->method('get') - ->will($this->returnValue($stubMatchForTotalAmountManager)); - - $stubContainer->expects($this->any()) - ->method('has') - ->will($this->returnValue(true)); - - $stubFacade->expects($this->any()) - ->method('getContainer') - ->will($this->returnValue($stubContainer)); - $stubFacade->expects($this->any()) - ->method('getCheckoutCurrency') - ->will($this->returnValue('EUR')); - $stubFacade->expects($this->any()) - ->method('getCartTotalPrice') - ->will($this->returnValue(401.00)); - - $condition1 = new MatchForTotalAmountManager($stubFacade); - $operators = array( - MatchForTotalAmountManager::INPUT1 => '>', - MatchForTotalAmountManager::INPUT2 => '==' - ); - $values = array( - MatchForTotalAmountManager::INPUT1 => 400.00, - MatchForTotalAmountManager::INPUT2 => 'EUR'); - $condition1->setValidatorsFromForm($operators, $values); - - $conditions = new ConditionCollection(); - $conditions->add($condition1); - - $conditionEvaluator = new ConditionEvaluator(); - $isValid = $conditionEvaluator->isMatching($conditions); - - $expected = true; - $actual =$isValid; - $this->assertEquals($expected, $actual); - } - - public function testTestFail1Rules() - { - $stubTranslator = $this->getMockBuilder('\Thelia\Core\Translation\Translator') - ->disableOriginalConstructor() - ->getMock(); - - /** @var FacadeInterface $stubFacade */ - $stubFacade = $this->getMockBuilder('\Thelia\Coupon\BaseFacade') - ->disableOriginalConstructor() - ->getMock(); - - $stubFacade->expects($this->any()) - ->method('getTranslator') - ->will($this->returnValue($stubTranslator)); - $stubFacade->expects($this->any()) - ->method('getConditionEvaluator') - ->will($this->returnValue(new ConditionEvaluator())); - - $currencies = CurrencyQuery::create(); - $currencies = $currencies->find(); - $stubFacade->expects($this->any()) - ->method('getAvailableCurrencies') - ->will($this->returnValue($currencies)); - - $stubContainer = $this->getMockBuilder('\Symfony\Component\DependencyInjection\Container') - ->disableOriginalConstructor() - ->getMock(); - - $stubMatchForTotalAmountManager = $this->getMockBuilder('\Thelia\Condition\Implementation\MatchForTotalAmountManager') - ->disableOriginalConstructor() - ->getMock(); - $stubMatchForTotalAmountManager->expects($this->any()) - ->method('isMatching') - ->will($this->returnValue(true)); - - $stubContainer->expects($this->any()) - ->method('get') - ->will($this->returnValue($stubMatchForTotalAmountManager)); - - $stubContainer->expects($this->any()) - ->method('has') - ->will($this->returnValue(true)); - - $stubFacade->expects($this->any()) - ->method('getContainer') - ->will($this->returnValue($stubContainer)); - $stubFacade->expects($this->any()) - ->method('getCheckoutCurrency') - ->will($this->returnValue('EUR')); - $stubFacade->expects($this->any()) - ->method('getCartTotalPrice') - ->will($this->returnValue(400.00)); - - $condition1 = new MatchForTotalAmountManager($stubFacade); - $operators = array( - MatchForTotalAmountManager::INPUT1 => '>', - MatchForTotalAmountManager::INPUT2 => '==' - ); - $values = array( - MatchForTotalAmountManager::INPUT1 => 400.00, - MatchForTotalAmountManager::INPUT2 => 'EUR'); - $condition1->setValidatorsFromForm($operators, $values); - - $conditions = new ConditionCollection(); - $conditions->add($condition1); - - $conditionEvaluator = new ConditionEvaluator(); - $isValid = $conditionEvaluator->isMatching($conditions); - - $expected = false; - $actual =$isValid; - $this->assertEquals($expected, $actual, 'Conditions evaluator always think Customer is matching conditions'); - } - - public function testTestSuccess2Rules() - { - $stubTranslator = $this->getMockBuilder('\Thelia\Core\Translation\Translator') - ->disableOriginalConstructor() - ->getMock(); - - /** @var FacadeInterface $stubFacade */ - $stubFacade = $this->getMockBuilder('\Thelia\Coupon\BaseFacade') - ->disableOriginalConstructor() - ->getMock(); - - $stubFacade->expects($this->any()) - ->method('getTranslator') - ->will($this->returnValue($stubTranslator)); - $stubFacade->expects($this->any()) - ->method('getConditionEvaluator') - ->will($this->returnValue(new ConditionEvaluator())); - - $currencies = CurrencyQuery::create(); - $currencies = $currencies->find(); - $stubFacade->expects($this->any()) - ->method('getAvailableCurrencies') - ->will($this->returnValue($currencies)); - - $stubContainer = $this->getMockBuilder('\Symfony\Component\DependencyInjection\Container') - ->disableOriginalConstructor() - ->getMock(); - - $stubMatchForTotalAmountManager = $this->getMockBuilder('\Thelia\Condition\Implementation\MatchForTotalAmountManager') - ->disableOriginalConstructor() - ->getMock(); - $stubMatchForTotalAmountManager->expects($this->any()) - ->method('isMatching') - ->will($this->returnValue(true)); - - $stubContainer->expects($this->any()) - ->method('get') - ->will($this->returnValue($stubMatchForTotalAmountManager)); - - $stubContainer->expects($this->any()) - ->method('has') - ->will($this->returnValue(true)); - - $stubFacade->expects($this->any()) - ->method('getContainer') - ->will($this->returnValue($stubContainer)); - $stubFacade->expects($this->any()) - ->method('getCheckoutCurrency') - ->will($this->returnValue('EUR')); - $stubFacade->expects($this->any()) - ->method('getCartTotalPrice') - ->will($this->returnValue(401.00)); - $stubFacade->expects($this->any()) - ->method('getNbArticlesInCart') - ->will($this->returnValue(5)); - - $condition1 = new MatchForTotalAmountManager($stubFacade); - $operators = array( - MatchForTotalAmountManager::INPUT1 => '>', - MatchForTotalAmountManager::INPUT2 => '==' - ); - $values = array( - MatchForTotalAmountManager::INPUT1 => 400.00, - MatchForTotalAmountManager::INPUT2 => 'EUR'); - $condition1->setValidatorsFromForm($operators, $values); - - $condition2 = new MatchForXArticlesManager($stubFacade); - $operators = array( - MatchForXArticlesManager::INPUT1 => '>' - ); - $values = array( - MatchForXArticlesManager::INPUT1 => 4 - ); - $condition2->setValidatorsFromForm($operators, $values); - - $conditions = new ConditionCollection(); - $conditions->add($condition1); - $conditions->add($condition2); - - $conditionEvaluator = new ConditionEvaluator(); - $isValid = $conditionEvaluator->isMatching($conditions); - - $expected = true; - $actual =$isValid; - $this->assertEquals($expected, $actual); - } - - public function testTestFail2Rules() - { - $stubTranslator = $this->getMockBuilder('\Thelia\Core\Translation\Translator') - ->disableOriginalConstructor() - ->getMock(); - - /** @var FacadeInterface $stubFacade */ - $stubFacade = $this->getMockBuilder('\Thelia\Coupon\BaseFacade') - ->disableOriginalConstructor() - ->getMock(); - - $stubFacade->expects($this->any()) - ->method('getTranslator') - ->will($this->returnValue($stubTranslator)); - $stubFacade->expects($this->any()) - ->method('getConditionEvaluator') - ->will($this->returnValue(new ConditionEvaluator())); - - $currencies = CurrencyQuery::create(); - $currencies = $currencies->find(); - $stubFacade->expects($this->any()) - ->method('getAvailableCurrencies') - ->will($this->returnValue($currencies)); - - $stubContainer = $this->getMockBuilder('\Symfony\Component\DependencyInjection\Container') - ->disableOriginalConstructor() - ->getMock(); - - $stubMatchForTotalAmountManager = $this->getMockBuilder('\Thelia\Condition\Implementation\MatchForTotalAmountManager') - ->disableOriginalConstructor() - ->getMock(); - $stubMatchForTotalAmountManager->expects($this->any()) - ->method('isMatching') - ->will($this->returnValue(true)); - - $stubContainer->expects($this->any()) - ->method('get') - ->will($this->returnValue($stubMatchForTotalAmountManager)); - - $stubContainer->expects($this->any()) - ->method('has') - ->will($this->returnValue(true)); - - $stubFacade->expects($this->any()) - ->method('getContainer') - ->will($this->returnValue($stubContainer)); - $stubFacade->expects($this->any()) - ->method('getCheckoutCurrency') - ->will($this->returnValue('EUR')); - $stubFacade->expects($this->any()) - ->method('getCartTotalPrice') - ->will($this->returnValue(400.00)); - $stubFacade->expects($this->any()) - ->method('getNbArticlesInCart') - ->will($this->returnValue(5)); - - $condition1 = new MatchForTotalAmountManager($stubFacade); - $operators = array( - MatchForTotalAmountManager::INPUT1 => '>', - MatchForTotalAmountManager::INPUT2 => '==' - ); - $values = array( - MatchForTotalAmountManager::INPUT1 => 400.00, - MatchForTotalAmountManager::INPUT2 => 'EUR'); - $condition1->setValidatorsFromForm($operators, $values); - - $condition2 = new MatchForXArticlesManager($stubFacade); - $operators = array( - MatchForXArticlesManager::INPUT1 => '>' - ); - $values = array( - MatchForXArticlesManager::INPUT1 => 4 - ); - $condition2->setValidatorsFromForm($operators, $values); - - $conditions = new ConditionCollection(); - $conditions->add($condition1); - $conditions->add($condition2); - - $conditionEvaluator = new ConditionEvaluator(); - $isValid = $conditionEvaluator->isMatching($conditions); - - $expected = false; - $actual =$isValid; - $this->assertEquals($expected, $actual, 'Conditions evaluator always think Customer is matching conditions'); - } - + /** + * Test vatiable comparison + * + * @covers Thelia\Condition\ConditionEvaluator::variableOpComparison + */ public function testVariableOpComparisonSuccess() { $conditionEvaluator = new ConditionEvaluator(); @@ -409,6 +92,11 @@ class ConditionEvaluatorTest extends \PHPUnit_Framework_TestCase } + /** + * Test vatiable comparison + * + * @covers Thelia\Condition\ConditionEvaluator::variableOpComparison + */ public function testVariableOpComparisonFail() { $conditionEvaluator = new ConditionEvaluator(); @@ -440,7 +128,10 @@ class ConditionEvaluatorTest extends \PHPUnit_Framework_TestCase } /** + * Test vatiable comparison + * * @expectedException \Exception + * @covers Thelia\Condition\ConditionEvaluator::variableOpComparison */ public function testVariableOpComparisonException() { @@ -450,6 +141,68 @@ class ConditionEvaluatorTest extends \PHPUnit_Framework_TestCase $this->assertEquals($expected, $actual); } + /** + * Test condition collection matching + * + * @covers Thelia\Condition\ConditionEvaluator::isMatching + */ + public function testIsMatchingTrue() + { + $stubConditionTrue1 = $this->getMockBuilder('\Thelia\Condition\Implementation\MatchForXArticlesManager') + ->disableOriginalConstructor() + ->getMock(); + $stubConditionTrue1->expects($this->any()) + ->method('isMatching') + ->will($this->returnValue(true)); + + $stubConditionTrue2 = $this->getMockBuilder('\Thelia\Condition\Implementation\MatchForXArticlesManager') + ->disableOriginalConstructor() + ->getMock(); + $stubConditionTrue2->expects($this->any()) + ->method('isMatching') + ->will($this->returnValue(true)); + + $collection = new ConditionCollection(); + $collection->add($stubConditionTrue1); + $collection->add($stubConditionTrue2); + + $conitionEvaluator = new ConditionEvaluator(); + $actual = $conitionEvaluator->isMatching($collection); + + $this->assertTrue($actual); + } + + /** + * Test condition collection matching + * + * @covers Thelia\Condition\ConditionEvaluator::isMatching + */ + public function testIsMatchingFalse() + { + $stubConditionTrue = $this->getMockBuilder('\Thelia\Condition\Implementation\MatchForXArticlesManager') + ->disableOriginalConstructor() + ->getMock(); + $stubConditionTrue->expects($this->any()) + ->method('isMatching') + ->will($this->returnValue(true)); + + $stubConditionFalse = $this->getMockBuilder('\Thelia\Condition\Implementation\MatchForXArticlesManager') + ->disableOriginalConstructor() + ->getMock(); + $stubConditionFalse->expects($this->any()) + ->method('isMatching') + ->will($this->returnValue(false)); + + $collection = new ConditionCollection(); + $collection->add($stubConditionTrue); + $collection->add($stubConditionFalse); + + $conitionEvaluator = new ConditionEvaluator(); + $actual = $conitionEvaluator->isMatching($collection); + + $this->assertFalse($actual); + } + /** * Tears down the fixture, for example, closes a network connection. * This method is called after a test is executed.