- Refactor Coupon : is Customer matching Coupon rules
This commit is contained in:
gmorel
2013-09-08 17:36:21 +02:00
parent 49be95a2e7
commit e78ba1f670
14 changed files with 2415 additions and 1234 deletions

View File

@@ -220,11 +220,11 @@
<service id="thelia.constraint.manager" class="Thelia\Constraint\ConstraintManager"> <service id="thelia.constraint.manager" class="Thelia\Constraint\ConstraintManager">
<argument type="service" id="service_container" /> <argument type="service" id="service_container" />
</service> </service>
<service id="thelia.constraint.rule.available_for_x_articles" class="Thelia\Constraint\Rule\AvailableForXArticles"> <service id="thelia.constraint.rule.available_for_x_articles" class="Thelia\Constraint\Rule\AvailableForXArticlesManager">
<argument type="service" id="thelia.translator" /> <argument type="service" id="thelia.translator" />
<tag name="thelia.coupon.addRule"/> <tag name="thelia.coupon.addRule"/>
</service> </service>
<service id="thelia.constraint.rule.available_for_total_amount" class="Thelia\Constraint\Rule\AvailableForTotalAmount"> <service id="thelia.constraint.rule.available_for_total_amount" class="Thelia\Constraint\Rule\AvailableForTotalAmountManager">
<argument type="service" id="thelia.translator" /> <argument type="service" id="thelia.translator" />
<tag name="thelia.coupon.addRule"/> <tag name="thelia.coupon.addRule"/>
</service> </service>

View File

