From c24e82758f494d045ba79321e69b955e604210d7 Mon Sep 17 00:00:00 2001 From: gmorel Date: Tue, 27 Aug 2013 18:53:18 +0200 Subject: [PATCH] WIP Coupon Refactor : creating dedicated reusable module for Constraints Adding ConstraintManager Secured : - Effects : RemoveXPercent + RemoveXAmount - Validators : all except ModelParam (need CustomerModelParam, AreaModelParam, CountryModelParam ?) - Conditions : AvailableForTotalAmount --- .../Thelia/Constraint/ConstraintManager.php | 82 ++++ .../Rule/AvailableForDate.php | 4 +- .../Rule/AvailableForLocationX.php | 4 +- .../Rule/AvailableForPeriod.php | 4 +- .../Rule/AvailableForRepeatedDate.php | 4 +- .../Rule/AvailableForRepeatedPeriod.php | 4 +- .../Rule/AvailableForTotalAmount.php | 45 +- .../AvailableForTotalAmountForCategoryY.php | 4 +- .../Rule/AvailableForXArticles.php | 4 +- .../Rule/CouponRuleAbstract.php | 8 +- .../Rule/CouponRuleInterface.php | 19 +- .../{Coupon => Constraint}/Rule/Operators.php | 68 ++- .../Validator/ComparableInterface.php | 2 +- .../Validator/DateParam.php | 25 +- .../Validator/IntegerParam.php | 24 +- .../Validator/IntervalParam.php | 57 ++- .../Constraint/Validator/ModelParam.php | 115 +++++ .../Validator/PriceParam.php | 32 +- .../Validator/QuantityParam.php | 29 +- .../Validator/RepeatedDateParam.php | 23 +- .../Validator/RepeatedIntervalParam.php | 23 +- .../Validator/RepeatedParam.php | 50 ++- .../Validator/RuleParameterAbstract.php | 20 +- .../Validator/RuleValidator.php | 4 +- .../Thelia/Coupon/CouponAdapterInterface.php | 8 + core/lib/Thelia/Coupon/CouponBaseAdapter.php | 14 +- core/lib/Thelia/Coupon/CouponManager.php | 2 +- .../Thelia/Coupon/CouponRuleCollection.php | 2 +- .../lib/Thelia/Coupon/Type/CouponAbstract.php | 23 +- .../Thelia/Coupon/Type/CouponInterface.php | 14 + core/lib/Thelia/Coupon/Type/RemoveXAmount.php | 71 ++- .../lib/Thelia/Coupon/Type/RemoveXPercent.php | 76 +++- .../Constraint/ConstraintManagerTest.php | 142 ++++++ .../Rule/AvailableForTotalAmountTest.php | 43 +- .../Rule/AvailableForXArticlesTest.php | 4 +- .../Rule/OperatorsTest.php | 42 +- .../Validator}/DateParamTest.php | 37 +- .../Validator}/IntegerParamTest.php | 36 +- .../Validator}/IntervalParamTest.php | 40 +- .../Validator}/PriceParamTest.php | 57 ++- .../Validator}/QuantityParamTest.php | 43 +- .../Validator}/RepeatedDateParamTest.php | 63 ++- .../Validator}/RepeatedIntervalParamTest.php | 81 +++- .../Thelia/Tests/Coupon/CouponFactoryTest.php | 14 +- .../Thelia/Tests/Coupon/CouponManagerTest.php | 30 +- .../Tests/Coupon/CouponRuleCollectionTest.php | 14 +- .../Tests/Coupon/Type/RemoveXAmountTest.php | 48 +- .../Tests/Coupon/Type/RemoveXPercentTest.php | 424 ++++++++++++++---- 48 files changed, 1632 insertions(+), 350 deletions(-) create mode 100644 core/lib/Thelia/Constraint/ConstraintManager.php rename core/lib/Thelia/{Coupon => Constraint}/Rule/AvailableForDate.php (97%) rename core/lib/Thelia/{Coupon => Constraint}/Rule/AvailableForLocationX.php (97%) rename core/lib/Thelia/{Coupon => Constraint}/Rule/AvailableForPeriod.php (97%) rename core/lib/Thelia/{Coupon => Constraint}/Rule/AvailableForRepeatedDate.php (97%) rename core/lib/Thelia/{Coupon => Constraint}/Rule/AvailableForRepeatedPeriod.php (98%) rename core/lib/Thelia/{Coupon => Constraint}/Rule/AvailableForTotalAmount.php (84%) rename core/lib/Thelia/{Coupon => Constraint}/Rule/AvailableForTotalAmountForCategoryY.php (97%) rename core/lib/Thelia/{Coupon => Constraint}/Rule/AvailableForXArticles.php (97%) rename core/lib/Thelia/{Coupon => Constraint}/Rule/CouponRuleAbstract.php (97%) rename core/lib/Thelia/{Coupon => Constraint}/Rule/CouponRuleInterface.php (91%) rename core/lib/Thelia/{Coupon => Constraint}/Rule/Operators.php (69%) rename core/lib/Thelia/{Coupon => Constraint}/Validator/ComparableInterface.php (98%) rename core/lib/Thelia/{Coupon => Constraint}/Validator/DateParam.php (84%) rename core/lib/Thelia/{Coupon => Constraint}/Validator/IntegerParam.php (85%) rename core/lib/Thelia/{Coupon => Constraint}/Validator/IntervalParam.php (76%) create mode 100644 core/lib/Thelia/Constraint/Validator/ModelParam.php rename core/lib/Thelia/{Coupon => Constraint}/Validator/PriceParam.php (81%) rename core/lib/Thelia/{Coupon => Constraint}/Validator/QuantityParam.php (81%) rename core/lib/Thelia/{Coupon => Constraint}/Validator/RepeatedDateParam.php (87%) rename core/lib/Thelia/{Coupon => Constraint}/Validator/RepeatedIntervalParam.php (89%) rename core/lib/Thelia/{Coupon => Constraint}/Validator/RepeatedParam.php (89%) rename core/lib/Thelia/{Coupon => Constraint}/Validator/RuleParameterAbstract.php (85%) rename core/lib/Thelia/{Coupon => Constraint}/Validator/RuleValidator.php (97%) create mode 100644 core/lib/Thelia/Tests/Constraint/ConstraintManagerTest.php rename core/lib/Thelia/Tests/{Coupon => Constraint}/Rule/AvailableForTotalAmountTest.php (94%) rename core/lib/Thelia/Tests/{Coupon => Constraint}/Rule/AvailableForXArticlesTest.php (99%) rename core/lib/Thelia/Tests/{Coupon => Constraint}/Rule/OperatorsTest.php (92%) rename core/lib/Thelia/Tests/{Coupon/Parameter => Constraint/Validator}/DateParamTest.php (75%) rename core/lib/Thelia/Tests/{Coupon/Parameter => Constraint/Validator}/IntegerParamTest.php (75%) rename core/lib/Thelia/Tests/{Coupon/Parameter => Constraint/Validator}/IntervalParamTest.php (74%) rename core/lib/Thelia/Tests/{Coupon/Parameter => Constraint/Validator}/PriceParamTest.php (73%) rename core/lib/Thelia/Tests/{Coupon/Parameter => Constraint/Validator}/QuantityParamTest.php (75%) rename core/lib/Thelia/Tests/{Coupon/Parameter => Constraint/Validator}/RepeatedDateParamTest.php (78%) rename core/lib/Thelia/Tests/{Coupon/Parameter => Constraint/Validator}/RepeatedIntervalParamTest.php (80%) diff --git a/core/lib/Thelia/Constraint/ConstraintManager.php b/core/lib/Thelia/Constraint/ConstraintManager.php new file mode 100644 index 000000000..6cbcbb1be --- /dev/null +++ b/core/lib/Thelia/Constraint/ConstraintManager.php @@ -0,0 +1,82 @@ +. */ +/* */ +/**********************************************************************************/ + +namespace Thelia\Constraint; + +use Thelia\Coupon\CouponAdapterInterface; +use Thelia\Coupon\CouponRuleCollection; + + +/** + * Created by JetBrains PhpStorm. + * Date: 8/19/13 + * Time: 3:24 PM + * + * Manage how Constraint could interact + * + * @package Constraint + * @author Guillaume MOREL + * + */ +class ConstraintManager +{ + /** @var CouponAdapterInterface Provide necessary value from Thelia*/ + protected $adapter; + + /** @var array CouponRuleCollection to process*/ + protected $rules = null; + + /** + * Constructor + * + * @param CouponAdapterInterface $adapter Provide necessary value from Thelia + * @param CouponRuleCollection $rules Rules associated with the Constraint + */ + function __construct(CouponAdapterInterface $adapter, CouponRuleCollection $rules) + { + $this->adapter = $adapter; + $this->rule = $rules; + } + + /** + * Check if the current Coupon is matching its conditions (Rules) + * Thelia variables are given by the CouponAdapterInterface + * + * @return bool + */ + public function isMatching() + { + $isMatching = true; + + /** @var CouponRuleInterface $rule */ + foreach ($this->rules->getRules() as $rule) { + if (!$rule->isMatching($this->adapter)) { + $isMatching = false; + } + } + + return $isMatching; + } + + +} \ No newline at end of file diff --git a/core/lib/Thelia/Coupon/Rule/AvailableForDate.php b/core/lib/Thelia/Constraint/Rule/AvailableForDate.php similarity index 97% rename from core/lib/Thelia/Coupon/Rule/AvailableForDate.php rename to core/lib/Thelia/Constraint/Rule/AvailableForDate.php index 81cb54a91..1ddfd0350 100644 --- a/core/lib/Thelia/Coupon/Rule/AvailableForDate.php +++ b/core/lib/Thelia/Constraint/Rule/AvailableForDate.php @@ -21,14 +21,14 @@ /* */ /**********************************************************************************/ -namespace Thelia\Coupon\Rule; +namespace Thelia\Constraint\Rule; /** * Created by JetBrains PhpStorm. * Date: 8/19/13 * Time: 3:24 PM * - * @package Coupon + * @package Constraint * @author Guillaume MOREL * */ diff --git a/core/lib/Thelia/Coupon/Rule/AvailableForLocationX.php b/core/lib/Thelia/Constraint/Rule/AvailableForLocationX.php similarity index 97% rename from core/lib/Thelia/Coupon/Rule/AvailableForLocationX.php rename to core/lib/Thelia/Constraint/Rule/AvailableForLocationX.php index 7f4d1bb33..5c3ec494c 100644 --- a/core/lib/Thelia/Coupon/Rule/AvailableForLocationX.php +++ b/core/lib/Thelia/Constraint/Rule/AvailableForLocationX.php @@ -21,14 +21,14 @@ /* */ /**********************************************************************************/ -namespace Thelia\Coupon\Rule; +namespace Thelia\Constraint\Rule; /** * Created by JetBrains PhpStorm. * Date: 8/19/13 * Time: 3:24 PM * - * @package Coupon + * @package Constraint * @author Guillaume MOREL * */ diff --git a/core/lib/Thelia/Coupon/Rule/AvailableForPeriod.php b/core/lib/Thelia/Constraint/Rule/AvailableForPeriod.php similarity index 97% rename from core/lib/Thelia/Coupon/Rule/AvailableForPeriod.php rename to core/lib/Thelia/Constraint/Rule/AvailableForPeriod.php index 7097ca52c..30679a973 100644 --- a/core/lib/Thelia/Coupon/Rule/AvailableForPeriod.php +++ b/core/lib/Thelia/Constraint/Rule/AvailableForPeriod.php @@ -21,14 +21,14 @@ /* */ /**********************************************************************************/ -namespace Thelia\Coupon\Rule; +namespace Thelia\Constraint\Rule; /** * Created by JetBrains PhpStorm. * Date: 8/19/13 * Time: 3:24 PM * - * @package Coupon + * @package Constraint * @author Guillaume MOREL * */ diff --git a/core/lib/Thelia/Coupon/Rule/AvailableForRepeatedDate.php b/core/lib/Thelia/Constraint/Rule/AvailableForRepeatedDate.php similarity index 97% rename from core/lib/Thelia/Coupon/Rule/AvailableForRepeatedDate.php rename to core/lib/Thelia/Constraint/Rule/AvailableForRepeatedDate.php index f4d4be7d3..3449a7f5b 100644 --- a/core/lib/Thelia/Coupon/Rule/AvailableForRepeatedDate.php +++ b/core/lib/Thelia/Constraint/Rule/AvailableForRepeatedDate.php @@ -21,14 +21,14 @@ /* */ /**********************************************************************************/ -namespace Thelia\Coupon\Rule; +namespace Thelia\Constraint\Rule; /** * Created by JetBrains PhpStorm. * Date: 8/19/13 * Time: 3:24 PM * - * @package Coupon + * @package Constraint * @author Guillaume MOREL * */ diff --git a/core/lib/Thelia/Coupon/Rule/AvailableForRepeatedPeriod.php b/core/lib/Thelia/Constraint/Rule/AvailableForRepeatedPeriod.php similarity index 98% rename from core/lib/Thelia/Coupon/Rule/AvailableForRepeatedPeriod.php rename to core/lib/Thelia/Constraint/Rule/AvailableForRepeatedPeriod.php index 4f67889c2..f5bf9bafc 100644 --- a/core/lib/Thelia/Coupon/Rule/AvailableForRepeatedPeriod.php +++ b/core/lib/Thelia/Constraint/Rule/AvailableForRepeatedPeriod.php @@ -21,7 +21,7 @@ /* */ /**********************************************************************************/ -namespace Thelia\Coupon\Rule; +namespace Thelia\Constraint\Rule; use Thelia\Coupon\CouponAdapterInterface; @@ -30,7 +30,7 @@ use Thelia\Coupon\CouponAdapterInterface; * Date: 8/19/13 * Time: 3:24 PM * - * @package Coupon + * @package Constraint * @author Guillaume MOREL * */ diff --git a/core/lib/Thelia/Coupon/Rule/AvailableForTotalAmount.php b/core/lib/Thelia/Constraint/Rule/AvailableForTotalAmount.php similarity index 84% rename from core/lib/Thelia/Coupon/Rule/AvailableForTotalAmount.php rename to core/lib/Thelia/Constraint/Rule/AvailableForTotalAmount.php index aa1e40339..704df073e 100644 --- a/core/lib/Thelia/Coupon/Rule/AvailableForTotalAmount.php +++ b/core/lib/Thelia/Constraint/Rule/AvailableForTotalAmount.php @@ -21,12 +21,12 @@ /* */ /**********************************************************************************/ -namespace Thelia\Coupon\Rule; +namespace Thelia\Constraint\Rule; use Symfony\Component\Intl\Exception\NotImplementedException; use Thelia\Coupon\CouponAdapterInterface; -use Thelia\Coupon\Validator\PriceParam; -use Thelia\Coupon\Validator\RuleValidator; +use Thelia\Constraint\Validator\PriceParam; +use Thelia\Constraint\Validator\RuleValidator; use Thelia\Exception\InvalidRuleException; use Thelia\Exception\InvalidRuleOperatorException; use Thelia\Exception\InvalidRuleValueException; @@ -39,7 +39,7 @@ use Thelia\Exception\InvalidRuleValueException; * Rule AvailableForTotalAmount * Check if a Checkout total amount match criteria * - * @package Coupon + * @package Constraint * @author Guillaume MOREL * */ @@ -182,5 +182,42 @@ class AvailableForTotalAmount extends CouponRuleAbstract return $this->validators; } + /** + * Get I18n name + * + * @return string + */ + public function getName() + { + return $this->adapter + ->getTranslator() + ->trans('Cart total amount', null, 'constraint'); + } + + /** + * Get I18n tooltip + * + * @return string + */ + public function getToolTip() + { + $i18nOperator = Operators::getI18n( + $this->adapter, $this->priceValidator->getOperator() + ); + + $toolTip = $this->adapter + ->getTranslator() + ->trans( + 'If cart total amount is %operator% %amount% %currency%', + array( + '%operator%' => $i18nOperator, + '%amount%' => $this->priceValidator->getParam()->getPrice(), + '%currency%' => $this->priceValidator->getParam()->getCurrency() + ), + 'constraint' + ); + + return $toolTip; + } } \ No newline at end of file diff --git a/core/lib/Thelia/Coupon/Rule/AvailableForTotalAmountForCategoryY.php b/core/lib/Thelia/Constraint/Rule/AvailableForTotalAmountForCategoryY.php similarity index 97% rename from core/lib/Thelia/Coupon/Rule/AvailableForTotalAmountForCategoryY.php rename to core/lib/Thelia/Constraint/Rule/AvailableForTotalAmountForCategoryY.php index 6493da92a..0f9f7f10b 100644 --- a/core/lib/Thelia/Coupon/Rule/AvailableForTotalAmountForCategoryY.php +++ b/core/lib/Thelia/Constraint/Rule/AvailableForTotalAmountForCategoryY.php @@ -21,14 +21,14 @@ /* */ /**********************************************************************************/ -namespace Thelia\Coupon\Rule; +namespace Thelia\Constraint\Rule; /** * Created by JetBrains PhpStorm. * Date: 8/19/13 * Time: 3:24 PM * - * @package Coupon + * @package Constraint * @author Guillaume MOREL * */ diff --git a/core/lib/Thelia/Coupon/Rule/AvailableForXArticles.php b/core/lib/Thelia/Constraint/Rule/AvailableForXArticles.php similarity index 97% rename from core/lib/Thelia/Coupon/Rule/AvailableForXArticles.php rename to core/lib/Thelia/Constraint/Rule/AvailableForXArticles.php index 7a898205c..b38a53ce4 100644 --- a/core/lib/Thelia/Coupon/Rule/AvailableForXArticles.php +++ b/core/lib/Thelia/Constraint/Rule/AvailableForXArticles.php @@ -21,7 +21,7 @@ /* */ /**********************************************************************************/ -namespace Thelia\Coupon\Rule; +namespace Thelia\Constraint\Rule; /** * Created by JetBrains PhpStorm. @@ -30,7 +30,7 @@ namespace Thelia\Coupon\Rule; * * Check a Checkout against its Product number * - * @package Coupon + * @package Constraint * @author Guillaume MOREL * */ diff --git a/core/lib/Thelia/Coupon/Rule/CouponRuleAbstract.php b/core/lib/Thelia/Constraint/Rule/CouponRuleAbstract.php similarity index 97% rename from core/lib/Thelia/Coupon/Rule/CouponRuleAbstract.php rename to core/lib/Thelia/Constraint/Rule/CouponRuleAbstract.php index 7d0b741bb..9fbcc21fc 100644 --- a/core/lib/Thelia/Coupon/Rule/CouponRuleAbstract.php +++ b/core/lib/Thelia/Constraint/Rule/CouponRuleAbstract.php @@ -21,12 +21,12 @@ /* */ /**********************************************************************************/ -namespace Thelia\Coupon\Rule; +namespace Thelia\Constraint\Rule; use Symfony\Component\Intl\Exception\NotImplementedException; use Thelia\Coupon\CouponAdapterInterface; -use Thelia\Coupon\Validator\ComparableInterface; -use Thelia\Coupon\Validator\RuleValidator; +use Thelia\Constraint\Validator\ComparableInterface; +use Thelia\Constraint\Validator\RuleValidator; use Thelia\Exception\InvalidRuleException; use Thelia\Exception\InvalidRuleOperatorException; @@ -37,7 +37,7 @@ use Thelia\Exception\InvalidRuleOperatorException; * * Assist in writing a condition of whether the Rule is applied or not * - * @package Coupon + * @package Constraint * @author Guillaume MOREL * */ diff --git a/core/lib/Thelia/Coupon/Rule/CouponRuleInterface.php b/core/lib/Thelia/Constraint/Rule/CouponRuleInterface.php similarity index 91% rename from core/lib/Thelia/Coupon/Rule/CouponRuleInterface.php rename to core/lib/Thelia/Constraint/Rule/CouponRuleInterface.php index 6aaca802c..9e75c7fe1 100644 --- a/core/lib/Thelia/Coupon/Rule/CouponRuleInterface.php +++ b/core/lib/Thelia/Constraint/Rule/CouponRuleInterface.php @@ -21,7 +21,7 @@ /* */ /**********************************************************************************/ -namespace Thelia\Coupon\Rule; +namespace Thelia\Constraint\Rule; use Thelia\Coupon\CouponAdapterInterface; @@ -32,7 +32,7 @@ use Thelia\Coupon\CouponAdapterInterface; * * Represents a condition of whether the Rule is applied or not * - * @package Coupon + * @package Constraint * @author Guillaume MOREL * */ @@ -69,4 +69,19 @@ interface CouponRuleInterface */ public function getAvailableOperators(); + + /** + * Get I18n name + * + * @return string + */ + public function getName(); + + /** + * Get I18n tooltip + * + * @return string + */ + public function getToolTip(); + } diff --git a/core/lib/Thelia/Coupon/Rule/Operators.php b/core/lib/Thelia/Constraint/Rule/Operators.php similarity index 69% rename from core/lib/Thelia/Coupon/Rule/Operators.php rename to core/lib/Thelia/Constraint/Rule/Operators.php index fb188c3cd..41ed7428c 100644 --- a/core/lib/Thelia/Coupon/Rule/Operators.php +++ b/core/lib/Thelia/Constraint/Rule/Operators.php @@ -21,9 +21,9 @@ /* */ /**********************************************************************************/ -namespace Thelia\Coupon\Rule; +namespace Thelia\Constraint\Rule; -use Thelia\Coupon\Validator\ComparableInterface; +use Thelia\Constraint\Validator\ComparableInterface; /** * Created by JetBrains PhpStorm. @@ -32,7 +32,7 @@ use Thelia\Coupon\Validator\ComparableInterface; * * Represent available Operations in rule checking * - * @package Coupon + * @package Constraint * @author Guillaume MOREL * */ @@ -106,4 +106,66 @@ abstract class Operators return $ret; } + + /** + * Get operator translation + * + * @param CouponAdapterInterface $adapter Provide necessary value from Thelia + * @param string $operator Operator const + * + * @return string + */ + public static function getI18n(CouponAdapterInterface $adapter, $operator) + { + $translator = $adapter->getTranslator(); + + $ret = $operator; + switch ($operator) { + case self::INFERIOR: + $ret = $translator->trans( + 'inferior to', + null, + 'constraint' + ); + break; + case self::INFERIOR_OR_EQUAL: + $ret = $translator->trans( + 'inferior or equals to', + null, + 'constraint' + ); + break; + case self::EQUAL: + $ret = $translator->trans( + 'equals to', + null, + 'constraint' + ); + break; + case self::SUPERIOR_OR_EQUAL: + $ret = $translator->trans( + 'superior or equals to', + null, + 'constraint' + ); + break; + case self::SUPERIOR: + $ret = $translator->trans( + 'superior to', + null, + 'constraint' + ); + break; + case self::DIFFERENT: + $ret = $translator->trans( + 'different from', + null, + 'constraint' + ); + break; + default: + } + + return $ret; + } } \ No newline at end of file diff --git a/core/lib/Thelia/Coupon/Validator/ComparableInterface.php b/core/lib/Thelia/Constraint/Validator/ComparableInterface.php similarity index 98% rename from core/lib/Thelia/Coupon/Validator/ComparableInterface.php rename to core/lib/Thelia/Constraint/Validator/ComparableInterface.php index 3b70c5e37..d06187fb0 100644 --- a/core/lib/Thelia/Coupon/Validator/ComparableInterface.php +++ b/core/lib/Thelia/Constraint/Validator/ComparableInterface.php @@ -21,7 +21,7 @@ /* */ /**********************************************************************************/ -namespace Thelia\Coupon\Validator; +namespace Thelia\Constraint\Validator; /** * Comparable interface diff --git a/core/lib/Thelia/Coupon/Validator/DateParam.php b/core/lib/Thelia/Constraint/Validator/DateParam.php similarity index 84% rename from core/lib/Thelia/Coupon/Validator/DateParam.php rename to core/lib/Thelia/Constraint/Validator/DateParam.php index 062c750e5..ae7eff858 100644 --- a/core/lib/Thelia/Coupon/Validator/DateParam.php +++ b/core/lib/Thelia/Constraint/Validator/DateParam.php @@ -21,7 +21,9 @@ /* */ /**********************************************************************************/ -namespace Thelia\Coupon\Validator; +namespace Thelia\Constraint\Validator; + +use Thelia\Coupon\CouponAdapterInterface; /** * Created by JetBrains PhpStorm. @@ -30,7 +32,7 @@ namespace Thelia\Coupon\Validator; * * Represent a DateTime * - * @package Coupon + * @package Constraint * @author Guillaume MOREL * */ @@ -42,11 +44,13 @@ class DateParam extends RuleParameterAbstract /** * Constructor * - * @param \DateTime $dateTime DateTime + * @param CouponAdapterInterface $adapter Provide necessary value from Thelia + * @param \DateTime $dateTime DateTime */ - public function __construct(\DateTime $dateTime) + public function __construct(CouponAdapterInterface $adapter, \DateTime $dateTime) { $this->dateTime = $dateTime; + $this->adapter = $adapter; } /** @@ -101,5 +105,16 @@ class DateParam extends RuleParameterAbstract return clone $this->dateTime; } + /** + * Get I18n tooltip + * + * @return string + */ + public function getToolTip() + { + return $this->adapter + ->getTranslator() + ->trans('A date (ex: YYYY-MM-DD HH:MM:SS)', null, 'constraint'); + } -} \ No newline at end of file +} diff --git a/core/lib/Thelia/Coupon/Validator/IntegerParam.php b/core/lib/Thelia/Constraint/Validator/IntegerParam.php similarity index 85% rename from core/lib/Thelia/Coupon/Validator/IntegerParam.php rename to core/lib/Thelia/Constraint/Validator/IntegerParam.php index 7c8303809..c783655c8 100644 --- a/core/lib/Thelia/Coupon/Validator/IntegerParam.php +++ b/core/lib/Thelia/Constraint/Validator/IntegerParam.php @@ -21,7 +21,9 @@ /* */ /**********************************************************************************/ -namespace Thelia\Coupon\Validator; +namespace Thelia\Constraint\Validator; + +use Thelia\Coupon\CouponAdapterInterface; /** * Created by JetBrains PhpStorm. @@ -30,7 +32,7 @@ namespace Thelia\Coupon\Validator; * * Represent an Integer * - * @package Coupon + * @package Constraint * @author Guillaume MOREL * */ @@ -42,11 +44,13 @@ class IntegerParam extends RuleParameterAbstract /** * Constructor * - * @param int $integer Integer + * @param CouponAdapterInterface $adapter Provide necessary value from Thelia + * @param int $integer Integer */ - public function __construct($integer) + public function __construct(CouponAdapterInterface $adapter, $integer) { $this->integer = $integer; + $this->adapter = $adapter; } /** @@ -102,4 +106,16 @@ class IntegerParam extends RuleParameterAbstract return $this->integer; } + /** + * Get I18n tooltip + * + * @return string + */ + public function getToolTip() + { + return $this->adapter + ->getTranslator() + ->trans('A number (ex: 42)', null, 'constraint'); + } + } \ No newline at end of file diff --git a/core/lib/Thelia/Coupon/Validator/IntervalParam.php b/core/lib/Thelia/Constraint/Validator/IntervalParam.php similarity index 76% rename from core/lib/Thelia/Coupon/Validator/IntervalParam.php rename to core/lib/Thelia/Constraint/Validator/IntervalParam.php index 1b310c9d1..a4554760a 100644 --- a/core/lib/Thelia/Coupon/Validator/IntervalParam.php +++ b/core/lib/Thelia/Constraint/Validator/IntervalParam.php @@ -21,7 +21,9 @@ /* */ /**********************************************************************************/ -namespace Thelia\Coupon\Validator; +namespace Thelia\Constraint\Validator; + +use Thelia\Coupon\CouponAdapterInterface; /** * Created by JetBrains PhpStorm. @@ -30,7 +32,7 @@ namespace Thelia\Coupon\Validator; * * Represent an DateTime period * - * @package Coupon + * @package Constraint * @author Guillaume MOREL * */ @@ -39,17 +41,50 @@ class IntervalParam extends RuleParameterAbstract /** @var \DatePeriod Date period */ protected $datePeriod = null; + /** @var \DateTime Start date */ + protected $start = null; + + /** @var \DateInterval Interval date */ + protected $interval = null; + /** * Constructor * - * @param \DateTime $start Start interval - * @param \DateInterval $interval Period + * @param CouponAdapterInterface $adapter Provide necessary value from Thelia + * @param \DateTime $start Start interval + * @param \DateInterval $interval Period */ - public function __construct(\DateTime $start, \DateInterval $interval) + public function __construct(CouponAdapterInterface $adapter, \DateTime $start, \DateInterval $interval) { $this->datePeriod = new \DatePeriod($start, $interval, 1); + $this->adapter = $adapter; + + $this->start = $start; + $this->interval = $interval; } + /** + * Get Interval + * + * @return \DateInterval + */ + public function getInterval() + { + return $this->interval; + } + + /** + * Get start date + * + * @return \DateTime + */ + public function getStart() + { + return $this->start; + } + + + /** * Get DatePeriod * @@ -115,4 +150,16 @@ class IntervalParam extends RuleParameterAbstract { return clone $this->datePeriod; } + + /** + * Get I18n tooltip + * + * @return string + */ + public function getToolTip() + { + return $this->adapter + ->getTranslator() + ->trans('An interval between two dates', null, 'constraint'); + } } \ No newline at end of file diff --git a/core/lib/Thelia/Constraint/Validator/ModelParam.php b/core/lib/Thelia/Constraint/Validator/ModelParam.php new file mode 100644 index 000000000..7ff4567b8 --- /dev/null +++ b/core/lib/Thelia/Constraint/Validator/ModelParam.php @@ -0,0 +1,115 @@ +. */ +/* */ +/**********************************************************************************/ + +namespace Thelia\Constraint\Validator; + +use InvalidArgumentException; +use Thelia\Coupon\CouponAdapterInterface; + +/** + * Created by JetBrains PhpStorm. + * Date: 8/19/13 + * Time: 3:24 PM + * + * Represent a Model + * + * @package Constraint + * @author Guillaume MOREL + * + */ +class ModelParam extends IntegerParam +{ + /** @var string Model Class name */ + protected $modelClass = null; + + /** @var ModelCriteria */ + protected $queryBuilder = null; + + /** + * Constructor + * + * @param CouponAdapterInterface $adapter Provide necessary value from Thelia + * @param int $integer Integer + * @param string $modelClass Model class name + * + * @throws InvalidArgumentException + */ + public function __construct(CouponAdapterInterface $adapter, $integer, $modelClass) + { + if ($integer < 0) { + $integer = 0; + } + $this->integer = $integer; + $this->adapter = $adapter; + + $this->modelClass = $modelClass; + $queryClassName = $modelClass . 'Query'; + try { + $this->queryBuilder = $queryClassName::create(); + } catch (\Exception $e) { + throw new InvalidArgumentException('ModelParam can only compare Models'); + } + } + + /** + * Compare the current object to the passed $other. + * + * Returns 0 if they are semantically equal, 1 if the other object + * is less than the current one, or -1 if its more than the current one. + * + * This method should not check for identity using ===, only for semantically equality for example + * when two different DateTime instances point to the exact same Date + TZ. + * + * @param mixed $other Object + * + * @throws InvalidArgumentException + * @return int + */ + public function compareTo($other) + { + if (!is_integer($other) || $other < 0) { + throw new InvalidArgumentException( + 'IntegerParam can compare only positive int' + ); + } + + return parent::compareTo($other); + } + + /** + * Get I18n tooltip + * + * @return string + */ + public function getToolTip() + { + return $this->adapter + ->getTranslator() + ->trans( + 'A Model', + null, + 'constraint' + ); + } + +} diff --git a/core/lib/Thelia/Coupon/Validator/PriceParam.php b/core/lib/Thelia/Constraint/Validator/PriceParam.php similarity index 81% rename from core/lib/Thelia/Coupon/Validator/PriceParam.php rename to core/lib/Thelia/Constraint/Validator/PriceParam.php index 44eae3234..90538a73e 100644 --- a/core/lib/Thelia/Coupon/Validator/PriceParam.php +++ b/core/lib/Thelia/Constraint/Validator/PriceParam.php @@ -21,7 +21,9 @@ /* */ /**********************************************************************************/ -namespace Thelia\Coupon\Validator; +namespace Thelia\Constraint\Validator; + +use Thelia\Coupon\CouponAdapterInterface; /** * Created by JetBrains PhpStorm. @@ -31,7 +33,7 @@ namespace Thelia\Coupon\Validator; * Represent a Price * Positive value with currency * - * @package Coupon + * @package Constraint * @author Guillaume MOREL * */ @@ -46,13 +48,15 @@ class PriceParam extends RuleParameterAbstract /** * Constructor * - * @param float $price Positive float - * @param string $currency Currency Code ISO 4217 EUR|USD|GBP + * @param CouponAdapterInterface $adapter Provide necessary value from Thelia + * @param float $price Positive float + * @param string $currency Currency Code ISO 4217 EUR|USD|GBP */ - public function __construct($price, $currency) + public function __construct(CouponAdapterInterface $adapter, $price, $currency) { $this->price = $price; $this->currency = $currency; + $this->adapter = $adapter; } /** @@ -120,4 +124,22 @@ class PriceParam extends RuleParameterAbstract { return $this->price; } + + /** + * Get I18n tooltip + * + * @return string + */ + public function getToolTip() + { + return $this->adapter + ->getTranslator() + ->trans( + 'A price in %currency% (ex: 14.50)', + array( + '%currency%' => $this->currency + ), + 'constraint' + ); + } } \ No newline at end of file diff --git a/core/lib/Thelia/Coupon/Validator/QuantityParam.php b/core/lib/Thelia/Constraint/Validator/QuantityParam.php similarity index 81% rename from core/lib/Thelia/Coupon/Validator/QuantityParam.php rename to core/lib/Thelia/Constraint/Validator/QuantityParam.php index dc842605f..5d5132e5b 100644 --- a/core/lib/Thelia/Coupon/Validator/QuantityParam.php +++ b/core/lib/Thelia/Constraint/Validator/QuantityParam.php @@ -21,7 +21,9 @@ /* */ /**********************************************************************************/ -namespace Thelia\Coupon\Validator; +namespace Thelia\Constraint\Validator; + +use Thelia\Coupon\CouponAdapterInterface; /** * Created by JetBrains PhpStorm. @@ -30,7 +32,7 @@ namespace Thelia\Coupon\Validator; * * Represent a Quantity * - * @package Coupon + * @package Constraint * @author Guillaume MOREL * */ @@ -40,14 +42,16 @@ class QuantityParam extends IntegerParam /** * Constructor * - * @param int $integer Integer + * @param CouponAdapterInterface $adapter Provide necessary value from Thelia + * @param int $integer Integer */ - public function __construct($integer) + public function __construct(CouponAdapterInterface $adapter, $integer) { if ($integer < 0) { $integer = 0; } $this->integer = $integer; + $this->adapter = $adapter; } /** @@ -75,4 +79,21 @@ class QuantityParam extends IntegerParam return parent::compareTo($other); } + + /** + * Get I18n tooltip + * + * @return string + */ + public function getToolTip() + { + return $this->adapter + ->getTranslator() + ->trans( + 'A positive quantity (ex: 42)', + null, + 'constraint' + ); + } + } diff --git a/core/lib/Thelia/Coupon/Validator/RepeatedDateParam.php b/core/lib/Thelia/Constraint/Validator/RepeatedDateParam.php similarity index 87% rename from core/lib/Thelia/Coupon/Validator/RepeatedDateParam.php rename to core/lib/Thelia/Constraint/Validator/RepeatedDateParam.php index f0524edc7..0e8a558cf 100644 --- a/core/lib/Thelia/Coupon/Validator/RepeatedDateParam.php +++ b/core/lib/Thelia/Constraint/Validator/RepeatedDateParam.php @@ -21,7 +21,9 @@ /* */ /**********************************************************************************/ -namespace Thelia\Coupon\Validator; +namespace Thelia\Constraint\Validator; + +use Thelia\Coupon\CouponAdapterInterface; /** * Created by JetBrains PhpStorm. @@ -38,7 +40,7 @@ namespace Thelia\Coupon\Validator; * x5 : $this->recurrences How many repeated cycle, 1st excluded * x6 : How many occurrence * - * @package Coupon + * @package Constraint * @author Guillaume MOREL * */ @@ -46,10 +48,13 @@ class RepeatedDateParam extends RepeatedParam { /** * Constructor + * + * @param CouponAdapterInterface $adapter Provide necessary value from Thelia */ - public function __construct() + public function __construct(CouponAdapterInterface $adapter) { $this->defaultConstructor(); + $this->adapter = $adapter; } /** @@ -97,4 +102,16 @@ class RepeatedDateParam extends RepeatedParam { return clone $this->datePeriod; } + + /** + * Get I18n tooltip + * + * @return string + */ + public function getToolTip() + { + return $this->adapter + ->getTranslator() + ->trans('A date (ex: YYYY-MM-DD HH:MM:SS)', null, 'constraint'); + } } \ No newline at end of file diff --git a/core/lib/Thelia/Coupon/Validator/RepeatedIntervalParam.php b/core/lib/Thelia/Constraint/Validator/RepeatedIntervalParam.php similarity index 89% rename from core/lib/Thelia/Coupon/Validator/RepeatedIntervalParam.php rename to core/lib/Thelia/Constraint/Validator/RepeatedIntervalParam.php index 9f61f4db1..e37cd3b45 100644 --- a/core/lib/Thelia/Coupon/Validator/RepeatedIntervalParam.php +++ b/core/lib/Thelia/Constraint/Validator/RepeatedIntervalParam.php @@ -21,7 +21,9 @@ /* */ /**********************************************************************************/ -namespace Thelia\Coupon\Validator; +namespace Thelia\Constraint\Validator; + +use Thelia\Coupon\CouponAdapterInterface; /** * Created by JetBrains PhpStorm. @@ -39,7 +41,7 @@ namespace Thelia\Coupon\Validator; * x6 : How many occurrence * **** : $this->durationInDays Duration of a period * - * @package Coupon + * @package Constraint * @author Guillaume MOREL * */ @@ -75,10 +77,13 @@ class RepeatedIntervalParam extends RepeatedParam /** * Constructor + * + * @param CouponAdapterInterface $adapter Provide necessary value from Thelia */ - public function __construct() + public function __construct(CouponAdapterInterface $adapter) { $this->defaultConstructor(); + $this->adapter = $adapter; } /** @@ -131,4 +136,16 @@ class RepeatedIntervalParam extends RepeatedParam { return clone $this->datePeriod; } + + /** + * Get I18n tooltip + * + * @return string + */ + public function getToolTip() + { + return $this->adapter + ->getTranslator() + ->trans('A date (ex: YYYY-MM-DD HH:MM:SS)', null, 'constraint'); + } } \ No newline at end of file diff --git a/core/lib/Thelia/Coupon/Validator/RepeatedParam.php b/core/lib/Thelia/Constraint/Validator/RepeatedParam.php similarity index 89% rename from core/lib/Thelia/Coupon/Validator/RepeatedParam.php rename to core/lib/Thelia/Constraint/Validator/RepeatedParam.php index 675228456..1188e4fbb 100644 --- a/core/lib/Thelia/Coupon/Validator/RepeatedParam.php +++ b/core/lib/Thelia/Constraint/Validator/RepeatedParam.php @@ -21,7 +21,7 @@ /* */ /**********************************************************************************/ -namespace Thelia\Coupon\Validator; +namespace Thelia\Constraint\Validator; use DateInterval; use DatePeriod; @@ -34,7 +34,7 @@ use DateTime; * * Allow to set the way a parameter can be repeated across the time * - * @package Coupon + * @package Constraint * @author Guillaume MOREL * */ @@ -52,6 +52,52 @@ abstract class RepeatedParam extends RuleParameterAbstract /** @var DatePeriod dates recurring at regular intervals, over a given period */ protected $datePeriod = null; + /** @var int Frequency the object will be repeated */ + protected $frequency = null; + + /** @var int $nbRepetition Time the object will be repeated */ + protected $nbRepetition = null; + + /** + * Get frequency + * + * @return int + */ + public function getFrequency() + { + return $this->frequency; + } + + /** + * Get Interval + * + * @return \DateInterval + */ + public function getInterval() + { + return $this->interval; + } + + /** + * Get number of time it will be repeated + * + * @return int + */ + public function getNbRepetition() + { + return $this->nbRepetition; + } + + /** + * Get number of recurrences + * + * @return int + */ + public function getRecurrences() + { + return $this->recurrences; + } + /** * Generate default repetition * Every 1 week 100 times from now diff --git a/core/lib/Thelia/Coupon/Validator/RuleParameterAbstract.php b/core/lib/Thelia/Constraint/Validator/RuleParameterAbstract.php similarity index 85% rename from core/lib/Thelia/Coupon/Validator/RuleParameterAbstract.php rename to core/lib/Thelia/Constraint/Validator/RuleParameterAbstract.php index 3e96682cd..d8326d66f 100644 --- a/core/lib/Thelia/Coupon/Validator/RuleParameterAbstract.php +++ b/core/lib/Thelia/Constraint/Validator/RuleParameterAbstract.php @@ -21,8 +21,9 @@ /* */ /**********************************************************************************/ -namespace Thelia\Coupon\Validator; +namespace Thelia\Constraint\Validator; +use Thelia\Coupon\CouponAdapterInterface; use Thelia\Exception\NotImplementedException; /** @@ -32,12 +33,15 @@ use Thelia\Exception\NotImplementedException; * * Get a Param value * - * @package Coupon + * @package Constraint * @author Guillaume MOREL * */ abstract class RuleParameterAbstract implements ComparableInterface { + /** @var CouponAdapterInterface Provide necessary value from Thelia*/ + protected $adapter; + /** * Get Parameter value to test against * @@ -47,4 +51,16 @@ abstract class RuleParameterAbstract implements ComparableInterface { return new NotImplementedException(); } + + /** + * Get I18n tooltip + * + * @return string + */ + public function getToolTip() + { + return new NotImplementedException(); + } + + } \ No newline at end of file diff --git a/core/lib/Thelia/Coupon/Validator/RuleValidator.php b/core/lib/Thelia/Constraint/Validator/RuleValidator.php similarity index 97% rename from core/lib/Thelia/Coupon/Validator/RuleValidator.php rename to core/lib/Thelia/Constraint/Validator/RuleValidator.php index 064677751..9b0093bc9 100644 --- a/core/lib/Thelia/Coupon/Validator/RuleValidator.php +++ b/core/lib/Thelia/Constraint/Validator/RuleValidator.php @@ -21,7 +21,7 @@ /* */ /**********************************************************************************/ -namespace Thelia\Coupon\Validator; +namespace Thelia\Constraint\Validator; /** * Created by JetBrains PhpStorm. @@ -30,7 +30,7 @@ namespace Thelia\Coupon\Validator; * * Allow to validate parameters * - * @package Coupon + * @package Constraint * @author Guillaume MOREL * */ diff --git a/core/lib/Thelia/Coupon/CouponAdapterInterface.php b/core/lib/Thelia/Coupon/CouponAdapterInterface.php index 21ff870f6..4325da3ae 100644 --- a/core/lib/Thelia/Coupon/CouponAdapterInterface.php +++ b/core/lib/Thelia/Coupon/CouponAdapterInterface.php @@ -131,4 +131,12 @@ interface CouponAdapterInterface */ public function getTranslator(); + /** + * Return the main currency + * THe one used to set prices in BackOffice + * + * @return string + */ + public function getMainCurrency(); + } \ No newline at end of file diff --git a/core/lib/Thelia/Coupon/CouponBaseAdapter.php b/core/lib/Thelia/Coupon/CouponBaseAdapter.php index 3809612a7..70c9ebf94 100644 --- a/core/lib/Thelia/Coupon/CouponBaseAdapter.php +++ b/core/lib/Thelia/Coupon/CouponBaseAdapter.php @@ -179,7 +179,7 @@ class CouponBaseAdapter implements CouponAdapterInterface */ public function getContainer() { - // TODO: Implement getCheckoutPostagePrice() method. + // TODO: Implement getContainer() method. } /** @@ -191,4 +191,16 @@ class CouponBaseAdapter implements CouponAdapterInterface { return $this->getContainer()->get('thelia.translator'); } + + + /** + * Return the main currency + * THe one used to set prices in BackOffice + * + * @return string + */ + public function getMainCurrency() + { + // TODO: Implement getMainCurrency() method. + } } diff --git a/core/lib/Thelia/Coupon/CouponManager.php b/core/lib/Thelia/Coupon/CouponManager.php index b913de66d..272cb7ed7 100644 --- a/core/lib/Thelia/Coupon/CouponManager.php +++ b/core/lib/Thelia/Coupon/CouponManager.php @@ -38,7 +38,7 @@ use Thelia\Coupon\Type\CouponInterface; */ class CouponManager { - /** @var CouponAdapterInterface Provide necessary value from Thelia*/ + /** @var CouponAdapterInterface Provides necessary value from Thelia */ protected $adapter; /** @var array CouponInterface to process*/ diff --git a/core/lib/Thelia/Coupon/CouponRuleCollection.php b/core/lib/Thelia/Coupon/CouponRuleCollection.php index 06f0b15af..2f1a51dc0 100644 --- a/core/lib/Thelia/Coupon/CouponRuleCollection.php +++ b/core/lib/Thelia/Coupon/CouponRuleCollection.php @@ -24,7 +24,7 @@ namespace Thelia\Coupon; use Symfony\Component\Serializer\Encoder\JsonEncoder; -use Thelia\Coupon\Rule\CouponRuleInterface; +use Thelia\Constraint\Rule\CouponRuleInterface; use Thelia\Exception\InvalidRuleException; /** diff --git a/core/lib/Thelia/Coupon/Type/CouponAbstract.php b/core/lib/Thelia/Coupon/Type/CouponAbstract.php index 750c62e5c..a5e82c83e 100644 --- a/core/lib/Thelia/Coupon/Type/CouponAbstract.php +++ b/core/lib/Thelia/Coupon/Type/CouponAbstract.php @@ -24,8 +24,8 @@ namespace Thelia\Coupon\Type; use Symfony\Component\Intl\Exception\NotImplementedException; +use Thelia\Constraint\ConstraintManager; use Thelia\Coupon\CouponAdapterInterface; -use Thelia\Coupon\Rule\CouponRuleInterface; use Thelia\Coupon\CouponRuleCollection; use Thelia\Coupon\RuleOrganizerInterface; use Thelia\Exception\InvalidRuleException; @@ -43,12 +43,18 @@ use Thelia\Exception\InvalidRuleException; */ abstract class CouponAbstract implements CouponInterface { + /** @var CouponAdapterInterface Provides necessary value from Thelia */ + protected $adapter = null; + /** @var RuleOrganizerInterface */ protected $organizer = null; /** @var CouponRuleCollection Array of CouponRuleInterface */ protected $rules = null; + /** @var ConstraintManager CouponRuleInterface Manager*/ + protected $constraintManager = null; + /** @var string Coupon code (ex: XMAS) */ protected $code = null; @@ -194,6 +200,10 @@ abstract class CouponAbstract implements CouponInterface public function setRules(CouponRuleCollection $rules) { $this->rules = $rules; + $this->constraintManager = new ConstraintManager( + $this->adapter, + $this->rules + ); return $this; } @@ -209,16 +219,7 @@ abstract class CouponAbstract implements CouponInterface */ public function isMatching(CouponAdapterInterface $adapter) { - $isMatching = true; - - /** @var CouponRuleInterface $rule */ - foreach ($this->rules->getRules() as $rule) { - if (!$rule->isMatching($adapter)) { - $isMatching = false; - } - } - - return $isMatching; + return $this->constraintManager->isMatching(); } /** diff --git a/core/lib/Thelia/Coupon/Type/CouponInterface.php b/core/lib/Thelia/Coupon/Type/CouponInterface.php index df1a82f40..334520104 100644 --- a/core/lib/Thelia/Coupon/Type/CouponInterface.php +++ b/core/lib/Thelia/Coupon/Type/CouponInterface.php @@ -165,4 +165,18 @@ interface CouponInterface * @return bool */ public function isExpired(); + + /** + * Get I18n name + * + * @return string + */ + public function getName(); + + /** + * Get I18n tooltip + * + * @return string + */ + public function getToolTip(); } diff --git a/core/lib/Thelia/Coupon/Type/RemoveXAmount.php b/core/lib/Thelia/Coupon/Type/RemoveXAmount.php index a0d715141..672c8a856 100644 --- a/core/lib/Thelia/Coupon/Type/RemoveXAmount.php +++ b/core/lib/Thelia/Coupon/Type/RemoveXAmount.php @@ -23,6 +23,7 @@ namespace Thelia\Coupon\Type; +use Thelia\Constraint\ConstraintManager; use Thelia\Coupon\Type\CouponAbstract; /** @@ -41,20 +42,34 @@ class RemoveXAmount extends CouponAbstract /** * Constructor * - * @param string $code Coupon code (ex: XMAS) - * @param string $title Coupon title (ex: Coupon for XMAS) - * @param string $shortDescription Coupon short description - * @param string $description Coupon description - * @param float $amount Coupon amount to deduce - * @param bool $isCumulative If Coupon is cumulative - * @param bool $isRemovingPostage If Coupon is removing postage - * @param bool $isAvailableOnSpecialOffers If available on Product already - * on special offer price - * @param bool $isEnabled False if Coupon is disabled by admin - * @param int $maxUsage How many usage left - * @param \Datetime $expirationDate When the Code is expiring + * @param CouponInterface $adapter Provides necessary value from Thelia + * @param string $code Coupon code (ex: XMAS) + * @param string $title Coupon title (ex: Coupon for XMAS) + * @param string $shortDescription Coupon short description + * @param string $description Coupon description + * @param float $amount Coupon amount to deduce + * @param bool $isCumulative If Coupon is cumulative + * @param bool $isRemovingPostage If Coupon is removing postage + * @param bool $isAvailableOnSpecialOffers If available on Product already + * on special offer price + * @param bool $isEnabled False if Coupon is disabled by admin + * @param int $maxUsage How many usage left + * @param \Datetime $expirationDate When the Code is expiring */ - function __construct($code, $title, $shortDescription, $description, $amount, $isCumulative, $isRemovingPostage, $isAvailableOnSpecialOffers, $isEnabled, $maxUsage, \DateTime $expirationDate) + function __construct( + $adapter, + $code, + $title, + $shortDescription, + $description, + $amount, + $isCumulative, + $isRemovingPostage, + $isAvailableOnSpecialOffers, + $isEnabled, + $maxUsage, + \DateTime $expirationDate + ) { $this->code = $code; $this->title = $title; @@ -70,6 +85,36 @@ class RemoveXAmount extends CouponAbstract $this->isEnabled = $isEnabled; $this->maxUsage = $maxUsage; $this->expirationDate = $expirationDate; + $this->adapter = $adapter; } + /** + * Get I18n name + * + * @return string + */ + public function getName() + { + return $this->adapter + ->getTranslator() + ->trans('Remove X amount to total cart', null, 'constraint'); + } + + /** + * Get I18n tooltip + * + * @return string + */ + public function getToolTip() + { + $toolTip = $this->adapter + ->getTranslator() + ->trans( + 'This coupon will remove the entered amount to the customer total checkout. If the discount is superior to the total checkout price the customer will only pay the postage. Unless if the coupon is set to remove postage too.', + null, + 'constraint' + ); + + return $toolTip; + } } diff --git a/core/lib/Thelia/Coupon/Type/RemoveXPercent.php b/core/lib/Thelia/Coupon/Type/RemoveXPercent.php index da63a4281..5d1d14b63 100644 --- a/core/lib/Thelia/Coupon/Type/RemoveXPercent.php +++ b/core/lib/Thelia/Coupon/Type/RemoveXPercent.php @@ -43,15 +43,33 @@ class RemoveXPercent extends CouponAbstract /** * Constructor * - * @param string $code Coupon code (ex: XMAS) - * @param string $title Coupon title (ex: Coupon for XMAS) - * @param string $shortDescription Coupon short description - * @param string $description Coupon description - * @param float $percent Coupon % to deduce (ex:3.14 for 3.14%) - * @param bool $isCumulative if Coupon is cumulative - * @param bool $isRemovingPostage if Coupon is removing postage + * @param CouponInterface $adapter Provides necessary value from Thelia + * @param string $code Coupon code (ex: XMAS) + * @param string $title Coupon title (ex: Coupon for XMAS) + * @param string $shortDescription Coupon short description + * @param string $description Coupon description + * @param float $percent Coupon percentage to deduce + * @param bool $isCumulative If Coupon is cumulative + * @param bool $isRemovingPostage If Coupon is removing postage + * @param bool $isAvailableOnSpecialOffers If available on Product already + * on special offer price + * @param bool $isEnabled False if Coupon is disabled by admin + * @param int $maxUsage How many usage left + * @param \Datetime $expirationDate When the Code is expiring */ - function __construct($code, $title, $shortDescription, $description, $percent, $isCumulative, $isRemovingPostage) + function __construct( + $adapter, + $code, + $title, + $shortDescription, + $description, + $percent, + $isCumulative, + $isRemovingPostage, + $isAvailableOnSpecialOffers, + $isEnabled, + $maxUsage, + \DateTime $expirationDate) { $this->code = $code; $this->title = $title; @@ -62,6 +80,12 @@ class RemoveXPercent extends CouponAbstract $this->isRemovingPostage = $isRemovingPostage; $this->percent = $percent; + + $this->isAvailableOnSpecialOffers = $isAvailableOnSpecialOffers; + $this->isEnabled = $isEnabled; + $this->maxUsage = $maxUsage; + $this->expirationDate = $expirationDate; + $this->adapter = $adapter; } /** @@ -83,10 +107,40 @@ class RemoveXPercent extends CouponAbstract ); } - $basePrice =$adapter - ->getCartTotalPrice(); + $basePrice = $adapter->getCartTotalPrice(); - return $basePrice * (( 100 - $this->percent ) / 100); + return $basePrice * (( $this->percent ) / 100); + } + + + /** + * Get I18n name + * + * @return string + */ + public function getName() + { + return $this->adapter + ->getTranslator() + ->trans('Remove X percent to total cart', null, 'constraint'); + } + + /** + * Get I18n tooltip + * + * @return string + */ + public function getToolTip() + { + $toolTip = $this->adapter + ->getTranslator() + ->trans( + 'This coupon will remove the entered percentage to the customer total checkout. If the discount is superior to the total checkout price the customer will only pay the postage. Unless if the coupon is set to remove postage too.', + null, + 'constraint' + ); + + return $toolTip; } } \ No newline at end of file diff --git a/core/lib/Thelia/Tests/Constraint/ConstraintManagerTest.php b/core/lib/Thelia/Tests/Constraint/ConstraintManagerTest.php new file mode 100644 index 000000000..770746ba1 --- /dev/null +++ b/core/lib/Thelia/Tests/Constraint/ConstraintManagerTest.php @@ -0,0 +1,142 @@ +. */ +/* */ +/**********************************************************************************/ + +namespace Thelia\Constraint; + +use Thelia\Constraint\Validator\PriceParam; +use Thelia\Constraint\Validator\RuleValidator; +use Thelia\Constraint\Rule\AvailableForTotalAmount; +use Thelia\Constraint\Rule\Operators; +use Thelia\Coupon\CouponRuleCollection; +use Thelia\Coupon\Type\CouponInterface; +use Thelia\Coupon\Type\RemoveXAmount; +use Thelia\Tools\PhpUnitUtils; + +/** + * Created by JetBrains PhpStorm. + * Date: 8/19/13 + * Time: 3:24 PM + * + * Unit Test ConstraintManager Class + * + * @package Constraint + * @author Guillaume MOREL + * + */ +class ConstraintManagerTest extends \PHPUnit_Framework_TestCase +{ + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + */ + protected function setUp() + { + } + + + + /** + * Generate valid CouponRuleInterfaces + * + * @return array Array of CouponRuleInterface + */ + public static function generateValidRules() + { + $rule1 = new AvailableForTotalAmount( + array( + AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( + Operators::SUPERIOR, + new PriceParam( + , 40.00, 'EUR' + ) + ) + ) + ); + $rule2 = new AvailableForTotalAmount( + array( + AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( + Operators::INFERIOR, + new PriceParam( + , 400.00, 'EUR' + ) + ) + ) + ); + $rules = new CouponRuleCollection(array($rule1, $rule2)); + + return $rules; + } + + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + */ + protected function tearDown() + { + } + + /** + * Generate a fake Adapter + * + * @param array $coupons Coupons + * @param float $cartTotalPrice Cart total price + * @param float $checkoutTotalPrice Checkout total price + * @param float $postagePrice Checkout postage price + * + * @return \PHPUnit_Framework_MockObject_MockObject + */ + public function generateFakeAdapter(array $coupons, $cartTotalPrice, $checkoutTotalPrice, $postagePrice = 6.00) + { + $stubCouponBaseAdapter = $this->getMock( + 'Thelia\Coupon\CouponBaseAdapter', + array( + 'getCurrentCoupons', + 'getCartTotalPrice', + 'getCheckoutTotalPrice', + 'getCheckoutPostagePrice' + ), + array() + ); + + $stubCouponBaseAdapter->expects($this->any()) + ->method('getCurrentCoupons') + ->will($this->returnValue(($coupons))); + + // Return Cart product amount = $cartTotalPrice euros + $stubCouponBaseAdapter->expects($this->any()) + ->method('getCartTotalPrice') + ->will($this->returnValue($cartTotalPrice)); + + // Return Checkout amount = $checkoutTotalPrice euros + $stubCouponBaseAdapter->expects($this->any()) + ->method('getCheckoutTotalPrice') + ->will($this->returnValue($checkoutTotalPrice)); + + $stubCouponBaseAdapter->expects($this->any()) + ->method('getCheckoutPostagePrice') + ->will($this->returnValue($postagePrice)); + + return $stubCouponBaseAdapter; + } +} diff --git a/core/lib/Thelia/Tests/Coupon/Rule/AvailableForTotalAmountTest.php b/core/lib/Thelia/Tests/Constraint/Rule/AvailableForTotalAmountTest.php similarity index 94% rename from core/lib/Thelia/Tests/Coupon/Rule/AvailableForTotalAmountTest.php rename to core/lib/Thelia/Tests/Constraint/Rule/AvailableForTotalAmountTest.php index 63d78615f..05e507863 100644 --- a/core/lib/Thelia/Tests/Coupon/Rule/AvailableForTotalAmountTest.php +++ b/core/lib/Thelia/Tests/Constraint/Rule/AvailableForTotalAmountTest.php @@ -23,10 +23,10 @@ namespace Thelia\Coupon; -use Thelia\Coupon\Validator\PriceParam; -use Thelia\Coupon\Validator\RuleValidator; -use Thelia\Coupon\Rule\AvailableForTotalAmount; -use Thelia\Coupon\Rule\Operators; +use Thelia\Constraint\Validator\PriceParam; +use Thelia\Constraint\Validator\RuleValidator; +use Thelia\Constraint\Rule\AvailableForTotalAmount; +use Thelia\Constraint\Rule\Operators; use Thelia\Exception\InvalidRuleOperatorException; use Thelia\Exception\InvalidRuleValueException; @@ -37,7 +37,7 @@ use Thelia\Exception\InvalidRuleValueException; * * Unit Test AvailableForTotalAmount Class * - * @package Coupon + * @package Constraint * @author Guillaume MOREL * */ @@ -86,8 +86,7 @@ class AvailableForTotalAmountTest extends \PHPUnit_Framework_TestCase AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( Operators::SUPERIOR, new PriceParam( - 421.23, - 'EUR' + , 421.23, 'EUR' ) ) ); @@ -116,8 +115,7 @@ class AvailableForTotalAmountTest extends \PHPUnit_Framework_TestCase AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( 'X', new PriceParam( - 421.23, - 'EUR' + , 421.23, 'EUR' ) ) ); @@ -176,8 +174,7 @@ class AvailableForTotalAmountTest extends \PHPUnit_Framework_TestCase AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( Operators::SUPERIOR, new PriceParam( - 421.23, - 'EUR' + , 421.23, 'EUR' ) ) ); @@ -204,8 +201,7 @@ class AvailableForTotalAmountTest extends \PHPUnit_Framework_TestCase AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( Operators::SUPERIOR, new PriceParam( - 421.23, - 'EUR' + , 421.23, 'EUR' ) ) ); @@ -232,8 +228,7 @@ class AvailableForTotalAmountTest extends \PHPUnit_Framework_TestCase AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( Operators::SUPERIOR, new PriceParam( - 421.23, - 'EUR' + , 421.23, 'EUR' ) ) ); @@ -259,8 +254,7 @@ class AvailableForTotalAmountTest extends \PHPUnit_Framework_TestCase AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( Operators::INFERIOR, new PriceParam( - 421.23, - 'EUR' + , 421.23, 'EUR' ) ) ); @@ -286,8 +280,7 @@ class AvailableForTotalAmountTest extends \PHPUnit_Framework_TestCase AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( Operators::INFERIOR, new PriceParam( - 421.23, - 'EUR' + , 421.23, 'EUR' ) ) ); @@ -316,8 +309,7 @@ class AvailableForTotalAmountTest extends \PHPUnit_Framework_TestCase AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( Operators::EQUAL, new PriceParam( - 421.23, - 'EUR' + , 421.23, 'EUR' ) ) ); @@ -343,8 +335,7 @@ class AvailableForTotalAmountTest extends \PHPUnit_Framework_TestCase AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( Operators::EQUAL, new PriceParam( - 421.23, - 'EUR' + , 421.23, 'EUR' ) ) ); @@ -370,8 +361,7 @@ class AvailableForTotalAmountTest extends \PHPUnit_Framework_TestCase AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( Operators::SUPERIOR, new PriceParam( - 421.23, - 'EUR' + , 421.23, 'EUR' ) ) ); @@ -397,8 +387,7 @@ class AvailableForTotalAmountTest extends \PHPUnit_Framework_TestCase AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( Operators::SUPERIOR, new PriceParam( - 421.23, - 'EUR' + , 421.23, 'EUR' ) ) ); diff --git a/core/lib/Thelia/Tests/Coupon/Rule/AvailableForXArticlesTest.php b/core/lib/Thelia/Tests/Constraint/Rule/AvailableForXArticlesTest.php similarity index 99% rename from core/lib/Thelia/Tests/Coupon/Rule/AvailableForXArticlesTest.php rename to core/lib/Thelia/Tests/Constraint/Rule/AvailableForXArticlesTest.php index 3579eacf3..69bfd13a6 100644 --- a/core/lib/Thelia/Tests/Coupon/Rule/AvailableForXArticlesTest.php +++ b/core/lib/Thelia/Tests/Constraint/Rule/AvailableForXArticlesTest.php @@ -23,7 +23,7 @@ namespace Thelia\Coupon; -use Thelia\Coupon\Rule\AvailableForXArticles; +use Thelia\Constraint\Rule\AvailableForXArticles; /** * Created by JetBrains PhpStorm. @@ -32,7 +32,7 @@ use Thelia\Coupon\Rule\AvailableForXArticles; * * Unit Test AvailableForXArticles Class * - * @package Coupon + * @package Constraint * @author Guillaume MOREL * */ diff --git a/core/lib/Thelia/Tests/Coupon/Rule/OperatorsTest.php b/core/lib/Thelia/Tests/Constraint/Rule/OperatorsTest.php similarity index 92% rename from core/lib/Thelia/Tests/Coupon/Rule/OperatorsTest.php rename to core/lib/Thelia/Tests/Constraint/Rule/OperatorsTest.php index 22a83dc8b..51d5d06f1 100644 --- a/core/lib/Thelia/Tests/Coupon/Rule/OperatorsTest.php +++ b/core/lib/Thelia/Tests/Constraint/Rule/OperatorsTest.php @@ -23,8 +23,8 @@ namespace Thelia\Coupon; -use Thelia\Coupon\Validator\QuantityParam; -use Thelia\Coupon\Rule\Operators; +use Thelia\Constraint\Validator\QuantityParam; +use Thelia\Constraint\Rule\Operators; /** * Created by JetBrains PhpStorm. @@ -33,7 +33,7 @@ use Thelia\Coupon\Rule\Operators; * * Unit Test Operators Class * - * @package Coupon + * @package Constraint * @author Guillaume MOREL * */ @@ -58,7 +58,7 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase // Given $a = 11; $operator = Operators::INFERIOR; - $b = new QuantityParam(12); + $b = new QuantityParam(, 12); // When $actual = Operators::isValid($a, $operator, $b); @@ -77,7 +77,7 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase // Given $a = 12; $operator = Operators::INFERIOR; - $b = new QuantityParam(12); + $b = new QuantityParam(, 12); // When $actual = Operators::isValid($a, $operator, $b); @@ -96,7 +96,7 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase // Given $a = 13; $operator = Operators::INFERIOR; - $b = new QuantityParam(12); + $b = new QuantityParam(, 12); // When $actual = Operators::isValid($a, $operator, $b); @@ -115,7 +115,7 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase // Given $a = 11; $operator = Operators::INFERIOR_OR_EQUAL; - $b = new QuantityParam(11); + $b = new QuantityParam(, 11); // When $actual = Operators::isValid($a, $operator, $b); @@ -134,7 +134,7 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase // Given $a = 10; $operator = Operators::INFERIOR_OR_EQUAL; - $b = new QuantityParam(11); + $b = new QuantityParam(, 11); // When $actual = Operators::isValid($a, $operator, $b); @@ -153,7 +153,7 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase // Given $a = 12; $operator = Operators::INFERIOR_OR_EQUAL; - $b = new QuantityParam(11); + $b = new QuantityParam(, 11); // When $actual = Operators::isValid($a, $operator, $b); @@ -172,7 +172,7 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase // Given $a = 12; $operator = Operators::EQUAL; - $b = new QuantityParam(12); + $b = new QuantityParam(, 12); // When $actual = Operators::isValid($a, $operator, $b); @@ -191,7 +191,7 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase // Given $a = 11; $operator = Operators::EQUAL; - $b = new QuantityParam(12); + $b = new QuantityParam(, 12); // When $actual = Operators::isValid($a, $operator, $b); @@ -210,7 +210,7 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase // Given $a = 13; $operator = Operators::EQUAL; - $b = new QuantityParam(12); + $b = new QuantityParam(, 12); // When $actual = Operators::isValid($a, $operator, $b); @@ -229,7 +229,7 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase // Given $a = 13; $operator = Operators::SUPERIOR_OR_EQUAL; - $b = new QuantityParam(13); + $b = new QuantityParam(, 13); // When $actual = Operators::isValid($a, $operator, $b); @@ -248,7 +248,7 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase // Given $a = 14; $operator = Operators::SUPERIOR_OR_EQUAL; - $b = new QuantityParam(13); + $b = new QuantityParam(, 13); // When $actual = Operators::isValid($a, $operator, $b); @@ -267,7 +267,7 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase // Given $a = 12; $operator = Operators::SUPERIOR_OR_EQUAL; - $b = new QuantityParam(13); + $b = new QuantityParam(, 13); // When $actual = Operators::isValid($a, $operator, $b); @@ -286,7 +286,7 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase // Given $a = 13; $operator = Operators::SUPERIOR; - $b = new QuantityParam(12); + $b = new QuantityParam(, 12); // When $actual = Operators::isValid($a, $operator, $b); @@ -305,7 +305,7 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase // Given $a = 12; $operator = Operators::SUPERIOR; - $b = new QuantityParam(12); + $b = new QuantityParam(, 12); // When $actual = Operators::isValid($a, $operator, $b); @@ -324,7 +324,7 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase // Given $a = 11; $operator = Operators::SUPERIOR; - $b = new QuantityParam(12); + $b = new QuantityParam(, 12); // When $actual = Operators::isValid($a, $operator, $b); @@ -343,7 +343,7 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase // Given $a = 12; $operator = Operators::DIFFERENT; - $b = new QuantityParam(11); + $b = new QuantityParam(, 11); // When $actual = Operators::isValid($a, $operator, $b); @@ -362,7 +362,7 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase // Given $a = 11; $operator = Operators::DIFFERENT; - $b = new QuantityParam(11); + $b = new QuantityParam(, 11); // When $actual = Operators::isValid($a, $operator, $b); @@ -381,7 +381,7 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase // Given $a = 12; $operator = 'X'; - $b = new QuantityParam(11); + $b = new QuantityParam(, 11); // When $actual = Operators::isValid($a, $operator, $b); diff --git a/core/lib/Thelia/Tests/Coupon/Parameter/DateParamTest.php b/core/lib/Thelia/Tests/Constraint/Validator/DateParamTest.php similarity index 75% rename from core/lib/Thelia/Tests/Coupon/Parameter/DateParamTest.php rename to core/lib/Thelia/Tests/Constraint/Validator/DateParamTest.php index bbfe4015a..1b3739c5c 100644 --- a/core/lib/Thelia/Tests/Coupon/Parameter/DateParamTest.php +++ b/core/lib/Thelia/Tests/Constraint/Validator/DateParamTest.php @@ -24,7 +24,7 @@ namespace Thelia\Coupon; use InvalidArgumentException; -use Thelia\Coupon\Validator\DateParam; +use Thelia\Constraint\Validator\DateParam; /** * Created by JetBrains PhpStorm. @@ -33,7 +33,7 @@ use Thelia\Coupon\Validator\DateParam; * * Unit Test DateParam Class * - * @package Coupon + * @package Constraint * @author Guillaume MOREL * */ @@ -55,10 +55,11 @@ class DateParamTest extends \PHPUnit_Framework_TestCase */ public function testInferiorDate() { + $adapter = new CouponBaseAdapter(); $dateValidator = new \DateTime("2012-07-08"); $dateToValidate = new \DateTime("2012-07-07"); - $dateParam = new DateParam($dateValidator); + $dateParam = new DateParam($adapter, $dateValidator); $expected = 1; $actual = $dateParam->compareTo($dateToValidate); @@ -72,10 +73,11 @@ class DateParamTest extends \PHPUnit_Framework_TestCase */ public function testEqualsDate() { + $adapter = new CouponBaseAdapter(); $dateValidator = new \DateTime("2012-07-08"); $dateToValidate = new \DateTime("2012-07-08"); - $dateParam = new DateParam($dateValidator); + $dateParam = new DateParam($adapter, $dateValidator); $expected = 0; $actual = $dateParam->compareTo($dateToValidate); @@ -89,10 +91,11 @@ class DateParamTest extends \PHPUnit_Framework_TestCase */ public function testSuperiorDate() { + $adapter = new CouponBaseAdapter(); $dateValidator = new \DateTime("2012-07-08"); $dateToValidate = new \DateTime("2012-07-09"); - $dateParam = new DateParam($dateValidator); + $dateParam = new DateParam($adapter, $dateValidator); $expected = -1; $actual = $dateParam->compareTo($dateToValidate); @@ -105,14 +108,36 @@ class DateParamTest extends \PHPUnit_Framework_TestCase */ public function testInvalidArgumentException() { + $adapter = new CouponBaseAdapter(); $dateValidator = new \DateTime("2012-07-08"); $dateToValidate = 1377012588; - $dateParam = new DateParam($dateValidator); + $dateParam = new DateParam($adapter, $dateValidator); $dateParam->compareTo($dateToValidate); } + /** + * Test is the object is serializable + * If no data is lost during the process + */ + protected function isSerializableTest() + { + $adapter = new CouponBaseAdapter(); + $dateValidator = new \DateTime("2012-07-08"); + + $param = new DateParam($adapter, $dateValidator); + + $serialized = base64_encode(serialize($param)); + /** @var DateParam $unserialized */ + $unserialized = base64_decode(serialize($serialized)); + + $this->assertEquals($param->getValue(), $unserialized->getValue()); + $this->assertEquals($param->getDateTime(), $unserialized->getDateTime()); + + $new = new DateParam($adapter, $unserialized->getDateTime()); + $this->assertEquals($param->getDateTime(), $new->getDateTime()); + } /** diff --git a/core/lib/Thelia/Tests/Coupon/Parameter/IntegerParamTest.php b/core/lib/Thelia/Tests/Constraint/Validator/IntegerParamTest.php similarity index 75% rename from core/lib/Thelia/Tests/Coupon/Parameter/IntegerParamTest.php rename to core/lib/Thelia/Tests/Constraint/Validator/IntegerParamTest.php index b8938d102..c7b5b2ac0 100644 --- a/core/lib/Thelia/Tests/Coupon/Parameter/IntegerParamTest.php +++ b/core/lib/Thelia/Tests/Constraint/Validator/IntegerParamTest.php @@ -24,7 +24,7 @@ namespace Thelia\Coupon; use InvalidArgumentException; -use Thelia\Coupon\Validator\IntegerParam; +use Thelia\Constraint\Validator\IntegerParam; /** * Created by JetBrains PhpStorm. @@ -33,7 +33,7 @@ use Thelia\Coupon\Validator\IntegerParam; * * Unit Test IntegerParam Class * - * @package Coupon + * @package Constraint * @author Guillaume MOREL * */ @@ -55,10 +55,11 @@ class IntegerParamTest extends \PHPUnit_Framework_TestCase */ public function testInferiorInteger() { + $adapter = new CouponBaseAdapter(); $intValidator = 42; $intToValidate = 41; - $integerParam = new IntegerParam($intValidator); + $integerParam = new IntegerParam($adapter, $intValidator); $expected = 1; $actual = $integerParam->compareTo($intToValidate); @@ -72,10 +73,11 @@ class IntegerParamTest extends \PHPUnit_Framework_TestCase */ public function testEqualsInteger() { + $adapter = new CouponBaseAdapter(); $intValidator = 42; $intToValidate = 42; - $integerParam = new IntegerParam($intValidator); + $integerParam = new IntegerParam($adapter, $intValidator); $expected = 0; $actual = $integerParam->compareTo($intToValidate); @@ -89,10 +91,11 @@ class IntegerParamTest extends \PHPUnit_Framework_TestCase */ public function testSuperiorInteger() { + $adapter = new CouponBaseAdapter(); $intValidator = 42; $intToValidate = 43; - $integerParam = new IntegerParam($intValidator); + $integerParam = new IntegerParam($adapter, $intValidator); $expected = -1; $actual = $integerParam->compareTo($intToValidate); @@ -105,17 +108,38 @@ class IntegerParamTest extends \PHPUnit_Framework_TestCase */ public function testInvalidArgumentException() { + $adapter = new CouponBaseAdapter(); $intValidator = 42; $intToValidate = '42'; - $integerParam = new IntegerParam($intValidator); + $integerParam = new IntegerParam($adapter, $intValidator); $expected = 0; $actual = $integerParam->compareTo($intToValidate); $this->assertEquals($expected, $actual); } + /** + * Test is the object is serializable + * If no data is lost during the process + */ + protected function isSerializableTest() + { + $adapter = new CouponBaseAdapter(); + $intValidator = 42; + $param = new IntegerParam($adapter, $intValidator); + + $serialized = base64_encode(serialize($param)); + /** @var IntegerParam $unserialized */ + $unserialized = base64_decode(serialize($serialized)); + + $this->assertEquals($param->getValue(), $unserialized->getValue()); + $this->assertEquals($param->getInteger(), $unserialized->getInteger()); + + $new = new IntegerParam($adapter, $unserialized->getInteger()); + $this->assertEquals($param->getInteger(), $new->getInteger()); + } /** * Tears down the fixture, for example, closes a network connection. diff --git a/core/lib/Thelia/Tests/Coupon/Parameter/IntervalParamTest.php b/core/lib/Thelia/Tests/Constraint/Validator/IntervalParamTest.php similarity index 74% rename from core/lib/Thelia/Tests/Coupon/Parameter/IntervalParamTest.php rename to core/lib/Thelia/Tests/Constraint/Validator/IntervalParamTest.php index c068dfc01..05dad2f0c 100644 --- a/core/lib/Thelia/Tests/Coupon/Parameter/IntervalParamTest.php +++ b/core/lib/Thelia/Tests/Constraint/Validator/IntervalParamTest.php @@ -24,7 +24,7 @@ namespace Thelia\Coupon; use InvalidArgumentException; -use Thelia\Coupon\Validator\IntervalParam; +use Thelia\Constraint\Validator\IntervalParam; /** * Created by JetBrains PhpStorm. @@ -33,7 +33,7 @@ use Thelia\Coupon\Validator\IntervalParam; * * Unit Test IntervalParam Class * - * @package Coupon + * @package Constraint * @author Guillaume MOREL * */ @@ -55,11 +55,12 @@ class IntervalParamTest extends \PHPUnit_Framework_TestCase */ public function testInferiorDate() { + $adapter = new CouponBaseAdapter(); $dateValidatorStart = new \DateTime("2012-07-08"); $dateValidatorInterval = new \DateInterval("P1M"); //1month $dateToValidate = new \DateTime("2012-07-07"); - $dateParam = new IntervalParam($dateValidatorStart, $dateValidatorInterval); + $dateParam = new IntervalParam($adapter, $dateValidatorStart, $dateValidatorInterval); $expected = 1; $actual = $dateParam->compareTo($dateToValidate); @@ -73,6 +74,7 @@ class IntervalParamTest extends \PHPUnit_Framework_TestCase */ public function testEqualsDate() { + $adapter = new CouponBaseAdapter(); $dateValidatorStart = new \DateTime("2012-07-08"); $dateValidatorInterval = new \DateInterval("P1M"); //1month $dateToValidate = new \DateTime("2012-07-08"); @@ -80,7 +82,7 @@ class IntervalParamTest extends \PHPUnit_Framework_TestCase echo '1 ' . date_format($dateValidatorStart, 'g:ia \o\n l jS F Y') . "\n"; echo '2 ' . date_format($dateToValidate, 'g:ia \o\n l jS F Y') . "\n"; - $dateParam = new IntervalParam($dateValidatorStart, $dateValidatorInterval); + $dateParam = new IntervalParam($adapter, $dateValidatorStart, $dateValidatorInterval); $expected = 0; $actual = $dateParam->compareTo($dateToValidate); @@ -94,11 +96,12 @@ class IntervalParamTest extends \PHPUnit_Framework_TestCase */ public function testEqualsDate2() { + $adapter = new CouponBaseAdapter(); $dateValidatorStart = new \DateTime("2012-07-08"); $dateValidatorInterval = new \DateInterval("P1M"); //1month $dateToValidate = new \DateTime("2012-08-08"); - $dateParam = new IntervalParam($dateValidatorStart, $dateValidatorInterval); + $dateParam = new IntervalParam($adapter, $dateValidatorStart, $dateValidatorInterval); $expected = 0; $actual = $dateParam->compareTo($dateToValidate); @@ -112,11 +115,12 @@ class IntervalParamTest extends \PHPUnit_Framework_TestCase */ public function testSuperiorDate() { + $adapter = new CouponBaseAdapter(); $dateValidatorStart = new \DateTime("2012-07-08"); $dateValidatorInterval = new \DateInterval("P1M"); //1month $dateToValidate = new \DateTime("2012-08-09"); - $dateParam = new IntervalParam($dateValidatorStart, $dateValidatorInterval); + $dateParam = new IntervalParam($adapter, $dateValidatorStart, $dateValidatorInterval); $expected = -1; $actual = $dateParam->compareTo($dateToValidate); @@ -129,16 +133,38 @@ class IntervalParamTest extends \PHPUnit_Framework_TestCase */ public function testInvalidArgumentException() { + $adapter = new CouponBaseAdapter(); $dateValidatorStart = new \DateTime("2012-07-08"); $dateValidatorInterval = new \DateInterval("P1M"); //1month $dateToValidate = 1377012588; - $dateParam = new IntervalParam($dateValidatorStart, $dateValidatorInterval); + $dateParam = new IntervalParam($adapter, $dateValidatorStart, $dateValidatorInterval); $dateParam->compareTo($dateToValidate); } + /** + * Test is the object is serializable + * If no data is lost during the process + */ + protected function isSerializableTest() + { + $adapter = new CouponBaseAdapter(); + $dateValidatorStart = new \DateTime("2012-07-08"); + $dateValidatorInterval = new \DateInterval("P1M"); //1month + $param = new IntervalParam($adapter, $dateValidatorStart, $dateValidatorInterval); + + $serialized = base64_encode(serialize($param)); + /** @var IntervalParam $unserialized */ + $unserialized = base64_decode(serialize($serialized)); + + $this->assertEquals($param->getValue(), $unserialized->getValue()); + $this->assertEquals($param->getDatePeriod(), $unserialized->getDatePeriod()); + + $new = new IntervalParam($adapter, $unserialized->getStart(), $unserialized->getInterval()); + $this->assertEquals($param->getDatePeriod(), $new->getDatePeriod()); + } /** * Tears down the fixture, for example, closes a network connection. diff --git a/core/lib/Thelia/Tests/Coupon/Parameter/PriceParamTest.php b/core/lib/Thelia/Tests/Constraint/Validator/PriceParamTest.php similarity index 73% rename from core/lib/Thelia/Tests/Coupon/Parameter/PriceParamTest.php rename to core/lib/Thelia/Tests/Constraint/Validator/PriceParamTest.php index 19f19fd4e..7ccc508aa 100644 --- a/core/lib/Thelia/Tests/Coupon/Parameter/PriceParamTest.php +++ b/core/lib/Thelia/Tests/Constraint/Validator/PriceParamTest.php @@ -24,7 +24,7 @@ namespace Thelia\Coupon; use InvalidArgumentException; -use Thelia\Coupon\Validator\PriceParam; +use Thelia\Constraint\Validator\PriceParam; /** * Created by JetBrains PhpStorm. @@ -33,7 +33,7 @@ use Thelia\Coupon\Validator\PriceParam; * * Unit Test PriceParam Class * - * @package Coupon + * @package Constraint * @author Guillaume MOREL * */ @@ -55,10 +55,12 @@ class PriceParamTest extends \PHPUnit_Framework_TestCase */ public function testInferiorPrice() { + $adapter = new CouponBaseAdapter(); + $priceValidator = 42.50; $priceToValidate = 1.00; - $integerParam = new PriceParam($priceValidator, 'EUR'); + $integerParam = new PriceParam($adapter, $priceValidator, 'EUR'); $expected = 1; $actual = $integerParam->compareTo($priceToValidate); @@ -72,10 +74,12 @@ class PriceParamTest extends \PHPUnit_Framework_TestCase */ public function testInferiorPrice2() { + $adapter = new CouponBaseAdapter(); + $priceValidator = 42.50; $priceToValidate = 42.49; - $integerParam = new PriceParam($priceValidator, 'EUR'); + $integerParam = new PriceParam($adapter, $priceValidator, 'EUR'); $expected = 1; $actual = $integerParam->compareTo($priceToValidate); @@ -89,10 +93,12 @@ class PriceParamTest extends \PHPUnit_Framework_TestCase */ public function testEqualsPrice() { + $adapter = new CouponBaseAdapter(); + $priceValidator = 42.50; $priceToValidate = 42.50; - $integerParam = new PriceParam($priceValidator, 'EUR'); + $integerParam = new PriceParam($adapter, $priceValidator, 'EUR'); $expected = 0; $actual = $integerParam->compareTo($priceToValidate); @@ -106,10 +112,12 @@ class PriceParamTest extends \PHPUnit_Framework_TestCase */ public function testSuperiorPrice() { + $adapter = new CouponBaseAdapter(); + $priceValidator = 42.50; $priceToValidate = 42.51; - $integerParam = new PriceParam($priceValidator, 'EUR'); + $integerParam = new PriceParam($adapter, $priceValidator, 'EUR'); $expected = -1; $actual = $integerParam->compareTo($priceToValidate); @@ -122,10 +130,12 @@ class PriceParamTest extends \PHPUnit_Framework_TestCase */ public function testInvalidArgumentException() { + $adapter = new CouponBaseAdapter(); + $priceValidator = 42.50; $priceToValidate = '42.50'; - $integerParam = new PriceParam($priceValidator, 'EUR'); + $integerParam = new PriceParam($adapter, $priceValidator, 'EUR'); $expected = 0; $actual = $integerParam->compareTo($priceToValidate); @@ -138,10 +148,12 @@ class PriceParamTest extends \PHPUnit_Framework_TestCase */ public function testInvalidArgumentException2() { + $adapter = new CouponBaseAdapter(); + $priceValidator = 42.50; $priceToValidate = -1; - $integerParam = new PriceParam($priceValidator, 'EUR'); + $integerParam = new PriceParam($adapter, $priceValidator, 'EUR'); $expected = 0; $actual = $integerParam->compareTo($priceToValidate); @@ -154,10 +166,12 @@ class PriceParamTest extends \PHPUnit_Framework_TestCase */ public function testInvalidArgumentException3() { + $adapter = new CouponBaseAdapter(); + $priceValidator = 42.50; $priceToValidate = 0; - $integerParam = new PriceParam($priceValidator, 'EUR'); + $integerParam = new PriceParam($adapter, $priceValidator, 'EUR'); $expected = 0; $actual = $integerParam->compareTo($priceToValidate); @@ -170,17 +184,40 @@ class PriceParamTest extends \PHPUnit_Framework_TestCase */ public function testInvalidArgumentException4() { + $adapter = new CouponBaseAdapter(); $priceValidator = 42.50; $priceToValidate = 1; - $integerParam = new PriceParam($priceValidator, 'EUR'); + $integerParam = new PriceParam($adapter, $priceValidator, 'GBP'); $expected = 0; $actual = $integerParam->compareTo($priceToValidate); $this->assertEquals($expected, $actual); } + /** + * Test is the object is serializable + * If no data is lost during the process + */ + protected function isSerializableTest() + { + $adapter = new CouponBaseAdapter(); + $priceValidator = 42.50; + $param = new PriceParam($adapter, $priceValidator, 'GBP'); + + $serialized = base64_encode(serialize($param)); + /** @var PriceParam $unserialized */ + $unserialized = base64_decode(serialize($serialized)); + + $this->assertEquals($param->getValue(), $unserialized->getValue()); + $this->assertEquals($param->getPrice(), $unserialized->getPrice()); + $this->assertEquals($param->getCurrency(), $unserialized->getCurrency()); + + $new = new PriceParam($adapter, $unserialized->getPrice(), $unserialized->getCurrency()); + $this->assertEquals($param->getPrice(), $new->getPrice()); + $this->assertEquals($param->getCurrency(), $new->getCurrency()); + } /** * Tears down the fixture, for example, closes a network connection. diff --git a/core/lib/Thelia/Tests/Coupon/Parameter/QuantityParamTest.php b/core/lib/Thelia/Tests/Constraint/Validator/QuantityParamTest.php similarity index 75% rename from core/lib/Thelia/Tests/Coupon/Parameter/QuantityParamTest.php rename to core/lib/Thelia/Tests/Constraint/Validator/QuantityParamTest.php index 17343121c..9543388f6 100644 --- a/core/lib/Thelia/Tests/Coupon/Parameter/QuantityParamTest.php +++ b/core/lib/Thelia/Tests/Constraint/Validator/QuantityParamTest.php @@ -24,7 +24,7 @@ namespace Thelia\Coupon; use InvalidArgumentException; -use Thelia\Coupon\Validator\QuantityParam; +use Thelia\Constraint\Validator\QuantityParam; /** * Created by JetBrains PhpStorm. @@ -33,7 +33,7 @@ use Thelia\Coupon\Validator\QuantityParam; * * Unit Test QuantityParam Class * - * @package Coupon + * @package Constraint * @author Guillaume MOREL * */ @@ -55,10 +55,11 @@ class QuantityParamTest extends \PHPUnit_Framework_TestCase */ public function testInferiorQuantity() { + $adapter = new CouponBaseAdapter(); $intValidator = 42; $intToValidate = 0; - $integerParam = new QuantityParam($intValidator); + $integerParam = new QuantityParam($adapter, $intValidator); $expected = 1; $actual = $integerParam->compareTo($intToValidate); @@ -72,10 +73,11 @@ class QuantityParamTest extends \PHPUnit_Framework_TestCase */ public function testInferiorQuantity2() { + $adapter = new CouponBaseAdapter(); $intValidator = 42; $intToValidate = 41; - $integerParam = new QuantityParam($intValidator); + $integerParam = new QuantityParam($adapter, $intValidator); $expected = 1; $actual = $integerParam->compareTo($intToValidate); @@ -89,10 +91,11 @@ class QuantityParamTest extends \PHPUnit_Framework_TestCase */ public function testEqualsQuantity() { + $adapter = new CouponBaseAdapter(); $intValidator = 42; $intToValidate = 42; - $integerParam = new QuantityParam($intValidator); + $integerParam = new QuantityParam($adapter, $intValidator); $expected = 0; $actual = $integerParam->compareTo($intToValidate); @@ -106,10 +109,11 @@ class QuantityParamTest extends \PHPUnit_Framework_TestCase */ public function testSuperiorQuantity() { + $adapter = new CouponBaseAdapter(); $intValidator = 42; $intToValidate = 43; - $integerParam = new QuantityParam($intValidator); + $integerParam = new QuantityParam($adapter, $intValidator); $expected = -1; $actual = $integerParam->compareTo($intToValidate); @@ -122,10 +126,11 @@ class QuantityParamTest extends \PHPUnit_Framework_TestCase */ public function testInvalidArgumentException() { + $adapter = new CouponBaseAdapter(); $intValidator = 42; $intToValidate = '42'; - $integerParam = new QuantityParam($intValidator); + $integerParam = new QuantityParam($adapter, $intValidator); $expected = 0; $actual = $integerParam->compareTo($intToValidate); @@ -138,17 +143,39 @@ class QuantityParamTest extends \PHPUnit_Framework_TestCase */ public function testInvalidArgumentException2() { + $adapter = new CouponBaseAdapter(); $intValidator = 42; $intToValidate = -1; - $integerParam = new QuantityParam($intValidator); + $integerParam = new QuantityParam($adapter, $intValidator); $expected = 0; $actual = $integerParam->compareTo($intToValidate); $this->assertEquals($expected, $actual); } + /** + * Test is the object is serializable + * If no data is lost during the process + */ + protected function isSerializableTest() + { + $adapter = new CouponBaseAdapter(); + $intValidator = 42; + $intToValidate = -1; + $param = new QuantityParam($adapter, $intValidator); + + $serialized = base64_encode(serialize($param)); + /** @var QuantityParam $unserialized */ + $unserialized = base64_decode(serialize($serialized)); + + $this->assertEquals($param->getValue(), $unserialized->getValue()); + $this->assertEquals($param->getInteger(), $unserialized->getInteger()); + + $new = new PriceParam($adapter, $unserialized->getInteger()); + $this->assertEquals($param->getInteger(), $new->getInteger()); + } /** * Tears down the fixture, for example, closes a network connection. diff --git a/core/lib/Thelia/Tests/Coupon/Parameter/RepeatedDateParamTest.php b/core/lib/Thelia/Tests/Constraint/Validator/RepeatedDateParamTest.php similarity index 78% rename from core/lib/Thelia/Tests/Coupon/Parameter/RepeatedDateParamTest.php rename to core/lib/Thelia/Tests/Constraint/Validator/RepeatedDateParamTest.php index 66c970a4f..5766cb6b1 100644 --- a/core/lib/Thelia/Tests/Coupon/Parameter/RepeatedDateParamTest.php +++ b/core/lib/Thelia/Tests/Constraint/Validator/RepeatedDateParamTest.php @@ -25,7 +25,7 @@ namespace Thelia\Coupon; use InvalidArgumentException; use Symfony\Component\Intl\Exception\NotImplementedException; -use Thelia\Coupon\Validator\RepeatedDateParam; +use Thelia\Constraint\Validator\RepeatedDateParam; /** * Created by JetBrains PhpStorm. @@ -34,7 +34,7 @@ use Thelia\Coupon\Validator\RepeatedDateParam; * * Unit Test RepeatedDateParam Class * - * @package Coupon + * @package Constraint * @author Guillaume MOREL * */ @@ -56,10 +56,11 @@ class RepeatedDateParamTest extends \PHPUnit_Framework_TestCase */ public function testInferiorDate() { + $adapter = new CouponBaseAdapter(); $startDateValidator = new \DateTime("2012-07-08"); $dateToValidate = new \DateTime("2012-07-07"); - $repeatedDateParam = new RepeatedDateParam(); + $repeatedDateParam = new RepeatedDateParam($adapter); $repeatedDateParam->setFrom($startDateValidator); $repeatedDateParam->repeatEveryMonth(); @@ -75,10 +76,11 @@ class RepeatedDateParamTest extends \PHPUnit_Framework_TestCase */ public function testEqualsDateRepeatEveryMonthOneTimeFirstPeriod() { + $adapter = new CouponBaseAdapter(); $startDateValidator = new \DateTime("2012-07-08"); $dateToValidate = new \DateTime("2012-07-08"); - $repeatedDateParam = new RepeatedDateParam(); + $repeatedDateParam = new RepeatedDateParam($adapter); $repeatedDateParam->setFrom($startDateValidator); $repeatedDateParam->repeatEveryMonth(); @@ -94,12 +96,13 @@ class RepeatedDateParamTest extends \PHPUnit_Framework_TestCase */ public function testEqualsDateRepeatEveryMonthOneTimeSecondPeriod() { + $adapter = new CouponBaseAdapter(); $startDateValidator = new \DateTime("2012-07-08"); $dateToValidate = new \DateTime("2012-08-08"); - $repeatedDateParam = new RepeatedDateParam(); + $repeatedDateParam = new RepeatedDateParam($adapter); $repeatedDateParam->setFrom($startDateValidator); - $repeatedDateParam->repeatEveryMonth(); + $repeatedDateParam->repeatEveryMonth(1, 1); $expected = 0; $actual = $repeatedDateParam->compareTo($dateToValidate); @@ -113,10 +116,11 @@ class RepeatedDateParamTest extends \PHPUnit_Framework_TestCase */ public function testEqualsDateRepeatEveryMonthTenTimesThirdPeriod() { + $adapter = new CouponBaseAdapter(); $startDateValidator = new \DateTime("2012-07-08"); $dateToValidate = new \DateTime("2012-09-08"); - $repeatedDateParam = new RepeatedDateParam(); + $repeatedDateParam = new RepeatedDateParam($adapter); $repeatedDateParam->setFrom($startDateValidator); $repeatedDateParam->repeatEveryMonth(1, 10); @@ -132,10 +136,11 @@ class RepeatedDateParamTest extends \PHPUnit_Framework_TestCase */ public function testEqualsDateRepeatEveryMonthTenTimesTensPeriod() { + $adapter = new CouponBaseAdapter(); $startDateValidator = new \DateTime("2012-07-08"); $dateToValidate = new \DateTime("2013-05-08"); - $repeatedDateParam = new RepeatedDateParam(); + $repeatedDateParam = new RepeatedDateParam($adapter); $repeatedDateParam->setFrom($startDateValidator); $repeatedDateParam->repeatEveryMonth(1, 10); @@ -151,10 +156,11 @@ class RepeatedDateParamTest extends \PHPUnit_Framework_TestCase */ public function testEqualsDateRepeatEveryFourMonthTwoTimesSecondPeriod() { + $adapter = new CouponBaseAdapter(); $startDateValidator = new \DateTime("2012-07-08"); $dateToValidate = new \DateTime("2012-11-08"); - $repeatedDateParam = new RepeatedDateParam(); + $repeatedDateParam = new RepeatedDateParam($adapter); $repeatedDateParam->setFrom($startDateValidator); $repeatedDateParam->repeatEveryMonth(4, 2); @@ -170,10 +176,11 @@ class RepeatedDateParamTest extends \PHPUnit_Framework_TestCase */ public function testEqualsDateRepeatEveryFourMonthTwoTimesLastPeriod() { + $adapter = new CouponBaseAdapter(); $startDateValidator = new \DateTime("2012-07-08"); $dateToValidate = new \DateTime("2013-03-08"); - $repeatedDateParam = new RepeatedDateParam(); + $repeatedDateParam = new RepeatedDateParam($adapter); $repeatedDateParam->setFrom($startDateValidator); $repeatedDateParam->repeatEveryMonth(4, 2); @@ -189,10 +196,11 @@ class RepeatedDateParamTest extends \PHPUnit_Framework_TestCase */ public function testNotEqualsDateRepeatEveryFourMonthTwoTimes1() { + $adapter = new CouponBaseAdapter(); $startDateValidator = new \DateTime("2012-07-08"); $dateToValidate = new \DateTime("2012-08-08"); - $repeatedDateParam = new RepeatedDateParam(); + $repeatedDateParam = new RepeatedDateParam($adapter); $repeatedDateParam->setFrom($startDateValidator); $repeatedDateParam->repeatEveryMonth(4, 2); @@ -208,10 +216,11 @@ class RepeatedDateParamTest extends \PHPUnit_Framework_TestCase */ public function testNotEqualsDateRepeatEveryFourMonthTwoTimes2() { + $adapter = new CouponBaseAdapter(); $startDateValidator = new \DateTime("2012-07-08"); $dateToValidate = new \DateTime("2012-12-08"); - $repeatedDateParam = new RepeatedDateParam(); + $repeatedDateParam = new RepeatedDateParam($adapter); $repeatedDateParam->setFrom($startDateValidator); $repeatedDateParam->repeatEveryMonth(4, 2); @@ -227,10 +236,11 @@ class RepeatedDateParamTest extends \PHPUnit_Framework_TestCase */ public function testSuperiorDateRepeatEveryFourMonthTwoTimes() { + $adapter = new CouponBaseAdapter(); $startDateValidator = new \DateTime("2012-07-08"); $dateToValidate = new \DateTime("2013-03-09"); - $repeatedDateParam = new RepeatedDateParam(); + $repeatedDateParam = new RepeatedDateParam($adapter); $repeatedDateParam->setFrom($startDateValidator); $repeatedDateParam->repeatEveryMonth(4, 2); @@ -245,17 +255,42 @@ class RepeatedDateParamTest extends \PHPUnit_Framework_TestCase */ public function testInvalidArgumentException() { + $adapter = new CouponBaseAdapter(); $startDateValidator = new \DateTime("2012-07-08"); $dateToValidate = 1377012588; - $repeatedDateParam = new RepeatedDateParam(); + $repeatedDateParam = new RepeatedDateParam($adapter); $repeatedDateParam->setFrom($startDateValidator); $repeatedDateParam->repeatEveryMonth(4, 2); $repeatedDateParam->compareTo($dateToValidate); } + /** + * Test is the object is serializable + * If no data is lost during the process + */ + protected function isSerializableTest() + { + $adapter = new CouponBaseAdapter(); + $startDateValidator = new \DateTime("2012-07-08"); + $param = new RepeatedDateParam($adapter); + $param->setFrom($startDateValidator); + $param->repeatEveryMonth(4, 2); + + $serialized = base64_encode(serialize($param)); + /** @var RepeatedDateParam $unserialized */ + $unserialized = base64_decode(serialize($serialized)); + + $this->assertEquals($param->getValue(), $unserialized->getValue()); + $this->assertEquals($param->getDatePeriod(), $unserialized->getDatePeriod()); + + $new = new RepeatedDateParam($adapter); + $new->setFrom($unserialized->getFrom()); + $new->repeatEveryMonth($unserialized->getFrequency(), $unserialized->getNbRepetition()); + $this->assertEquals($param->getDatePeriod(), $new->getDatePeriod()); + } /** * Tears down the fixture, for example, closes a network connection. diff --git a/core/lib/Thelia/Tests/Coupon/Parameter/RepeatedIntervalParamTest.php b/core/lib/Thelia/Tests/Constraint/Validator/RepeatedIntervalParamTest.php similarity index 80% rename from core/lib/Thelia/Tests/Coupon/Parameter/RepeatedIntervalParamTest.php rename to core/lib/Thelia/Tests/Constraint/Validator/RepeatedIntervalParamTest.php index dc7a1335d..22a105ffb 100644 --- a/core/lib/Thelia/Tests/Coupon/Parameter/RepeatedIntervalParamTest.php +++ b/core/lib/Thelia/Tests/Constraint/Validator/RepeatedIntervalParamTest.php @@ -24,7 +24,7 @@ namespace Thelia\Coupon; use Symfony\Component\Intl\Exception\NotImplementedException; -use Thelia\Coupon\Validator\RepeatedIntervalParam; +use Thelia\Constraint\Validator\RepeatedIntervalParam; /** * Created by JetBrains PhpStorm. @@ -33,7 +33,7 @@ use Thelia\Coupon\Validator\RepeatedIntervalParam; * * Unit Test RepeatedIntervalParam Class * - * @package Coupon + * @package Constraint * @author Guillaume MOREL * */ @@ -55,11 +55,12 @@ class RepeatedIntervalParamTest extends \PHPUnit_Framework_TestCase */ public function testInferiorDate() { + $adapter = new CouponBaseAdapter(); $startDateValidator = new \DateTime("2012-07-08"); $dateToValidate = new \DateTime("2012-07-07"); $duration = 10; - $RepeatedIntervalParam = new RepeatedIntervalParam(); + $RepeatedIntervalParam = new RepeatedIntervalParam($adapter); $RepeatedIntervalParam->setFrom($startDateValidator); $RepeatedIntervalParam->setDurationInDays($duration); @@ -77,11 +78,12 @@ class RepeatedIntervalParamTest extends \PHPUnit_Framework_TestCase */ public function testEqualsDateRepeatEveryMonthOneTimeFirstPeriodBeginning() { + $adapter = new CouponBaseAdapter(); $startDateValidator = new \DateTime("2012-07-08"); $dateToValidate = new \DateTime("2012-07-08"); $duration = 10; - $RepeatedIntervalParam = new RepeatedIntervalParam(); + $RepeatedIntervalParam = new RepeatedIntervalParam($adapter); $RepeatedIntervalParam->setFrom($startDateValidator); $RepeatedIntervalParam->setDurationInDays($duration); $RepeatedIntervalParam->repeatEveryMonth(); @@ -98,11 +100,12 @@ class RepeatedIntervalParamTest extends \PHPUnit_Framework_TestCase */ public function testEqualsDateRepeatEveryMonthOneTimeFirstPeriodMiddle() { + $adapter = new CouponBaseAdapter(); $startDateValidator = new \DateTime("2012-07-08"); $dateToValidate = new \DateTime("2012-07-13"); $duration = 10; - $RepeatedIntervalParam = new RepeatedIntervalParam(); + $RepeatedIntervalParam = new RepeatedIntervalParam($adapter); $RepeatedIntervalParam->setFrom($startDateValidator); $RepeatedIntervalParam->setDurationInDays($duration); $RepeatedIntervalParam->repeatEveryMonth(); @@ -119,11 +122,12 @@ class RepeatedIntervalParamTest extends \PHPUnit_Framework_TestCase */ public function testEqualsDateRepeatEveryMonthOneTimeFirstPeriodEnding() { + $adapter = new CouponBaseAdapter(); $startDateValidator = new \DateTime("2012-07-08"); $dateToValidate = new \DateTime("2012-07-18"); $duration = 10; - $RepeatedIntervalParam = new RepeatedIntervalParam(); + $RepeatedIntervalParam = new RepeatedIntervalParam($adapter); $RepeatedIntervalParam->setFrom($startDateValidator); $RepeatedIntervalParam->setDurationInDays($duration); $RepeatedIntervalParam->repeatEveryMonth(); @@ -140,11 +144,12 @@ class RepeatedIntervalParamTest extends \PHPUnit_Framework_TestCase */ public function testEqualsDateRepeatEveryMonthOneTimeSecondPeriodBeginning() { + $adapter = new CouponBaseAdapter(); $startDateValidator = new \DateTime("2012-08-08"); $dateToValidate = new \DateTime("2012-08-08"); $duration = 10; - $RepeatedIntervalParam = new RepeatedIntervalParam(); + $RepeatedIntervalParam = new RepeatedIntervalParam($adapter); $RepeatedIntervalParam->setFrom($startDateValidator); $RepeatedIntervalParam->setDurationInDays($duration); $RepeatedIntervalParam->repeatEveryMonth(); @@ -161,11 +166,12 @@ class RepeatedIntervalParamTest extends \PHPUnit_Framework_TestCase */ public function testEqualsDateRepeatEveryMonthOneTimeSecondPeriodMiddle() { + $adapter = new CouponBaseAdapter(); $startDateValidator = new \DateTime("2012-08-08"); $dateToValidate = new \DateTime("2012-08-13"); $duration = 10; - $RepeatedIntervalParam = new RepeatedIntervalParam(); + $RepeatedIntervalParam = new RepeatedIntervalParam($adapter); $RepeatedIntervalParam->setFrom($startDateValidator); $RepeatedIntervalParam->setDurationInDays($duration); $RepeatedIntervalParam->repeatEveryMonth(); @@ -182,11 +188,12 @@ class RepeatedIntervalParamTest extends \PHPUnit_Framework_TestCase */ public function testEqualsDateRepeatEveryMonthOneTimeSecondPeriodEnding() { + $adapter = new CouponBaseAdapter(); $startDateValidator = new \DateTime("2012-08-08"); $dateToValidate = new \DateTime("2012-08-18"); $duration = 10; - $RepeatedIntervalParam = new RepeatedIntervalParam(); + $RepeatedIntervalParam = new RepeatedIntervalParam($adapter); $RepeatedIntervalParam->setFrom($startDateValidator); $RepeatedIntervalParam->setDurationInDays($duration); $RepeatedIntervalParam->repeatEveryMonth(); @@ -201,13 +208,14 @@ class RepeatedIntervalParamTest extends \PHPUnit_Framework_TestCase * @covers Thelia\Coupon\Parameter\RepeatedIntervalParam::compareTo * */ - public function testEqualsDateRepeatEveryMonthFourTimeLastPeriodBegining() + public function testEqualsDateRepeatEveryMonthFourTimeLastPeriodBeginning() { + $adapter = new CouponBaseAdapter(); $startDateValidator = new \DateTime("2012-10-08"); $dateToValidate = new \DateTime("2012-10-08"); $duration = 10; - $RepeatedIntervalParam = new RepeatedIntervalParam(); + $RepeatedIntervalParam = new RepeatedIntervalParam($adapter); $RepeatedIntervalParam->setFrom($startDateValidator); $RepeatedIntervalParam->setDurationInDays($duration); $RepeatedIntervalParam->repeatEveryMonth(1, 4); @@ -224,11 +232,12 @@ class RepeatedIntervalParamTest extends \PHPUnit_Framework_TestCase */ public function testEqualsDateRepeatEveryMonthFourTimeLastPeriodMiddle() { + $adapter = new CouponBaseAdapter(); $startDateValidator = new \DateTime("2012-10-08"); $dateToValidate = new \DateTime("2012-10-13"); $duration = 10; - $RepeatedIntervalParam = new RepeatedIntervalParam(); + $RepeatedIntervalParam = new RepeatedIntervalParam($adapter); $RepeatedIntervalParam->setFrom($startDateValidator); $RepeatedIntervalParam->setDurationInDays($duration); $RepeatedIntervalParam->repeatEveryMonth(1, 4); @@ -245,11 +254,12 @@ class RepeatedIntervalParamTest extends \PHPUnit_Framework_TestCase */ public function testEqualsDateRepeatEveryMonthFourTimeLastPeriodEnding() { + $adapter = new CouponBaseAdapter(); $startDateValidator = new \DateTime("2012-10-08"); $dateToValidate = new \DateTime("2012-10-18"); $duration = 10; - $RepeatedIntervalParam = new RepeatedIntervalParam(); + $RepeatedIntervalParam = new RepeatedIntervalParam($adapter); $RepeatedIntervalParam->setFrom($startDateValidator); $RepeatedIntervalParam->setDurationInDays($duration); $RepeatedIntervalParam->repeatEveryMonth(1, 4); @@ -266,11 +276,12 @@ class RepeatedIntervalParamTest extends \PHPUnit_Framework_TestCase */ public function testNotEqualsDateRepeatEveryMonthFourTimeInTheBeginning() { + $adapter = new CouponBaseAdapter(); $startDateValidator = new \DateTime("2012-10-08"); $dateToValidate = new \DateTime("2012-07-19"); $duration = 10; - $RepeatedIntervalParam = new RepeatedIntervalParam(); + $RepeatedIntervalParam = new RepeatedIntervalParam($adapter); $RepeatedIntervalParam->setFrom($startDateValidator); $RepeatedIntervalParam->setDurationInDays($duration); $RepeatedIntervalParam->repeatEveryMonth(1, 4); @@ -287,11 +298,12 @@ class RepeatedIntervalParamTest extends \PHPUnit_Framework_TestCase */ public function testNotEqualsDateRepeatEveryMonthFourTimeInTheMiddle() { + $adapter = new CouponBaseAdapter(); $startDateValidator = new \DateTime("2012-10-08"); $dateToValidate = new \DateTime("2012-08-01"); $duration = 10; - $RepeatedIntervalParam = new RepeatedIntervalParam(); + $RepeatedIntervalParam = new RepeatedIntervalParam($adapter); $RepeatedIntervalParam->setFrom($startDateValidator); $RepeatedIntervalParam->setDurationInDays($duration); $RepeatedIntervalParam->repeatEveryMonth(1, 4); @@ -309,11 +321,12 @@ class RepeatedIntervalParamTest extends \PHPUnit_Framework_TestCase */ public function testNotEqualsDateRepeatEveryMonthFourTimeInTheEnd() { + $adapter = new CouponBaseAdapter(); $startDateValidator = new \DateTime("2012-10-08"); $dateToValidate = new \DateTime("2012-08-07"); $duration = 10; - $RepeatedIntervalParam = new RepeatedIntervalParam(); + $RepeatedIntervalParam = new RepeatedIntervalParam($adapter); $RepeatedIntervalParam->setFrom($startDateValidator); $RepeatedIntervalParam->setDurationInDays($duration); $RepeatedIntervalParam->repeatEveryMonth(1, 4); @@ -332,11 +345,12 @@ class RepeatedIntervalParamTest extends \PHPUnit_Framework_TestCase */ public function testSuperiorDateRepeatEveryMonthFourTime() { + $adapter = new CouponBaseAdapter(); $startDateValidator = new \DateTime("2012-07-08"); $dateToValidate = new \DateTime("2012-10-19"); $duration = 10; - $RepeatedIntervalParam = new RepeatedIntervalParam(); + $RepeatedIntervalParam = new RepeatedIntervalParam($adapter); $RepeatedIntervalParam->setFrom($startDateValidator); $RepeatedIntervalParam->setDurationInDays($duration); $RepeatedIntervalParam->repeatEveryMonth(1, 0); @@ -352,11 +366,12 @@ class RepeatedIntervalParamTest extends \PHPUnit_Framework_TestCase */ public function testInvalidArgumentException() { + $adapter = new CouponBaseAdapter(); $startDateValidator = new \DateTime("2012-07-08"); $dateToValidate = 1377012588; $duration = 10; - $RepeatedIntervalParam = new RepeatedIntervalParam(); + $RepeatedIntervalParam = new RepeatedIntervalParam($adapter); $RepeatedIntervalParam->setFrom($startDateValidator); $RepeatedIntervalParam->setDurationInDays($duration); $RepeatedIntervalParam->repeatEveryMonth(1, 4); @@ -364,6 +379,36 @@ class RepeatedIntervalParamTest extends \PHPUnit_Framework_TestCase $RepeatedIntervalParam->compareTo($dateToValidate); } + /** + * Test is the object is serializable + * If no data is lost during the process + */ + protected function isSerializableTest() + { + $adapter = new CouponBaseAdapter(); + $startDateValidator = new \DateTime("2012-07-08"); + $dateToValidate = 1377012588; + $duration = 10; + + $param = new RepeatedIntervalParam($adapter); + $param->setFrom($startDateValidator); + $param->setDurationInDays($duration); + $param->repeatEveryMonth(1, 4); + + $serialized = base64_encode(serialize($param)); + /** @var RepeatedIntervalParam $unserialized */ + $unserialized = base64_decode(serialize($serialized)); + + $this->assertEquals($param->getValue(), $unserialized->getValue()); + $this->assertEquals($param->getDatePeriod(), $unserialized->getDatePeriod()); + + $new = new RepeatedIntervalParam($adapter); + $new->setFrom($unserialized->getFrom()); + $new->repeatEveryMonth($unserialized->getFrequency(), $unserialized->getNbRepetition()); + $new->setDurationInDays($unserialized->getDurationInDays()); + $this->assertEquals($param->getDatePeriod(), $new->getDatePeriod()); + } + /** * Tears down the fixture, for example, closes a network connection. * This method is called after a test is executed. diff --git a/core/lib/Thelia/Tests/Coupon/CouponFactoryTest.php b/core/lib/Thelia/Tests/Coupon/CouponFactoryTest.php index c33b2327a..f3cf99054 100644 --- a/core/lib/Thelia/Tests/Coupon/CouponFactoryTest.php +++ b/core/lib/Thelia/Tests/Coupon/CouponFactoryTest.php @@ -23,10 +23,10 @@ namespace Thelia\Coupon; -use Thelia\Coupon\Validator\PriceParam; -use Thelia\Coupon\Validator\RuleValidator; -use Thelia\Coupon\Rule\AvailableForTotalAmount; -use Thelia\Coupon\Rule\Operators; +use Thelia\Constraint\Validator\PriceParam; +use Thelia\Constraint\Validator\RuleValidator; +use Thelia\Constraint\Rule\AvailableForTotalAmount; +use Thelia\Constraint\Rule\Operators; use Thelia\Coupon\Type\CouponInterface; use Thelia\Exception\CouponExpiredException; use Thelia\Model\Coupon; @@ -208,8 +208,7 @@ class CouponFactoryTest extends \PHPUnit_Framework_TestCase AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( Operators::SUPERIOR, new PriceParam( - 40.00, - 'EUR' + , 40.00, 'EUR' ) ) ) @@ -219,8 +218,7 @@ class CouponFactoryTest extends \PHPUnit_Framework_TestCase AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( Operators::INFERIOR, new PriceParam( - 400.00, - 'EUR' + , 400.00, 'EUR' ) ) ) diff --git a/core/lib/Thelia/Tests/Coupon/CouponManagerTest.php b/core/lib/Thelia/Tests/Coupon/CouponManagerTest.php index b5b9cefbe..08673746e 100644 --- a/core/lib/Thelia/Tests/Coupon/CouponManagerTest.php +++ b/core/lib/Thelia/Tests/Coupon/CouponManagerTest.php @@ -23,10 +23,10 @@ namespace Thelia\Coupon; -use Thelia\Coupon\Validator\PriceParam; -use Thelia\Coupon\Validator\RuleValidator; -use Thelia\Coupon\Rule\AvailableForTotalAmount; -use Thelia\Coupon\Rule\Operators; +use Thelia\Constraint\Validator\PriceParam; +use Thelia\Constraint\Validator\RuleValidator; +use Thelia\Constraint\Rule\AvailableForTotalAmount; +use Thelia\Constraint\Rule\Operators; use Thelia\Coupon\Type\CouponInterface; use Thelia\Coupon\Type\RemoveXAmount; use Thelia\Tools\PhpUnitUtils; @@ -103,6 +103,7 @@ Sed facilisis pellentesque nisl, eu tincidunt erat scelerisque a. Nullam malesua */ public function testGetDiscountTwoCoupon() { + $adapter = new CouponBaseAdapter(); $cartTotalPrice = 100.00; $checkoutTotalPrice = 120.00; @@ -113,8 +114,7 @@ Sed facilisis pellentesque nisl, eu tincidunt erat scelerisque a. Nullam malesua AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( Operators::SUPERIOR, new PriceParam( - 40.00, - 'EUR' + $adapter, 40.00, 'EUR' ) ) ) @@ -143,6 +143,7 @@ Sed facilisis pellentesque nisl, eu tincidunt erat scelerisque a. Nullam malesua */ public function testGetDiscountAlwaysInferiorToPrice() { + $adapter = new CouponBaseAdapter(); $cartTotalPrice = 21.00; $checkoutTotalPrice = 26.00; @@ -151,8 +152,7 @@ Sed facilisis pellentesque nisl, eu tincidunt erat scelerisque a. Nullam malesua AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( Operators::SUPERIOR, new PriceParam( - 20.00, - 'EUR' + $adapter, 20.00, 'EUR' ) ) ) @@ -180,6 +180,7 @@ Sed facilisis pellentesque nisl, eu tincidunt erat scelerisque a. Nullam malesua */ public function testIsCouponRemovingPostage() { + $adapter = new CouponBaseAdapter(); $cartTotalPrice = 21.00; $checkoutTotalPrice = 27.00; @@ -188,8 +189,7 @@ Sed facilisis pellentesque nisl, eu tincidunt erat scelerisque a. Nullam malesua AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( Operators::SUPERIOR, new PriceParam( - 20.00, - 'EUR' + $adapter, 20.00, 'EUR' ) ) ) @@ -640,13 +640,13 @@ Sed facilisis pellentesque nisl, eu tincidunt erat scelerisque a. Nullam malesua */ public static function generateValidRules() { + $adapter = new CouponBaseAdapter(); $rule1 = new AvailableForTotalAmount( array( AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( Operators::SUPERIOR, new PriceParam( - 40.00, - 'EUR' + $adapter, 40.00, 'EUR' ) ) ) @@ -656,8 +656,7 @@ Sed facilisis pellentesque nisl, eu tincidunt erat scelerisque a. Nullam malesua AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( Operators::INFERIOR, new PriceParam( - 400.00, - 'EUR' + $adapter, 400.00, 'EUR' ) ) ) @@ -754,6 +753,7 @@ Sed facilisis pellentesque nisl, eu tincidunt erat scelerisque a. Nullam malesua $isAvailableOnSpecialOffers = null, $maxUsage = null ) { + $adapter = new CouponBaseAdapter(); if ($code === null) { $code = self::VALID_CODE; } @@ -790,7 +790,7 @@ Sed facilisis pellentesque nisl, eu tincidunt erat scelerisque a. Nullam malesua $expirationDate->setTimestamp(strtotime("today + 2 months")); } - $coupon = new RemoveXAmount($code, $title, $shortDescription, $description, $amount, $isCumulative, $isRemovingPostage, $isAvailableOnSpecialOffers, $isEnabled, $maxUsage, $expirationDate); + $coupon = new RemoveXAmount($adapter, $code, $title, $shortDescription, $description, $amount, $isCumulative, $isRemovingPostage, $isAvailableOnSpecialOffers, $isEnabled, $maxUsage, $expirationDate); if ($rules === null) { $rules = self::generateValidRules(); diff --git a/core/lib/Thelia/Tests/Coupon/CouponRuleCollectionTest.php b/core/lib/Thelia/Tests/Coupon/CouponRuleCollectionTest.php index e399b6f37..eab22758b 100644 --- a/core/lib/Thelia/Tests/Coupon/CouponRuleCollectionTest.php +++ b/core/lib/Thelia/Tests/Coupon/CouponRuleCollectionTest.php @@ -23,10 +23,10 @@ namespace Thelia\Coupon; -use Thelia\Coupon\Validator\PriceParam; -use Thelia\Coupon\Validator\RuleValidator; -use Thelia\Coupon\Rule\AvailableForTotalAmount; -use Thelia\Coupon\Rule\Operators; +use Thelia\Constraint\Validator\PriceParam; +use Thelia\Constraint\Validator\RuleValidator; +use Thelia\Constraint\Rule\AvailableForTotalAmount; +use Thelia\Constraint\Rule\Operators; /** * Created by JetBrains PhpStorm. @@ -51,8 +51,7 @@ class CouponRuleCollectionTest extends \PHPUnit_Framework_TestCase AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( Operators::SUPERIOR, new PriceParam( - 40.00, - 'EUR' + , 40.00, 'EUR' ) ) ) @@ -62,8 +61,7 @@ class CouponRuleCollectionTest extends \PHPUnit_Framework_TestCase AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( Operators::INFERIOR, new PriceParam( - 400.00, - 'EUR' + , 400.00, 'EUR' ) ) ) diff --git a/core/lib/Thelia/Tests/Coupon/Type/RemoveXAmountTest.php b/core/lib/Thelia/Tests/Coupon/Type/RemoveXAmountTest.php index 72313f9ec..69e01549f 100644 --- a/core/lib/Thelia/Tests/Coupon/Type/RemoveXAmountTest.php +++ b/core/lib/Thelia/Tests/Coupon/Type/RemoveXAmountTest.php @@ -23,10 +23,10 @@ namespace Thelia\Coupon; -use Thelia\Coupon\Validator\PriceParam; -use Thelia\Coupon\Validator\RuleValidator; -use Thelia\Coupon\Rule\AvailableForTotalAmount; -use Thelia\Coupon\Rule\Operators; +use Thelia\Constraint\Validator\PriceParam; +use Thelia\Constraint\Validator\RuleValidator; +use Thelia\Constraint\Rule\AvailableForTotalAmount; +use Thelia\Constraint\Rule\Operators; use Thelia\Coupon\Type\RemoveXAmount; require_once '../CouponManagerTest.php'; @@ -54,6 +54,7 @@ class RemoveXAmountTest extends \PHPUnit_Framework_TestCase } /** + * Test if a Coupon is well displayed * * @covers Thelia\Coupon\type\RemoveXAmount::getCode * @covers Thelia\Coupon\type\RemoveXAmount::getTitle @@ -83,13 +84,13 @@ class RemoveXAmountTest extends \PHPUnit_Framework_TestCase } /** + * Test if a Coupon can be Cumulative * * @covers Thelia\Coupon\type\RemoveXAmount::isCumulative * */ public function testIsCumulative() { - $coupon = CouponManagerTest::generateValidCoupon(null, null, null, null, null, null, null, null, true, true); $actual = $coupon->isCumulative(); @@ -97,13 +98,13 @@ class RemoveXAmountTest extends \PHPUnit_Framework_TestCase } /** + * Test if a Coupon can be non cumulative * * @covers Thelia\Coupon\type\RemoveXAmount::isCumulative * */ public function testIsNotCumulative() { - $coupon = CouponManagerTest::generateValidCoupon(null, null, null, null, null, null, null, null, false, false); $actual = $coupon->isCumulative(); @@ -112,6 +113,7 @@ class RemoveXAmountTest extends \PHPUnit_Framework_TestCase /** + * Test if a Coupon can remove postage * * @covers Thelia\Coupon\type\RemoveXAmount::isRemovingPostage * @@ -125,13 +127,12 @@ class RemoveXAmountTest extends \PHPUnit_Framework_TestCase } /** + * Test if a Coupon won't remove postage if not set to * * @covers Thelia\Coupon\type\RemoveXAmount::isRemovingPostage - * */ public function testIsNotRemovingPostage() { - $coupon = CouponManagerTest::generateValidCoupon(null, null, null, null, null, null, null, null, false, false); $actual = $coupon->isRemovingPostage(); @@ -140,25 +141,25 @@ class RemoveXAmountTest extends \PHPUnit_Framework_TestCase /** + * Test if a Coupon has the effect expected (discount 10euros) * * @covers Thelia\Coupon\type\RemoveXAmount::getEffect - * */ public function testGetEffect() { - + $adapter = new CouponBaseAdapter(); $coupon = CouponManagerTest::generateValidCoupon(null, null, null, null, null, null, null, null, false, false); $expected = 10; - $actual = $coupon->getDiscount(); + $actual = $coupon->getDiscount($adapter); $this->assertEquals($expected, $actual); } /** + * Test Coupon rule setter * * @covers Thelia\Coupon\type\RemoveXAmount::setRules * @covers Thelia\Coupon\type\RemoveXAmount::getRules - * */ public function testSetRulesValid() { @@ -187,6 +188,7 @@ class RemoveXAmountTest extends \PHPUnit_Framework_TestCase } /** + * Test Coupon rule setter * * @covers Thelia\Coupon\type\RemoveXAmount::setRules * @expectedException \Thelia\Exception\InvalidRuleException @@ -212,6 +214,7 @@ class RemoveXAmountTest extends \PHPUnit_Framework_TestCase } /** + * Test Coupon effect for rule Total Amount < 400 * * @covers Thelia\Coupon\type\RemoveXAmount::getEffect * @@ -219,6 +222,7 @@ class RemoveXAmountTest extends \PHPUnit_Framework_TestCase public function testGetEffectIfTotalAmountInferiorTo400Valid() { // Given + $adapter = new CouponBaseAdapter(); $rule0 = $this->generateValidRuleAvailableForTotalAmountOperatorTo( Operators::INFERIOR, 400.00 @@ -230,11 +234,12 @@ class RemoveXAmountTest extends \PHPUnit_Framework_TestCase // Then $expected = 10.00; - $actual = $coupon->getDiscount(); + $actual = $coupon->getDiscount($adapter); $this->assertEquals($expected, $actual); } /** + * Test Coupon effect for rule Total Amount <= 400 * * @covers Thelia\Coupon\type\RemoveXAmount::getEffect * @@ -242,6 +247,7 @@ class RemoveXAmountTest extends \PHPUnit_Framework_TestCase public function testGetEffectIfTotalAmountInferiorOrEqualTo400Valid() { // Given + $adapter = new CouponBaseAdapter(); $rule0 = $this->generateValidRuleAvailableForTotalAmountOperatorTo( Operators::INFERIOR_OR_EQUAL, 400.00 @@ -253,11 +259,12 @@ class RemoveXAmountTest extends \PHPUnit_Framework_TestCase // Then $expected = 10.00; - $actual = $coupon->getDiscount(); + $actual = $coupon->getDiscount($adapter); $this->assertEquals($expected, $actual); } /** + * Test Coupon effect for rule Total Amount == 400 * * @covers Thelia\Coupon\type\RemoveXAmount::getEffect * @@ -265,6 +272,7 @@ class RemoveXAmountTest extends \PHPUnit_Framework_TestCase public function testGetEffectIfTotalAmountEqualTo400Valid() { // Given + $adapter = new CouponBaseAdapter(); $rule0 = $this->generateValidRuleAvailableForTotalAmountOperatorTo( Operators::EQUAL, 400.00 @@ -276,11 +284,12 @@ class RemoveXAmountTest extends \PHPUnit_Framework_TestCase // Then $expected = 10.00; - $actual = $coupon->getDiscount(); + $actual = $coupon->getDiscount($adapter); $this->assertEquals($expected, $actual); } /** + * Test Coupon effect for rule Total Amount >= 400 * * @covers Thelia\Coupon\type\RemoveXAmount::getEffect * @@ -288,6 +297,7 @@ class RemoveXAmountTest extends \PHPUnit_Framework_TestCase public function testGetEffectIfTotalAmountSuperiorOrEqualTo400Valid() { // Given + $adapter = new CouponBaseAdapter(); $rule0 = $this->generateValidRuleAvailableForTotalAmountOperatorTo( Operators::SUPERIOR_OR_EQUAL, 400.00 @@ -299,11 +309,12 @@ class RemoveXAmountTest extends \PHPUnit_Framework_TestCase // Then $expected = 10.00; - $actual = $coupon->getDiscount(); + $actual = $coupon->getDiscount($adapter); $this->assertEquals($expected, $actual); } /** + * Test Coupon effect for rule Total Amount > 400 * * @covers Thelia\Coupon\type\RemoveXAmount::getEffect * @@ -311,6 +322,7 @@ class RemoveXAmountTest extends \PHPUnit_Framework_TestCase public function testGetEffectIfTotalAmountSuperiorTo400Valid() { // Given + $adapter = new CouponBaseAdapter(); $rule0 = $this->generateValidRuleAvailableForTotalAmountOperatorTo( Operators::SUPERIOR, 400.00 @@ -322,7 +334,7 @@ class RemoveXAmountTest extends \PHPUnit_Framework_TestCase // Then $expected = 10.00; - $actual = $coupon->getDiscount(); + $actual = $coupon->getDiscount($adapter); $this->assertEquals($expected, $actual); } @@ -347,10 +359,12 @@ class RemoveXAmountTest extends \PHPUnit_Framework_TestCase */ protected function generateValidRuleAvailableForTotalAmountOperatorTo($operator, $amount) { + $adapter = new CouponBaseAdapter(); $validators = array( AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( $operator, new PriceParam( + $adapter, $amount, 'EUR' ) diff --git a/core/lib/Thelia/Tests/Coupon/Type/RemoveXPercentTest.php b/core/lib/Thelia/Tests/Coupon/Type/RemoveXPercentTest.php index 5d110d3df..5b80ac1a3 100644 --- a/core/lib/Thelia/Tests/Coupon/Type/RemoveXPercentTest.php +++ b/core/lib/Thelia/Tests/Coupon/Type/RemoveXPercentTest.php @@ -24,8 +24,15 @@ namespace Thelia\Coupon; use PHPUnit_Framework_TestCase; +use Thelia\Constraint\Rule\AvailableForTotalAmount; +use Thelia\Constraint\Rule\Operators; +use Thelia\Constraint\Validator\PriceParam; +use Thelia\Constraint\Validator\RuleValidator; +use Thelia\Coupon\Type\CouponInterface; use Thelia\Coupon\Type\RemoveXPercent; +require_once '../CouponManagerTest.php'; + /** * Created by JetBrains PhpStorm. * Date: 8/19/13 @@ -48,92 +55,29 @@ class RemoveXPercentTest extends \PHPUnit_Framework_TestCase { } - - protected function generateValidCumulativeRemovingPostageCoupon() - { - $coupon = new RemoveXPercent( - self::VALID_COUPON_CODE, - self::VALID_COUPON_TITLE, - self::VALID_COUPON_SHORT_DESCRIPTION, - self::VALID_COUPON_DESCRIPTION, - 30.00, - true, - true - ); - - return $coupon; - } - - protected function generateValidNonCumulativeNonRemovingPostageCoupon() - { - $coupon = new RemoveXPercent( - self::VALID_COUPON_CODE, - self::VALID_COUPON_TITLE, - self::VALID_COUPON_SHORT_DESCRIPTION, - self::VALID_COUPON_DESCRIPTION, - 30.00, - false, - false - ); - - return $coupon; - } - /** + * Test if a Coupon can be Cumulative * - * @covers Thelia\Coupon\Type\RemoveXPercent::getCode - * @covers Thelia\Coupon\Type\RemoveXPercent::getTitle - * @covers Thelia\Coupon\Type\RemoveXPercent::getShortDescription - * @covers Thelia\Coupon\Type\RemoveXPercent::getDescription - * - */ - public function testDisplay() - { - - $coupon = $this->generateValidCumulativeRemovingPostageCoupon(); - - $expected = self::VALID_COUPON_CODE; - $actual = $coupon->getCode(); - $this->assertEquals($expected, $actual); - - $expected = self::VALID_COUPON_TITLE; - $actual = $coupon->getTitle(); - $this->assertEquals($expected, $actual); - - $expected = self::VALID_COUPON_SHORT_DESCRIPTION; - $actual = $coupon->getShortDescription(); - $this->assertEquals($expected, $actual); - - $expected = self::VALID_COUPON_DESCRIPTION; - $actual = $coupon->getDescription(); - $this->assertEquals($expected, $actual); - - } - - - /** - * - * @covers Thelia\Coupon\Type\RemoveXPercent::isCumulative + * @covers Thelia\Coupon\type\RemoveXAmount::isCumulative * */ public function testIsCumulative() { - - $coupon = $this->generateValidCumulativeRemovingPostageCoupon(); + $coupon = self::generateValidCoupon(null, null, null, null, null, null, null, null, true, true); $actual = $coupon->isCumulative(); $this->assertTrue($actual); } /** + * Test if a Coupon can be non cumulative * - * @covers Thelia\Coupon\Type\RemoveXPercent::isCumulative + * @covers Thelia\Coupon\type\RemoveXAmount::isCumulative * */ public function testIsNotCumulative() { - - $coupon = $this->generateValidNonCumulativeNonRemovingPostageCoupon(); + $coupon = self::generateValidCoupon(null, null, null, null, null, null, null, null, false, false); $actual = $coupon->isCumulative(); $this->assertFalse($actual); @@ -141,28 +85,27 @@ class RemoveXPercentTest extends \PHPUnit_Framework_TestCase /** + * Test if a Coupon can remove postage * - * @covers Thelia\Coupon\Type\RemoveXPercent::isRemovingPostage + * @covers Thelia\Coupon\type\RemoveXAmount::isRemovingPostage * */ public function testIsRemovingPostage() { - - $coupon = $this->generateValidCumulativeRemovingPostageCoupon(); + $coupon = self::generateValidCoupon(null, null, null, null, null, null, null, null, true, true); $actual = $coupon->isRemovingPostage(); $this->assertTrue($actual); } /** + * Test if a Coupon won't remove postage if not set to * - * @covers Thelia\Coupon\Type\RemoveXPercent::isRemovingPostage - * + * @covers Thelia\Coupon\type\RemoveXAmount::isRemovingPostage */ public function testIsNotRemovingPostage() { - - $coupon = $this->generateValidNonCumulativeNonRemovingPostageCoupon(); + $coupon = self::generateValidCoupon(null, null, null, null, null, null, null, null, false, false); $actual = $coupon->isRemovingPostage(); $this->assertFalse($actual); @@ -170,20 +113,337 @@ class RemoveXPercentTest extends \PHPUnit_Framework_TestCase /** + * Test if a Coupon has the effect expected (discount 10euros) * - * @covers Thelia\Coupon\Type\RemoveXPercent::getDiscount - * + * @covers Thelia\Coupon\type\RemoveXAmount::getEffect */ public function testGetEffect() { + $adapter = $this->generateFakeAdapter(245); + $coupon = self::generateValidCoupon(null, null, null, null, null, null, null, null, false, false); - $coupon = $this->generateValidNonCumulativeNonRemovingPostageCoupon(); - - $expected = -30.00; - $actual = $coupon->getDiscount(); + $expected = 24.50; + $actual = $coupon->getDiscount($adapter); $this->assertEquals($expected, $actual); } + /** + * Test Coupon rule setter + * + * @covers Thelia\Coupon\type\RemoveXAmount::setRules + * @covers Thelia\Coupon\type\RemoveXAmount::getRules + */ + public function testSetRulesValid() + { + // Given + $rule0 = $this->generateValidRuleAvailableForTotalAmountOperatorTo( + Operators::EQUAL, + 20.00 + ); + $rule1 = $this->generateValidRuleAvailableForTotalAmountOperatorTo( + Operators::INFERIOR, + 100.23 + ); + $rule2 = $this->generateValidRuleAvailableForTotalAmountOperatorTo( + Operators::SUPERIOR, + 421.23 + ); + + $coupon = self::generateValidCoupon(null, null, null, null, null, null, null, null, false, false); + + // When + $coupon->setRules(new CouponRuleCollection(array($rule0, $rule1, $rule2))); + + // Then + $expected = 3; + $this->assertCount($expected, $coupon->getRules()->getRules()); + } + + /** + * Test Coupon rule setter + * + * @covers Thelia\Coupon\type\RemoveXAmount::setRules + * @expectedException \Thelia\Exception\InvalidRuleException + * + */ + public function testSetRulesInvalid() + { + // Given + $rule0 = $this->generateValidRuleAvailableForTotalAmountOperatorTo( + Operators::EQUAL, + 20.00 + ); + $rule1 = $this->generateValidRuleAvailableForTotalAmountOperatorTo( + Operators::INFERIOR, + 100.23 + ); + $rule2 = $this; + + $coupon = self::generateValidCoupon(null, null, null, null, null, null, null, null, false, false); + + // When + $coupon->setRules(new CouponRuleCollection(array($rule0, $rule1, $rule2))); + } + + /** + * Test Coupon effect for rule Total Amount < 400 + * + * @covers Thelia\Coupon\type\RemoveXAmount::getEffect + * + */ + public function testGetEffectIfTotalAmountInferiorTo400Valid() + { + // Given + $adapter = $this->generateFakeAdapter(245); + $rule0 = $this->generateValidRuleAvailableForTotalAmountOperatorTo( + Operators::INFERIOR, + 400.00 + ); + $coupon = self::generateValidCoupon(null, null, null, null, null, null, null, null, false, false); + + // When + $coupon->setRules(new CouponRuleCollection(array($rule0))); + + // Then + $expected = 24.50; + $actual = $coupon->getDiscount($adapter); + $this->assertEquals($expected, $actual); + } + + /** + * Test Coupon effect for rule Total Amount <= 400 + * + * @covers Thelia\Coupon\type\RemoveXAmount::getEffect + * + */ + public function testGetEffectIfTotalAmountInferiorOrEqualTo400Valid() + { + // Given + $adapter = $this->generateFakeAdapter(245); + $rule0 = $this->generateValidRuleAvailableForTotalAmountOperatorTo( + Operators::INFERIOR_OR_EQUAL, + 400.00 + ); + $coupon = self::generateValidCoupon(null, null, null, null, null, null, null, null, false, false); + + // When + $coupon->setRules(new CouponRuleCollection(array($rule0))); + + // Then + $expected = 24.50; + $actual = $coupon->getDiscount($adapter); + $this->assertEquals($expected, $actual); + } + + /** + * Test Coupon effect for rule Total Amount == 400 + * + * @covers Thelia\Coupon\type\RemoveXAmount::getEffect + * + */ + public function testGetEffectIfTotalAmountEqualTo400Valid() + { + // Given + $adapter = $this->generateFakeAdapter(245); + $rule0 = $this->generateValidRuleAvailableForTotalAmountOperatorTo( + Operators::EQUAL, + 400.00 + ); + $coupon = self::generateValidCoupon(null, null, null, null, null, null, null, null, false, false); + + // When + $coupon->setRules(new CouponRuleCollection(array($rule0))); + + // Then + $expected = 24.50; + $actual = $coupon->getDiscount($adapter); + $this->assertEquals($expected, $actual); + } + + /** + * Test Coupon effect for rule Total Amount >= 400 + * + * @covers Thelia\Coupon\type\RemoveXAmount::getEffect + * + */ + public function testGetEffectIfTotalAmountSuperiorOrEqualTo400Valid() + { + // Given + $adapter = $this->generateFakeAdapter(245); + $rule0 = $this->generateValidRuleAvailableForTotalAmountOperatorTo( + Operators::SUPERIOR_OR_EQUAL, + 400.00 + ); + $coupon = self::generateValidCoupon(null, null, null, null, null, null, null, null, false, false); + + // When + $coupon->setRules(new CouponRuleCollection(array($rule0))); + + // Then + $expected = 24.50; + $actual = $coupon->getDiscount($adapter); + $this->assertEquals($expected, $actual); + } + + /** + * Test Coupon effect for rule Total Amount > 400 + * + * @covers Thelia\Coupon\type\RemoveXAmount::getEffect + * + */ + public function testGetEffectIfTotalAmountSuperiorTo400Valid() + { + // Given + $adapter = $this->generateFakeAdapter(245); + $rule0 = $this->generateValidRuleAvailableForTotalAmountOperatorTo( + Operators::SUPERIOR, + 400.00 + ); + $coupon = self::generateValidCoupon(null, null, null, null, null, null, null, null, false, false); + + // When + $coupon->setRules(new CouponRuleCollection(array($rule0))); + + // Then + $expected = 24.50; + $actual = $coupon->getDiscount($adapter); + $this->assertEquals($expected, $actual); + } + + /** + * Generate valid CouponInterface + * + * @param string $code Coupon Code + * @param string $title Coupon Title + * @param string $shortDescription Coupon short + * description + * @param string $description Coupon description + * @param float $amount Coupon discount + * @param bool $isEnabled Is Coupon enabled + * @param \DateTime $expirationDate Coupon expiration date + * @param CouponRuleCollection $rules Coupon rules + * @param bool $isCumulative If is cumulative + * @param bool $isRemovingPostage If is removing postage + * @param bool $isAvailableOnSpecialOffers If is available on + * special offers or not + * @param int $maxUsage How many time a Coupon + * can be used + * + * @return CouponInterface + */ + public static function generateValidCoupon( + $code = null, + $title = null, + $shortDescription = null, + $description = null, + $percent = null, + $isEnabled = null, + $expirationDate = null, + $rules = null, + $isCumulative = null, + $isRemovingPostage = null, + $isAvailableOnSpecialOffers = null, + $maxUsage = null + ) { + $adapter = new CouponBaseAdapter(); + if ($code === null) { + $code = CouponManagerTest::VALID_CODE; + } + if ($title === null) { + $title = CouponManagerTest::VALID_TITLE; + } + if ($shortDescription === null) { + $shortDescription = CouponManagerTest::VALID_SHORT_DESCRIPTION; + } + if ($description === null) { + $description = CouponManagerTest::VALID_DESCRIPTION; + } + if ($percent === null) { + $percent = 10.00; + } + if ($isEnabled === null) { + $isEnabled = true; + } + if ($isCumulative === null) { + $isCumulative = true; + } + if ($isRemovingPostage === null) { + $isRemovingPostage = false; + } + if ($isAvailableOnSpecialOffers === null) { + $isAvailableOnSpecialOffers = true; + } + if ($maxUsage === null) { + $maxUsage = 40; + } + + if ($expirationDate === null) { + $expirationDate = new \DateTime(); + $expirationDate->setTimestamp(strtotime("today + 2 months")); + } + + $coupon = new RemoveXPercent($adapter, $code, $title, $shortDescription, $description, $percent, $isCumulative, $isRemovingPostage, $isAvailableOnSpecialOffers, $isEnabled, $maxUsage, $expirationDate); + + if ($rules === null) { + $rules = CouponManagerTest::generateValidRules(); + } + + $coupon->setRules($rules); + + return $coupon; + } + + + /** + * Generate valid rule AvailableForTotalAmount + * according to given operator and amount + * + * @param string $operator Operators::CONST + * @param float $amount Amount with 2 decimals + * + * @return AvailableForTotalAmount + */ + protected function generateValidRuleAvailableForTotalAmountOperatorTo($operator, $amount) + { + $adapter = new CouponBaseAdapter(); + $validators = array( + AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( + $operator, + new PriceParam( + $adapter, + $amount, + 'EUR' + ) + ) + ); + + return new AvailableForTotalAmount($validators); + } + + /** + * Generate a fake Adapter + * + * @param float $cartTotalPrice Cart total price + * + * @return \PHPUnit_Framework_MockObject_MockObject + */ + public function generateFakeAdapter($cartTotalPrice) + { + $stubCouponBaseAdapter = $this->getMock( + 'Thelia\Coupon\CouponBaseAdapter', + array( + 'getCartTotalPrice' + ), + array() + ); + + $stubCouponBaseAdapter->expects($this->any()) + ->method('getCartTotalPrice') + ->will($this->returnValue(($cartTotalPrice))); + + return $stubCouponBaseAdapter; + } + /** * Tears down the fixture, for example, closes a network connection. * This method is called after a test is executed.