From e78ba1f6701881871d13ad1325c6fd26fb1fc1e9 Mon Sep 17 00:00:00 2001 From: gmorel Date: Sun, 8 Sep 2013 17:36:21 +0200 Subject: [PATCH] WIP - Refactor Coupon : is Customer matching Coupon rules --- core/lib/Thelia/Config/Resources/config.xml | 4 +- ...raintManager.php => ConstraintFactory.php} | 56 +- .../Thelia/Constraint/ConstraintValidator.php | 133 +++ ...php => AvailableForTotalAmountManager.php} | 282 +++++-- ...s.php => AvailableForXArticlesManager.php} | 173 ++-- .../Constraint/Rule/CouponRuleAbstract.php | 141 ++-- .../Constraint/Rule/CouponRuleInterface.php | 57 +- core/lib/Thelia/Constraint/Rule/Operators.php | 128 +-- core/lib/Thelia/Coupon/CouponFactory.php | 22 +- ...agerTest.php => ConstraintFactoryTest.php} | 137 ++-- .../Constraint/ConstraintValidatorTest.php | 337 ++++++++ .../Rule/AvailableForTotalAmountTest.php | 763 ++++++++++++------ .../Rule/AvailableForXArticlesTest.php | 698 ++++++++++------ .../Tests/Constraint/Rule/OperatorsTest.php | 718 ++++++++-------- 14 files changed, 2415 insertions(+), 1234 deletions(-) rename core/lib/Thelia/Constraint/{ConstraintManager.php => ConstraintFactory.php} (84%) create mode 100644 core/lib/Thelia/Constraint/ConstraintValidator.php rename core/lib/Thelia/Constraint/Rule/{AvailableForTotalAmount.php => AvailableForTotalAmountManager.php} (51%) rename core/lib/Thelia/Constraint/Rule/{AvailableForXArticles.php => AvailableForXArticlesManager.php} (62%) rename core/lib/Thelia/Tests/Constraint/{ConstraintManagerTest.php => ConstraintFactoryTest.php} (59%) create mode 100644 core/lib/Thelia/Tests/Constraint/ConstraintValidatorTest.php diff --git a/core/lib/Thelia/Config/Resources/config.xml b/core/lib/Thelia/Config/Resources/config.xml index 70eeb6ba8..3b23901cc 100755 --- a/core/lib/Thelia/Config/Resources/config.xml +++ b/core/lib/Thelia/Config/Resources/config.xml @@ -220,11 +220,11 @@ - + - + diff --git a/core/lib/Thelia/Constraint/ConstraintManager.php b/core/lib/Thelia/Constraint/ConstraintFactory.php similarity index 84% rename from core/lib/Thelia/Constraint/ConstraintManager.php rename to core/lib/Thelia/Constraint/ConstraintFactory.php index 4505229b2..699459847 100644 --- a/core/lib/Thelia/Constraint/ConstraintManager.php +++ b/core/lib/Thelia/Constraint/ConstraintFactory.php @@ -25,9 +25,7 @@ namespace Thelia\Constraint; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\Serializer\Encoder\JsonEncoder; -use Symfony\Component\Serializer\Encoder\XmlEncoder; -use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer; -use Symfony\Component\Serializer\Serializer; +use Thelia\Constraint\Rule\AvailableForTotalAmountManager; use Thelia\Constraint\Rule\CouponRuleInterface; use Thelia\Constraint\Rule\SerializableRule; use Thelia\Coupon\CouponAdapterInterface; @@ -45,7 +43,7 @@ use Thelia\Coupon\CouponRuleCollection; * @author Guillaume MOREL * */ -class ConstraintManager +class ConstraintFactory { /** @var ContainerInterface Service Container */ protected $container = null; @@ -67,28 +65,6 @@ class ConstraintManager $this->adapter = $container->get('thelia.adapter'); } - /** - * Check if the current Coupon is matching its conditions (Rules) - * Thelia variables are given by the CouponAdapterInterface - * - * @param CouponRuleCollection $collection A collection of rules - * - * @return bool - */ - public function isMatching(CouponRuleCollection $collection) - { - $isMatching = true; - - /** @var CouponRuleInterface $rule */ - foreach ($collection->getRules() as $rule) { - if (!$rule->isMatching($this->adapter)) { - $isMatching = false; - } - } - - return $isMatching; - } - /** * Serialize a collection of rules * @@ -128,8 +104,8 @@ class ConstraintManager foreach ($unserializedRules as $rule) { if ($this->container->has($rule->ruleServiceId)) { /** @var CouponRuleInterface $couponRule */ - $couponRule = $this->container->get($rule->ruleServiceId); - $couponRule->populateFromForm( + $couponRule = $this->build( + $rule->ruleServiceId, (array) $rule->operators, (array) $rule->values ); @@ -140,4 +116,28 @@ class ConstraintManager return $collection; } + + + /** + * Build a Coupon Rule from form + * + * @param string $ruleServiceId Rule class name + * @param array $operators Rule Operator (<, >, = ) + * @param array $values Values setting this Rule + * + * @throws \InvalidArgumentException + * @return CouponRuleInterface Ready to use Rule or false + */ + public function build($ruleServiceId, array $operators, array $values) + { + if (!$this->container->has($ruleServiceId)) { + return false; + } + + /** @var CouponRuleInterface $rule */ + $rule = $this->container->get($ruleServiceId); + $rule->setValidatorsFromForm($operators, $values); + + return $rule; + } } \ No newline at end of file diff --git a/core/lib/Thelia/Constraint/ConstraintValidator.php b/core/lib/Thelia/Constraint/ConstraintValidator.php new file mode 100644 index 000000000..edacee317 --- /dev/null +++ b/core/lib/Thelia/Constraint/ConstraintValidator.php @@ -0,0 +1,133 @@ +. */ +/* */ +/**********************************************************************************/ + +namespace Thelia\Constraint; + +use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\Serializer\Encoder\JsonEncoder; +use Thelia\Constraint\Rule\AvailableForTotalAmountManager; +use Thelia\Constraint\Rule\CouponRuleInterface; +use Thelia\Constraint\Rule\Operators; +use Thelia\Coupon\CouponRuleCollection; + + +/** + * Created by JetBrains PhpStorm. + * Date: 8/19/13 + * Time: 3:24 PM + * + * Validate Constraints + * + * @package Constraint + * @author Guillaume MOREL + * + */ +class ConstraintValidator +{ + + /** + * Check if a Customer meets SerializableRule + * + * @param CouponRuleCollection $rules Rules to check against the Customer + * + * @return bool + */ + public function test(CouponRuleCollection $rules) + { + $isMatching = true; + /** @var CouponRuleInterface $rule */ + foreach ($rules->getRules() as $rule) { + if (!$rule->isMatching()) { + $isMatching = false; + } + } + + return $isMatching; + + } + + /** + * Do variable comparison + * + * @param mixed $v1 Variable 1 + * @param string $o Operator + * + * @param mixed $v2 Variable 2 + * @throws \Exception + * @return bool + */ + public function variableOpComparison($v1, $o, $v2) { + if ($o == Operators::DIFFERENT) { + return ($v1 != $v2); + } // could put this elsewhere... +// $operators = str_split($o); +// foreach($o as $operator) { + switch ($o) { // return will exit switch, foreach loop, function + case Operators::SUPERIOR : // > + if ($v1 > $v2) { + return true; + } else { + continue; + } break; + case Operators::SUPERIOR_OR_EQUAL : // >= + if ($v1 >= $v2) { + return true; + } else { + continue; + } break; + case Operators::INFERIOR : // < + if ($v1 < $v2) { + return true; + } else { + continue; + } break; + case Operators::INFERIOR_OR_EQUAL : // <= + if ($v1 <= $v2) { + return true; + } else { + continue; + } break; + case Operators::EQUAL : // == + if ($v1 == $v2) { + return true; + } else { + continue; + } break; + case Operators::IN: + if (in_array($v1, $v2)) { // in + return true; + } else { + continue; + } break; + case Operators::OUT: + if (!in_array($v1, $v2)) { // not in + return true; + } else { + continue; + } break; + default: throw new \Exception('Unrecognized operator ' . $o); + } +// } + return false; + } +} \ No newline at end of file diff --git a/core/lib/Thelia/Constraint/Rule/AvailableForTotalAmount.php b/core/lib/Thelia/Constraint/Rule/AvailableForTotalAmountManager.php similarity index 51% rename from core/lib/Thelia/Constraint/Rule/AvailableForTotalAmount.php rename to core/lib/Thelia/Constraint/Rule/AvailableForTotalAmountManager.php index 1cb85734a..98663fd4a 100644 --- a/core/lib/Thelia/Constraint/Rule/AvailableForTotalAmount.php +++ b/core/lib/Thelia/Constraint/Rule/AvailableForTotalAmountManager.php @@ -25,12 +25,14 @@ namespace Thelia\Constraint\Rule; use Symfony\Component\Intl\Exception\NotImplementedException; use Symfony\Component\Translation\Translator; +use Thelia\Constraint\ConstraintValidator; use Thelia\Coupon\CouponAdapterInterface; use Thelia\Constraint\Validator\PriceParam; use Thelia\Constraint\Validator\RuleValidator; use Thelia\Exception\InvalidRuleException; use Thelia\Exception\InvalidRuleOperatorException; use Thelia\Exception\InvalidRuleValueException; +use Thelia\Type\FloatType; /** * Created by JetBrains PhpStorm. @@ -44,26 +46,33 @@ use Thelia\Exception\InvalidRuleValueException; * @author Guillaume MOREL * */ -class AvailableForTotalAmount extends CouponRuleAbstract +class AvailableForTotalAmountManager extends CouponRuleAbstract { /** Rule 1st parameter : price */ - CONST PARAM1_PRICE = 'price'; + CONST INPUT1 = 'price'; /** Rule 1st parameter : currency */ - CONST PARAM1_CURRENCY = 'currency'; + CONST INPUT2 = 'currency'; /** @var string Service Id from Resources/config.xml */ protected $serviceId = 'thelia.constraint.rule.available_for_total_amount'; /** @var array Available Operators (Operators::CONST) */ protected $availableOperators = array( - Operators::INFERIOR, - Operators::EQUAL, - Operators::SUPERIOR, + self::INPUT1 => array( + Operators::INFERIOR, + Operators::INFERIOR_OR_EQUAL, + Operators::EQUAL, + Operators::SUPERIOR_OR_EQUAL, + Operators::SUPERIOR + ), + self::INPUT2 => array( + Operators::EQUAL, + ) ); - /** @var RuleValidator Price Validator */ - protected $priceValidator = null; +// /** @var RuleValidator Price Validator */ +// protected $priceValidator = null; /** * Check if backoffice inputs are relevant or not @@ -96,34 +105,146 @@ class AvailableForTotalAmount extends CouponRuleAbstract return $this->isPriceValid($price->getPrice(), $price->getCurrency()); } +// /** +// * Check if Checkout inputs are relevant or not +// * +// * @throws InvalidRuleValueException if Value is not allowed +// * @return bool +// */ +// public function checkCheckoutInput() +// { +// $currency = $this->adapter->getCheckoutCurrency(); +// if (empty($currency)) { +// throw new InvalidRuleValueException( +// get_class(), self::PARAM1_CURRENCY +// ); +// } +// +// $price = $this->adapter->getCartTotalPrice(); +// if (empty($price)) { +// throw new InvalidRuleValueException( +// get_class(), self::PARAM1_PRICE +// ); +// } +// +// $this->paramsToValidate = array( +// self::PARAM1_PRICE => $this->adapter->getCartTotalPrice(), +// self::PARAM1_CURRENCY => $this->adapter->getCheckoutCurrency() +// ); +// +// return $this->isPriceValid($price, $currency); +// } + /** - * Check if Checkout inputs are relevant or not + * Check validators relevancy and store them * - * @throws InvalidRuleValueException if Value is not allowed - * @return bool + * @param array $operators Operators the Admin set in BackOffice + * @param array $values Values the Admin set in BackOffice + * + * @throws \InvalidArgumentException + * @return $this */ - public function checkCheckoutInput() + public function setValidatorsFromForm(array $operators, array $values) { - $currency = $this->adapter->getCheckoutCurrency(); - if (empty($currency)) { - throw new InvalidRuleValueException( - get_class(), self::PARAM1_CURRENCY - ); - } - - $price = $this->adapter->getCartTotalPrice(); - if (empty($price)) { - throw new InvalidRuleValueException( - get_class(), self::PARAM1_PRICE - ); - } - - $this->paramsToValidate = array( - self::PARAM1_PRICE => $this->adapter->getCartTotalPrice(), - self::PARAM1_CURRENCY => $this->adapter->getCheckoutCurrency() + $this->setValidators( + $operators[self::INPUT1], + $values[self::INPUT1], + $operators[self::INPUT2], + $values[self::INPUT2] ); - return $this->isPriceValid($price, $currency); + return $this; + } + + /** + * Check validators relevancy and store them + * + * @param string $priceOperator Price Operator ex < + * @param float $priceValue Price set to meet condition + * @param string $currencyOperator Currency Operator ex = + * @param string $currencyValue Currency set to meet condition + * + * @throws \InvalidArgumentException + * @return $this + */ + protected function setValidators($priceOperator, $priceValue, $currencyOperator, $currencyValue) + { + $isOperator1Legit = $this->isOperatorLegit( + $priceOperator, + $this->availableOperators[self::INPUT1] + ); + if (!$isOperator1Legit) { + throw new \InvalidArgumentException( + 'Operator for price field is not legit' + ); + } + + $isOperator1Legit = $this->isOperatorLegit( + $currencyOperator, + $this->availableOperators[self::INPUT2] + ); + if (!$isOperator1Legit) { + throw new \InvalidArgumentException( + 'Operator for currency field is not legit' + ); + } + + $floatType = new FloatType(); + if (!$floatType->isValid($priceValue) || $priceValue <= 0) { + throw new \InvalidArgumentException( + 'Value for price field is not legit' + ); + } + + // @todo check currency is legit or not + + $this->operators = array( + self::INPUT1 => $priceOperator, + self::INPUT2 => $currencyOperator, + ); + $this->values = array( + self::INPUT1 => $priceValue, + self::INPUT2 => $currencyValue, + ); + + return $this; + } + + /** + * Test if Customer meets conditions + * + * @return bool + */ + public function isMatching() + { + $isOperator1Legit = $this->isOperatorLegit( + $this->operators[self::INPUT1], + $this->availableOperators[self::INPUT1] + ); + $isOperator2Legit = $this->isOperatorLegit( + $this->operators[self::INPUT2], + $this->availableOperators[self::INPUT2] + ); + + if (!$isOperator1Legit || !$isOperator2Legit) { + return false; + } + + $constrainValidator = new ConstraintValidator(); + $constraint1 =$constrainValidator->variableOpComparison( + $this->adapter->getCartTotalPrice(), + $this->operators[self::INPUT1], + $this->values[self::INPUT1] + ); + $constraint2 =$constrainValidator->variableOpComparison( + $this->adapter->getCheckoutCurrency(), + $this->operators[self::INPUT2], + $this->values[self::INPUT2] + ); + if ($constraint1 && $constraint2) { + return true; + } + return false; } /** @@ -154,20 +275,20 @@ class AvailableForTotalAmount extends CouponRuleAbstract return true; } - /** - * Generate current Rule param to be validated from adapter - * - * @return $this - */ - protected function setParametersToValidate() - { - $this->paramsToValidate = array( - self::PARAM1_PRICE => $this->adapter->getCartTotalPrice(), - self::PARAM1_CURRENCY => $this->adapter->getCheckoutCurrency() - ); - - return $this; - } +// /** +// * Generate current Rule param to be validated from adapter +// * +// * @return $this +// */ +// protected function setParametersToValidate() +// { +// $this->paramsToValidate = array( +// self::PARAM1_PRICE => $this->adapter->getCartTotalPrice(), +// self::PARAM1_CURRENCY => $this->adapter->getCheckoutCurrency() +// ); +// +// return $this; +// } /** * Get I18n name @@ -207,38 +328,38 @@ class AvailableForTotalAmount extends CouponRuleAbstract return $toolTip; } - /** - * Populate a Rule from a form admin - * - * @param array $operators Rule Operator set by the Admin - * @param array $values Rule Values set by the Admin - * - * @throws \InvalidArgumentException - * @return $this - */ - public function populateFromForm(array $operators, array $values) - { - if ($values[self::PARAM1_PRICE] === null - || $values[self::PARAM1_CURRENCY] === null - ) { - throw new \InvalidArgumentException( - 'The Rule ' . get_class() . 'needs at least a quantity set (' . self::PARAM1_PRICE . ', ' . self::PARAM1_CURRENCY . ')' - ); - } - - $this->priceValidator = new RuleValidator( - $operators[self::PARAM1_PRICE], - new PriceParam( - $this->translator, - $values[self::PARAM1_PRICE], - $values[self::PARAM1_CURRENCY] - ) - ); - - $this->validators = array(self::PARAM1_PRICE => $this->priceValidator); - - return $this; - } +// /** +// * Populate a Rule from a form admin +// * +// * @param array $operators Rule Operator set by the Admin +// * @param array $values Rule Values set by the Admin +// * +// * @throws \InvalidArgumentException +// * @return $this +// */ +// public function populateFromForm(array $operators, array $values) +// { +// if ($values[self::PARAM1_PRICE] === null +// || $values[self::PARAM1_CURRENCY] === null +// ) { +// throw new \InvalidArgumentException( +// 'The Rule ' . get_class() . 'needs at least a quantity set (' . self::PARAM1_PRICE . ', ' . self::PARAM1_CURRENCY . ')' +// ); +// } +// +// $this->priceValidator = new RuleValidator( +// $operators[self::PARAM1_PRICE], +// new PriceParam( +// $this->translator, +// $values[self::PARAM1_PRICE], +// $values[self::PARAM1_CURRENCY] +// ) +// ); +// +// $this->validators = array(self::PARAM1_PRICE => $this->priceValidator); +// +// return $this; +// } /** * Return a serializable Rule @@ -249,14 +370,9 @@ class AvailableForTotalAmount extends CouponRuleAbstract { $serializableRule = new SerializableRule(); $serializableRule->ruleServiceId = $this->serviceId; - $serializableRule->operators = array( - self::PARAM1_PRICE => $this->priceValidator->getOperator() - ); + $serializableRule->operators = $this->operators; - $serializableRule->values = array( - self::PARAM1_PRICE => $this->priceValidator->getParam()->getPrice(), - self::PARAM1_CURRENCY => $this->priceValidator->getParam()->getCurrency() - ); + $serializableRule->values = $this->values; return $serializableRule; } diff --git a/core/lib/Thelia/Constraint/Rule/AvailableForXArticles.php b/core/lib/Thelia/Constraint/Rule/AvailableForXArticlesManager.php similarity index 62% rename from core/lib/Thelia/Constraint/Rule/AvailableForXArticles.php rename to core/lib/Thelia/Constraint/Rule/AvailableForXArticlesManager.php index a15562128..f56596bc2 100644 --- a/core/lib/Thelia/Constraint/Rule/AvailableForXArticles.php +++ b/core/lib/Thelia/Constraint/Rule/AvailableForXArticlesManager.php @@ -25,10 +25,13 @@ namespace Thelia\Constraint\Rule; use InvalidArgumentException; use Symfony\Component\Translation\Translator; +use Thelia\Constraint\ConstraintValidator; use Thelia\Constraint\Validator\QuantityParam; use Thelia\Constraint\Validator\RuleValidator; use Thelia\Coupon\CouponAdapterInterface; +use Thelia\Exception\InvalidRuleException; use Thelia\Exception\InvalidRuleValueException; +use Thelia\Type\FloatType; /** * Created by JetBrains PhpStorm. @@ -41,19 +44,23 @@ use Thelia\Exception\InvalidRuleValueException; * @author Guillaume MOREL * */ -class AvailableForXArticles extends CouponRuleAbstract +class AvailableForXArticlesManager extends CouponRuleAbstract { /** Rule 1st parameter : quantity */ - CONST PARAM1_QUANTITY = 'quantity'; + CONST INPUT1 = 'quantity'; /** @var string Service Id from Resources/config.xml */ protected $serviceId = 'thelia.constraint.rule.available_for_x_articles'; /** @var array Available Operators (Operators::CONST) */ protected $availableOperators = array( - Operators::INFERIOR, - Operators::EQUAL, - Operators::SUPERIOR, + self::INPUT1 => array( + Operators::INFERIOR, + Operators::INFERIOR_OR_EQUAL, + Operators::EQUAL, + Operators::SUPERIOR_OR_EQUAL, + Operators::SUPERIOR + ) ); /** @var QuantityParam Quantity Validator */ @@ -107,24 +114,100 @@ class AvailableForXArticles extends CouponRuleAbstract return $this; } +// /** +// * Check if Checkout inputs are relevant or not +// * +// * @throws \Thelia\Exception\InvalidRuleValueException +// * @return bool +// */ +// public function checkCheckoutInput() +// { +// if (!isset($this->paramsToValidate) +// || empty($this->paramsToValidate) +// ||!isset($this->paramsToValidate[self::PARAM1_QUANTITY]) +// ) { +// throw new InvalidRuleValueException(get_class(), self::PARAM1_QUANTITY); +// } +// +// $price = $this->paramsToValidate[self::PARAM1_QUANTITY]; +// +// return $this->isQuantityValid($price); +// } + /** - * Check if Checkout inputs are relevant or not + * Check validators relevancy and store them * - * @throws \Thelia\Exception\InvalidRuleValueException - * @return bool + * @param array $operators Operators the Admin set in BackOffice + * @param array $values Values the Admin set in BackOffice + * + * @throws \InvalidArgumentException + * @return $this */ - public function checkCheckoutInput() + public function setValidatorsFromForm(array $operators, array $values) { - if (!isset($this->paramsToValidate) - || empty($this->paramsToValidate) - ||!isset($this->paramsToValidate[self::PARAM1_QUANTITY]) - ) { - throw new InvalidRuleValueException(get_class(), self::PARAM1_QUANTITY); + $this->setValidators( + $operators[self::INPUT1], + $values[self::INPUT1] + ); + + return $this; + } + + /** + * Check validators relevancy and store them + * + * @param string $quantityOperator Quantity Operator ex < + * @param int $quantityValue Quantity set to meet condition + * + * @throws \InvalidArgumentException + * @return $this + */ + protected function setValidators($quantityOperator, $quantityValue) + { + $isOperator1Legit = $this->isOperatorLegit( + $quantityOperator, + $this->availableOperators[self::INPUT1] + ); + if (!$isOperator1Legit) { + throw new \InvalidArgumentException( + 'Operator for quantity field is not legit' + ); } - $price = $this->paramsToValidate[self::PARAM1_QUANTITY]; + if (!is_int($quantityValue) || $quantityValue <= 0) { + throw new \InvalidArgumentException( + 'Value for quantity field is not legit' + ); + } - return $this->isQuantityValid($price); + $this->operators = array( + self::INPUT1 => $quantityOperator, + ); + $this->values = array( + self::INPUT1 => $quantityValue, + ); + + return $this; + } + + /** + * Test if Customer meets conditions + * + * @return bool + */ + public function isMatching() + { + $constrainValidator = new ConstraintValidator(); + $constraint1 =$constrainValidator->variableOpComparison( + $this->adapter->getNbArticlesInCart(), + $this->operators[self::INPUT1], + $this->values[self::INPUT1] + ); + + if ($constraint1) { + return true; + } + return false; } /** @@ -184,35 +267,35 @@ class AvailableForXArticles extends CouponRuleAbstract return $toolTip; } - /** - * Populate a Rule from a form admin - * - * @param array $operators Rule Operator set by the Admin - * @param array $values Rule Values set by the Admin - * - * @throws InvalidArgumentException - * @return $this - */ - public function populateFromForm(array $operators, array $values) - { - if ($values[self::PARAM1_QUANTITY] === null) { - throw new InvalidArgumentException( - 'The Rule ' . get_class() . 'needs at least a quantity set (' . self::PARAM1_QUANTITY. ')' - ); - } - - $this->quantityValidator = new RuleValidator( - $operators[self::PARAM1_QUANTITY], - new QuantityParam( - $this->adapter, - $values[self::PARAM1_QUANTITY] - ) - ); - - $this->validators = array(self::PARAM1_QUANTITY => $this->quantityValidator); - - return $this; - } +// /** +// * Populate a Rule from a form admin +// * +// * @param array $operators Rule Operator set by the Admin +// * @param array $values Rule Values set by the Admin +// * +// * @throws InvalidArgumentException +// * @return $this +// */ +// public function populateFromForm(array $operators, array $values) +// { +// if ($values[self::PARAM1_QUANTITY] === null) { +// throw new InvalidArgumentException( +// 'The Rule ' . get_class() . 'needs at least a quantity set (' . self::PARAM1_QUANTITY. ')' +// ); +// } +// +// $this->quantityValidator = new RuleValidator( +// $operators[self::PARAM1_QUANTITY], +// new QuantityParam( +// $this->adapter, +// $values[self::PARAM1_QUANTITY] +// ) +// ); +// +// $this->validators = array(self::PARAM1_QUANTITY => $this->quantityValidator); +// +// return $this; +// } /** * Return a serializable Rule diff --git a/core/lib/Thelia/Constraint/Rule/CouponRuleAbstract.php b/core/lib/Thelia/Constraint/Rule/CouponRuleAbstract.php index fa5544c00..1c781d97f 100644 --- a/core/lib/Thelia/Constraint/Rule/CouponRuleAbstract.php +++ b/core/lib/Thelia/Constraint/Rule/CouponRuleAbstract.php @@ -67,6 +67,12 @@ abstract class CouponRuleAbstract implements CouponRuleInterface /** @var Translator Service Translator */ protected $translator = null; + /** @var array Operators set by Admin in BackOffice */ + protected $operators = array(); + + /** @var array Values set by Admin in BackOffice */ + protected $values = array(); + /** * Constructor * @@ -78,59 +84,61 @@ abstract class CouponRuleAbstract implements CouponRuleInterface $this->translator = $adapter->getTranslator(); } - /** - * Check validator relevancy and store them - * - * @param array $validators Array of RuleValidator - * validating $paramsToValidate against - * - * @return $this - * @throws InvalidRuleException - */ - protected function setValidators(array $validators) - { - foreach ($validators as $validator) { - if (!$validator instanceof RuleValidator) { - throw new InvalidRuleException(get_class()); - } - if (!in_array($validator->getOperator(), $this->availableOperators)) { - throw new InvalidRuleOperatorException( - get_class(), - $validator->getOperator() - ); - } - } - $this->validators = $validators; +// /** +// * Check validator relevancy and store them +// * +// * @param array $validators Array of RuleValidator +// * validating $paramsToValidate against +// * +// * @return $this +// * @throws InvalidRuleException +// */ +// protected function setValidators(array $validators) +// { +// foreach ($validators as $validator) { +// if (!$validator instanceof RuleValidator) { +// throw new InvalidRuleException(get_class()); +// } +// if (!in_array($validator->getOperator(), $this->availableOperators)) { +// throw new InvalidRuleOperatorException( +// get_class(), +// $validator->getOperator() +// ); +// } +// } +// $this->validators = $validators; +// +// return $this; +// } - return $this; - } - /** - * Check if the current Checkout matches this condition - * - * @return bool - */ - public function isMatching() - { - $this->checkBackOfficeInput(); - $this->checkCheckoutInput(); - $isMatching = true; - /** @var $validator RuleValidator*/ - foreach ($this->validators as $param => $validator) { - $a = $this->paramsToValidate[$param]; - $operator = $validator->getOperator(); - /** @var ComparableInterface, RuleParameterAbstract $b */ - $b = $validator->getParam(); - - if (!Operators::isValid($a, $operator, $b)) { - $isMatching = false; - } - } - - return $isMatching; - - } +// /** +// * Check if the current Checkout matches this condition +// * +// * @return bool +// */ +// public function isMatching() +// { +// $this->checkBackOfficeInput(); +// $this->checkCheckoutInput(); +// +// $isMatching = true; +// /** @var $validator RuleValidator*/ +// foreach ($this->validators as $param => $validator) { +// $a = $this->paramsToValidate[$param]; +// $operator = $validator->getOperator(); +// /** @var ComparableInterface, RuleParameterAbstract $b */ +// $b = $validator->getParam(); +// +// if (!Operators::isValid($a, $operator, $b)) { +// $isMatching = false; +// } +// } +// +// return $isMatching; +// +// } /** * Return all available Operators for this Rule @@ -162,16 +170,16 @@ abstract class CouponRuleAbstract implements CouponRuleInterface return true; } - /** - * Generate current Rule param to be validated from adapter - * - * @throws \Thelia\Exception\NotImplementedException - * @return $this - */ - protected function setParametersToValidate() - { - throw new \Thelia\Exception\NotImplementedException(); - } +// /** +// * Generate current Rule param to be validated from adapter +// * +// * @throws \Thelia\Exception\NotImplementedException +// * @return $this +// */ +// protected function setParametersToValidate() +// { +// throw new \Thelia\Exception\NotImplementedException(); +// } /** * Return all validators @@ -194,6 +202,17 @@ abstract class CouponRuleAbstract implements CouponRuleInterface return $this->serviceId; } - + /** + * Validate if Operator given is available for this Coupon + * + * @param string $operator Operator to validate ex < + * @param array $availableOperators Available operators + * + * @return bool + */ + protected function isOperatorLegit($operator, array $availableOperators) + { + return in_array($operator, $availableOperators); + } } \ No newline at end of file diff --git a/core/lib/Thelia/Constraint/Rule/CouponRuleInterface.php b/core/lib/Thelia/Constraint/Rule/CouponRuleInterface.php index ac856d212..4c5575159 100644 --- a/core/lib/Thelia/Constraint/Rule/CouponRuleInterface.php +++ b/core/lib/Thelia/Constraint/Rule/CouponRuleInterface.php @@ -46,6 +46,13 @@ interface CouponRuleInterface */ function __construct(CouponAdapterInterface $adapter); + /** + * Get Rule Service id + * + * @return string + */ + public function getServiceId(); + /** * Check if backoffice inputs are relevant or not * @@ -53,15 +60,33 @@ interface CouponRuleInterface */ public function checkBackOfficeInput(); - /** - * Check if Checkout inputs are relevant or not - * - * @return bool - */ - public function checkCheckoutInput(); +// /** +// * Check if Checkout inputs are relevant or not +// * +// * @return bool +// */ +// public function checkCheckoutInput(); /** - * Check if the current Checkout matches this condition + * Check validators relevancy and store them + * + * @param array $operators Operators the Admin set in BackOffice + * @param array $values Values the Admin set in BackOffice + * + * @throws \InvalidArgumentException + * @return $this + */ + public function setValidatorsFromForm(array $operators, array $values); + +// /** +// * Check if the current Checkout matches this condition +// * +// * @return bool +// */ +// public function isMatching(); + + /** + * Test if Customer meets conditions * * @return bool */ @@ -96,15 +121,15 @@ interface CouponRuleInterface */ public function getValidators(); - /** - * Populate a Rule from a form admin - * - * @param array $operators Rule Operator set by the Admin - * @param array $values Rule Values set by the Admin - * - * @return bool - */ - public function populateFromForm(array$operators, array $values); +// /** +// * Populate a Rule from a form admin +// * +// * @param array $operators Rule Operator set by the Admin +// * @param array $values Rule Values set by the Admin +// * +// * @return bool +// */ +// public function populateFromForm(array$operators, array $values); /** diff --git a/core/lib/Thelia/Constraint/Rule/Operators.php b/core/lib/Thelia/Constraint/Rule/Operators.php index 61e8337c6..237c6a5e0 100644 --- a/core/lib/Thelia/Constraint/Rule/Operators.php +++ b/core/lib/Thelia/Constraint/Rule/Operators.php @@ -51,62 +51,66 @@ abstract class Operators CONST SUPERIOR = '>'; /** Param1 is different to Param2 */ CONST DIFFERENT = '!='; + /** Param1 is in Param2 */ + CONST IN = 'in'; + /** Param1 is not in Param2 */ + CONST OUT = 'out'; - /** - * Check if a parameter is valid against a ComparableInterface from its operator - * - * @param mixed $a Parameter to validate - * @param string $operator Operator to validate against - * @param ComparableInterface $b Comparable to validate against - * - * @return bool - */ - public static function isValid($a, $operator, ComparableInterface $b) - { - $ret = false; - - try { - $comparison = $b->compareTo($a); - } catch (\Exception $e) { - return false; - } - - switch ($operator) { - case self::INFERIOR: - if ($comparison == 1) { - return true; - } - break; - case self::INFERIOR_OR_EQUAL: - if ($comparison == 1 || $comparison == 0) { - return true; - } - break; - case self::EQUAL: - if ($comparison == 0) { - return true; - } - break; - case self::SUPERIOR_OR_EQUAL: - if ($comparison == -1 || $comparison == 0) { - return true; - } - break; - case self::SUPERIOR: - if ($comparison == -1) { - return true; - } - break; - case self::DIFFERENT: - if ($comparison != 0) { - return true; - } - break; - default: - } - - return $ret; - } +// /** +// * Check if a parameter is valid against a ComparableInterface from its operator +// * +// * @param mixed $a Parameter to validate +// * @param string $operator Operator to validate against +// * @param ComparableInterface $b Comparable to validate against +// * +// * @return bool +// */ +// public static function isValid($a, $operator, ComparableInterface $b) +// { +// $ret = false; +// +// try { +// $comparison = $b->compareTo($a); +// } catch (\Exception $e) { +// return false; +// } +// +// switch ($operator) { +// case self::INFERIOR: +// if ($comparison == 1) { +// return true; +// } +// break; +// case self::INFERIOR_OR_EQUAL: +// if ($comparison == 1 || $comparison == 0) { +// return true; +// } +// break; +// case self::EQUAL: +// if ($comparison == 0) { +// return true; +// } +// break; +// case self::SUPERIOR_OR_EQUAL: +// if ($comparison == -1 || $comparison == 0) { +// return true; +// } +// break; +// case self::SUPERIOR: +// if ($comparison == -1) { +// return true; +// } +// break; +// case self::DIFFERENT: +// if ($comparison != 0) { +// return true; +// } +// break; +// default: +// } +// +// return $ret; +// } /** * Get operator translation @@ -162,6 +166,20 @@ abstract class Operators 'constraint' ); break; + case self::IN: + $ret = $translator->trans( + 'in', + array(), + 'constraint' + ); + break; + case self::OUT: + $ret = $translator->trans( + 'not in', + array(), + 'constraint' + ); + break; default: } diff --git a/core/lib/Thelia/Coupon/CouponFactory.php b/core/lib/Thelia/Coupon/CouponFactory.php index 3ff064601..b23eb56ea 100644 --- a/core/lib/Thelia/Coupon/CouponFactory.php +++ b/core/lib/Thelia/Coupon/CouponFactory.php @@ -128,25 +128,5 @@ class CouponFactory } -// /** -// * Build a Coupon Rule from form -// * -// * @param string $type Rule class name -// * @param string $operator Rule Operator (<, >, = ) -// * @param array $values Values setting this Rule -// * -// * @return CouponRuleInterface Ready to use Rule or false -// */ -// public function buildCouponRuleFromForm($ruleServiceId, $operator, array $values) -// { -// /** @var CouponAdapterInterface $adapter */ -// $adapter = $this->container->get('thelia.adapter'); -// $validator = new PriceParam() -// try { -// $rule = new AvailableForTotalAmount($adapter, $validators); -// $rule = new $type($adapter, $validators); -// } catch (\Exception $e) { -// return false; -// } -// } + } diff --git a/core/lib/Thelia/Tests/Constraint/ConstraintManagerTest.php b/core/lib/Thelia/Tests/Constraint/ConstraintFactoryTest.php similarity index 59% rename from core/lib/Thelia/Tests/Constraint/ConstraintManagerTest.php rename to core/lib/Thelia/Tests/Constraint/ConstraintFactoryTest.php index a5f55cee1..70d6e498c 100644 --- a/core/lib/Thelia/Tests/Constraint/ConstraintManagerTest.php +++ b/core/lib/Thelia/Tests/Constraint/ConstraintFactoryTest.php @@ -24,17 +24,11 @@ namespace Thelia\Constraint; use Symfony\Component\DependencyInjection\ContainerBuilder; -use Thelia\Constraint\Rule\AvailableForXArticles; -use Thelia\Constraint\Validator\PriceParam; -use Thelia\Constraint\Validator\RuleValidator; -use Thelia\Constraint\Rule\AvailableForTotalAmount; +use Thelia\Constraint\Rule\AvailableForTotalAmountManager; +use Thelia\Constraint\Rule\AvailableForXArticlesManager; use Thelia\Constraint\Rule\Operators; use Thelia\Coupon\CouponBaseAdapter; -use Thelia\Coupon\CouponBaseAdapterTest; use Thelia\Coupon\CouponRuleCollection; -use Thelia\Coupon\Type\CouponInterface; -use Thelia\Coupon\Type\RemoveXAmount; -use Thelia\Tools\PhpUnitUtils; /** * Created by JetBrains PhpStorm. @@ -47,7 +41,7 @@ use Thelia\Tools\PhpUnitUtils; * @author Guillaume MOREL * */ -class ConstraintManagerTest extends \PHPUnit_Framework_TestCase +class ConstraintFactoryTest extends \PHPUnit_Framework_TestCase { /** @@ -59,9 +53,9 @@ class ConstraintManagerTest extends \PHPUnit_Framework_TestCase } /** - * Check the if the Constraint Manager is able to check RuleValidators + * Check the Rules serialization module */ - public function testIsMatching() + public function testBuild() { $stubTranslator = $this->getMockBuilder('\Thelia\Core\Translation\Translator') ->disableOriginalConstructor() @@ -75,43 +69,68 @@ class ConstraintManagerTest extends \PHPUnit_Framework_TestCase ->method('getTranslator') ->will($this->returnValue($stubTranslator)); - $stubAdapter->expects($this->any()) - ->method('getCartTotalPrice') - ->will($this->returnValue(321.98)); - - $stubAdapter->expects($this->any()) - ->method('getCheckoutCurrency') - ->will($this->returnValue('USD')); - - $rule1 = new AvailableForTotalAmount($stubAdapter); - $operators = array(AvailableForTotalAmount::PARAM1_PRICE => Operators::SUPERIOR); - $values = array( - AvailableForTotalAmount::PARAM1_PRICE => 40.00, - AvailableForTotalAmount::PARAM1_CURRENCY => 'USD' + $rule1 = new AvailableForTotalAmountManager($stubAdapter); + $operators = array( + AvailableForTotalAmountManager::INPUT1 => Operators::SUPERIOR, + AvailableForTotalAmountManager::INPUT2 => Operators::EQUAL ); - $rule1->populateFromForm($operators, $values); - - $rule2 = new AvailableForTotalAmount($stubAdapter); - $operators = array(AvailableForTotalAmount::PARAM1_PRICE => Operators::INFERIOR); $values = array( - AvailableForTotalAmount::PARAM1_PRICE => 400.00, - AvailableForTotalAmount::PARAM1_CURRENCY => 'USD' + AvailableForTotalAmountManager::INPUT1 => 40.00, + AvailableForTotalAmountManager::INPUT2 => 'EUR' ); - $rule2->populateFromForm($operators, $values); - - $rules = new CouponRuleCollection(); - $rules->add($rule1); - $rules->add($rule2); + $rule1->setValidatorsFromForm($operators, $values); /** @var ConstraintManager $constraintManager */ - $constraintManager = new ConstraintManager($this->getContainer()); + $constraintFactory = new ConstraintFactory($this->getContainer()); + $ruleManager1 = $constraintFactory->build($rule1->getServiceId(), $operators, $values); - $expected = true; - $actual = $constraintManager->isMatching($rules); + $expected = $rule1; + $actual = $ruleManager1; - $this->assertEquals($expected, $actual, 'The ConstraintManager is no more able to check if a Rule is matching'); + $this->assertEquals($expected, $actual); + $this->assertEquals($rule1->getServiceId(), $ruleManager1->getServiceId()); + $this->assertEquals($rule1->getValidators(), $ruleManager1->getValidators()); } + /** + * Check the Rules serialization module + */ + public function testBuildFail() + { + $stubTranslator = $this->getMockBuilder('\Thelia\Core\Translation\Translator') + ->disableOriginalConstructor() + ->getMock(); + + $stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter') + ->disableOriginalConstructor() + ->getMock(); + + $stubAdapter->expects($this->any()) + ->method('getTranslator') + ->will($this->returnValue($stubTranslator)); + + $rule1 = new AvailableForTotalAmountManager($stubAdapter); + $operators = array( + AvailableForTotalAmountManager::INPUT1 => Operators::SUPERIOR, + AvailableForTotalAmountManager::INPUT2 => Operators::EQUAL + ); + $values = array( + AvailableForTotalAmountManager::INPUT1 => 40.00, + AvailableForTotalAmountManager::INPUT2 => 'EUR' + ); + $rule1->setValidatorsFromForm($operators, $values); + + /** @var ConstraintManager $constraintManager */ + $constraintFactory = new ConstraintFactory($this->getContainer()); + $ruleManager1 = $constraintFactory->build('unset.service', $operators, $values); + + $expected = false; + $actual = $ruleManager1; + + $this->assertEquals($expected, $actual); + } + + /** * Check the Rules serialization module */ @@ -129,31 +148,37 @@ class ConstraintManagerTest extends \PHPUnit_Framework_TestCase ->method('getTranslator') ->will($this->returnValue($stubTranslator)); - $rule1 = new AvailableForTotalAmount($stubAdapter); - $operators = array(AvailableForTotalAmount::PARAM1_PRICE => Operators::SUPERIOR); - $values = array( - AvailableForTotalAmount::PARAM1_PRICE => 40.00, - AvailableForTotalAmount::PARAM1_CURRENCY => 'EUR' + $rule1 = new AvailableForTotalAmountManager($stubAdapter); + $operators = array( + AvailableForTotalAmountManager::INPUT1 => Operators::SUPERIOR, + AvailableForTotalAmountManager::INPUT2 => Operators::EQUAL ); - $rule1->populateFromForm($operators, $values); + $values = array( + AvailableForTotalAmountManager::INPUT1 => 40.00, + AvailableForTotalAmountManager::INPUT2 => 'EUR' + ); + $rule1->setValidatorsFromForm($operators, $values); - $rule2 = new AvailableForTotalAmount($stubAdapter); - $operators = array(AvailableForTotalAmount::PARAM1_PRICE => Operators::INFERIOR); - $values = array( - AvailableForTotalAmount::PARAM1_PRICE => 400.00, - AvailableForTotalAmount::PARAM1_CURRENCY => 'EUR' + $rule2 = new AvailableForTotalAmountManager($stubAdapter); + $operators = array( + AvailableForTotalAmountManager::INPUT1 => Operators::SUPERIOR, + AvailableForTotalAmountManager::INPUT2 => Operators::EQUAL ); - $rule2->populateFromForm($operators, $values); + $values = array( + AvailableForTotalAmountManager::INPUT1 => 400.00, + AvailableForTotalAmountManager::INPUT2 => 'EUR' + ); + $rule2->setValidatorsFromForm($operators, $values); $rules = new CouponRuleCollection(); $rules->add($rule1); $rules->add($rule2); /** @var ConstraintManager $constraintManager */ - $constraintManager = new ConstraintManager($this->getContainer()); + $constraintFactory = new ConstraintFactory($this->getContainer()); - $serializedRules = $constraintManager->serializeCouponRuleCollection($rules); - $unserializedRules = $constraintManager->unserializeCouponRuleCollection($serializedRules); + $serializedRules = $constraintFactory->serializeCouponRuleCollection($rules); + $unserializedRules = $constraintFactory->unserializeCouponRuleCollection($serializedRules); $expected = (string)$rules; $actual = (string)$unserializedRules; @@ -182,8 +207,8 @@ class ConstraintManagerTest extends \PHPUnit_Framework_TestCase ->method('getTranslator') ->will($this->returnValue($stubTranslator)); - $rule1 = new AvailableForTotalAmount($stubAdapter); - $rule2 = new AvailableForXArticles($stubAdapter); + $rule1 = new AvailableForTotalAmountManager($stubAdapter); + $rule2 = new AvailableForXArticlesManager($stubAdapter); $adapter = new CouponBaseAdapter($container); diff --git a/core/lib/Thelia/Tests/Constraint/ConstraintValidatorTest.php b/core/lib/Thelia/Tests/Constraint/ConstraintValidatorTest.php new file mode 100644 index 000000000..28ac4b952 --- /dev/null +++ b/core/lib/Thelia/Tests/Constraint/ConstraintValidatorTest.php @@ -0,0 +1,337 @@ +. */ +/* */ +/**********************************************************************************/ + +namespace Thelia\Constraint; + +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Thelia\Constraint\Rule\AvailableForTotalAmountManager; +use Thelia\Constraint\Rule\AvailableForXArticlesManager; +use Thelia\Constraint\Rule\Operators; +use Thelia\Coupon\CouponBaseAdapter; +use Thelia\Coupon\CouponRuleCollection; + +/** + * Created by JetBrains PhpStorm. + * Date: 8/19/13 + * Time: 3:24 PM + * + * Unit Test ConstraintValidator Class + * + * @package Constraint + * @author Guillaume MOREL + * + */ +class ConstraintValidatorTest extends \PHPUnit_Framework_TestCase +{ + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + */ + public function setUp() + { + } + + public function testTestSuccess1Rules() + { + $ConstraintValidator = new ConstraintValidator(); + $stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter') + ->disableOriginalConstructor() + ->getMock(); + + $stubAdapter->expects($this->any()) + ->method('getCartTotalPrice') + ->will($this->returnValue(401)); + $stubAdapter->expects($this->any()) + ->method('getCheckoutCurrency') + ->will($this->returnValue('EUR')); + + $rule1 = new AvailableForTotalAmountManager($stubAdapter); + $operators = array( + AvailableForTotalAmountManager::INPUT1 => '>', + AvailableForTotalAmountManager::INPUT2 => '==' + ); + $values = array( + AvailableForTotalAmountManager::INPUT1 => 400.00, + AvailableForTotalAmountManager::INPUT2 => 'EUR'); + $rule1->setValidatorsFromForm($operators, $values); + + + $rules = new CouponRuleCollection(); + $rules->add($rule1); + + $isValid = $ConstraintValidator->test($rules); + + $expected = true; + $actual =$isValid; + $this->assertEquals($expected, $actual); + } + + public function testTestFail1Rules() + { + $ConstraintValidator = new ConstraintValidator(); + $stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter') + ->disableOriginalConstructor() + ->getMock(); + + $stubAdapter->expects($this->any()) + ->method('getCartTotalPrice') + ->will($this->returnValue(400)); + $stubAdapter->expects($this->any()) + ->method('getCheckoutCurrency') + ->will($this->returnValue('EUR')); + + $rule1 = new AvailableForTotalAmountManager($stubAdapter); + $operators = array( + AvailableForTotalAmountManager::INPUT1 => '>', + AvailableForTotalAmountManager::INPUT2 => '==' + ); + $values = array( + AvailableForTotalAmountManager::INPUT1 => 400.00, + AvailableForTotalAmountManager::INPUT2 => 'EUR'); + $rule1->setValidatorsFromForm($operators, $values); + + + $rules = new CouponRuleCollection(); + $rules->add($rule1); + + $isValid = $ConstraintValidator->test($rules); + + $expected = false; + $actual =$isValid; + $this->assertEquals($expected, $actual, 'Constraints validator always think Customer is matching rules'); + } + + public function testTestSuccess2Rules() + { + $ConstraintValidator = new ConstraintValidator(); + $stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter') + ->disableOriginalConstructor() + ->getMock(); + + $stubAdapter->expects($this->any()) + ->method('getCartTotalPrice') + ->will($this->returnValue(401)); + $stubAdapter->expects($this->any()) + ->method('getCheckoutCurrency') + ->will($this->returnValue('EUR')); + $stubAdapter->expects($this->any()) + ->method('getNbArticlesInCart') + ->will($this->returnValue(5)); + + $rule1 = new AvailableForTotalAmountManager($stubAdapter); + $operators = array( + AvailableForTotalAmountManager::INPUT1 => '>', + AvailableForTotalAmountManager::INPUT2 => '==' + ); + $values = array( + AvailableForTotalAmountManager::INPUT1 => 400.00, + AvailableForTotalAmountManager::INPUT2 => 'EUR'); + $rule1->setValidatorsFromForm($operators, $values); + + $rule2 = new AvailableForXArticlesManager($stubAdapter); + $operators = array( + AvailableForXArticlesManager::INPUT1 => '>' + ); + $values = array( + AvailableForXArticlesManager::INPUT1 => 4 + ); + $rule2->setValidatorsFromForm($operators, $values); + + $rules = new CouponRuleCollection(); + $rules->add($rule1); + $rules->add($rule2); + + $isValid = $ConstraintValidator->test($rules); + + $expected = true; + $actual =$isValid; + $this->assertEquals($expected, $actual); + } + + public function testTestFail2Rules() + { + $ConstraintValidator = new ConstraintValidator(); + $stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter') + ->disableOriginalConstructor() + ->getMock(); + + $stubAdapter->expects($this->any()) + ->method('getCartTotalPrice') + ->will($this->returnValue(400)); + $stubAdapter->expects($this->any()) + ->method('getCheckoutCurrency') + ->will($this->returnValue('EUR')); + $stubAdapter->expects($this->any()) + ->method('getNbArticlesInCart') + ->will($this->returnValue(5)); + + $rule1 = new AvailableForTotalAmountManager($stubAdapter); + $operators = array( + AvailableForTotalAmountManager::INPUT1 => '>', + AvailableForTotalAmountManager::INPUT2 => '==' + ); + $values = array( + AvailableForTotalAmountManager::INPUT1 => 400.00, + AvailableForTotalAmountManager::INPUT2 => 'EUR'); + $rule1->setValidatorsFromForm($operators, $values); + + $rule2 = new AvailableForXArticlesManager($stubAdapter); + $operators = array( + AvailableForXArticlesManager::INPUT1 => '>' + ); + $values = array( + AvailableForXArticlesManager::INPUT1 => 4 + ); + $rule2->setValidatorsFromForm($operators, $values); + + $rules = new CouponRuleCollection(); + $rules->add($rule1); + $rules->add($rule2); + + $isValid = $ConstraintValidator->test($rules); + + $expected = false; + $actual =$isValid; + $this->assertEquals($expected, $actual, 'Constraints validator always think Customer is matching rules'); + } + + + public function testVariableOpComparisonSuccess() + { + $ConstraintValidator = new ConstraintValidator(); + $expected = true; + $actual = $ConstraintValidator->variableOpComparison(1, Operators::EQUAL, 1); + $this->assertEquals($expected, $actual); + + $actual = $ConstraintValidator->variableOpComparison(1, Operators::DIFFERENT, 2); + $this->assertEquals($expected, $actual); + + $actual = $ConstraintValidator->variableOpComparison(1, Operators::SUPERIOR, 0); + $this->assertEquals($expected, $actual); + + $actual = $ConstraintValidator->variableOpComparison(1, Operators::INFERIOR, 2); + $this->assertEquals($expected, $actual); + + $actual = $ConstraintValidator->variableOpComparison(1, Operators::INFERIOR_OR_EQUAL, 1); + $this->assertEquals($expected, $actual); + + $actual = $ConstraintValidator->variableOpComparison(1, Operators::INFERIOR_OR_EQUAL, 2); + $this->assertEquals($expected, $actual); + + $actual = $ConstraintValidator->variableOpComparison(1, Operators::SUPERIOR_OR_EQUAL, 1); + $this->assertEquals($expected, $actual); + + $actual = $ConstraintValidator->variableOpComparison(1, Operators::SUPERIOR_OR_EQUAL, 0); + $this->assertEquals($expected, $actual); + + $actual = $ConstraintValidator->variableOpComparison(1, Operators::IN, array(1, 2, 3)); + $this->assertEquals($expected, $actual); + + $actual = $ConstraintValidator->variableOpComparison(1, Operators::OUT, array(0, 2, 3)); + $this->assertEquals($expected, $actual); + + } + + public function testVariableOpComparisonFail() + { + $ConstraintValidator = new ConstraintValidator(); + $expected = false; + $actual = $ConstraintValidator->variableOpComparison(2, Operators::EQUAL, 1); + $this->assertEquals($expected, $actual); + + $actual = $ConstraintValidator->variableOpComparison(2, Operators::DIFFERENT, 2); + $this->assertEquals($expected, $actual); + + $actual = $ConstraintValidator->variableOpComparison(0, Operators::SUPERIOR, 0); + $this->assertEquals($expected, $actual); + + $actual = $ConstraintValidator->variableOpComparison(3, Operators::INFERIOR, 2); + $this->assertEquals($expected, $actual); + + $actual = $ConstraintValidator->variableOpComparison(2, Operators::INFERIOR_OR_EQUAL, 1); + $this->assertEquals($expected, $actual); + + $actual = $ConstraintValidator->variableOpComparison(3, Operators::SUPERIOR_OR_EQUAL, 4); + $this->assertEquals($expected, $actual); + + $actual = $ConstraintValidator->variableOpComparison(0, Operators::IN, array(1, 2, 3)); + $this->assertEquals($expected, $actual); + + $actual = $ConstraintValidator->variableOpComparison(2, Operators::OUT, array(0, 2, 3)); + $this->assertEquals($expected, $actual); + + } + + /** + * @expectedException \Exception + */ + public function testVariableOpComparisonException() + { + $ConstraintValidator = new ConstraintValidator(); + $expected = true; + $actual = $ConstraintValidator->variableOpComparison(1, 'bad', 1); + $this->assertEquals($expected, $actual); + } + + /** + * Get Mocked Container with 2 Rules + * + * @return ContainerBuilder + */ + public function getContainer() + { + $container = new ContainerBuilder(); + + $stubTranslator = $this->getMockBuilder('\Thelia\Core\Translation\Translator') + ->disableOriginalConstructor() + ->getMock(); + + $stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter') + ->disableOriginalConstructor() + ->getMock(); + + $stubAdapter->expects($this->any()) + ->method('getTranslator') + ->will($this->returnValue($stubTranslator)); + + $rule1 = new AvailableForTotalAmountManager($stubAdapter); + $rule2 = new AvailableForXArticlesManager($stubAdapter); + + $adapter = new CouponBaseAdapter($container); + + $container->set('thelia.constraint.rule.available_for_total_amount', $rule1); + $container->set('thelia.constraint.rule.available_for_x_articles', $rule2); + $container->set('thelia.adapter', $adapter); + + return $container; + } + + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + */ + protected function tearDown() + { + } +} diff --git a/core/lib/Thelia/Tests/Constraint/Rule/AvailableForTotalAmountTest.php b/core/lib/Thelia/Tests/Constraint/Rule/AvailableForTotalAmountTest.php index f13e59238..bfcde8a21 100644 --- a/core/lib/Thelia/Tests/Constraint/Rule/AvailableForTotalAmountTest.php +++ b/core/lib/Thelia/Tests/Constraint/Rule/AvailableForTotalAmountTest.php @@ -23,12 +23,8 @@ namespace Thelia\Coupon; -use Thelia\Constraint\Validator\PriceParam; -use Thelia\Constraint\Validator\RuleValidator; -use Thelia\Constraint\Rule\AvailableForTotalAmount; +use Thelia\Constraint\Rule\AvailableForTotalAmountManager; use Thelia\Constraint\Rule\Operators; -use Thelia\Exception\InvalidRuleOperatorException; -use Thelia\Exception\InvalidRuleValueException; /** * Created by JetBrains PhpStorm. @@ -52,344 +48,613 @@ class AvailableForTotalAmountTest extends \PHPUnit_Framework_TestCase */ protected function setUp() { - /** @var CouponAdapterInterface $stubTheliaAdapter */ - $this->stubTheliaAdapter = $this->generateValidCouponBaseAdapterMock(); +// /** @var CouponAdapterInterface $stubTheliaAdapter */ +// $this->stubTheliaAdapter = $this->generateValidCouponBaseAdapterMock(); } - /** - * Generate valid CouponBaseAdapter - * - * @param float $cartTotalPrice Total amount of the current Cart - * - * @return CouponAdapterInterface - */ - protected function generateValidCouponBaseAdapterMock($cartTotalPrice = 421.23) - { - /** @var CouponAdapterInterface $stubTheliaAdapter */ - $stubTheliaAdapter = $this->getMock( - 'Thelia\Coupon\CouponBaseAdapter', - array('getCartTotalPrice'), - array() - ); - $stubTheliaAdapter->expects($this->any()) - ->method('getCartTotalPrice') - ->will($this->returnValue($cartTotalPrice)); +// /** +// * Generate valid CouponBaseAdapter +// * +// * @param float $cartTotalPrice Total amount of the current Cart +// * +// * @return CouponAdapterInterface +// */ +// protected function generateValidCouponBaseAdapterMock($cartTotalPrice = 421.23) +// { +// /** @var CouponAdapterInterface $stubTheliaAdapter */ +// $stubTheliaAdapter = $this->getMock( +// 'Thelia\Coupon\CouponBaseAdapter', +// array('getCartTotalPrice'), +// array() +// ); +// $stubTheliaAdapter->expects($this->any()) +// ->method('getCartTotalPrice') +// ->will($this->returnValue($cartTotalPrice)); +// +// return $stubTheliaAdapter; +// } - return $stubTheliaAdapter; - } +// /** +// * Check if validity test on BackOffice inputs are working +// * +// * @covers Thelia\Coupon\Rule\AvailableForTotalAmount::checkBackOfficeInput +// * +// */ +// public function testValidBackOfficeInput() +// { +// $adapter = new CouponBaseAdapter(); +// +// $validators = array( +// AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( +// Operators::SUPERIOR, +// new PriceParam( +// $adapter, 421.23, 'EUR' +// ) +// ) +// ); +// $rule = new AvailableForTotalAmount($adapter, $validators); +// +// $expected = true; +// $actual = $rule->checkBackOfficeInput(); +// $this->assertEquals($expected, $actual); +// } - /** - * Check if validity test on BackOffice inputs are working - * - * @covers Thelia\Coupon\Rule\AvailableForTotalAmount::checkBackOfficeInput - * - */ - public function testValidBackOfficeInput() - { - $adapter = new CouponBaseAdapter(); +// /** +// * Check if validity test on BackOffice inputs are working +// * +// * @covers Thelia\Coupon\Rule\AvailableForTotalAmount::checkBackOfficeInput +// * @expectedException \Thelia\Exception\InvalidRuleOperatorException +// * +// */ +// public function testInValidBackOfficeInputOperator() +// { +// $adapter = new CouponBaseAdapter(); +// +// $validators = array( +// AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( +// 'X', +// new PriceParam( +// $adapter, 421.23, 'EUR' +// ) +// ) +// ); +// +// $rule = new AvailableForTotalAmount($adapter, $validators); +// +// $expected = false; +// $actual = $rule->checkBackOfficeInput(); +// $this->assertEquals($expected, $actual); +// } - $validators = array( - AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( - Operators::SUPERIOR, - new PriceParam( - $adapter, 421.23, 'EUR' - ) - ) - ); - $rule = new AvailableForTotalAmount($adapter, $validators); +// /** +// * Check if validity test on BackOffice inputs are working +// * +// * @covers Thelia\Coupon\Rule\AvailableForTotalAmount::checkBackOfficeInput +// * @expectedException \ErrorException +// * +// */ +// public function testInValidBackOfficeInputValue() +// { +// $adapter = $this->generateValidCouponBaseAdapterMock(); +// +// $validators = array( +// AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( +// Operators::SUPERIOR, +// 421 +// ) +// ); +// +// $rule = new AvailableForTotalAmount($adapter, $validators); +// +// $expected = false; +// $actual = $rule->checkBackOfficeInput(); +// $this->assertEquals($expected, $actual); +// } - $expected = true; - $actual = $rule->checkBackOfficeInput(); - $this->assertEquals($expected, $actual); - } - - /** - * Check if validity test on BackOffice inputs are working - * - * @covers Thelia\Coupon\Rule\AvailableForTotalAmount::checkBackOfficeInput - * @expectedException \Thelia\Exception\InvalidRuleOperatorException - * - */ - public function testInValidBackOfficeInputOperator() - { - $adapter = new CouponBaseAdapter(); - - $validators = array( - AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( - 'X', - new PriceParam( - $adapter, 421.23, 'EUR' - ) - ) - ); - - $rule = new AvailableForTotalAmount($adapter, $validators); - - $expected = false; - $actual = $rule->checkBackOfficeInput(); - $this->assertEquals($expected, $actual); - } - - /** - * Check if validity test on BackOffice inputs are working - * - * @covers Thelia\Coupon\Rule\AvailableForTotalAmount::checkBackOfficeInput - * @expectedException \ErrorException - * - */ - public function testInValidBackOfficeInputValue() - { - $adapter = $this->generateValidCouponBaseAdapterMock(); - - $validators = array( - AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( - Operators::SUPERIOR, - 421 - ) - ); - - $rule = new AvailableForTotalAmount($adapter, $validators); - - $expected = false; - $actual = $rule->checkBackOfficeInput(); - $this->assertEquals($expected, $actual); - } - - - - /** - * Check if validity test on FrontOffice inputs are working - * - * @covers Thelia\Coupon\Rule\AvailableForTotalAmount::checkCheckoutInput - * - */ - public function testValidCheckoutInput() - { - $adapter = $this->stubTheliaAdapter; - - $validators = array( - AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( - Operators::SUPERIOR, - new PriceParam( - $adapter, 421.23, 'EUR' - ) - ) - ); - - $rule = new AvailableForTotalAmount($adapter, $validators); - - $expected = true; - $actual = $rule->checkCheckoutInput(); - $this->assertEquals($expected, $actual); - } - - /** - * Check if validity test on FrontOffice inputs are working - * - * @covers Thelia\Coupon\Rule\AvailableForTotalAmount::checkCheckoutInput - * @expectedException \Thelia\Exception\InvalidRuleValueException - * - */ - public function testInValidCheckoutInputValue() - { - $adapter = $this->generateValidCouponBaseAdapterMock(421); - - $validators = array( - AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( - Operators::SUPERIOR, - new PriceParam( - $adapter, 421.23, 'EUR' - ) - ) - ); - - $rule = new AvailableForTotalAmount($adapter, $validators); - - $expected = false; - $actual = $rule->checkCheckoutInput(); - $this->assertEquals($expected, $actual); - } - - /** - * Check if validity test on FrontOffice inputs are working - * - * @covers Thelia\Coupon\Rule\AvailableForTotalAmount::checkCheckoutInput - * @expectedException \Thelia\Exception\InvalidRuleValueException - * - */ - public function testInValidCheckoutInputType() - { - $adapter = $this->generateValidCouponBaseAdapterMock(421); - - $validators = array( - AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( - Operators::SUPERIOR, - new PriceParam( - $adapter, 421.23, 'EUR' - ) - ) - ); - - $rule = new AvailableForTotalAmount($adapter, $validators); - - $expected = false; - $actual = $rule->checkCheckoutInput(); - $this->assertEquals($expected, $actual); - } /** * Check if test inferior operator is working * - * @covers Thelia\Coupon\Rule\AvailableForTotalAmount::isMatching + * @covers Thelia\Constraint\Rule\AvailableForTotalAmountManager::isMatching * */ public function testMatchingRuleInferior() { - $adapter = $this->generateValidCouponBaseAdapterMock(421.22); + $stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter') + ->disableOriginalConstructor() + ->getMock(); - $validators = array( - AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( - Operators::INFERIOR, - new PriceParam( - $adapter, 421.23, 'EUR' - ) - ) + $stubAdapter->expects($this->any()) + ->method('getCartTotalPrice') + ->will($this->returnValue(399)); + $stubAdapter->expects($this->any()) + ->method('getCheckoutCurrency') + ->will($this->returnValue('EUR')); + + $rule1 = new AvailableForTotalAmountManager($stubAdapter); + $operators = array( + AvailableForTotalAmountManager::INPUT1 => Operators::INFERIOR, + AvailableForTotalAmountManager::INPUT2 => Operators::EQUAL ); + $values = array( + AvailableForTotalAmountManager::INPUT1 => 400.00, + AvailableForTotalAmountManager::INPUT2 => 'EUR'); + $rule1->setValidatorsFromForm($operators, $values); - $rule = new AvailableForTotalAmount($adapter, $validators); + $isValid = $rule1->isMatching(); $expected = true; - $actual = $rule->isMatching(); + $actual =$isValid; $this->assertEquals($expected, $actual); } /** * Check if test inferior operator is working * - * @covers Thelia\Coupon\Rule\AvailableForTotalAmount::isMatching + * @covers Thelia\Constraint\Rule\AvailableForTotalAmountManager::isMatching * */ public function testNotMatchingRuleInferior() { - $adapter = $this->generateValidCouponBaseAdapterMock(421.23); + $stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter') + ->disableOriginalConstructor() + ->getMock(); - $validators = array( - AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( - Operators::INFERIOR, - new PriceParam( - $adapter, 421.23, 'EUR' - ) - ) + $stubAdapter->expects($this->any()) + ->method('getCartTotalPrice') + ->will($this->returnValue(400)); + $stubAdapter->expects($this->any()) + ->method('getCheckoutCurrency') + ->will($this->returnValue('EUR')); + + $rule1 = new AvailableForTotalAmountManager($stubAdapter); + $operators = array( + AvailableForTotalAmountManager::INPUT1 => Operators::INFERIOR, + AvailableForTotalAmountManager::INPUT2 => Operators::EQUAL ); + $values = array( + AvailableForTotalAmountManager::INPUT1 => 400.00, + AvailableForTotalAmountManager::INPUT2 => 'EUR'); + $rule1->setValidatorsFromForm($operators, $values); - $rule = new AvailableForTotalAmount($adapter, $validators); + $isValid = $rule1->isMatching(); $expected = false; - $actual = $rule->isMatching(); + $actual =$isValid; + $this->assertEquals($expected, $actual); + } + + /** + * Check if test inferior operator is working + * + * @covers Thelia\Constraint\Rule\AvailableForTotalAmountManager::isMatching + * + */ + public function testMatchingRuleInferiorEquals() + { + $stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter') + ->disableOriginalConstructor() + ->getMock(); + + $stubAdapter->expects($this->any()) + ->method('getCartTotalPrice') + ->will($this->returnValue(400)); + $stubAdapter->expects($this->any()) + ->method('getCheckoutCurrency') + ->will($this->returnValue('EUR')); + + $rule1 = new AvailableForTotalAmountManager($stubAdapter); + $operators = array( + AvailableForTotalAmountManager::INPUT1 => Operators::INFERIOR_OR_EQUAL, + AvailableForTotalAmountManager::INPUT2 => Operators::EQUAL + ); + $values = array( + AvailableForTotalAmountManager::INPUT1 => 400.00, + AvailableForTotalAmountManager::INPUT2 => 'EUR'); + $rule1->setValidatorsFromForm($operators, $values); + + $isValid = $rule1->isMatching(); + + $expected = true; + $actual =$isValid; + $this->assertEquals($expected, $actual); + } + + /** + * Check if test inferior operator is working + * + * @covers Thelia\Constraint\Rule\AvailableForTotalAmountManager::isMatching + * + */ + public function testMatchingRuleInferiorEquals2() + { + $stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter') + ->disableOriginalConstructor() + ->getMock(); + + $stubAdapter->expects($this->any()) + ->method('getCartTotalPrice') + ->will($this->returnValue(399)); + $stubAdapter->expects($this->any()) + ->method('getCheckoutCurrency') + ->will($this->returnValue('EUR')); + + $rule1 = new AvailableForTotalAmountManager($stubAdapter); + $operators = array( + AvailableForTotalAmountManager::INPUT1 => Operators::INFERIOR_OR_EQUAL, + AvailableForTotalAmountManager::INPUT2 => Operators::EQUAL + ); + $values = array( + AvailableForTotalAmountManager::INPUT1 => 400.00, + AvailableForTotalAmountManager::INPUT2 => 'EUR'); + $rule1->setValidatorsFromForm($operators, $values); + + $isValid = $rule1->isMatching(); + + $expected = true; + $actual =$isValid; + $this->assertEquals($expected, $actual); + } + + /** + * Check if test inferior operator is working + * + * @covers Thelia\Constraint\Rule\AvailableForTotalAmountManager::isMatching + * + */ + public function testNotMatchingRuleInferiorEquals() + { + $stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter') + ->disableOriginalConstructor() + ->getMock(); + + $stubAdapter->expects($this->any()) + ->method('getCartTotalPrice') + ->will($this->returnValue(401)); + $stubAdapter->expects($this->any()) + ->method('getCheckoutCurrency') + ->will($this->returnValue('EUR')); + + $rule1 = new AvailableForTotalAmountManager($stubAdapter); + $operators = array( + AvailableForTotalAmountManager::INPUT1 => Operators::INFERIOR_OR_EQUAL, + AvailableForTotalAmountManager::INPUT2 => Operators::EQUAL + ); + $values = array( + AvailableForTotalAmountManager::INPUT1 => 400.00, + AvailableForTotalAmountManager::INPUT2 => 'EUR'); + $rule1->setValidatorsFromForm($operators, $values); + + $isValid = $rule1->isMatching(); + + $expected = false; + $actual =$isValid; $this->assertEquals($expected, $actual); } /** * Check if test equals operator is working * - * @covers Thelia\Coupon\Rule\AvailableForTotalAmount::isMatching + * @covers Thelia\Constraint\Rule\AvailableForTotalAmountManager::isMatching * */ public function testMatchingRuleEqual() { - $adapter = $this->stubTheliaAdapter; + $stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter') + ->disableOriginalConstructor() + ->getMock(); - $validators = array( - AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( - Operators::EQUAL, - new PriceParam( - $adapter, 421.23, 'EUR' - ) - ) + $stubAdapter->expects($this->any()) + ->method('getCartTotalPrice') + ->will($this->returnValue(400)); + $stubAdapter->expects($this->any()) + ->method('getCheckoutCurrency') + ->will($this->returnValue('EUR')); + + $rule1 = new AvailableForTotalAmountManager($stubAdapter); + $operators = array( + AvailableForTotalAmountManager::INPUT1 => Operators::EQUAL, + AvailableForTotalAmountManager::INPUT2 => Operators::EQUAL ); + $values = array( + AvailableForTotalAmountManager::INPUT1 => 400.00, + AvailableForTotalAmountManager::INPUT2 => 'EUR'); + $rule1->setValidatorsFromForm($operators, $values); - $rule = new AvailableForTotalAmount($adapter, $validators); + $isValid = $rule1->isMatching(); $expected = true; - $actual = $rule->isMatching(); + $actual =$isValid; $this->assertEquals($expected, $actual); } /** * Check if test equals operator is working * - * @covers Thelia\Coupon\Rule\AvailableForTotalAmount::isMatching + * @covers Thelia\Constraint\Rule\AvailableForTotalAmountManager::isMatching * */ public function testNotMatchingRuleEqual() { - $adapter = $this->generateValidCouponBaseAdapterMock(421.22); + $stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter') + ->disableOriginalConstructor() + ->getMock(); - $validators = array( - AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( - Operators::EQUAL, - new PriceParam( - $adapter, 421.23, 'EUR' - ) - ) + $stubAdapter->expects($this->any()) + ->method('getCartTotalPrice') + ->will($this->returnValue(399)); + $stubAdapter->expects($this->any()) + ->method('getCheckoutCurrency') + ->will($this->returnValue('EUR')); + + $rule1 = new AvailableForTotalAmountManager($stubAdapter); + $operators = array( + AvailableForTotalAmountManager::INPUT1 => Operators::EQUAL, + AvailableForTotalAmountManager::INPUT2 => Operators::EQUAL ); + $values = array( + AvailableForTotalAmountManager::INPUT1 => 400.00, + AvailableForTotalAmountManager::INPUT2 => 'EUR'); + $rule1->setValidatorsFromForm($operators, $values); - $rule = new AvailableForTotalAmount($adapter, $validators); + $isValid = $rule1->isMatching(); $expected = false; - $actual = $rule->isMatching(); + $actual =$isValid; $this->assertEquals($expected, $actual); } /** * Check if test superior operator is working * - * @covers Thelia\Coupon\Rule\AvailableForTotalAmount::isMatching + * @covers Thelia\Constraint\Rule\AvailableForTotalAmountManager::isMatching + * + */ + public function testMatchingRuleSuperiorEquals() + { + $stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter') + ->disableOriginalConstructor() + ->getMock(); + + $stubAdapter->expects($this->any()) + ->method('getCartTotalPrice') + ->will($this->returnValue(401)); + $stubAdapter->expects($this->any()) + ->method('getCheckoutCurrency') + ->will($this->returnValue('EUR')); + + $rule1 = new AvailableForTotalAmountManager($stubAdapter); + $operators = array( + AvailableForTotalAmountManager::INPUT1 => Operators::SUPERIOR_OR_EQUAL, + AvailableForTotalAmountManager::INPUT2 => Operators::EQUAL + ); + $values = array( + AvailableForTotalAmountManager::INPUT1 => 400.00, + AvailableForTotalAmountManager::INPUT2 => 'EUR'); + $rule1->setValidatorsFromForm($operators, $values); + + $isValid = $rule1->isMatching(); + + $expected = true; + $actual =$isValid; + $this->assertEquals($expected, $actual); + } + + /** + * Check if test superior operator is working + * + * @covers Thelia\Constraint\Rule\AvailableForTotalAmountManager::isMatching + * + */ + public function testMatchingRuleSuperiorEquals2() + { + $stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter') + ->disableOriginalConstructor() + ->getMock(); + + $stubAdapter->expects($this->any()) + ->method('getCartTotalPrice') + ->will($this->returnValue(400)); + $stubAdapter->expects($this->any()) + ->method('getCheckoutCurrency') + ->will($this->returnValue('EUR')); + + $rule1 = new AvailableForTotalAmountManager($stubAdapter); + $operators = array( + AvailableForTotalAmountManager::INPUT1 => Operators::SUPERIOR_OR_EQUAL, + AvailableForTotalAmountManager::INPUT2 => Operators::EQUAL + ); + $values = array( + AvailableForTotalAmountManager::INPUT1 => 400.00, + AvailableForTotalAmountManager::INPUT2 => 'EUR'); + $rule1->setValidatorsFromForm($operators, $values); + + $isValid = $rule1->isMatching(); + + $expected = true; + $actual =$isValid; + $this->assertEquals($expected, $actual); + } + + /** + * Check if test superior operator is working + * + * @covers Thelia\Constraint\Rule\AvailableForTotalAmountManager::isMatching + * + */ + public function testNotMatchingRuleSuperiorEquals() + { + $stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter') + ->disableOriginalConstructor() + ->getMock(); + + $stubAdapter->expects($this->any()) + ->method('getCartTotalPrice') + ->will($this->returnValue(399.00)); + $stubAdapter->expects($this->any()) + ->method('getCheckoutCurrency') + ->will($this->returnValue('EUR')); + + $rule1 = new AvailableForTotalAmountManager($stubAdapter); + $operators = array( + AvailableForTotalAmountManager::INPUT1 => Operators::SUPERIOR_OR_EQUAL, + AvailableForTotalAmountManager::INPUT2 => Operators::EQUAL + ); + $values = array( + AvailableForTotalAmountManager::INPUT1 => 400.00, + AvailableForTotalAmountManager::INPUT2 => 'EUR'); + $rule1->setValidatorsFromForm($operators, $values); + + $isValid = $rule1->isMatching(); + + $expected = false; + $actual =$isValid; + $this->assertEquals($expected, $actual); + } + + + /** + * Check if test superior operator is working + * + * @covers Thelia\Constraint\Rule\AvailableForTotalAmountManager::isMatching * */ public function testMatchingRuleSuperior() { - $adapter = $this->generateValidCouponBaseAdapterMock(421.24); + $stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter') + ->disableOriginalConstructor() + ->getMock(); - $validators = array( - AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( - Operators::SUPERIOR, - new PriceParam( - $adapter, 421.23, 'EUR' - ) - ) + $stubAdapter->expects($this->any()) + ->method('getCartTotalPrice') + ->will($this->returnValue(401)); + $stubAdapter->expects($this->any()) + ->method('getCheckoutCurrency') + ->will($this->returnValue('EUR')); + + $rule1 = new AvailableForTotalAmountManager($stubAdapter); + $operators = array( + AvailableForTotalAmountManager::INPUT1 => Operators::SUPERIOR, + AvailableForTotalAmountManager::INPUT2 => Operators::EQUAL ); + $values = array( + AvailableForTotalAmountManager::INPUT1 => 400.00, + AvailableForTotalAmountManager::INPUT2 => 'EUR'); + $rule1->setValidatorsFromForm($operators, $values); - $rule = new AvailableForTotalAmount($adapter, $validators); + $isValid = $rule1->isMatching(); $expected = true; - $actual = $rule->isMatching(); + $actual =$isValid; $this->assertEquals($expected, $actual); } /** * Check if test superior operator is working * - * @covers Thelia\Coupon\Rule\AvailableForTotalAmount::isMatching + * @covers Thelia\Constraint\Rule\AvailableForTotalAmountManager::isMatching * */ public function testNotMatchingRuleSuperior() { - $adapter = $this->generateValidCouponBaseAdapterMock(421.23); + $stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter') + ->disableOriginalConstructor() + ->getMock(); - $validators = array( - AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( - Operators::SUPERIOR, - new PriceParam( - $adapter, 421.23, 'EUR' - ) - ) + $stubAdapter->expects($this->any()) + ->method('getCartTotalPrice') + ->will($this->returnValue(399.00)); + $stubAdapter->expects($this->any()) + ->method('getCheckoutCurrency') + ->will($this->returnValue('EUR')); + + $rule1 = new AvailableForTotalAmountManager($stubAdapter); + $operators = array( + AvailableForTotalAmountManager::INPUT1 => Operators::SUPERIOR, + AvailableForTotalAmountManager::INPUT2 => Operators::EQUAL ); + $values = array( + AvailableForTotalAmountManager::INPUT1 => 400.00, + AvailableForTotalAmountManager::INPUT2 => 'EUR'); + $rule1->setValidatorsFromForm($operators, $values); - $rule = new AvailableForTotalAmount($adapter, $validators); + $isValid = $rule1->isMatching(); $expected = false; - $actual = $rule->isMatching(); + $actual =$isValid; + $this->assertEquals($expected, $actual); + } + + + /** + * Check currency is checked + * + * @covers Thelia\Constraint\Rule\AvailableForTotalAmountManager::isMatching + * + */ + public function testMatchingRuleCurrency() + { + $stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter') + ->disableOriginalConstructor() + ->getMock(); + + $stubAdapter->expects($this->any()) + ->method('getCartTotalPrice') + ->will($this->returnValue(400.00)); + $stubAdapter->expects($this->any()) + ->method('getCheckoutCurrency') + ->will($this->returnValue('EUR')); + + $rule1 = new AvailableForTotalAmountManager($stubAdapter); + $operators = array( + AvailableForTotalAmountManager::INPUT1 => Operators::EQUAL, + AvailableForTotalAmountManager::INPUT2 => Operators::EQUAL + ); + $values = array( + AvailableForTotalAmountManager::INPUT1 => 400.00, + AvailableForTotalAmountManager::INPUT2 => 'EUR'); + $rule1->setValidatorsFromForm($operators, $values); + + $isValid = $rule1->isMatching(); + + $expected = true; + $actual =$isValid; + $this->assertEquals($expected, $actual); + } + + /** + * Check currency is checked + * + * @covers Thelia\Constraint\Rule\AvailableForTotalAmountManager::isMatching + * + */ + public function testNotMatchingRuleCurrency() + { + $stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter') + ->disableOriginalConstructor() + ->getMock(); + + $stubAdapter->expects($this->any()) + ->method('getCartTotalPrice') + ->will($this->returnValue(400.00)); + $stubAdapter->expects($this->any()) + ->method('getCheckoutCurrency') + ->will($this->returnValue('EUR')); + + $rule1 = new AvailableForTotalAmountManager($stubAdapter); + $operators = array( + AvailableForTotalAmountManager::INPUT1 => Operators::EQUAL, + AvailableForTotalAmountManager::INPUT2 => Operators::EQUAL + ); + $values = array( + AvailableForTotalAmountManager::INPUT1 => 400.00, + AvailableForTotalAmountManager::INPUT2 => 'USD'); + $rule1->setValidatorsFromForm($operators, $values); + + $isValid = $rule1->isMatching(); + + $expected = false; + $actual =$isValid; $this->assertEquals($expected, $actual); } diff --git a/core/lib/Thelia/Tests/Constraint/Rule/AvailableForXArticlesTest.php b/core/lib/Thelia/Tests/Constraint/Rule/AvailableForXArticlesTest.php index a40591d13..d7b7523aa 100644 --- a/core/lib/Thelia/Tests/Constraint/Rule/AvailableForXArticlesTest.php +++ b/core/lib/Thelia/Tests/Constraint/Rule/AvailableForXArticlesTest.php @@ -23,11 +23,8 @@ namespace Thelia\Coupon; -use Thelia\Constraint\Rule\AvailableForXArticles; +use Thelia\Constraint\Rule\AvailableForXArticlesManager; use Thelia\Constraint\Rule\Operators; -use Thelia\Constraint\Validator\QuantityParam; -use Thelia\Constraint\Validator\RuleValidator; -use Thelia\Exception\InvalidRuleOperatorException; /** * Created by JetBrains PhpStorm. @@ -43,8 +40,8 @@ use Thelia\Exception\InvalidRuleOperatorException; class AvailableForXArticlesTest extends \PHPUnit_Framework_TestCase { - /** @var CouponAdapterInterface $stubTheliaAdapter */ - protected $stubTheliaAdapter = null; +// /** @var CouponAdapterInterface $stubTheliaAdapter */ +// protected $stubTheliaAdapter = null; /** * Sets up the fixture, for example, opens a network connection. @@ -52,54 +49,54 @@ class AvailableForXArticlesTest extends \PHPUnit_Framework_TestCase */ protected function setUp() { - /** @var CouponAdapterInterface $stubTheliaAdapter */ - $this->stubTheliaAdapter = $this->generateValidCouponBaseAdapterMock(); +// /** @var CouponAdapterInterface $stubTheliaAdapter */ +// $this->stubTheliaAdapter = $this->generateValidCouponBaseAdapterMock(); } - /** - * Generate valid CouponBaseAdapter - * - * @param int $nbArticlesInCart Total articles in the current Cart - * - * @return CouponAdapterInterface - */ - protected function generateValidCouponBaseAdapterMock($nbArticlesInCart = 4) - { - /** @var CouponAdapterInterface $stubTheliaAdapter */ - $stubTheliaAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter') - ->disableOriginalConstructor() - ->setMethods(array('getNbArticlesInCart')) - ->getMock(); - $stubTheliaAdapter->expects($this->any()) - ->method('getNbArticlesInCart') - ->will($this->returnValue($nbArticlesInCart)); +// /** +// * Generate valid CouponBaseAdapter +// * +// * @param int $nbArticlesInCart Total articles in the current Cart +// * +// * @return CouponAdapterInterface +// */ +// protected function generateValidCouponBaseAdapterMock($nbArticlesInCart = 4) +// { +// /** @var CouponAdapterInterface $stubTheliaAdapter */ +// $stubTheliaAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter') +// ->disableOriginalConstructor() +// ->setMethods(array('getNbArticlesInCart')) +// ->getMock(); +// $stubTheliaAdapter->expects($this->any()) +// ->method('getNbArticlesInCart') +// ->will($this->returnValue($nbArticlesInCart)); +// +// return $stubTheliaAdapter; +// } - return $stubTheliaAdapter; - } - - /** - * Check if validity test on BackOffice inputs are working - * - * @covers Thelia\Coupon\Rule\AvailableForXArticles::checkBackOfficeInput - * - */ - public function testValidBackOfficeInput() - { - $translator = $this->getMockBuilder('\Thelia\Core\Translation\Translator') - ->disableOriginalConstructor() - ->getMock(); - - $rule = new AvailableForXArticles($translator); - $operators = array(AvailableForXArticles::PARAM1_QUANTITY => Operators::SUPERIOR); - $values = array( - AvailableForXArticles::PARAM1_QUANTITY => 4 - ); - $rule->populateFromForm($operators, $values); - - $expected = true; - $actual = $rule->checkBackOfficeInput(); - $this->assertEquals($expected, $actual); - } +// /** +// * Check if validity test on BackOffice inputs are working +// * +// * @covers Thelia\Coupon\Rule\AvailableForXArticles::checkBackOfficeInput +// * +// */ +// public function testValidBackOfficeInput() +// { +// $translator = $this->getMockBuilder('\Thelia\Core\Translation\Translator') +// ->disableOriginalConstructor() +// ->getMock(); +// +// $rule = new AvailableForXArticles($translator); +// $operators = array(AvailableForXArticles::PARAM1_QUANTITY => Operators::SUPERIOR); +// $values = array( +// AvailableForXArticles::PARAM1_QUANTITY => 4 +// ); +// $rule->populateFromForm($operators, $values); +// +// $expected = true; +// $actual = $rule->checkBackOfficeInput(); +// $this->assertEquals($expected, $actual); +// } // /** // * Check if validity test on BackOffice inputs are working @@ -126,7 +123,7 @@ class AvailableForXArticlesTest extends \PHPUnit_Framework_TestCase // $actual = $rule->checkBackOfficeInput(); // $this->assertEquals($expected, $actual); // } -// + // /** // * Check if validity test on BackOffice inputs are working // * @@ -152,7 +149,7 @@ class AvailableForXArticlesTest extends \PHPUnit_Framework_TestCase // $actual = $rule->checkBackOfficeInput(); // $this->assertEquals($expected, $actual); // } -// + // /** // * Check if validity test on BackOffice inputs are working // * @@ -178,212 +175,395 @@ class AvailableForXArticlesTest extends \PHPUnit_Framework_TestCase // $actual = $rule->checkBackOfficeInput(); // $this->assertEquals($expected, $actual); // } -// -// -// -// -// -// /** -// * Check if validity test on FrontOffice inputs are working -// * -// * @covers Thelia\Coupon\Rule\AvailableForXArticles::checkCheckoutInput -// */ -// public function testValidCheckoutInput() -// { -// $adapter = $this->stubTheliaAdapter; -// $validators = array( -// AvailableForXArticles::PARAM1_QUANTITY => new RuleValidator( -// Operators::SUPERIOR, -// new QuantityParam( -// $adapter, -// 4 -// ) -// ) -// ); -// $rule = new AvailableForXArticles($adapter, $validators); -// -// $expected = true; -// $actual = $rule->checkCheckoutInput(); -// $this->assertEquals($expected, $actual); -// } -// -// /** -// * Check if validity test on FrontOffice inputs are working -// * -// * @covers Thelia\Coupon\Rule\AvailableForXArticles::checkCheckoutInput -// * @expectedException \Thelia\Exception\InvalidRuleValueException -// */ -// public function testInValidCheckoutInputFloat() -// { -// $adapter = $this->generateValidCouponBaseAdapterMock(4.5); -// $validators = array( -// AvailableForXArticles::PARAM1_QUANTITY => new RuleValidator( -// Operators::SUPERIOR, -// new QuantityParam( -// $adapter, -// 4 -// ) -// ) -// ); -// $rule = new AvailableForXArticles($adapter, $validators); -// -// $expected = false; -// $actual = $rule->checkCheckoutInput(); -// $this->assertEquals($expected, $actual); -// } -// -// /** -// * Check if validity test on FrontOffice inputs are working -// * -// * @covers Thelia\Coupon\Rule\AvailableForXArticles::checkCheckoutInput -// * @expectedException \Thelia\Exception\InvalidRuleValueException -// */ -// public function testInValidCheckoutInputNegative() -// { -// $adapter = $this->generateValidCouponBaseAdapterMock(-1); -// -// $validators = array( -// AvailableForXArticles::PARAM1_QUANTITY => new RuleValidator( -// Operators::SUPERIOR, -// new QuantityParam( -// $adapter, -// 4 -// ) -// ) -// ); -// $rule = new AvailableForXArticles($adapter, $validators); -// -// $expected = false; -// $actual = $rule->checkCheckoutInput(); -// $this->assertEquals($expected, $actual); -// } -// -// /** -// * Check if validity test on FrontOffice inputs are working -// * -// * @covers Thelia\Coupon\Rule\AvailableForXArticles::checkCheckoutInput -// * @expectedException \Thelia\Exception\InvalidRuleValueException -// */ -// public function testInValidCheckoutInputString() -// { -// $adapter = $this->generateValidCouponBaseAdapterMock('bad'); -// -// $validators = array( -// AvailableForXArticles::PARAM1_QUANTITY => new RuleValidator( -// Operators::SUPERIOR, -// new QuantityParam( -// $adapter, -// 4 -// ) -// ) -// ); -// $rule = new AvailableForXArticles($adapter, $validators); -// -// $expected = false; -// $actual = $rule->checkCheckoutInput(); -// $this->assertEquals($expected, $actual); -// } -// -// /** -// * Check if test inferior operator is working -// * -// * @covers Thelia\Coupon\Rule\AvailableForXArticles::isMatching -// * -// */ -// public function testMatchingRuleInferior() -// { -// $adapter = $this->stubTheliaAdapter; -// $validators = array( -// AvailableForXArticles::PARAM1_QUANTITY => new RuleValidator( -// Operators::INFERIOR, -// new QuantityParam( -// $adapter, -// 5 -// ) -// ) -// ); -// $rule = new AvailableForXArticles($adapter, $validators); -// -// $expected = true; -// $actual = $rule->isMatching(); -// $this->assertEquals($expected, $actual); -// } -// -// /** -// * Check if test equals operator is working -// * -// * @covers Thelia\Coupon\Rule\AvailableForXArticles::isMatching -// * -// */ -// public function testMatchingRuleEqual() -// { -// $adapter = $this->stubTheliaAdapter; -// $validators = array( -// AvailableForXArticles::PARAM1_QUANTITY => new RuleValidator( -// Operators::EQUAL, -// new QuantityParam( -// $adapter, -// 4 -// ) -// ) -// ); -// $rule = new AvailableForXArticles($adapter, $validators); -// -// $expected = true; -// $actual = $rule->isMatching(); -// $this->assertEquals($expected, $actual); -// } -// -// /** -// * Check if test superior operator is working -// * -// * @covers Thelia\Coupon\Rule\AvailableForXArticles::isMatching -// * -// */ -// public function testMatchingRuleSuperior() -// { -// $adapter = $this->stubTheliaAdapter; -// $validators = array( -// AvailableForXArticles::PARAM1_QUANTITY => new RuleValidator( -// Operators::SUPERIOR, -// new QuantityParam( -// $adapter, -// 3 -// ) -// ) -// ); -// $rule = new AvailableForXArticles($adapter, $validators); -// -// $expected = true; -// $actual = $rule->isMatching(); -// $this->assertEquals($expected, $actual); -// } -// -// /** -// * Check if test unavailable operator is working -// * -// * @covers Thelia\Coupon\Rule\AvailableForXArticles::isMatching -// * @expectedException \Thelia\Exception\InvalidRuleOperatorException -// * -// */ -// public function testNotMatchingRule() -// { -// $adapter = $this->stubTheliaAdapter; -// $validators = array( -// AvailableForXArticles::PARAM1_QUANTITY => new RuleValidator( -// Operators::DIFFERENT, -// new QuantityParam( -// $adapter, -// 3 -// ) -// ) -// ); -// $rule = new AvailableForXArticles($adapter, $validators); -// -// $expected = false; -// $actual = $rule->isMatching(); -// $this->assertEquals($expected, $actual); -// } + + + + + + /** + * Check if test inferior operator is working + * + * @covers Thelia\Constraint\Rule\AvailableForXArticlesManager::isMatching + * + */ + public function testMatchingRuleInferior() + { + $stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter') + ->disableOriginalConstructor() + ->getMock(); + + $stubAdapter->expects($this->any()) + ->method('getNbArticlesInCart') + ->will($this->returnValue(4)); + + $rule1 = new AvailableForXArticlesManager($stubAdapter); + $operators = array( + AvailableForXArticlesManager::INPUT1 => Operators::INFERIOR + ); + $values = array( + AvailableForXArticlesManager::INPUT1 => 5 + ); + $rule1->setValidatorsFromForm($operators, $values); + + $isValid = $rule1->isMatching(); + + $expected = true; + $actual =$isValid; + $this->assertEquals($expected, $actual); + } + + /** + * Check if test inferior operator is working + * + * @covers Thelia\Constraint\Rule\AvailableForXArticlesManager::isMatching + * + */ + public function testNotMatchingRuleInferior() + { + $stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter') + ->disableOriginalConstructor() + ->getMock(); + + $stubAdapter->expects($this->any()) + ->method('getNbArticlesInCart') + ->will($this->returnValue(4)); + + $rule1 = new AvailableForXArticlesManager($stubAdapter); + $operators = array( + AvailableForXArticlesManager::INPUT1 => Operators::INFERIOR + ); + $values = array( + AvailableForXArticlesManager::INPUT1 => 4, + ); + $rule1->setValidatorsFromForm($operators, $values); + + $isValid = $rule1->isMatching(); + + $expected = false; + $actual =$isValid; + $this->assertEquals($expected, $actual); + } + + /** + * Check if test inferior operator is working + * + * @covers Thelia\Constraint\Rule\AvailableForXArticlesManager::isMatching + * + */ + public function testMatchingRuleInferiorEquals() + { + $stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter') + ->disableOriginalConstructor() + ->getMock(); + + $stubAdapter->expects($this->any()) + ->method('getNbArticlesInCart') + ->will($this->returnValue(4)); + + $rule1 = new AvailableForXArticlesManager($stubAdapter); + $operators = array( + AvailableForXArticlesManager::INPUT1 => Operators::INFERIOR_OR_EQUAL, + ); + $values = array( + AvailableForXArticlesManager::INPUT1 => 5, + ); + $rule1->setValidatorsFromForm($operators, $values); + + $isValid = $rule1->isMatching(); + + $expected = true; + $actual =$isValid; + $this->assertEquals($expected, $actual); + } + + /** + * Check if test inferior operator is working + * + * @covers Thelia\Constraint\Rule\AvailableForXArticlesManager::isMatching + * + */ + public function testMatchingRuleInferiorEquals2() + { + $stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter') + ->disableOriginalConstructor() + ->getMock(); + + $stubAdapter->expects($this->any()) + ->method('getNbArticlesInCart') + ->will($this->returnValue(4)); + + $rule1 = new AvailableForXArticlesManager($stubAdapter); + $operators = array( + AvailableForXArticlesManager::INPUT1 => Operators::INFERIOR_OR_EQUAL + ); + $values = array( + AvailableForXArticlesManager::INPUT1 => 4 + ); + $rule1->setValidatorsFromForm($operators, $values); + + $isValid = $rule1->isMatching(); + + $expected = true; + $actual =$isValid; + $this->assertEquals($expected, $actual); + } + + /** + * Check if test inferior operator is working + * + * @covers Thelia\Constraint\Rule\AvailableForXArticlesManager::isMatching + * + */ + public function testNotMatchingRuleInferiorEquals() + { + $stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter') + ->disableOriginalConstructor() + ->getMock(); + + $stubAdapter->expects($this->any()) + ->method('getNbArticlesInCart') + ->will($this->returnValue(4)); + + $rule1 = new AvailableForXArticlesManager($stubAdapter); + $operators = array( + AvailableForXArticlesManager::INPUT1 => Operators::INFERIOR_OR_EQUAL + ); + $values = array( + AvailableForXArticlesManager::INPUT1 => 3 + ); + $rule1->setValidatorsFromForm($operators, $values); + + $isValid = $rule1->isMatching(); + + $expected = false; + $actual =$isValid; + $this->assertEquals($expected, $actual); + } + + /** + * Check if test equals operator is working + * + * @covers Thelia\Constraint\Rule\AvailableForXArticlesManager::isMatching + * + */ + public function testMatchingRuleEqual() + { + $stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter') + ->disableOriginalConstructor() + ->getMock(); + + $stubAdapter->expects($this->any()) + ->method('getNbArticlesInCart') + ->will($this->returnValue(4)); + + $rule1 = new AvailableForXArticlesManager($stubAdapter); + $operators = array( + AvailableForXArticlesManager::INPUT1 => Operators::EQUAL + ); + $values = array( + AvailableForXArticlesManager::INPUT1 => 4 + ); + $rule1->setValidatorsFromForm($operators, $values); + + $isValid = $rule1->isMatching(); + + $expected = true; + $actual =$isValid; + $this->assertEquals($expected, $actual); + } + + /** + * Check if test equals operator is working + * + * @covers Thelia\Constraint\Rule\AvailableForXArticlesManager::isMatching + * + */ + public function testNotMatchingRuleEqual() + { + $stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter') + ->disableOriginalConstructor() + ->getMock(); + + $stubAdapter->expects($this->any()) + ->method('getNbArticlesInCart') + ->will($this->returnValue(4)); + + $rule1 = new AvailableForXArticlesManager($stubAdapter); + $operators = array( + AvailableForXArticlesManager::INPUT1 => Operators::EQUAL + ); + $values = array( + AvailableForXArticlesManager::INPUT1 => 5 + ); + $rule1->setValidatorsFromForm($operators, $values); + + $isValid = $rule1->isMatching(); + + $expected = false; + $actual =$isValid; + $this->assertEquals($expected, $actual); + } + + /** + * Check if test superior operator is working + * + * @covers Thelia\Constraint\Rule\AvailableForXArticlesManager::isMatching + * + */ + public function testMatchingRuleSuperiorEquals() + { + $stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter') + ->disableOriginalConstructor() + ->getMock(); + + $stubAdapter->expects($this->any()) + ->method('getNbArticlesInCart') + ->will($this->returnValue(4)); + + $rule1 = new AvailableForXArticlesManager($stubAdapter); + $operators = array( + AvailableForXArticlesManager::INPUT1 => Operators::SUPERIOR_OR_EQUAL + ); + $values = array( + AvailableForXArticlesManager::INPUT1 => 4 + ); + $rule1->setValidatorsFromForm($operators, $values); + + $isValid = $rule1->isMatching(); + + $expected = true; + $actual =$isValid; + $this->assertEquals($expected, $actual); + } + + /** + * Check if test superior operator is working + * + * @covers Thelia\Constraint\Rule\AvailableForXArticlesManager::isMatching + * + */ + public function testMatchingRuleSuperiorEquals2() + { + $stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter') + ->disableOriginalConstructor() + ->getMock(); + + $stubAdapter->expects($this->any()) + ->method('getNbArticlesInCart') + ->will($this->returnValue(4)); + + $rule1 = new AvailableForXArticlesManager($stubAdapter); + $operators = array( + AvailableForXArticlesManager::INPUT1 => Operators::SUPERIOR_OR_EQUAL + ); + $values = array( + AvailableForXArticlesManager::INPUT1 => 3 + ); + $rule1->setValidatorsFromForm($operators, $values); + + $isValid = $rule1->isMatching(); + + $expected = true; + $actual =$isValid; + $this->assertEquals($expected, $actual); + } + + /** + * Check if test superior operator is working + * + * @covers Thelia\Constraint\Rule\AvailableForXArticlesManager::isMatching + * + */ + public function testNotMatchingRuleSuperiorEquals() + { + $stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter') + ->disableOriginalConstructor() + ->getMock(); + + $stubAdapter->expects($this->any()) + ->method('getNbArticlesInCart') + ->will($this->returnValue(4)); + + $rule1 = new AvailableForXArticlesManager($stubAdapter); + $operators = array( + AvailableForXArticlesManager::INPUT1 => Operators::SUPERIOR_OR_EQUAL + ); + $values = array( + AvailableForXArticlesManager::INPUT1 => 5 + ); + $rule1->setValidatorsFromForm($operators, $values); + + $isValid = $rule1->isMatching(); + + $expected = false; + $actual =$isValid; + $this->assertEquals($expected, $actual); + } + + + /** + * Check if test superior operator is working + * + * @covers Thelia\Constraint\Rule\AvailableForXArticlesManager::isMatching + * + */ + public function testMatchingRuleSuperior() + { + $stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter') + ->disableOriginalConstructor() + ->getMock(); + + $stubAdapter->expects($this->any()) + ->method('getNbArticlesInCart') + ->will($this->returnValue(4)); + + $rule1 = new AvailableForXArticlesManager($stubAdapter); + $operators = array( + AvailableForXArticlesManager::INPUT1 => Operators::SUPERIOR + ); + $values = array( + AvailableForXArticlesManager::INPUT1 => 3 + ); + $rule1->setValidatorsFromForm($operators, $values); + + $isValid = $rule1->isMatching(); + + $expected = true; + $actual =$isValid; + $this->assertEquals($expected, $actual); + } + + /** + * Check if test superior operator is working + * + * @covers Thelia\Constraint\Rule\AvailableForXArticlesManager::isMatching + * + */ + public function testNotMatchingRuleSuperior() + { + $stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter') + ->disableOriginalConstructor() + ->getMock(); + + $stubAdapter->expects($this->any()) + ->method('getNbArticlesInCart') + ->will($this->returnValue(4)); + + $rule1 = new AvailableForXArticlesManager($stubAdapter); + $operators = array( + AvailableForXArticlesManager::INPUT1 => Operators::SUPERIOR + ); + $values = array( + AvailableForXArticlesManager::INPUT1 => 4 + ); + $rule1->setValidatorsFromForm($operators, $values); + + $isValid = $rule1->isMatching(); + + $expected = false; + $actual =$isValid; + $this->assertEquals($expected, $actual); + } /** diff --git a/core/lib/Thelia/Tests/Constraint/Rule/OperatorsTest.php b/core/lib/Thelia/Tests/Constraint/Rule/OperatorsTest.php index aecd303b2..01d753201 100644 --- a/core/lib/Thelia/Tests/Constraint/Rule/OperatorsTest.php +++ b/core/lib/Thelia/Tests/Constraint/Rule/OperatorsTest.php @@ -48,365 +48,365 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase { } - /** - * - * @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator - * - */ - public function testOperatorInferiorValidBefore() - { - $adapter = new CouponBaseAdapter(); - // Given - $a = 11; - $operator = Operators::INFERIOR; - $b = new QuantityParam($adapter, 12); - - // When - $actual = Operators::isValid($a, $operator, $b); - - // Then - $this->assertTrue($actual); - } - - /** - * - * @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator - * - */ - public function testOperatorInferiorInvalidEquals() - { - // Given - $adapter = new CouponBaseAdapter(); - $a = 12; - $operator = Operators::INFERIOR; - $b = new QuantityParam($adapter, 12); - - // When - $actual = Operators::isValid($a, $operator, $b); - - // Then - $this->assertFalse($actual); - } - - /** - * - * @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator - * - */ - public function testOperatorInferiorInvalidAfter() - { - // Given - $adapter = new CouponBaseAdapter(); - $a = 13; - $operator = Operators::INFERIOR; - $b = new QuantityParam($adapter, 12); - - // When - $actual = Operators::isValid($a, $operator, $b); - - // Then - $this->assertFalse($actual); - } - - /** - * - * @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator - * - */ - public function testOperatorInferiorOrEqualValidEqual() - { - // Given - $adapter = new CouponBaseAdapter(); - $a = 11; - $operator = Operators::INFERIOR_OR_EQUAL; - $b = new QuantityParam($adapter, 11); - - // When - $actual = Operators::isValid($a, $operator, $b); - - // Then - $this->assertTrue($actual); - } - - /** - * - * @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator - * - */ - public function testOperatorInferiorOrEqualValidBefore() - { - // Given - $adapter = new CouponBaseAdapter(); - $a = 10; - $operator = Operators::INFERIOR_OR_EQUAL; - $b = new QuantityParam($adapter, 11); - - // When - $actual = Operators::isValid($a, $operator, $b); - - // Then - $this->assertTrue($actual); - } - - /** - * - * @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator - * - */ - public function testOperatorInferiorOrEqualInValidAfter() - { - // Given - $adapter = new CouponBaseAdapter(); - $a = 12; - $operator = Operators::INFERIOR_OR_EQUAL; - $b = new QuantityParam($adapter, 11); - - // When - $actual = Operators::isValid($a, $operator, $b); - - // Then - $this->assertFalse($actual); - } - - /** - * - * @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator - * - */ - public function testOperatorEqualValidEqual() - { - // Given - $adapter = new CouponBaseAdapter(); - $a = 12; - $operator = Operators::EQUAL; - $b = new QuantityParam($adapter, 12); - - // When - $actual = Operators::isValid($a, $operator, $b); - - // Then - $this->assertTrue($actual); - } - - /** - * - * @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator - * - */ - public function testOperatorEqualInValidBefore() - { - // Given - $adapter = new CouponBaseAdapter(); - $a = 11; - $operator = Operators::EQUAL; - $b = new QuantityParam($adapter, 12); - - // When - $actual = Operators::isValid($a, $operator, $b); - - // Then - $this->assertFalse($actual); - } - - /** - * - * @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator - * - */ - public function testOperatorEqualInValidAfter() - { - // Given - $adapter = new CouponBaseAdapter(); - $a = 13; - $operator = Operators::EQUAL; - $b = new QuantityParam($adapter, 12); - - // When - $actual = Operators::isValid($a, $operator, $b); - - // Then - $this->assertFalse($actual); - } - - /** - * - * @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator - * - */ - public function testOperatorSuperiorOrEqualValidEqual() - { - // Given - $adapter = new CouponBaseAdapter(); - $a = 13; - $operator = Operators::SUPERIOR_OR_EQUAL; - $b = new QuantityParam($adapter, 13); - - // When - $actual = Operators::isValid($a, $operator, $b); - - // Then - $this->assertTrue($actual); - } - - /** - * - * @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator - * - */ - public function testOperatorSuperiorOrEqualAfter() - { - // Given - $adapter = new CouponBaseAdapter(); - $a = 14; - $operator = Operators::SUPERIOR_OR_EQUAL; - $b = new QuantityParam($adapter, 13); - - // When - $actual = Operators::isValid($a, $operator, $b); - - // Then - $this->assertTrue($actual); - } - - /** - * - * @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator - * - */ - public function testOperatorSuperiorOrEqualInvalidBefore() - { - // Given - $adapter = new CouponBaseAdapter(); - $a = 12; - $operator = Operators::SUPERIOR_OR_EQUAL; - $b = new QuantityParam($adapter, 13); - - // When - $actual = Operators::isValid($a, $operator, $b); - - // Then - $this->assertFalse($actual); - } - - /** - * - * @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator - * - */ - public function testOperatorSuperiorValidAfter() - { - // Given - $adapter = new CouponBaseAdapter(); - $a = 13; - $operator = Operators::SUPERIOR; - $b = new QuantityParam($adapter, 12); - - // When - $actual = Operators::isValid($a, $operator, $b); - - // Then - $this->assertTrue($actual); - } - - /** - * - * @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator - * - */ - public function testOperatorSuperiorInvalidEqual() - { - // Given - $adapter = new CouponBaseAdapter(); - $a = 12; - $operator = Operators::SUPERIOR; - $b = new QuantityParam($adapter, 12); - - // When - $actual = Operators::isValid($a, $operator, $b); - - // Then - $this->assertFalse($actual); - } - - /** - * - * @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator - * - */ - public function testOperatorSuperiorInvalidBefore() - { - // Given - $adapter = new CouponBaseAdapter(); - $a = 11; - $operator = Operators::SUPERIOR; - $b = new QuantityParam($adapter, 12); - - // When - $actual = Operators::isValid($a, $operator, $b); - - // Then - $this->assertFalse($actual); - } - - /** - * - * @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator - * - */ - public function testOperatorDifferentValid() - { - // Given - $adapter = new CouponBaseAdapter(); - $a = 12; - $operator = Operators::DIFFERENT; - $b = new QuantityParam($adapter, 11); - - // When - $actual = Operators::isValid($a, $operator, $b); - - // Then - $this->assertTrue($actual); - } - - /** - * - * @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator - * - */ - public function testOperatorDifferentInvalidEquals() - { - // Given - $adapter = new CouponBaseAdapter(); - $a = 11; - $operator = Operators::DIFFERENT; - $b = new QuantityParam($adapter, 11); - - // When - $actual = Operators::isValid($a, $operator, $b); - - // Then - $this->assertFalse($actual); - } - - /** - * - * @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator - * - */ - public function testOperatorInValid() - { - // Given - $adapter = new CouponBaseAdapter(); - $a = 12; - $operator = 'X'; - $b = new QuantityParam($adapter, 11); - - // When - $actual = Operators::isValid($a, $operator, $b); - - // Then - $this->assertFalse($actual); - } +// /** +// * +// * @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator +// * +// */ +// public function testOperatorInferiorValidBefore() +// { +// $adapter = new CouponBaseAdapter(); +// // Given +// $a = 11; +// $operator = Operators::INFERIOR; +// $b = new QuantityParam($adapter, 12); +// +// // When +// $actual = Operators::isValid($a, $operator, $b); +// +// // Then +// $this->assertTrue($actual); +// } +// +// /** +// * +// * @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator +// * +// */ +// public function testOperatorInferiorInvalidEquals() +// { +// // Given +// $adapter = new CouponBaseAdapter(); +// $a = 12; +// $operator = Operators::INFERIOR; +// $b = new QuantityParam($adapter, 12); +// +// // When +// $actual = Operators::isValid($a, $operator, $b); +// +// // Then +// $this->assertFalse($actual); +// } +// +// /** +// * +// * @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator +// * +// */ +// public function testOperatorInferiorInvalidAfter() +// { +// // Given +// $adapter = new CouponBaseAdapter(); +// $a = 13; +// $operator = Operators::INFERIOR; +// $b = new QuantityParam($adapter, 12); +// +// // When +// $actual = Operators::isValid($a, $operator, $b); +// +// // Then +// $this->assertFalse($actual); +// } +// +// /** +// * +// * @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator +// * +// */ +// public function testOperatorInferiorOrEqualValidEqual() +// { +// // Given +// $adapter = new CouponBaseAdapter(); +// $a = 11; +// $operator = Operators::INFERIOR_OR_EQUAL; +// $b = new QuantityParam($adapter, 11); +// +// // When +// $actual = Operators::isValid($a, $operator, $b); +// +// // Then +// $this->assertTrue($actual); +// } +// +// /** +// * +// * @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator +// * +// */ +// public function testOperatorInferiorOrEqualValidBefore() +// { +// // Given +// $adapter = new CouponBaseAdapter(); +// $a = 10; +// $operator = Operators::INFERIOR_OR_EQUAL; +// $b = new QuantityParam($adapter, 11); +// +// // When +// $actual = Operators::isValid($a, $operator, $b); +// +// // Then +// $this->assertTrue($actual); +// } +// +// /** +// * +// * @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator +// * +// */ +// public function testOperatorInferiorOrEqualInValidAfter() +// { +// // Given +// $adapter = new CouponBaseAdapter(); +// $a = 12; +// $operator = Operators::INFERIOR_OR_EQUAL; +// $b = new QuantityParam($adapter, 11); +// +// // When +// $actual = Operators::isValid($a, $operator, $b); +// +// // Then +// $this->assertFalse($actual); +// } +// +// /** +// * +// * @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator +// * +// */ +// public function testOperatorEqualValidEqual() +// { +// // Given +// $adapter = new CouponBaseAdapter(); +// $a = 12; +// $operator = Operators::EQUAL; +// $b = new QuantityParam($adapter, 12); +// +// // When +// $actual = Operators::isValid($a, $operator, $b); +// +// // Then +// $this->assertTrue($actual); +// } +// +// /** +// * +// * @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator +// * +// */ +// public function testOperatorEqualInValidBefore() +// { +// // Given +// $adapter = new CouponBaseAdapter(); +// $a = 11; +// $operator = Operators::EQUAL; +// $b = new QuantityParam($adapter, 12); +// +// // When +// $actual = Operators::isValid($a, $operator, $b); +// +// // Then +// $this->assertFalse($actual); +// } +// +// /** +// * +// * @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator +// * +// */ +// public function testOperatorEqualInValidAfter() +// { +// // Given +// $adapter = new CouponBaseAdapter(); +// $a = 13; +// $operator = Operators::EQUAL; +// $b = new QuantityParam($adapter, 12); +// +// // When +// $actual = Operators::isValid($a, $operator, $b); +// +// // Then +// $this->assertFalse($actual); +// } +// +// /** +// * +// * @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator +// * +// */ +// public function testOperatorSuperiorOrEqualValidEqual() +// { +// // Given +// $adapter = new CouponBaseAdapter(); +// $a = 13; +// $operator = Operators::SUPERIOR_OR_EQUAL; +// $b = new QuantityParam($adapter, 13); +// +// // When +// $actual = Operators::isValid($a, $operator, $b); +// +// // Then +// $this->assertTrue($actual); +// } +// +// /** +// * +// * @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator +// * +// */ +// public function testOperatorSuperiorOrEqualAfter() +// { +// // Given +// $adapter = new CouponBaseAdapter(); +// $a = 14; +// $operator = Operators::SUPERIOR_OR_EQUAL; +// $b = new QuantityParam($adapter, 13); +// +// // When +// $actual = Operators::isValid($a, $operator, $b); +// +// // Then +// $this->assertTrue($actual); +// } +// +// /** +// * +// * @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator +// * +// */ +// public function testOperatorSuperiorOrEqualInvalidBefore() +// { +// // Given +// $adapter = new CouponBaseAdapter(); +// $a = 12; +// $operator = Operators::SUPERIOR_OR_EQUAL; +// $b = new QuantityParam($adapter, 13); +// +// // When +// $actual = Operators::isValid($a, $operator, $b); +// +// // Then +// $this->assertFalse($actual); +// } +// +// /** +// * +// * @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator +// * +// */ +// public function testOperatorSuperiorValidAfter() +// { +// // Given +// $adapter = new CouponBaseAdapter(); +// $a = 13; +// $operator = Operators::SUPERIOR; +// $b = new QuantityParam($adapter, 12); +// +// // When +// $actual = Operators::isValid($a, $operator, $b); +// +// // Then +// $this->assertTrue($actual); +// } +// +// /** +// * +// * @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator +// * +// */ +// public function testOperatorSuperiorInvalidEqual() +// { +// // Given +// $adapter = new CouponBaseAdapter(); +// $a = 12; +// $operator = Operators::SUPERIOR; +// $b = new QuantityParam($adapter, 12); +// +// // When +// $actual = Operators::isValid($a, $operator, $b); +// +// // Then +// $this->assertFalse($actual); +// } +// +// /** +// * +// * @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator +// * +// */ +// public function testOperatorSuperiorInvalidBefore() +// { +// // Given +// $adapter = new CouponBaseAdapter(); +// $a = 11; +// $operator = Operators::SUPERIOR; +// $b = new QuantityParam($adapter, 12); +// +// // When +// $actual = Operators::isValid($a, $operator, $b); +// +// // Then +// $this->assertFalse($actual); +// } +// +// /** +// * +// * @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator +// * +// */ +// public function testOperatorDifferentValid() +// { +// // Given +// $adapter = new CouponBaseAdapter(); +// $a = 12; +// $operator = Operators::DIFFERENT; +// $b = new QuantityParam($adapter, 11); +// +// // When +// $actual = Operators::isValid($a, $operator, $b); +// +// // Then +// $this->assertTrue($actual); +// } +// +// /** +// * +// * @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator +// * +// */ +// public function testOperatorDifferentInvalidEquals() +// { +// // Given +// $adapter = new CouponBaseAdapter(); +// $a = 11; +// $operator = Operators::DIFFERENT; +// $b = new QuantityParam($adapter, 11); +// +// // When +// $actual = Operators::isValid($a, $operator, $b); +// +// // Then +// $this->assertFalse($actual); +// } +// +// /** +// * +// * @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator +// * +// */ +// public function testOperatorInValid() +// { +// // Given +// $adapter = new CouponBaseAdapter(); +// $a = 12; +// $operator = 'X'; +// $b = new QuantityParam($adapter, 11); +// +// // When +// $actual = Operators::isValid($a, $operator, $b); +// +// // Then +// $this->assertFalse($actual); +// }