@@ -25,9 +25,7 @@ namespace Thelia\Constraint;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Serializer\Encoder\JsonEncoder; use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Encoder\XmlEncoder; use Thelia\Constraint\Rule\AvailableForTotalAmountManager;
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
use Symfony\Component\Serializer\Serializer;
use Thelia\Constraint\Rule\CouponRuleInterface; use Thelia\Constraint\Rule\CouponRuleInterface;
use Thelia\Constraint\Rule\SerializableRule; use Thelia\Constraint\Rule\SerializableRule;
use Thelia\Coupon\CouponAdapterInterface; use Thelia\Coupon\CouponAdapterInterface;
@@ -45,7 +43,7 @@ use Thelia\Coupon\CouponRuleCollection;
* @author Guillaume MOREL <gmorel@openstudio.fr> * @author Guillaume MOREL <gmorel@openstudio.fr>
* *
*/ */
class ConstraintManager class ConstraintFactory
{ {
/** @var ContainerInterface Service Container */ /** @var ContainerInterface Service Container */
protected $container = null; protected $container = null;
@@ -67,28 +65,6 @@ class ConstraintManager
$this->adapter = $container->get('thelia.adapter'); $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 * Serialize a collection of rules
* *
@@ -128,8 +104,8 @@ class ConstraintManager
foreach ($unserializedRules as $rule) { foreach ($unserializedRules as $rule) {
if ($this->container->has($rule->ruleServiceId)) { if ($this->container->has($rule->ruleServiceId)) {
/** @var CouponRuleInterface $couponRule */ /** @var CouponRuleInterface $couponRule */
$couponRule = $this->container->get($rule->ruleServiceId); $couponRule = $this->build(
$couponRule->populateFromForm( $rule->ruleServiceId,
(array) $rule->operators, (array) $rule->operators,
(array) $rule->values (array) $rule->values
); );
@@ -140,4 +116,28 @@ class ConstraintManager
return $collection; 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;
}
} }

View File

@@ -0,0 +1,133 @@
<?php
/**********************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/**********************************************************************************/
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 <gmorel@openstudio.fr>
*
*/
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;
}
}

View File

@@ -25,12 +25,14 @@ namespace Thelia\Constraint\Rule;
use Symfony\Component\Intl\Exception\NotImplementedException; use Symfony\Component\Intl\Exception\NotImplementedException;
use Symfony\Component\Translation\Translator; use Symfony\Component\Translation\Translator;
use Thelia\Constraint\ConstraintValidator;
use Thelia\Coupon\CouponAdapterInterface; use Thelia\Coupon\CouponAdapterInterface;
use Thelia\Constraint\Validator\PriceParam; use Thelia\Constraint\Validator\PriceParam;
use Thelia\Constraint\Validator\RuleValidator; use Thelia\Constraint\Validator\RuleValidator;
use Thelia\Exception\InvalidRuleException; use Thelia\Exception\InvalidRuleException;
use Thelia\Exception\InvalidRuleOperatorException; use Thelia\Exception\InvalidRuleOperatorException;
use Thelia\Exception\InvalidRuleValueException; use Thelia\Exception\InvalidRuleValueException;
use Thelia\Type\FloatType;
/** /**
* Created by JetBrains PhpStorm. * Created by JetBrains PhpStorm.
@@ -44,26 +46,33 @@ use Thelia\Exception\InvalidRuleValueException;
* @author Guillaume MOREL <gmorel@openstudio.fr> * @author Guillaume MOREL <gmorel@openstudio.fr>
* *
*/ */
class AvailableForTotalAmount extends CouponRuleAbstract class AvailableForTotalAmountManager extends CouponRuleAbstract
{ {
/** Rule 1st parameter : price */ /** Rule 1st parameter : price */
CONST PARAM1_PRICE = 'price'; CONST INPUT1 = 'price';
/** Rule 1st parameter : currency */ /** Rule 1st parameter : currency */
CONST PARAM1_CURRENCY = 'currency'; CONST INPUT2 = 'currency';
/** @var string Service Id from Resources/config.xml */ /** @var string Service Id from Resources/config.xml */
protected $serviceId = 'thelia.constraint.rule.available_for_total_amount'; protected $serviceId = 'thelia.constraint.rule.available_for_total_amount';
/** @var array Available Operators (Operators::CONST) */ /** @var array Available Operators (Operators::CONST) */
protected $availableOperators = array( protected $availableOperators = array(
Operators::INFERIOR, self::INPUT1 => array(
Operators::EQUAL, Operators::INFERIOR,
Operators::SUPERIOR, Operators::INFERIOR_OR_EQUAL,
Operators::EQUAL,
Operators::SUPERIOR_OR_EQUAL,
Operators::SUPERIOR
),
self::INPUT2 => array(
Operators::EQUAL,
)
); );
/** @var RuleValidator Price Validator */ // /** @var RuleValidator Price Validator */
protected $priceValidator = null; // protected $priceValidator = null;
/** /**
* Check if backoffice inputs are relevant or not * Check if backoffice inputs are relevant or not
@@ -96,34 +105,146 @@ class AvailableForTotalAmount extends CouponRuleAbstract
return $this->isPriceValid($price->getPrice(), $price->getCurrency()); 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 * @param array $operators Operators the Admin set in BackOffice
* @return bool * @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(); $this->setValidators(
if (empty($currency)) { $operators[self::INPUT1],
throw new InvalidRuleValueException( $values[self::INPUT1],
get_class(), self::PARAM1_CURRENCY $operators[self::INPUT2],
); $values[self::INPUT2]
}
$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); 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; return true;
} }
/** // /**
* Generate current Rule param to be validated from adapter // * Generate current Rule param to be validated from adapter
* // *
* @return $this // * @return $this
*/ // */
protected function setParametersToValidate() // protected function setParametersToValidate()
{ // {
$this->paramsToValidate = array( // $this->paramsToValidate = array(
self::PARAM1_PRICE => $this->adapter->getCartTotalPrice(), // self::PARAM1_PRICE => $this->adapter->getCartTotalPrice(),
self::PARAM1_CURRENCY => $this->adapter->getCheckoutCurrency() // self::PARAM1_CURRENCY => $this->adapter->getCheckoutCurrency()
); // );
//
return $this; // return $this;
} // }
/** /**
* Get I18n name * Get I18n name
@@ -207,38 +328,38 @@ class AvailableForTotalAmount extends CouponRuleAbstract
return $toolTip; return $toolTip;
} }
/** // /**
* Populate a Rule from a form admin // * Populate a Rule from a form admin
* // *
* @param array $operators Rule Operator set by the Admin // * @param array $operators Rule Operator set by the Admin
* @param array $values Rule Values set by the Admin // * @param array $values Rule Values set by the Admin
* // *
* @throws \InvalidArgumentException // * @throws \InvalidArgumentException
* @return $this // * @return $this
*/ // */
public function populateFromForm(array $operators, array $values) // public function populateFromForm(array $operators, array $values)
{ // {
if ($values[self::PARAM1_PRICE] === null // if ($values[self::PARAM1_PRICE] === null
|| $values[self::PARAM1_CURRENCY] === null // || $values[self::PARAM1_CURRENCY] === null
) { // ) {
throw new \InvalidArgumentException( // throw new \InvalidArgumentException(
'The Rule ' . get_class() . 'needs at least a quantity set (' . self::PARAM1_PRICE . ', ' . self::PARAM1_CURRENCY . ')' // 'The Rule ' . get_class() . 'needs at least a quantity set (' . self::PARAM1_PRICE . ', ' . self::PARAM1_CURRENCY . ')'
); // );
} // }
//
$this->priceValidator = new RuleValidator( // $this->priceValidator = new RuleValidator(
$operators[self::PARAM1_PRICE], // $operators[self::PARAM1_PRICE],
new PriceParam( // new PriceParam(
$this->translator, // $this->translator,
$values[self::PARAM1_PRICE], // $values[self::PARAM1_PRICE],
$values[self::PARAM1_CURRENCY] // $values[self::PARAM1_CURRENCY]
) // )
); // );
//
$this->validators = array(self::PARAM1_PRICE => $this->priceValidator); // $this->validators = array(self::PARAM1_PRICE => $this->priceValidator);
//
return $this; // return $this;
} // }
/** /**
* Return a serializable Rule * Return a serializable Rule
@@ -249,14 +370,9 @@ class AvailableForTotalAmount extends CouponRuleAbstract
{ {
$serializableRule = new SerializableRule(); $serializableRule = new SerializableRule();
$serializableRule->ruleServiceId = $this->serviceId; $serializableRule->ruleServiceId = $this->serviceId;
$serializableRule->operators = array( $serializableRule->operators = $this->operators;
self::PARAM1_PRICE => $this->priceValidator->getOperator()
);
$serializableRule->values = array( $serializableRule->values = $this->values;
self::PARAM1_PRICE => $this->priceValidator->getParam()->getPrice(),
self::PARAM1_CURRENCY => $this->priceValidator->getParam()->getCurrency()
);
return $serializableRule; return $serializableRule;
} }

View File

@@ -25,10 +25,13 @@ namespace Thelia\Constraint\Rule;
use InvalidArgumentException; use InvalidArgumentException;
use Symfony\Component\Translation\Translator; use Symfony\Component\Translation\Translator;
use Thelia\Constraint\ConstraintValidator;
use Thelia\Constraint\Validator\QuantityParam; use Thelia\Constraint\Validator\QuantityParam;
use Thelia\Constraint\Validator\RuleValidator; use Thelia\Constraint\Validator\RuleValidator;
use Thelia\Coupon\CouponAdapterInterface; use Thelia\Coupon\CouponAdapterInterface;
use Thelia\Exception\InvalidRuleException;
use Thelia\Exception\InvalidRuleValueException; use Thelia\Exception\InvalidRuleValueException;
use Thelia\Type\FloatType;
/** /**
* Created by JetBrains PhpStorm. * Created by JetBrains PhpStorm.
@@ -41,19 +44,23 @@ use Thelia\Exception\InvalidRuleValueException;
* @author Guillaume MOREL <gmorel@openstudio.fr> * @author Guillaume MOREL <gmorel@openstudio.fr>
* *
*/ */
class AvailableForXArticles extends CouponRuleAbstract class AvailableForXArticlesManager extends CouponRuleAbstract
{ {
/** Rule 1st parameter : quantity */ /** Rule 1st parameter : quantity */
CONST PARAM1_QUANTITY = 'quantity'; CONST INPUT1 = 'quantity';
/** @var string Service Id from Resources/config.xml */ /** @var string Service Id from Resources/config.xml */
protected $serviceId = 'thelia.constraint.rule.available_for_x_articles'; protected $serviceId = 'thelia.constraint.rule.available_for_x_articles';
/** @var array Available Operators (Operators::CONST) */ /** @var array Available Operators (Operators::CONST) */
protected $availableOperators = array( protected $availableOperators = array(
Operators::INFERIOR, self::INPUT1 => array(
Operators::EQUAL, Operators::INFERIOR,
Operators::SUPERIOR, Operators::INFERIOR_OR_EQUAL,
Operators::EQUAL,
Operators::SUPERIOR_OR_EQUAL,
Operators::SUPERIOR
)
); );
/** @var QuantityParam Quantity Validator */ /** @var QuantityParam Quantity Validator */
@@ -107,24 +114,100 @@ class AvailableForXArticles extends CouponRuleAbstract
return $this; 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 * @param array $operators Operators the Admin set in BackOffice
* @return bool * @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) $this->setValidators(
|| empty($this->paramsToValidate) $operators[self::INPUT1],
||!isset($this->paramsToValidate[self::PARAM1_QUANTITY]) $values[self::INPUT1]
) { );
throw new InvalidRuleValueException(get_class(), self::PARAM1_QUANTITY);
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; return $toolTip;
} }
/** // /**
* Populate a Rule from a form admin // * Populate a Rule from a form admin
* // *
* @param array $operators Rule Operator set by the Admin // * @param array $operators Rule Operator set by the Admin
* @param array $values Rule Values set by the Admin // * @param array $values Rule Values set by the Admin
* // *
* @throws InvalidArgumentException // * @throws InvalidArgumentException
* @return $this // * @return $this
*/ // */
public function populateFromForm(array $operators, array $values) // public function populateFromForm(array $operators, array $values)
{ // {
if ($values[self::PARAM1_QUANTITY] === null) { // if ($values[self::PARAM1_QUANTITY] === null) {
throw new InvalidArgumentException( // throw new InvalidArgumentException(
'The Rule ' . get_class() . 'needs at least a quantity set (' . self::PARAM1_QUANTITY. ')' // 'The Rule ' . get_class() . 'needs at least a quantity set (' . self::PARAM1_QUANTITY. ')'
); // );
} // }
//
$this->quantityValidator = new RuleValidator( // $this->quantityValidator = new RuleValidator(
$operators[self::PARAM1_QUANTITY], // $operators[self::PARAM1_QUANTITY],
new QuantityParam( // new QuantityParam(
$this->adapter, // $this->adapter,
$values[self::PARAM1_QUANTITY] // $values[self::PARAM1_QUANTITY]
) // )
); // );
//
$this->validators = array(self::PARAM1_QUANTITY => $this->quantityValidator); // $this->validators = array(self::PARAM1_QUANTITY => $this->quantityValidator);
//
return $this; // return $this;
} // }
/** /**
* Return a serializable Rule * Return a serializable Rule

View File

@@ -67,6 +67,12 @@ abstract class CouponRuleAbstract implements CouponRuleInterface
/** @var Translator Service Translator */ /** @var Translator Service Translator */
protected $translator = null; 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 * Constructor
* *
@@ -78,59 +84,61 @@ abstract class CouponRuleAbstract implements CouponRuleInterface
$this->translator = $adapter->getTranslator(); $this->translator = $adapter->getTranslator();
} }
/** // /**
* Check validator relevancy and store them // * Check validator relevancy and store them
* // *
* @param array $validators Array of RuleValidator // * @param array $validators Array of RuleValidator
* validating $paramsToValidate against // * validating $paramsToValidate against
* // *
* @return $this // * @return $this
* @throws InvalidRuleException // * @throws InvalidRuleException
*/ // */
protected function setValidators(array $validators) // protected function setValidators(array $validators)
{ // {
foreach ($validators as $validator) { // foreach ($validators as $validator) {
if (!$validator instanceof RuleValidator) { // if (!$validator instanceof RuleValidator) {
throw new InvalidRuleException(get_class()); // throw new InvalidRuleException(get_class());
} // }
if (!in_array($validator->getOperator(), $this->availableOperators)) { // if (!in_array($validator->getOperator(), $this->availableOperators)) {
throw new InvalidRuleOperatorException( // throw new InvalidRuleOperatorException(
get_class(), // get_class(),
$validator->getOperator() // $validator->getOperator()
); // );
} // }
} // }
$this->validators = $validators; // $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*/ // * Check if the current Checkout matches this condition
foreach ($this->validators as $param => $validator) { // *
$a = $this->paramsToValidate[$param]; // * @return bool
$operator = $validator->getOperator(); // */
/** @var ComparableInterface, RuleParameterAbstract $b */ // public function isMatching()
$b = $validator->getParam(); // {
// $this->checkBackOfficeInput();
if (!Operators::isValid($a, $operator, $b)) { // $this->checkCheckoutInput();
$isMatching = false; //
} // $isMatching = true;
} // /** @var $validator RuleValidator*/
// foreach ($this->validators as $param => $validator) {
return $isMatching; // $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 * Return all available Operators for this Rule
@@ -162,16 +170,16 @@ abstract class CouponRuleAbstract implements CouponRuleInterface
return true; return true;
} }
/** // /**
* Generate current Rule param to be validated from adapter // * Generate current Rule param to be validated from adapter
* // *
* @throws \Thelia\Exception\NotImplementedException // * @throws \Thelia\Exception\NotImplementedException
* @return $this // * @return $this
*/ // */
protected function setParametersToValidate() // protected function setParametersToValidate()
{ // {
throw new \Thelia\Exception\NotImplementedException(); // throw new \Thelia\Exception\NotImplementedException();
} // }
/** /**
* Return all validators * Return all validators
@@ -194,6 +202,17 @@ abstract class CouponRuleAbstract implements CouponRuleInterface
return $this->serviceId; 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);
}
} }

View File

@@ -46,6 +46,13 @@ interface CouponRuleInterface
*/ */
function __construct(CouponAdapterInterface $adapter); function __construct(CouponAdapterInterface $adapter);
/**
* Get Rule Service id
*
* @return string
*/
public function getServiceId();
/** /**
* Check if backoffice inputs are relevant or not * Check if backoffice inputs are relevant or not
* *
@@ -53,15 +60,33 @@ interface CouponRuleInterface
*/ */
public function checkBackOfficeInput(); public function checkBackOfficeInput();
/** // /**
* Check if Checkout inputs are relevant or not // * Check if Checkout inputs are relevant or not
* // *
* @return bool // * @return bool
*/ // */
public function checkCheckoutInput(); // 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 * @return bool
*/ */
@@ -96,15 +121,15 @@ interface CouponRuleInterface
*/ */
public function getValidators(); public function getValidators();
/** // /**
* Populate a Rule from a form admin // * Populate a Rule from a form admin
* // *
* @param array $operators Rule Operator set by the Admin // * @param array $operators Rule Operator set by the Admin
* @param array $values Rule Values set by the Admin // * @param array $values Rule Values set by the Admin
* // *
* @return bool // * @return bool
*/ // */
public function populateFromForm(array$operators, array $values); // public function populateFromForm(array$operators, array $values);
/** /**

View File

@@ -51,62 +51,66 @@ abstract class Operators
CONST SUPERIOR = '>'; CONST SUPERIOR = '>';
/** Param1 is different to Param2 */ /** Param1 is different to Param2 */
CONST DIFFERENT = '!='; 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 // * Check if a parameter is valid against a ComparableInterface from its operator
* // *
* @param mixed $a Parameter to validate // * @param mixed $a Parameter to validate
* @param string $operator Operator to validate against // * @param string $operator Operator to validate against
* @param ComparableInterface $b Comparable to validate against // * @param ComparableInterface $b Comparable to validate against
* // *
* @return bool // * @return bool
*/ // */
public static function isValid($a, $operator, ComparableInterface $b) // public static function isValid($a, $operator, ComparableInterface $b)
{ // {
$ret = false; // $ret = false;
//
try { // try {
$comparison = $b->compareTo($a); // $comparison = $b->compareTo($a);
} catch (\Exception $e) { // } catch (\Exception $e) {
return false; // return false;
} // }
//
switch ($operator) { // switch ($operator) {
case self::INFERIOR: // case self::INFERIOR:
if ($comparison == 1) { // if ($comparison == 1) {
return true; // return true;
} // }
break; // break;
case self::INFERIOR_OR_EQUAL: // case self::INFERIOR_OR_EQUAL:
if ($comparison == 1 || $comparison == 0) { // if ($comparison == 1 || $comparison == 0) {
return true; // return true;
} // }
break; // break;
case self::EQUAL: // case self::EQUAL:
if ($comparison == 0) { // if ($comparison == 0) {
return true; // return true;
} // }
break; // break;
case self::SUPERIOR_OR_EQUAL: // case self::SUPERIOR_OR_EQUAL:
if ($comparison == -1 || $comparison == 0) { // if ($comparison == -1 || $comparison == 0) {
return true; // return true;
} // }
break; // break;
case self::SUPERIOR: // case self::SUPERIOR:
if ($comparison == -1) { // if ($comparison == -1) {
return true; // return true;
} // }
break; // break;
case self::DIFFERENT: // case self::DIFFERENT:
if ($comparison != 0) { // if ($comparison != 0) {
return true; // return true;
} // }
break; // break;
default: // default:
} // }
//
return $ret; // return $ret;
} // }
/** /**
* Get operator translation * Get operator translation
@@ -162,6 +166,20 @@ abstract class Operators
'constraint' 'constraint'
); );
break; break;
case self::IN:
$ret = $translator->trans(
'in',
array(),
'constraint'
);
break;
case self::OUT:
$ret = $translator->trans(
'not in',
array(),
'constraint'
);
break;
default: default:
} }

View File

@@ -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;
// }
// }
} }

View File

@@ -24,17 +24,11 @@
namespace Thelia\Constraint; namespace Thelia\Constraint;
use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\ContainerBuilder;
use Thelia\Constraint\Rule\AvailableForXArticles; use Thelia\Constraint\Rule\AvailableForTotalAmountManager;
use Thelia\Constraint\Validator\PriceParam; use Thelia\Constraint\Rule\AvailableForXArticlesManager;
use Thelia\Constraint\Validator\RuleValidator;
use Thelia\Constraint\Rule\AvailableForTotalAmount;
use Thelia\Constraint\Rule\Operators; use Thelia\Constraint\Rule\Operators;
use Thelia\Coupon\CouponBaseAdapter; use Thelia\Coupon\CouponBaseAdapter;
use Thelia\Coupon\CouponBaseAdapterTest;
use Thelia\Coupon\CouponRuleCollection; use Thelia\Coupon\CouponRuleCollection;
use Thelia\Coupon\Type\CouponInterface;
use Thelia\Coupon\Type\RemoveXAmount;
use Thelia\Tools\PhpUnitUtils;
/** /**
* Created by JetBrains PhpStorm. * Created by JetBrains PhpStorm.
@@ -47,7 +41,7 @@ use Thelia\Tools\PhpUnitUtils;
* @author Guillaume MOREL <gmorel@openstudio.fr> * @author Guillaume MOREL <gmorel@openstudio.fr>
* *
*/ */
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') $stubTranslator = $this->getMockBuilder('\Thelia\Core\Translation\Translator')
->disableOriginalConstructor() ->disableOriginalConstructor()
@@ -75,43 +69,68 @@ class ConstraintManagerTest extends \PHPUnit_Framework_TestCase
->method('getTranslator') ->method('getTranslator')
->will($this->returnValue($stubTranslator)); ->will($this->returnValue($stubTranslator));
$stubAdapter->expects($this->any()) $rule1 = new AvailableForTotalAmountManager($stubAdapter);
->method('getCartTotalPrice') $operators = array(
->will($this->returnValue(321.98)); AvailableForTotalAmountManager::INPUT1 => Operators::SUPERIOR,
AvailableForTotalAmountManager::INPUT2 => Operators::EQUAL
$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->populateFromForm($operators, $values);
$rule2 = new AvailableForTotalAmount($stubAdapter);
$operators = array(AvailableForTotalAmount::PARAM1_PRICE => Operators::INFERIOR);
$values = array( $values = array(
AvailableForTotalAmount::PARAM1_PRICE => 400.00, AvailableForTotalAmountManager::INPUT1 => 40.00,
AvailableForTotalAmount::PARAM1_CURRENCY => 'USD' AvailableForTotalAmountManager::INPUT2 => 'EUR'
); );
$rule2->populateFromForm($operators, $values); $rule1->setValidatorsFromForm($operators, $values);
$rules = new CouponRuleCollection();
$rules->add($rule1);
$rules->add($rule2);
/** @var ConstraintManager $constraintManager */ /** @var ConstraintManager $constraintManager */
$constraintManager = new ConstraintManager($this->getContainer()); $constraintFactory = new ConstraintFactory($this->getContainer());
$ruleManager1 = $constraintFactory->build($rule1->getServiceId(), $operators, $values);
$expected = true; $expected = $rule1;
$actual = $constraintManager->isMatching($rules); $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 * Check the Rules serialization module
*/ */
@@ -129,31 +148,37 @@ class ConstraintManagerTest extends \PHPUnit_Framework_TestCase
->method('getTranslator') ->method('getTranslator')
->will($this->returnValue($stubTranslator)); ->will($this->returnValue($stubTranslator));
$rule1 = new AvailableForTotalAmount($stubAdapter); $rule1 = new AvailableForTotalAmountManager($stubAdapter);
$operators = array(AvailableForTotalAmount::PARAM1_PRICE => Operators::SUPERIOR); $operators = array(
$values = array( AvailableForTotalAmountManager::INPUT1 => Operators::SUPERIOR,
AvailableForTotalAmount::PARAM1_PRICE => 40.00, AvailableForTotalAmountManager::INPUT2 => Operators::EQUAL
AvailableForTotalAmount::PARAM1_CURRENCY => 'EUR'
); );
$rule1->populateFromForm($operators, $values); $values = array(
AvailableForTotalAmountManager::INPUT1 => 40.00,
AvailableForTotalAmountManager::INPUT2 => 'EUR'
);
$rule1->setValidatorsFromForm($operators, $values);
$rule2 = new AvailableForTotalAmount($stubAdapter); $rule2 = new AvailableForTotalAmountManager($stubAdapter);
$operators = array(AvailableForTotalAmount::PARAM1_PRICE => Operators::INFERIOR); $operators = array(
$values = array( AvailableForTotalAmountManager::INPUT1 => Operators::SUPERIOR,
AvailableForTotalAmount::PARAM1_PRICE => 400.00, AvailableForTotalAmountManager::INPUT2 => Operators::EQUAL
AvailableForTotalAmount::PARAM1_CURRENCY => 'EUR'
); );
$rule2->populateFromForm($operators, $values); $values = array(
AvailableForTotalAmountManager::INPUT1 => 400.00,
AvailableForTotalAmountManager::INPUT2 => 'EUR'
);
$rule2->setValidatorsFromForm($operators, $values);
$rules = new CouponRuleCollection(); $rules = new CouponRuleCollection();
$rules->add($rule1); $rules->add($rule1);
$rules->add($rule2); $rules->add($rule2);
/** @var ConstraintManager $constraintManager */ /** @var ConstraintManager $constraintManager */
$constraintManager = new ConstraintManager($this->getContainer()); $constraintFactory = new ConstraintFactory($this->getContainer());
$serializedRules = $constraintManager->serializeCouponRuleCollection($rules); $serializedRules = $constraintFactory->serializeCouponRuleCollection($rules);
$unserializedRules = $constraintManager->unserializeCouponRuleCollection($serializedRules); $unserializedRules = $constraintFactory->unserializeCouponRuleCollection($serializedRules);
$expected = (string)$rules; $expected = (string)$rules;
$actual = (string)$unserializedRules; $actual = (string)$unserializedRules;
@@ -182,8 +207,8 @@ class ConstraintManagerTest extends \PHPUnit_Framework_TestCase
->method('getTranslator') ->method('getTranslator')
->will($this->returnValue($stubTranslator)); ->will($this->returnValue($stubTranslator));
$rule1 = new AvailableForTotalAmount($stubAdapter); $rule1 = new AvailableForTotalAmountManager($stubAdapter);
$rule2 = new AvailableForXArticles($stubAdapter); $rule2 = new AvailableForXArticlesManager($stubAdapter);
$adapter = new CouponBaseAdapter($container); $adapter = new CouponBaseAdapter($container);

View File

@@ -0,0 +1,337 @@
<?php
/**********************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/**********************************************************************************/
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 <gmorel@openstudio.fr>
*
*/
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()
{
}
}

