- Refactor Coupon : is Customer matching Coupon rules (increase code coverage)
This commit is contained in:
gmorel
2013-09-08 18:00:31 +02:00
parent 91cf4ec752
commit 447488f063
5 changed files with 277 additions and 197 deletions

View File

@@ -74,36 +74,36 @@ class AvailableForTotalAmountManager extends CouponRuleAbstract
// /** @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
* // *
* @throws InvalidRuleOperatorException if Operator is not allowed // * @throws InvalidRuleOperatorException if Operator is not allowed
* @throws InvalidRuleValueException if Value is not allowed // * @throws InvalidRuleValueException if Value is not allowed
* @return bool // * @return bool
*/ // */
public function checkBackOfficeInput() // public function checkBackOfficeInput()
{ // {
if (!isset($this->validators) // if (!isset($this->validators)
|| empty($this->validators) // || empty($this->validators)
||!isset($this->validators[self::PARAM1_PRICE]) // ||!isset($this->validators[self::PARAM1_PRICE])
||!isset($this->validators[self::PARAM1_PRICE]) // ||!isset($this->validators[self::PARAM1_PRICE])
) { // ) {
throw new InvalidRuleValueException(get_class(), self::PARAM1_PRICE); // throw new InvalidRuleValueException(get_class(), self::PARAM1_PRICE);
} // }
//
/** @var RuleValidator $ruleValidator */ // /** @var RuleValidator $ruleValidator */
$ruleValidator = $this->validators[self::PARAM1_PRICE]; // $ruleValidator = $this->validators[self::PARAM1_PRICE];
/** @var PriceParam $price */ // /** @var PriceParam $price */
$price = $ruleValidator->getParam(); // $price = $ruleValidator->getParam();
//
if (!$price instanceof PriceParam) { // if (!$price instanceof PriceParam) {
throw new InvalidRuleValueException(get_class(), self::PARAM1_PRICE); // throw new InvalidRuleValueException(get_class(), self::PARAM1_PRICE);
} // }
//
$this->checkBackOfficeInputsOperators(); // $this->checkBackOfficeInputsOperators();
//
return $this->isPriceValid($price->getPrice(), $price->getCurrency()); // return $this->isPriceValid($price->getPrice(), $price->getCurrency());
} // }
// /** // /**
// * Check if Checkout inputs are relevant or not // * Check if Checkout inputs are relevant or not
@@ -247,33 +247,33 @@ class AvailableForTotalAmountManager extends CouponRuleAbstract
return false; return false;
} }
/** // /**
* Check if a price is valid // * Check if a price is valid
* // *
* @param float $price Price to check // * @param float $price Price to check
* @param string $currency Price currency // * @param string $currency Price currency
* // *
* @throws InvalidRuleValueException if Value is not allowed // * @throws InvalidRuleValueException if Value is not allowed
* @return bool // * @return bool
*/ // */
protected function isPriceValid($price, $currency) // protected function isPriceValid($price, $currency)
{ // {
$priceValidator = $this->priceValidator; // $priceValidator = $this->priceValidator;
//
/** @var PriceParam $param */ // /** @var PriceParam $param */
$param = $priceValidator->getParam(); // $param = $priceValidator->getParam();
if ($currency == $param->getCurrency()) { // if ($currency == $param->getCurrency()) {
try { // try {
$priceValidator->getParam()->compareTo($price); // $priceValidator->getParam()->compareTo($price);
} catch(\InvalidArgumentException $e) { // } catch(\InvalidArgumentException $e) {
throw new InvalidRuleValueException(get_class(), self::PARAM1_PRICE); // throw new InvalidRuleValueException(get_class(), self::PARAM1_PRICE);
} // }
} else { // } else {
throw new InvalidRuleValueException(get_class(), self::PARAM1_CURRENCY); // throw new InvalidRuleValueException(get_class(), self::PARAM1_CURRENCY);
} // }
//
return true; // return true;
} // }
// /** // /**
// * Generate current Rule param to be validated from adapter // * Generate current Rule param to be validated from adapter
@@ -312,15 +312,15 @@ class AvailableForTotalAmountManager extends CouponRuleAbstract
public function getToolTip() public function getToolTip()
{ {
$i18nOperator = Operators::getI18n( $i18nOperator = Operators::getI18n(
$this->translator, $this->priceValidator->getOperator() $this->translator, $this->operators[self::INPUT1]
); );
$toolTip = $this->translator->trans( $toolTip = $this->translator->trans(
'If cart total amount is <strong>%operator%</strong> %amount% %currency%', 'If cart total amount is <strong>%operator%</strong> %amount% %currency%',
array( array(
'%operator%' => $i18nOperator, '%operator%' => $i18nOperator,
'%amount%' => $this->priceValidator->getParam()->getPrice(), '%amount%' => $this->values[self::INPUT1],
'%currency%' => $this->priceValidator->getParam()->getCurrency() '%currency%' => $this->values[self::INPUT2]
), ),
'constraint' 'constraint'
); );
@@ -361,21 +361,7 @@ class AvailableForTotalAmountManager extends CouponRuleAbstract
// return $this; // return $this;
// } // }
/**
* Return a serializable Rule
*
* @return SerializableRule
*/
public function getSerializableRule()
{
$serializableRule = new SerializableRule();
$serializableRule->ruleServiceId = $this->serviceId;
$serializableRule->operators = $this->operators;
$serializableRule->values = $this->values;
return $serializableRule;
}

View File

@@ -63,56 +63,56 @@ class AvailableForXArticlesManager extends CouponRuleAbstract
) )
); );
/** @var QuantityParam Quantity Validator */ // /** @var QuantityParam Quantity Validator */
protected $quantityValidator = null; // protected $quantityValidator = null;
/** // /**
* Check if backoffice inputs are relevant or not // * Check if backoffice inputs are relevant or not
* // *
* @throws InvalidRuleOperatorException if Operator is not allowed // * @throws InvalidRuleOperatorException if Operator is not allowed
* @throws InvalidRuleValueException if Value is not allowed // * @throws InvalidRuleValueException if Value is not allowed
* @return bool // * @return bool
*/ // */
public function checkBackOfficeInput() // public function checkBackOfficeInput()
{ // {
if (!isset($this->validators) // if (!isset($this->validators)
|| empty($this->validators) // || empty($this->validators)
||!isset($this->validators[self::PARAM1_QUANTITY]) // ||!isset($this->validators[self::PARAM1_QUANTITY])
||!isset($this->validators[self::PARAM1_QUANTITY]) // ||!isset($this->validators[self::PARAM1_QUANTITY])
) { // ) {
throw new InvalidRuleValueException(get_class(), self::PARAM1_QUANTITY); // throw new InvalidRuleValueException(get_class(), self::PARAM1_QUANTITY);
} // }
//
// /** @var RuleValidator $ruleValidator */
// $ruleValidator = $this->validators[self::PARAM1_QUANTITY];
// /** @var QuantityParam $quantity */
// $quantity = $ruleValidator->getParam();
//
// if (!$quantity instanceof QuantityParam) {
// throw new InvalidRuleValueException(get_class(), self::PARAM1_QUANTITY);
// }
//
// $this->checkBackOfficeInputsOperators();
//
// return $this->isQuantityValid($quantity->getInteger());
// }
/** @var RuleValidator $ruleValidator */ // /**
$ruleValidator = $this->validators[self::PARAM1_QUANTITY]; // * Generate current Rule param to be validated from adapter
/** @var QuantityParam $quantity */ // *
$quantity = $ruleValidator->getParam(); // * @param CouponAdapterInterface $adapter allowing to gather
// * all necessary Thelia variables
if (!$quantity instanceof QuantityParam) { // *
throw new InvalidRuleValueException(get_class(), self::PARAM1_QUANTITY); // * @return $this
} // */
// protected function setParametersToValidate()
$this->checkBackOfficeInputsOperators(); // {
// $this->paramsToValidate = array(
return $this->isQuantityValid($quantity->getInteger()); // self::PARAM1_QUANTITY => $this->adapter->getNbArticlesInCart()
} // );
//
/** // return $this;
* Generate current Rule param to be validated from adapter // }
*
* @param CouponAdapterInterface $adapter allowing to gather
* all necessary Thelia variables
*
* @return $this
*/
protected function setParametersToValidate()
{
$this->paramsToValidate = array(
self::PARAM1_QUANTITY => $this->adapter->getNbArticlesInCart()
);
return $this;
}
// /** // /**
// * Check if Checkout inputs are relevant or not // * Check if Checkout inputs are relevant or not
@@ -210,25 +210,25 @@ class AvailableForXArticlesManager extends CouponRuleAbstract
return false; return false;
} }
/** // /**
* Check if a quantity is valid // * Check if a quantity is valid
* // *
* @param int $quantity Quantity to check // * @param int $quantity Quantity to check
* // *
* @throws InvalidRuleValueException if Value is not allowed // * @throws InvalidRuleValueException if Value is not allowed
* @return bool // * @return bool
*/ // */
protected function isQuantityValid($quantity) // protected function isQuantityValid($quantity)
{ // {
$quantityValidator = $this->quantityValidator; // $quantityValidator = $this->quantityValidator;
try { // try {
$quantityValidator->getParam()->compareTo($quantity); // $quantityValidator->getParam()->compareTo($quantity);
} catch(InvalidArgumentException $e) { // } catch(InvalidArgumentException $e) {
throw new InvalidRuleValueException(get_class(), self::PARAM1_QUANTITY); // throw new InvalidRuleValueException(get_class(), self::PARAM1_QUANTITY);
} // }
//
return true; // return true;
} // }
/** /**
* Get I18n name * Get I18n name
@@ -252,14 +252,14 @@ class AvailableForXArticlesManager extends CouponRuleAbstract
public function getToolTip() public function getToolTip()
{ {
$i18nOperator = Operators::getI18n( $i18nOperator = Operators::getI18n(
$this->translator, $this->priceValidator->getOperator() $this->translator, $this->operators[self::INPUT1]
); );
$toolTip = $this->translator->trans( $toolTip = $this->translator->trans(
'If cart products quantity is <strong>%operator%</strong> %quantity%', 'If cart products quantity is <strong>%operator%</strong> %quantity%',
array( array(
'%operator%' => $i18nOperator, '%operator%' => $i18nOperator,
'%quantity%' => $this->quantityValidator->getParam()->getInteger(), '%quantity%' => $this->values[self::INPUT1]
), ),
'constraint' 'constraint'
); );
@@ -297,24 +297,4 @@ class AvailableForXArticlesManager extends CouponRuleAbstract
// return $this; // return $this;
// } // }
/**
* Return a serializable Rule
*
* @return SerializableRule
*/
public function getSerializableRule()
{
$serializableRule = new SerializableRule();
$serializableRule->ruleServiceId = $this->serviceId;
$serializableRule->operators = array(
self::PARAM1_QUANTITY => $this->quantityValidator->getOperator()
);
$serializableRule->values = array(
self::PARAM1_QUANTITY => $this->quantityValidator->getInteger()
);
return $serializableRule;
}
} }

