From ad48823b4819fdfe0a7a615fce8fabd38b9a991a Mon Sep 17 00:00:00 2001 From: gmorel Date: Mon, 16 Sep 2013 23:51:47 +0200 Subject: [PATCH] WIP : Coupon : unit tests on Rules --- ...=> AvailableForTotalAmountManagerTest.php} | 259 +++++++++++------- ...p => AvailableForXArticlesManagerTest.php} | 122 +++++---- .../Tests/Constraint/Rule/OperatorsTest.php | 4 + 3 files changed, 236 insertions(+), 149 deletions(-) rename core/lib/Thelia/Tests/Constraint/Rule/{AvailableForTotalAmountTest.php => AvailableForTotalAmountManagerTest.php} (78%) rename core/lib/Thelia/Tests/Constraint/Rule/{AvailableForXArticlesTest.php => AvailableForXArticlesManagerTest.php} (89%) diff --git a/core/lib/Thelia/Tests/Constraint/Rule/AvailableForTotalAmountTest.php b/core/lib/Thelia/Tests/Constraint/Rule/AvailableForTotalAmountManagerTest.php similarity index 78% rename from core/lib/Thelia/Tests/Constraint/Rule/AvailableForTotalAmountTest.php rename to core/lib/Thelia/Tests/Constraint/Rule/AvailableForTotalAmountManagerTest.php index c3f7249df..4ad790a5b 100644 --- a/core/lib/Thelia/Tests/Constraint/Rule/AvailableForTotalAmountTest.php +++ b/core/lib/Thelia/Tests/Constraint/Rule/AvailableForTotalAmountManagerTest.php @@ -26,19 +26,20 @@ namespace Thelia\Coupon; use Thelia\Constraint\ConstraintValidator; use Thelia\Constraint\Rule\AvailableForTotalAmountManager; use Thelia\Constraint\Rule\Operators; +use Thelia\Exception\InvalidRuleValueException; /** * Created by JetBrains PhpStorm. * Date: 8/19/13 * Time: 3:24 PM * - * Unit Test AvailableForTotalAmount Class + * Unit Test AvailableForTotalAmountManager Class * * @package Constraint * @author Guillaume MOREL * */ -class AvailableForTotalAmountTest extends \PHPUnit_Framework_TestCase +class AvailableForTotalAmountManagerTest extends \PHPUnit_Framework_TestCase { /** @var CouponAdapterInterface $stubTheliaAdapter */ protected $stubTheliaAdapter = null; @@ -49,108 +50,168 @@ class AvailableForTotalAmountTest extends \PHPUnit_Framework_TestCase */ protected function setUp() { -// /** @var CouponAdapterInterface $stubTheliaAdapter */ -// $this->stubTheliaAdapter = $this->generateValidCouponBaseAdapterMock(); + } -// /** -// * Generate valid CouponBaseAdapter -// * -// * @param float $cartTotalPrice Total amount of the current Cart -// * -// * @return CouponAdapterInterface -// */ -// protected function generateValidCouponBaseAdapterMock($cartTotalPrice = 421.23) -// { -// /** @var CouponAdapterInterface $stubTheliaAdapter */ -// $stubTheliaAdapter = $this->getMock( -// 'Thelia\Coupon\CouponBaseAdapter', -// array('getCartTotalPrice'), -// array() -// ); -// $stubTheliaAdapter->expects($this->any()) -// ->method('getCartTotalPrice') -// ->will($this->returnValue($cartTotalPrice)); -// -// return $stubTheliaAdapter; -// } + /** + * Check if validity test on BackOffice inputs are working + * + * @covers Thelia\Coupon\Rule\AvailableForTotalAmountManager::setValidators + * @expectedException \Thelia\Exception\InvalidRuleOperatorException + * + */ + public function testInValidBackOfficeInputOperator() + { + $stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter') + ->disableOriginalConstructor() + ->getMock(); -// /** -// * Check if validity test on BackOffice inputs are working -// * -// * @covers Thelia\Coupon\Rule\AvailableForTotalAmount::checkBackOfficeInput -// * -// */ -// public function testValidBackOfficeInput() -// { -// $adapter = new CouponBaseAdapter(); -// -// $validators = array( -// AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( -// Operators::SUPERIOR, -// new PriceParam( -// $adapter, 421.23, 'EUR' -// ) -// ) -// ); -// $rule = new AvailableForTotalAmount($adapter, $validators); -// -// $expected = true; -// $actual = $rule->checkBackOfficeInput(); -// $this->assertEquals($expected, $actual); -// } + $stubAdapter->expects($this->any()) + ->method('getCartTotalPrice') + ->will($this->returnValue(399)); + $stubAdapter->expects($this->any()) + ->method('getCheckoutCurrency') + ->will($this->returnValue('EUR')); + $stubAdapter->expects($this->any()) + ->method('getConstraintValidator') + ->will($this->returnValue(new ConstraintValidator())); -// /** -// * Check if validity test on BackOffice inputs are working -// * -// * @covers Thelia\Coupon\Rule\AvailableForTotalAmount::checkBackOfficeInput -// * @expectedException \Thelia\Exception\InvalidRuleOperatorException -// * -// */ -// public function testInValidBackOfficeInputOperator() -// { -// $adapter = new CouponBaseAdapter(); -// -// $validators = array( -// AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( -// 'X', -// new PriceParam( -// $adapter, 421.23, 'EUR' -// ) -// ) -// ); -// -// $rule = new AvailableForTotalAmount($adapter, $validators); -// -// $expected = false; -// $actual = $rule->checkBackOfficeInput(); -// $this->assertEquals($expected, $actual); -// } + $rule1 = new AvailableForTotalAmountManager($stubAdapter); + $operators = array( + AvailableForTotalAmountManager::INPUT1 => Operators::IN, + AvailableForTotalAmountManager::INPUT2 => Operators::EQUAL + ); + $values = array( + AvailableForTotalAmountManager::INPUT1 => '400', + AvailableForTotalAmountManager::INPUT2 => 'EUR'); + $rule1->setValidatorsFromForm($operators, $values); -// /** -// * Check if validity test on BackOffice inputs are working -// * -// * @covers Thelia\Coupon\Rule\AvailableForTotalAmount::checkBackOfficeInput -// * @expectedException \ErrorException -// * -// */ -// public function testInValidBackOfficeInputValue() -// { -// $adapter = $this->generateValidCouponBaseAdapterMock(); -// -// $validators = array( -// AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( -// Operators::SUPERIOR, -// 421 -// ) -// ); -// -// $rule = new AvailableForTotalAmount($adapter, $validators); -// -// $expected = false; -// $actual = $rule->checkBackOfficeInput(); -// $this->assertEquals($expected, $actual); -// } + $isValid = $rule1->isMatching(); + + $expected = true; + $actual =$isValid; + $this->assertEquals($expected, $actual); + } + + /** + * Check if validity test on BackOffice inputs are working + * + * @covers Thelia\Coupon\Rule\AvailableForTotalAmountManager::setValidators + * @expectedException \Thelia\Exception\InvalidRuleOperatorException + * + */ + public function testInValidBackOfficeInputOperator2() + { + $stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter') + ->disableOriginalConstructor() + ->getMock(); + + $stubAdapter->expects($this->any()) + ->method('getCartTotalPrice') + ->will($this->returnValue(399)); + $stubAdapter->expects($this->any()) + ->method('getCheckoutCurrency') + ->will($this->returnValue('EUR')); + $stubAdapter->expects($this->any()) + ->method('getConstraintValidator') + ->will($this->returnValue(new ConstraintValidator())); + + $rule1 = new AvailableForTotalAmountManager($stubAdapter); + $operators = array( + AvailableForTotalAmountManager::INPUT1 => Operators::SUPERIOR, + AvailableForTotalAmountManager::INPUT2 => Operators::INFERIOR + ); + $values = array( + AvailableForTotalAmountManager::INPUT1 => '400', + AvailableForTotalAmountManager::INPUT2 => 'EUR'); + $rule1->setValidatorsFromForm($operators, $values); + + $isValid = $rule1->isMatching(); + + $expected = true; + $actual =$isValid; + $this->assertEquals($expected, $actual); + } + + /** + * Check if validity test on BackOffice inputs are working + * + * @covers Thelia\Coupon\Rule\AvailableForTotalAmountManager::setValidators + * @expectedException \Thelia\Exception\InvalidRuleValueException + * + */ + public function testInValidBackOfficeInputValue() + { + $stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter') + ->disableOriginalConstructor() + ->getMock(); + + $stubAdapter->expects($this->any()) + ->method('getCartTotalPrice') + ->will($this->returnValue(399)); + $stubAdapter->expects($this->any()) + ->method('getCheckoutCurrency') + ->will($this->returnValue('EUR')); + $stubAdapter->expects($this->any()) + ->method('getConstraintValidator') + ->will($this->returnValue(new ConstraintValidator())); + + $rule1 = new AvailableForTotalAmountManager($stubAdapter); + $operators = array( + AvailableForTotalAmountManager::INPUT1 => Operators::SUPERIOR, + AvailableForTotalAmountManager::INPUT2 => Operators::EQUAL + ); + $values = array( + AvailableForTotalAmountManager::INPUT1 => 'X', + AvailableForTotalAmountManager::INPUT2 => 'EUR'); + $rule1->setValidatorsFromForm($operators, $values); + + $isValid = $rule1->isMatching(); + + $expected = true; + $actual =$isValid; + $this->assertEquals($expected, $actual); + } + + /** + * Check if validity test on BackOffice inputs are working + * + * @covers Thelia\Coupon\Rule\AvailableForTotalAmountManager::setValidators + * @expectedException \Thelia\Exception\InvalidRuleValueException + * + */ + public function testInValidBackOfficeInputValue2() + { + $stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter') + ->disableOriginalConstructor() + ->getMock(); + + $stubAdapter->expects($this->any()) + ->method('getCartTotalPrice') + ->will($this->returnValue(399)); + $stubAdapter->expects($this->any()) + ->method('getCheckoutCurrency') + ->will($this->returnValue('EUR')); + $stubAdapter->expects($this->any()) + ->method('getConstraintValidator') + ->will($this->returnValue(new ConstraintValidator())); + + $rule1 = new AvailableForTotalAmountManager($stubAdapter); + $operators = array( + AvailableForTotalAmountManager::INPUT1 => Operators::SUPERIOR, + AvailableForTotalAmountManager::INPUT2 => Operators::EQUAL + ); + $values = array( + AvailableForTotalAmountManager::INPUT1 => 400, + AvailableForTotalAmountManager::INPUT2 => 'FLA'); + $rule1->setValidatorsFromForm($operators, $values); + + $isValid = $rule1->isMatching(); + + $expected = true; + $actual =$isValid; + $this->assertEquals($expected, $actual); + } /** * Check if test inferior operator is working diff --git a/core/lib/Thelia/Tests/Constraint/Rule/AvailableForXArticlesTest.php b/core/lib/Thelia/Tests/Constraint/Rule/AvailableForXArticlesManagerTest.php similarity index 89% rename from core/lib/Thelia/Tests/Constraint/Rule/AvailableForXArticlesTest.php rename to core/lib/Thelia/Tests/Constraint/Rule/AvailableForXArticlesManagerTest.php index 4ecbcb8ac..12deb34b8 100644 --- a/core/lib/Thelia/Tests/Constraint/Rule/AvailableForXArticlesTest.php +++ b/core/lib/Thelia/Tests/Constraint/Rule/AvailableForXArticlesManagerTest.php @@ -33,13 +33,13 @@ use Thelia\Constraint\Rule\SerializableRule; * Date: 8/19/13 * Time: 3:24 PM * - * Unit Test AvailableForXArticles Class + * Unit Test AvailableForXArticlesManager Class * * @package Constraint * @author Guillaume MOREL * */ -class AvailableForXArticlesTest extends \PHPUnit_Framework_TestCase +class AvailableForXArticlesManagerTest extends \PHPUnit_Framework_TestCase { // /** @var CouponAdapterInterface $stubTheliaAdapter */ @@ -51,30 +51,8 @@ class AvailableForXArticlesTest extends \PHPUnit_Framework_TestCase */ protected function setUp() { -// /** @var CouponAdapterInterface $stubTheliaAdapter */ -// $this->stubTheliaAdapter = $this->generateValidCouponBaseAdapterMock(); - } -// /** -// * Generate valid CouponBaseAdapter -// * -// * @param int $nbArticlesInCart Total articles in the current Cart -// * -// * @return CouponAdapterInterface -// */ -// protected function generateValidCouponBaseAdapterMock($nbArticlesInCart = 4) -// { -// /** @var CouponAdapterInterface $stubTheliaAdapter */ -// $stubTheliaAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter') -// ->disableOriginalConstructor() -// ->setMethods(array('getNbArticlesInCart')) -// ->getMock(); -// $stubTheliaAdapter->expects($this->any()) -// ->method('getNbArticlesInCart') -// ->will($this->returnValue($nbArticlesInCart)); -// -// return $stubTheliaAdapter; -// } + } // /** // * Check if validity test on BackOffice inputs are working @@ -100,31 +78,75 @@ class AvailableForXArticlesTest extends \PHPUnit_Framework_TestCase // $this->assertEquals($expected, $actual); // } -// /** -// * Check if validity test on BackOffice inputs are working -// * -// * @covers Thelia\Coupon\Rule\AvailableForXArticles::checkBackOfficeInput -// * @expectedException \Thelia\Exception\InvalidRuleValueException -// */ -// public function testInValidBackOfficeInputFloat() -// { -// $adapter = $this->stubTheliaAdapter; -// -// $validators = array( -// AvailableForXArticles::PARAM1_QUANTITY => new RuleValidator( -// Operators::SUPERIOR, -// new QuantityParam( -// $adapter, -// 4.5 -// ) -// ) -// ); -// $rule = new AvailableForXArticles($adapter, $validators); -// -// $expected = false; -// $actual = $rule->checkBackOfficeInput(); -// $this->assertEquals($expected, $actual); -// } + /** + * Check if validity test on BackOffice inputs are working + * + * @covers Thelia\Coupon\Rule\AvailableForXArticles::checkBackOfficeInput + * @expectedException \Thelia\Exception\InvalidRuleOperatorException + */ + public function testInValidBackOfficeInputOperator() + { + $stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter') + ->disableOriginalConstructor() + ->getMock(); + + $stubAdapter->expects($this->any()) + ->method('getNbArticlesInCart') + ->will($this->returnValue(4)); + $stubAdapter->expects($this->any()) + ->method('getConstraintValidator') + ->will($this->returnValue(new ConstraintValidator())); + + $rule1 = new AvailableForXArticlesManager($stubAdapter); + $operators = array( + AvailableForXArticlesManager::INPUT1 => Operators::IN + ); + $values = array( + AvailableForXArticlesManager::INPUT1 => 5 + ); + $rule1->setValidatorsFromForm($operators, $values); + + $isValid = $rule1->isMatching(); + + $expected = true; + $actual =$isValid; + $this->assertEquals($expected, $actual); + } + + /** + * Check if validity test on BackOffice inputs are working + * + * @covers Thelia\Coupon\Rule\AvailableForXArticles::checkBackOfficeInput + * @expectedException \Thelia\Exception\InvalidRuleValueException + */ + public function testInValidBackOfficeInputValue() + { + $stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter') + ->disableOriginalConstructor() + ->getMock(); + + $stubAdapter->expects($this->any()) + ->method('getNbArticlesInCart') + ->will($this->returnValue(4)); + $stubAdapter->expects($this->any()) + ->method('getConstraintValidator') + ->will($this->returnValue(new ConstraintValidator())); + + $rule1 = new AvailableForXArticlesManager($stubAdapter); + $operators = array( + AvailableForXArticlesManager::INPUT1 => Operators::SUPERIOR + ); + $values = array( + AvailableForXArticlesManager::INPUT1 => 'X' + ); + $rule1->setValidatorsFromForm($operators, $values); + + $isValid = $rule1->isMatching(); + + $expected = true; + $actual =$isValid; + $this->assertEquals($expected, $actual); + } // /** // * Check if validity test on BackOffice inputs are working diff --git a/core/lib/Thelia/Tests/Constraint/Rule/OperatorsTest.php b/core/lib/Thelia/Tests/Constraint/Rule/OperatorsTest.php index d5a340d2b..6cdcb3430 100644 --- a/core/lib/Thelia/Tests/Constraint/Rule/OperatorsTest.php +++ b/core/lib/Thelia/Tests/Constraint/Rule/OperatorsTest.php @@ -96,6 +96,10 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase $actual = Operators::getI18n($stubTranslator, Operators::OUT); $expected = 'not in'; $this->assertEquals($expected, $actual); + + $actual = Operators::getI18n($stubTranslator, 'unexpected operator'); + $expected = 'unexpected operator'; + $this->assertEquals($expected, $actual); }