View File

@@ -23,12 +23,8 @@
namespace Thelia\Coupon; namespace Thelia\Coupon;
use Thelia\Constraint\Validator\PriceParam; use Thelia\Constraint\Rule\AvailableForTotalAmountManager;
use Thelia\Constraint\Validator\RuleValidator;
use Thelia\Constraint\Rule\AvailableForTotalAmount;
use Thelia\Constraint\Rule\Operators; use Thelia\Constraint\Rule\Operators;
use Thelia\Exception\InvalidRuleOperatorException;
use Thelia\Exception\InvalidRuleValueException;
/** /**
* Created by JetBrains PhpStorm. * Created by JetBrains PhpStorm.
@@ -52,344 +48,613 @@ class AvailableForTotalAmountTest extends \PHPUnit_Framework_TestCase
*/ */
protected function setUp() protected function setUp()
{ {
/** @var CouponAdapterInterface $stubTheliaAdapter */ // /** @var CouponAdapterInterface $stubTheliaAdapter */
$this->stubTheliaAdapter = $this->generateValidCouponBaseAdapterMock(); // $this->stubTheliaAdapter = $this->generateValidCouponBaseAdapterMock();
} }
/** // /**
* Generate valid CouponBaseAdapter // * Generate valid CouponBaseAdapter
* // *
* @param float $cartTotalPrice Total amount of the current Cart // * @param float $cartTotalPrice Total amount of the current Cart
* // *
* @return CouponAdapterInterface // * @return CouponAdapterInterface
*/ // */
protected function generateValidCouponBaseAdapterMock($cartTotalPrice = 421.23) // protected function generateValidCouponBaseAdapterMock($cartTotalPrice = 421.23)
{ // {
/** @var CouponAdapterInterface $stubTheliaAdapter */ // /** @var CouponAdapterInterface $stubTheliaAdapter */
$stubTheliaAdapter = $this->getMock( // $stubTheliaAdapter = $this->getMock(
'Thelia\Coupon\CouponBaseAdapter', // 'Thelia\Coupon\CouponBaseAdapter',
array('getCartTotalPrice'), // array('getCartTotalPrice'),
array() // array()
); // );
$stubTheliaAdapter->expects($this->any()) // $stubTheliaAdapter->expects($this->any())
->method('getCartTotalPrice') // ->method('getCartTotalPrice')
->will($this->returnValue($cartTotalPrice)); // ->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 // * Check if validity test on BackOffice inputs are working
* // *
* @covers Thelia\Coupon\Rule\AvailableForTotalAmount::checkBackOfficeInput // * @covers Thelia\Coupon\Rule\AvailableForTotalAmount::checkBackOfficeInput
* // * @expectedException \Thelia\Exception\InvalidRuleOperatorException
*/ // *
public function testValidBackOfficeInput() // */
{ // public function testInValidBackOfficeInputOperator()
$adapter = new CouponBaseAdapter(); // {
// $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( // * Check if validity test on BackOffice inputs are working
Operators::SUPERIOR, // *
new PriceParam( // * @covers Thelia\Coupon\Rule\AvailableForTotalAmount::checkBackOfficeInput
$adapter, 421.23, 'EUR' // * @expectedException \ErrorException
) // *
) // */
); // public function testInValidBackOfficeInputValue()
$rule = new AvailableForTotalAmount($adapter, $validators); // {
// $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 * Check if test inferior operator is working
* *
* @covers Thelia\Coupon\Rule\AvailableForTotalAmount::isMatching * @covers Thelia\Constraint\Rule\AvailableForTotalAmountManager::isMatching
* *
*/ */
public function testMatchingRuleInferior() public function testMatchingRuleInferior()
{ {
$adapter = $this->generateValidCouponBaseAdapterMock(421.22); $stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter')
->disableOriginalConstructor()
->getMock();
$validators = array( $stubAdapter->expects($this->any())
AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( ->method('getCartTotalPrice')
Operators::INFERIOR, ->will($this->returnValue(399));
new PriceParam( $stubAdapter->expects($this->any())
$adapter, 421.23, 'EUR' ->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; $expected = true;
$actual = $rule->isMatching(); $actual =$isValid;
$this->assertEquals($expected, $actual); $this->assertEquals($expected, $actual);
} }
/** /**
* Check if test inferior operator is working * Check if test inferior operator is working
* *
* @covers Thelia\Coupon\Rule\AvailableForTotalAmount::isMatching * @covers Thelia\Constraint\Rule\AvailableForTotalAmountManager::isMatching
* *
*/ */
public function testNotMatchingRuleInferior() public function testNotMatchingRuleInferior()
{ {
$adapter = $this->generateValidCouponBaseAdapterMock(421.23); $stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter')
->disableOriginalConstructor()
->getMock();
$validators = array( $stubAdapter->expects($this->any())
AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( ->method('getCartTotalPrice')
Operators::INFERIOR, ->will($this->returnValue(400));
new PriceParam( $stubAdapter->expects($this->any())
$adapter, 421.23, 'EUR' ->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; $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); $this->assertEquals($expected, $actual);
} }
/** /**
* Check if test equals operator is working * Check if test equals operator is working
* *
* @covers Thelia\Coupon\Rule\AvailableForTotalAmount::isMatching * @covers Thelia\Constraint\Rule\AvailableForTotalAmountManager::isMatching
* *
*/ */
public function testMatchingRuleEqual() public function testMatchingRuleEqual()
{ {
$adapter = $this->stubTheliaAdapter; $stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter')
->disableOriginalConstructor()
->getMock();
$validators = array( $stubAdapter->expects($this->any())
AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( ->method('getCartTotalPrice')
Operators::EQUAL, ->will($this->returnValue(400));
new PriceParam( $stubAdapter->expects($this->any())
$adapter, 421.23, 'EUR' ->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; $expected = true;
$actual = $rule->isMatching(); $actual =$isValid;
$this->assertEquals($expected, $actual); $this->assertEquals($expected, $actual);
} }
/** /**
* Check if test equals operator is working * Check if test equals operator is working
* *
* @covers Thelia\Coupon\Rule\AvailableForTotalAmount::isMatching * @covers Thelia\Constraint\Rule\AvailableForTotalAmountManager::isMatching
* *
*/ */
public function testNotMatchingRuleEqual() public function testNotMatchingRuleEqual()
{ {
$adapter = $this->generateValidCouponBaseAdapterMock(421.22); $stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter')
->disableOriginalConstructor()
->getMock();
$validators = array( $stubAdapter->expects($this->any())
AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( ->method('getCartTotalPrice')
Operators::EQUAL, ->will($this->returnValue(399));
new PriceParam( $stubAdapter->expects($this->any())
$adapter, 421.23, 'EUR' ->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; $expected = false;
$actual = $rule->isMatching(); $actual =$isValid;
$this->assertEquals($expected, $actual); $this->assertEquals($expected, $actual);
} }
/** /**
* Check if test superior operator is working * 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() public function testMatchingRuleSuperior()
{ {
$adapter = $this->generateValidCouponBaseAdapterMock(421.24); $stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter')
->disableOriginalConstructor()
->getMock();
$validators = array( $stubAdapter->expects($this->any())
AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( ->method('getCartTotalPrice')
Operators::SUPERIOR, ->will($this->returnValue(401));
new PriceParam( $stubAdapter->expects($this->any())
$adapter, 421.23, 'EUR' ->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; $expected = true;
$actual = $rule->isMatching(); $actual =$isValid;
$this->assertEquals($expected, $actual); $this->assertEquals($expected, $actual);
} }
/** /**
* Check if test superior operator is working * Check if test superior operator is working
* *
* @covers Thelia\Coupon\Rule\AvailableForTotalAmount::isMatching * @covers Thelia\Constraint\Rule\AvailableForTotalAmountManager::isMatching
* *
*/ */
public function testNotMatchingRuleSuperior() public function testNotMatchingRuleSuperior()
{ {
$adapter = $this->generateValidCouponBaseAdapterMock(421.23); $stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter')
->disableOriginalConstructor()
->getMock();
$validators = array( $stubAdapter->expects($this->any())
AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( ->method('getCartTotalPrice')
Operators::SUPERIOR, ->will($this->returnValue(399.00));
new PriceParam( $stubAdapter->expects($this->any())
$adapter, 421.23, 'EUR' ->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; $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); $this->assertEquals($expected, $actual);
} }

View File

@@ -23,11 +23,8 @@
namespace Thelia\Coupon; namespace Thelia\Coupon;
use Thelia\Constraint\Rule\AvailableForXArticles; use Thelia\Constraint\Rule\AvailableForXArticlesManager;
use Thelia\Constraint\Rule\Operators; use Thelia\Constraint\Rule\Operators;
use Thelia\Constraint\Validator\QuantityParam;
use Thelia\Constraint\Validator\RuleValidator;
use Thelia\Exception\InvalidRuleOperatorException;
/** /**
* Created by JetBrains PhpStorm. * Created by JetBrains PhpStorm.
@@ -43,8 +40,8 @@ use Thelia\Exception\InvalidRuleOperatorException;
class AvailableForXArticlesTest extends \PHPUnit_Framework_TestCase class AvailableForXArticlesTest extends \PHPUnit_Framework_TestCase
{ {
/** @var CouponAdapterInterface $stubTheliaAdapter */ // /** @var CouponAdapterInterface $stubTheliaAdapter */
protected $stubTheliaAdapter = null; // protected $stubTheliaAdapter = null;
/** /**
* Sets up the fixture, for example, opens a network connection. * Sets up the fixture, for example, opens a network connection.
@@ -52,54 +49,54 @@ class AvailableForXArticlesTest extends \PHPUnit_Framework_TestCase
*/ */
protected function setUp() protected function setUp()
{ {
/** @var CouponAdapterInterface $stubTheliaAdapter */ // /** @var CouponAdapterInterface $stubTheliaAdapter */
$this->stubTheliaAdapter = $this->generateValidCouponBaseAdapterMock(); // $this->stubTheliaAdapter = $this->generateValidCouponBaseAdapterMock();
} }
/** // /**
* Generate valid CouponBaseAdapter // * Generate valid CouponBaseAdapter
* // *
* @param int $nbArticlesInCart Total articles in the current Cart // * @param int $nbArticlesInCart Total articles in the current Cart
* // *
* @return CouponAdapterInterface // * @return CouponAdapterInterface
*/ // */
protected function generateValidCouponBaseAdapterMock($nbArticlesInCart = 4) // protected function generateValidCouponBaseAdapterMock($nbArticlesInCart = 4)
{ // {
/** @var CouponAdapterInterface $stubTheliaAdapter */ // /** @var CouponAdapterInterface $stubTheliaAdapter */
$stubTheliaAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter') // $stubTheliaAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter')
->disableOriginalConstructor() // ->disableOriginalConstructor()
->setMethods(array('getNbArticlesInCart')) // ->setMethods(array('getNbArticlesInCart'))
->getMock(); // ->getMock();
$stubTheliaAdapter->expects($this->any()) // $stubTheliaAdapter->expects($this->any())
->method('getNbArticlesInCart') // ->method('getNbArticlesInCart')
->will($this->returnValue($nbArticlesInCart)); // ->will($this->returnValue($nbArticlesInCart));
//
// return $stubTheliaAdapter;
// }
return $stubTheliaAdapter; // /**
} // * Check if validity test on BackOffice inputs are working
// *
/** // * @covers Thelia\Coupon\Rule\AvailableForXArticles::checkBackOfficeInput
* 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')
public function testValidBackOfficeInput() // ->disableOriginalConstructor()
{ // ->getMock();
$translator = $this->getMockBuilder('\Thelia\Core\Translation\Translator') //
->disableOriginalConstructor() // $rule = new AvailableForXArticles($translator);
->getMock(); // $operators = array(AvailableForXArticles::PARAM1_QUANTITY => Operators::SUPERIOR);
// $values = array(
$rule = new AvailableForXArticles($translator); // AvailableForXArticles::PARAM1_QUANTITY => 4
$operators = array(AvailableForXArticles::PARAM1_QUANTITY => Operators::SUPERIOR); // );
$values = array( // $rule->populateFromForm($operators, $values);
AvailableForXArticles::PARAM1_QUANTITY => 4 //
); // $expected = true;
$rule->populateFromForm($operators, $values); // $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 // * Check if validity test on BackOffice inputs are working
@@ -126,7 +123,7 @@ class AvailableForXArticlesTest extends \PHPUnit_Framework_TestCase
// $actual = $rule->checkBackOfficeInput(); // $actual = $rule->checkBackOfficeInput();
// $this->assertEquals($expected, $actual); // $this->assertEquals($expected, $actual);
// } // }
//
// /** // /**
// * Check if validity test on BackOffice inputs are working // * Check if validity test on BackOffice inputs are working
// * // *
@@ -152,7 +149,7 @@ class AvailableForXArticlesTest extends \PHPUnit_Framework_TestCase
// $actual = $rule->checkBackOfficeInput(); // $actual = $rule->checkBackOfficeInput();
// $this->assertEquals($expected, $actual); // $this->assertEquals($expected, $actual);
// } // }
//
// /** // /**
// * Check if validity test on BackOffice inputs are working // * Check if validity test on BackOffice inputs are working
// * // *
@@ -178,212 +175,395 @@ class AvailableForXArticlesTest extends \PHPUnit_Framework_TestCase
// $actual = $rule->checkBackOfficeInput(); // $actual = $rule->checkBackOfficeInput();
// $this->assertEquals($expected, $actual); // $this->assertEquals($expected, $actual);
// } // }
//
//
//
//
//
// /** /**
// * Check if validity test on FrontOffice inputs are working * Check if test inferior operator is working
// * *
// * @covers Thelia\Coupon\Rule\AvailableForXArticles::checkCheckoutInput * @covers Thelia\Constraint\Rule\AvailableForXArticlesManager::isMatching
// */ *
// public function testValidCheckoutInput() */
// { public function testMatchingRuleInferior()
// $adapter = $this->stubTheliaAdapter; {
// $validators = array( $stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter')
// AvailableForXArticles::PARAM1_QUANTITY => new RuleValidator( ->disableOriginalConstructor()
// Operators::SUPERIOR, ->getMock();
// new QuantityParam(
// $adapter, $stubAdapter->expects($this->any())
// 4 ->method('getNbArticlesInCart')
// ) ->will($this->returnValue(4));
// )
// ); $rule1 = new AvailableForXArticlesManager($stubAdapter);
// $rule = new AvailableForXArticles($adapter, $validators); $operators = array(
// AvailableForXArticlesManager::INPUT1 => Operators::INFERIOR
// $expected = true; );
// $actual = $rule->checkCheckoutInput(); $values = array(
// $this->assertEquals($expected, $actual); AvailableForXArticlesManager::INPUT1 => 5
// } );
// $rule1->setValidatorsFromForm($operators, $values);
// /**
// * Check if validity test on FrontOffice inputs are working $isValid = $rule1->isMatching();
// *
// * @covers Thelia\Coupon\Rule\AvailableForXArticles::checkCheckoutInput $expected = true;
// * @expectedException \Thelia\Exception\InvalidRuleValueException $actual =$isValid;
// */ $this->assertEquals($expected, $actual);
// public function testInValidCheckoutInputFloat() }
// {
// $adapter = $this->generateValidCouponBaseAdapterMock(4.5); /**
// $validators = array( * Check if test inferior operator is working
// AvailableForXArticles::PARAM1_QUANTITY => new RuleValidator( *
// Operators::SUPERIOR, * @covers Thelia\Constraint\Rule\AvailableForXArticlesManager::isMatching
// new QuantityParam( *
// $adapter, */
// 4 public function testNotMatchingRuleInferior()
// ) {
// ) $stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter')
// ); ->disableOriginalConstructor()
// $rule = new AvailableForXArticles($adapter, $validators); ->getMock();
//
// $expected = false; $stubAdapter->expects($this->any())
// $actual = $rule->checkCheckoutInput(); ->method('getNbArticlesInCart')
// $this->assertEquals($expected, $actual); ->will($this->returnValue(4));
// }
// $rule1 = new AvailableForXArticlesManager($stubAdapter);
// /** $operators = array(
// * Check if validity test on FrontOffice inputs are working AvailableForXArticlesManager::INPUT1 => Operators::INFERIOR
// * );
// * @covers Thelia\Coupon\Rule\AvailableForXArticles::checkCheckoutInput $values = array(
// * @expectedException \Thelia\Exception\InvalidRuleValueException AvailableForXArticlesManager::INPUT1 => 4,
// */ );
// public function testInValidCheckoutInputNegative() $rule1->setValidatorsFromForm($operators, $values);
// {
// $adapter = $this->generateValidCouponBaseAdapterMock(-1); $isValid = $rule1->isMatching();
//
// $validators = array( $expected = false;
// AvailableForXArticles::PARAM1_QUANTITY => new RuleValidator( $actual =$isValid;
// Operators::SUPERIOR, $this->assertEquals($expected, $actual);
// new QuantityParam( }
// $adapter,
// 4 /**
// ) * Check if test inferior operator is working
// ) *
// ); * @covers Thelia\Constraint\Rule\AvailableForXArticlesManager::isMatching
// $rule = new AvailableForXArticles($adapter, $validators); *
// */
// $expected = false; public function testMatchingRuleInferiorEquals()
// $actual = $rule->checkCheckoutInput(); {
// $this->assertEquals($expected, $actual); $stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter')
// } ->disableOriginalConstructor()
// ->getMock();
// /**
// * Check if validity test on FrontOffice inputs are working $stubAdapter->expects($this->any())
// * ->method('getNbArticlesInCart')
// * @covers Thelia\Coupon\Rule\AvailableForXArticles::checkCheckoutInput ->will($this->returnValue(4));
// * @expectedException \Thelia\Exception\InvalidRuleValueException
// */ $rule1 = new AvailableForXArticlesManager($stubAdapter);
// public function testInValidCheckoutInputString() $operators = array(
// { AvailableForXArticlesManager::INPUT1 => Operators::INFERIOR_OR_EQUAL,
// $adapter = $this->generateValidCouponBaseAdapterMock('bad'); );
// $values = array(
// $validators = array( AvailableForXArticlesManager::INPUT1 => 5,
// AvailableForXArticles::PARAM1_QUANTITY => new RuleValidator( );
// Operators::SUPERIOR, $rule1->setValidatorsFromForm($operators, $values);
// new QuantityParam(
// $adapter, $isValid = $rule1->isMatching();
// 4
// ) $expected = true;
// ) $actual =$isValid;
// ); $this->assertEquals($expected, $actual);
// $rule = new AvailableForXArticles($adapter, $validators); }
//
// $expected = false; /**
// $actual = $rule->checkCheckoutInput(); * Check if test inferior operator is working
// $this->assertEquals($expected, $actual); *
// } * @covers Thelia\Constraint\Rule\AvailableForXArticlesManager::isMatching
// *
// /** */
// * Check if test inferior operator is working public function testMatchingRuleInferiorEquals2()
// * {
// * @covers Thelia\Coupon\Rule\AvailableForXArticles::isMatching $stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter')
// * ->disableOriginalConstructor()
// */ ->getMock();
// public function testMatchingRuleInferior()
// { $stubAdapter->expects($this->any())
// $adapter = $this->stubTheliaAdapter; ->method('getNbArticlesInCart')
// $validators = array( ->will($this->returnValue(4));
// AvailableForXArticles::PARAM1_QUANTITY => new RuleValidator(
// Operators::INFERIOR, $rule1 = new AvailableForXArticlesManager($stubAdapter);
// new QuantityParam( $operators = array(
// $adapter, AvailableForXArticlesManager::INPUT1 => Operators::INFERIOR_OR_EQUAL
// 5 );
// ) $values = array(
// ) AvailableForXArticlesManager::INPUT1 => 4
// ); );
// $rule = new AvailableForXArticles($adapter, $validators); $rule1->setValidatorsFromForm($operators, $values);
//
// $expected = true; $isValid = $rule1->isMatching();
// $actual = $rule->isMatching();
// $this->assertEquals($expected, $actual); $expected = true;
// } $actual =$isValid;
// $this->assertEquals($expected, $actual);
// /** }
// * Check if test equals operator is working
// * /**
// * @covers Thelia\Coupon\Rule\AvailableForXArticles::isMatching * Check if test inferior operator is working
// * *
// */ * @covers Thelia\Constraint\Rule\AvailableForXArticlesManager::isMatching
// public function testMatchingRuleEqual() *
// { */
// $adapter = $this->stubTheliaAdapter; public function testNotMatchingRuleInferiorEquals()
// $validators = array( {
// AvailableForXArticles::PARAM1_QUANTITY => new RuleValidator( $stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter')
// Operators::EQUAL, ->disableOriginalConstructor()
// new QuantityParam( ->getMock();
// $adapter,
// 4 $stubAdapter->expects($this->any())
// ) ->method('getNbArticlesInCart')
// ) ->will($this->returnValue(4));
// );
// $rule = new AvailableForXArticles($adapter, $validators); $rule1 = new AvailableForXArticlesManager($stubAdapter);
// $operators = array(
// $expected = true; AvailableForXArticlesManager::INPUT1 => Operators::INFERIOR_OR_EQUAL
// $actual = $rule->isMatching(); );
// $this->assertEquals($expected, $actual); $values = array(
// } AvailableForXArticlesManager::INPUT1 => 3
// );
// /** $rule1->setValidatorsFromForm($operators, $values);
// * Check if test superior operator is working
// * $isValid = $rule1->isMatching();
// * @covers Thelia\Coupon\Rule\AvailableForXArticles::isMatching
// * $expected = false;
// */ $actual =$isValid;
// public function testMatchingRuleSuperior() $this->assertEquals($expected, $actual);
// { }
// $adapter = $this->stubTheliaAdapter;
// $validators = array( /**
// AvailableForXArticles::PARAM1_QUANTITY => new RuleValidator( * Check if test equals operator is working
// Operators::SUPERIOR, *
// new QuantityParam( * @covers Thelia\Constraint\Rule\AvailableForXArticlesManager::isMatching
// $adapter, *
// 3 */
// ) public function testMatchingRuleEqual()
// ) {
// ); $stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter')
// $rule = new AvailableForXArticles($adapter, $validators); ->disableOriginalConstructor()
// ->getMock();
// $expected = true;
// $actual = $rule->isMatching(); $stubAdapter->expects($this->any())
// $this->assertEquals($expected, $actual); ->method('getNbArticlesInCart')
// } ->will($this->returnValue(4));
//
// /** $rule1 = new AvailableForXArticlesManager($stubAdapter);
// * Check if test unavailable operator is working $operators = array(
// * AvailableForXArticlesManager::INPUT1 => Operators::EQUAL
// * @covers Thelia\Coupon\Rule\AvailableForXArticles::isMatching );
// * @expectedException \Thelia\Exception\InvalidRuleOperatorException $values = array(
// * AvailableForXArticlesManager::INPUT1 => 4
// */ );
// public function testNotMatchingRule() $rule1->setValidatorsFromForm($operators, $values);
// {
// $adapter = $this->stubTheliaAdapter; $isValid = $rule1->isMatching();
// $validators = array(
// AvailableForXArticles::PARAM1_QUANTITY => new RuleValidator( $expected = true;
// Operators::DIFFERENT, $actual =$isValid;
// new QuantityParam( $this->assertEquals($expected, $actual);
// $adapter, }
// 3
// ) /**
// ) * Check if test equals operator is working
// ); *
// $rule = new AvailableForXArticles($adapter, $validators); * @covers Thelia\Constraint\Rule\AvailableForXArticlesManager::isMatching
// *
// $expected = false; */
// $actual = $rule->isMatching(); public function testNotMatchingRuleEqual()
// $this->assertEquals($expected, $actual); {
// } $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);
}
/** /**

View File

@@ -48,365 +48,365 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase
{ {
} }
/** // /**
* // *
* @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator // * @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator
* // *
*/ // */
public function testOperatorInferiorValidBefore() // public function testOperatorInferiorValidBefore()
{ // {
$adapter = new CouponBaseAdapter(); // $adapter = new CouponBaseAdapter();
// Given // // Given
$a = 11; // $a = 11;
$operator = Operators::INFERIOR; // $operator = Operators::INFERIOR;
$b = new QuantityParam($adapter, 12); // $b = new QuantityParam($adapter, 12);
//
// When // // When
$actual = Operators::isValid($a, $operator, $b); // $actual = Operators::isValid($a, $operator, $b);
//
// Then // // Then
$this->assertTrue($actual); // $this->assertTrue($actual);
} // }
//
/** // /**
* // *
* @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator // * @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator
* // *
*/ // */
public function testOperatorInferiorInvalidEquals() // public function testOperatorInferiorInvalidEquals()
{ // {
// Given // // Given
$adapter = new CouponBaseAdapter(); // $adapter = new CouponBaseAdapter();
$a = 12; // $a = 12;
$operator = Operators::INFERIOR; // $operator = Operators::INFERIOR;
$b = new QuantityParam($adapter, 12); // $b = new QuantityParam($adapter, 12);
//
// When // // When
$actual = Operators::isValid($a, $operator, $b); // $actual = Operators::isValid($a, $operator, $b);
//
// Then // // Then
$this->assertFalse($actual); // $this->assertFalse($actual);
} // }
//
/** // /**
* // *
* @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator // * @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator
* // *
*/ // */
public function testOperatorInferiorInvalidAfter() // public function testOperatorInferiorInvalidAfter()
{ // {
// Given // // Given
$adapter = new CouponBaseAdapter(); // $adapter = new CouponBaseAdapter();
$a = 13; // $a = 13;
$operator = Operators::INFERIOR; // $operator = Operators::INFERIOR;
$b = new QuantityParam($adapter, 12); // $b = new QuantityParam($adapter, 12);
//
// When // // When
$actual = Operators::isValid($a, $operator, $b); // $actual = Operators::isValid($a, $operator, $b);
//
// Then // // Then
$this->assertFalse($actual); // $this->assertFalse($actual);
} // }
//
/** // /**
* // *
* @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator // * @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator
* // *
*/ // */
public function testOperatorInferiorOrEqualValidEqual() // public function testOperatorInferiorOrEqualValidEqual()
{ // {
// Given // // Given
$adapter = new CouponBaseAdapter(); // $adapter = new CouponBaseAdapter();
$a = 11; // $a = 11;
$operator = Operators::INFERIOR_OR_EQUAL; // $operator = Operators::INFERIOR_OR_EQUAL;
$b = new QuantityParam($adapter, 11); // $b = new QuantityParam($adapter, 11);
//
// When // // When
$actual = Operators::isValid($a, $operator, $b); // $actual = Operators::isValid($a, $operator, $b);
//
// Then // // Then
$this->assertTrue($actual); // $this->assertTrue($actual);
} // }
//
/** // /**
* // *
* @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator // * @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator
* // *
*/ // */
public function testOperatorInferiorOrEqualValidBefore() // public function testOperatorInferiorOrEqualValidBefore()
{ // {
// Given // // Given
$adapter = new CouponBaseAdapter(); // $adapter = new CouponBaseAdapter();
$a = 10; // $a = 10;
$operator = Operators::INFERIOR_OR_EQUAL; // $operator = Operators::INFERIOR_OR_EQUAL;
$b = new QuantityParam($adapter, 11); // $b = new QuantityParam($adapter, 11);
//
// When // // When
$actual = Operators::isValid($a, $operator, $b); // $actual = Operators::isValid($a, $operator, $b);
//
// Then // // Then
$this->assertTrue($actual); // $this->assertTrue($actual);
} // }
//
/** // /**
* // *
* @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator // * @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator
* // *
*/ // */
public function testOperatorInferiorOrEqualInValidAfter() // public function testOperatorInferiorOrEqualInValidAfter()
{ // {
// Given // // Given
$adapter = new CouponBaseAdapter(); // $adapter = new CouponBaseAdapter();
$a = 12; // $a = 12;
$operator = Operators::INFERIOR_OR_EQUAL; // $operator = Operators::INFERIOR_OR_EQUAL;
$b = new QuantityParam($adapter, 11); // $b = new QuantityParam($adapter, 11);
//
// When // // When
$actual = Operators::isValid($a, $operator, $b); // $actual = Operators::isValid($a, $operator, $b);
//
// Then // // Then
$this->assertFalse($actual); // $this->assertFalse($actual);
} // }
//
/** // /**
* // *
* @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator // * @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator
* // *
*/ // */
public function testOperatorEqualValidEqual() // public function testOperatorEqualValidEqual()
{ // {
// Given // // Given
$adapter = new CouponBaseAdapter(); // $adapter = new CouponBaseAdapter();
$a = 12; // $a = 12;
$operator = Operators::EQUAL; // $operator = Operators::EQUAL;
$b = new QuantityParam($adapter, 12); // $b = new QuantityParam($adapter, 12);
//
// When // // When
$actual = Operators::isValid($a, $operator, $b); // $actual = Operators::isValid($a, $operator, $b);
//
// Then // // Then
$this->assertTrue($actual); // $this->assertTrue($actual);
} // }
//
/** // /**
* // *
* @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator // * @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator
* // *
*/ // */
public function testOperatorEqualInValidBefore() // public function testOperatorEqualInValidBefore()
{ // {
// Given // // Given
$adapter = new CouponBaseAdapter(); // $adapter = new CouponBaseAdapter();
$a = 11; // $a = 11;
$operator = Operators::EQUAL; // $operator = Operators::EQUAL;
$b = new QuantityParam($adapter, 12); // $b = new QuantityParam($adapter, 12);
//
// When // // When
$actual = Operators::isValid($a, $operator, $b); // $actual = Operators::isValid($a, $operator, $b);
//
// Then // // Then
$this->assertFalse($actual); // $this->assertFalse($actual);
} // }
//
/** // /**
* // *
* @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator // * @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator
* // *
*/ // */
public function testOperatorEqualInValidAfter() // public function testOperatorEqualInValidAfter()
{ // {
// Given // // Given
$adapter = new CouponBaseAdapter(); // $adapter = new CouponBaseAdapter();
$a = 13; // $a = 13;
$operator = Operators::EQUAL; // $operator = Operators::EQUAL;
$b = new QuantityParam($adapter, 12); // $b = new QuantityParam($adapter, 12);
//
// When // // When
$actual = Operators::isValid($a, $operator, $b); // $actual = Operators::isValid($a, $operator, $b);
//
// Then // // Then
$this->assertFalse($actual); // $this->assertFalse($actual);
} // }
//
/** // /**
* // *
* @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator // * @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator
* // *
*/ // */
public function testOperatorSuperiorOrEqualValidEqual() // public function testOperatorSuperiorOrEqualValidEqual()
{ // {
// Given // // Given
$adapter = new CouponBaseAdapter(); // $adapter = new CouponBaseAdapter();
$a = 13; // $a = 13;
$operator = Operators::SUPERIOR_OR_EQUAL; // $operator = Operators::SUPERIOR_OR_EQUAL;
$b = new QuantityParam($adapter, 13); // $b = new QuantityParam($adapter, 13);
//
// When // // When
$actual = Operators::isValid($a, $operator, $b); // $actual = Operators::isValid($a, $operator, $b);
//
// Then // // Then
$this->assertTrue($actual); // $this->assertTrue($actual);
} // }
//
/** // /**
* // *
* @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator // * @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator
* // *
*/ // */
public function testOperatorSuperiorOrEqualAfter() // public function testOperatorSuperiorOrEqualAfter()
{ // {
// Given // // Given
$adapter = new CouponBaseAdapter(); // $adapter = new CouponBaseAdapter();
$a = 14; // $a = 14;
$operator = Operators::SUPERIOR_OR_EQUAL; // $operator = Operators::SUPERIOR_OR_EQUAL;
$b = new QuantityParam($adapter, 13); // $b = new QuantityParam($adapter, 13);
//
// When // // When
$actual = Operators::isValid($a, $operator, $b); // $actual = Operators::isValid($a, $operator, $b);
//
// Then // // Then
$this->assertTrue($actual); // $this->assertTrue($actual);
} // }
//
/** // /**
* // *
* @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator // * @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator
* // *
*/ // */
public function testOperatorSuperiorOrEqualInvalidBefore() // public function testOperatorSuperiorOrEqualInvalidBefore()
{ // {
// Given // // Given
$adapter = new CouponBaseAdapter(); // $adapter = new CouponBaseAdapter();
$a = 12; // $a = 12;
$operator = Operators::SUPERIOR_OR_EQUAL; // $operator = Operators::SUPERIOR_OR_EQUAL;
$b = new QuantityParam($adapter, 13); // $b = new QuantityParam($adapter, 13);
//
// When // // When
$actual = Operators::isValid($a, $operator, $b); // $actual = Operators::isValid($a, $operator, $b);
//
// Then // // Then
$this->assertFalse($actual); // $this->assertFalse($actual);
} // }
//
/** // /**
* // *
* @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator // * @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator
* // *
*/ // */
public function testOperatorSuperiorValidAfter() // public function testOperatorSuperiorValidAfter()
{ // {
// Given // // Given
$adapter = new CouponBaseAdapter(); // $adapter = new CouponBaseAdapter();
$a = 13; // $a = 13;
$operator = Operators::SUPERIOR; // $operator = Operators::SUPERIOR;
$b = new QuantityParam($adapter, 12); // $b = new QuantityParam($adapter, 12);
//
// When // // When
$actual = Operators::isValid($a, $operator, $b); // $actual = Operators::isValid($a, $operator, $b);
//
// Then // // Then
$this->assertTrue($actual); // $this->assertTrue($actual);
} // }
//
/** // /**
* // *
* @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator // * @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator
* // *
*/ // */
public function testOperatorSuperiorInvalidEqual() // public function testOperatorSuperiorInvalidEqual()
{ // {
// Given // // Given
$adapter = new CouponBaseAdapter(); // $adapter = new CouponBaseAdapter();
$a = 12; // $a = 12;
$operator = Operators::SUPERIOR; // $operator = Operators::SUPERIOR;
$b = new QuantityParam($adapter, 12); // $b = new QuantityParam($adapter, 12);
//
// When // // When
$actual = Operators::isValid($a, $operator, $b); // $actual = Operators::isValid($a, $operator, $b);
//
// Then // // Then
$this->assertFalse($actual); // $this->assertFalse($actual);
} // }
//
/** // /**
* // *
* @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator // * @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator
* // *
*/ // */
public function testOperatorSuperiorInvalidBefore() // public function testOperatorSuperiorInvalidBefore()
{ // {
// Given // // Given
$adapter = new CouponBaseAdapter(); // $adapter = new CouponBaseAdapter();
$a = 11; // $a = 11;
$operator = Operators::SUPERIOR; // $operator = Operators::SUPERIOR;
$b = new QuantityParam($adapter, 12); // $b = new QuantityParam($adapter, 12);
//
// When // // When
$actual = Operators::isValid($a, $operator, $b); // $actual = Operators::isValid($a, $operator, $b);
//
// Then // // Then
$this->assertFalse($actual); // $this->assertFalse($actual);
} // }
//
/** // /**
* // *
* @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator // * @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator
* // *
*/ // */
public function testOperatorDifferentValid() // public function testOperatorDifferentValid()
{ // {
// Given // // Given
$adapter = new CouponBaseAdapter(); // $adapter = new CouponBaseAdapter();
$a = 12; // $a = 12;
$operator = Operators::DIFFERENT; // $operator = Operators::DIFFERENT;
$b = new QuantityParam($adapter, 11); // $b = new QuantityParam($adapter, 11);
//
// When // // When
$actual = Operators::isValid($a, $operator, $b); // $actual = Operators::isValid($a, $operator, $b);
//
// Then // // Then
$this->assertTrue($actual); // $this->assertTrue($actual);
} // }
//
/** // /**
* // *
* @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator // * @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator
* // *
*/ // */
public function testOperatorDifferentInvalidEquals() // public function testOperatorDifferentInvalidEquals()
{ // {
// Given // // Given
$adapter = new CouponBaseAdapter(); // $adapter = new CouponBaseAdapter();
$a = 11; // $a = 11;
$operator = Operators::DIFFERENT; // $operator = Operators::DIFFERENT;
$b = new QuantityParam($adapter, 11); // $b = new QuantityParam($adapter, 11);
//
// When // // When
$actual = Operators::isValid($a, $operator, $b); // $actual = Operators::isValid($a, $operator, $b);
//
// Then // // Then
$this->assertFalse($actual); // $this->assertFalse($actual);
} // }
//
/** // /**
* // *
* @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator // * @covers Thelia\Coupon\Rule\Operator::isValidAccordingToOperator
* // *
*/ // */
public function testOperatorInValid() // public function testOperatorInValid()
{ // {
// Given // // Given
$adapter = new CouponBaseAdapter(); // $adapter = new CouponBaseAdapter();
$a = 12; // $a = 12;
$operator = 'X'; // $operator = 'X';
$b = new QuantityParam($adapter, 11); // $b = new QuantityParam($adapter, 11);
//
// When // // When
$actual = Operators::isValid($a, $operator, $b); // $actual = Operators::isValid($a, $operator, $b);
//
// Then // // Then
$this->assertFalse($actual); // $this->assertFalse($actual);
} // }