View File

@@ -44,10 +44,10 @@ use Thelia\Exception\InvalidRuleOperatorException;
*/ */
abstract class CouponRuleAbstract implements CouponRuleInterface abstract class CouponRuleAbstract implements CouponRuleInterface
{ {
/** Operator key in $validators */ // /** Operator key in $validators */
CONST OPERATOR = 'operator'; // CONST OPERATOR = 'operator';
/** Value key in $validators */ // /** Value key in $validators */
CONST VALUE = 'value'; // CONST VALUE = 'value';
/** @var string Service Id from Resources/config.xml */ /** @var string Service Id from Resources/config.xml */
protected $serviceId = null; protected $serviceId = null;
@@ -58,8 +58,8 @@ abstract class CouponRuleAbstract implements CouponRuleInterface
/** @var array Parameters validating parameters against */ /** @var array Parameters validating parameters against */
protected $validators = array(); protected $validators = array();
/** @var array Parameters to be validated */ // /** @var array Parameters to be validated */
protected $paramsToValidate = array(); // protected $paramsToValidate = array();
/** @var CouponAdapterInterface Provide necessary value from Thelia */ /** @var CouponAdapterInterface Provide necessary value from Thelia */
protected $adapter = null; protected $adapter = null;
@@ -150,25 +150,25 @@ abstract class CouponRuleAbstract implements CouponRuleInterface
return $this->availableOperators; return $this->availableOperators;
} }
/** // /**
* Check if Operators set for this Rule in the BackOffice are legit // * Check if Operators set for this Rule in the BackOffice are legit
* // *
* @throws InvalidRuleOperatorException if Operator is not allowed // * @throws InvalidRuleOperatorException if Operator is not allowed
* @return bool // * @return bool
*/ // */
protected function checkBackOfficeInputsOperators() // protected function checkBackOfficeInputsOperators()
{ // {
/** @var RuleValidator $param */ // /** @var RuleValidator $param */
foreach ($this->validators as $key => $param) { // foreach ($this->validators as $key => $param) {
$operator = $param->getOperator(); // $operator = $param->getOperator();
if (!isset($operator) // if (!isset($operator)
||!in_array($operator, $this->availableOperators) // ||!in_array($operator, $this->availableOperators)
) { // ) {
throw new InvalidRuleOperatorException(get_class(), $key); // throw new InvalidRuleOperatorException(get_class(), $key);
} // }
} // }
return true; // return true;
} // }
// /** // /**
// * Generate current Rule param to be validated from adapter // * Generate current Rule param to be validated from adapter
@@ -183,13 +183,15 @@ abstract class CouponRuleAbstract implements CouponRuleInterface
/** /**
* Return all validators * Return all validators
* Serialization purpose
* *
* @return array * @return array
*/ */
public function getValidators() public function getValidators()
{ {
return $this->validators; return array(
$this->operators,
$this->values
);
} }
/** /**
@@ -215,4 +217,20 @@ abstract class CouponRuleAbstract implements CouponRuleInterface
return in_array($operator, $availableOperators); return in_array($operator, $availableOperators);
} }
/**
* Return a serializable Rule
*
* @return SerializableRule
*/
public function getSerializableRule()
{
$serializableRule = new SerializableRule();
$serializableRule->ruleServiceId = $this->serviceId;
$serializableRule->operators = $this->operators;
$serializableRule->values = $this->values;
return $serializableRule;
}
} }

View File

@@ -53,12 +53,12 @@ interface CouponRuleInterface
*/ */
public function getServiceId(); public function getServiceId();
/** // /**
* Check if backoffice inputs are relevant or not // * Check if backoffice inputs are relevant or not
* // *
* @return bool // * @return bool
*/ // */
public function checkBackOfficeInput(); // public function checkBackOfficeInput();
// /** // /**
// * Check if Checkout inputs are relevant or not // * Check if Checkout inputs are relevant or not
@@ -115,7 +115,7 @@ interface CouponRuleInterface
public function getToolTip(); public function getToolTip();
/** /**
* Get validators * Return all validators
* *
* @return array * @return array
*/ */

View File

@@ -25,6 +25,7 @@ namespace Thelia\Coupon;
use Thelia\Constraint\Rule\AvailableForXArticlesManager; use Thelia\Constraint\Rule\AvailableForXArticlesManager;
use Thelia\Constraint\Rule\Operators; use Thelia\Constraint\Rule\Operators;
use Thelia\Constraint\Rule\SerializableRule;
/** /**
* Created by JetBrains PhpStorm. * Created by JetBrains PhpStorm.
@@ -565,6 +566,101 @@ class AvailableForXArticlesTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($expected, $actual); $this->assertEquals($expected, $actual);
} }
public function testGetSerializableRule()
{
$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);
$serializableRule = $rule1->getSerializableRule();
$expected = new SerializableRule();
$expected->ruleServiceId = $rule1->getServiceId();
$expected->operators = $operators;
$expected->values = $values;
$actual = $serializableRule;
$this->assertEquals($expected, $actual);
}
public function testGetAvailableOperators()
{
$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);
$expected = array(
AvailableForXArticlesManager::INPUT1 => array(
Operators::INFERIOR,
Operators::INFERIOR_OR_EQUAL,
Operators::EQUAL,
Operators::SUPERIOR_OR_EQUAL,
Operators::SUPERIOR
)
);
$actual = $rule1->getAvailableOperators();
$this->assertEquals($expected, $actual);
}
public function testGetValidators()
{
$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);
$expected = array(
$operators,
$values
);
$actual = $rule1->getValidators();
$this->assertEquals($expected, $actual);
}
/** /**
* Tears down the fixture, for example, closes a network connection. * Tears down the fixture, for example, closes a network connection.