WIP
- Refactor Coupon : is Customer matching Coupon rules (increase code coverage)
This commit is contained in:
@@ -74,36 +74,36 @@ class AvailableForTotalAmountManager extends CouponRuleAbstract
|
||||
// /** @var RuleValidator Price Validator */
|
||||
// protected $priceValidator = null;
|
||||
|
||||
/**
|
||||
* Check if backoffice inputs are relevant or not
|
||||
*
|
||||
* @throws InvalidRuleOperatorException if Operator is not allowed
|
||||
* @throws InvalidRuleValueException if Value is not allowed
|
||||
* @return bool
|
||||
*/
|
||||
public function checkBackOfficeInput()
|
||||
{
|
||||
if (!isset($this->validators)
|
||||
|| empty($this->validators)
|
||||
||!isset($this->validators[self::PARAM1_PRICE])
|
||||
||!isset($this->validators[self::PARAM1_PRICE])
|
||||
) {
|
||||
throw new InvalidRuleValueException(get_class(), self::PARAM1_PRICE);
|
||||
}
|
||||
|
||||
/** @var RuleValidator $ruleValidator */
|
||||
$ruleValidator = $this->validators[self::PARAM1_PRICE];
|
||||
/** @var PriceParam $price */
|
||||
$price = $ruleValidator->getParam();
|
||||
|
||||
if (!$price instanceof PriceParam) {
|
||||
throw new InvalidRuleValueException(get_class(), self::PARAM1_PRICE);
|
||||
}
|
||||
|
||||
$this->checkBackOfficeInputsOperators();
|
||||
|
||||
return $this->isPriceValid($price->getPrice(), $price->getCurrency());
|
||||
}
|
||||
// /**
|
||||
// * Check if backoffice inputs are relevant or not
|
||||
// *
|
||||
// * @throws InvalidRuleOperatorException if Operator is not allowed
|
||||
// * @throws InvalidRuleValueException if Value is not allowed
|
||||
// * @return bool
|
||||
// */
|
||||
// public function checkBackOfficeInput()
|
||||
// {
|
||||
// if (!isset($this->validators)
|
||||
// || empty($this->validators)
|
||||
// ||!isset($this->validators[self::PARAM1_PRICE])
|
||||
// ||!isset($this->validators[self::PARAM1_PRICE])
|
||||
// ) {
|
||||
// throw new InvalidRuleValueException(get_class(), self::PARAM1_PRICE);
|
||||
// }
|
||||
//
|
||||
// /** @var RuleValidator $ruleValidator */
|
||||
// $ruleValidator = $this->validators[self::PARAM1_PRICE];
|
||||
// /** @var PriceParam $price */
|
||||
// $price = $ruleValidator->getParam();
|
||||
//
|
||||
// if (!$price instanceof PriceParam) {
|
||||
// throw new InvalidRuleValueException(get_class(), self::PARAM1_PRICE);
|
||||
// }
|
||||
//
|
||||
// $this->checkBackOfficeInputsOperators();
|
||||
//
|
||||
// return $this->isPriceValid($price->getPrice(), $price->getCurrency());
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * Check if Checkout inputs are relevant or not
|
||||
@@ -247,33 +247,33 @@ class AvailableForTotalAmountManager extends CouponRuleAbstract
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a price is valid
|
||||
*
|
||||
* @param float $price Price to check
|
||||
* @param string $currency Price currency
|
||||
*
|
||||
* @throws InvalidRuleValueException if Value is not allowed
|
||||
* @return bool
|
||||
*/
|
||||
protected function isPriceValid($price, $currency)
|
||||
{
|
||||
$priceValidator = $this->priceValidator;
|
||||
|
||||
/** @var PriceParam $param */
|
||||
$param = $priceValidator->getParam();
|
||||
if ($currency == $param->getCurrency()) {
|
||||
try {
|
||||
$priceValidator->getParam()->compareTo($price);
|
||||
} catch(\InvalidArgumentException $e) {
|
||||
throw new InvalidRuleValueException(get_class(), self::PARAM1_PRICE);
|
||||
}
|
||||
} else {
|
||||
throw new InvalidRuleValueException(get_class(), self::PARAM1_CURRENCY);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
// /**
|
||||
// * Check if a price is valid
|
||||
// *
|
||||
// * @param float $price Price to check
|
||||
// * @param string $currency Price currency
|
||||
// *
|
||||
// * @throws InvalidRuleValueException if Value is not allowed
|
||||
// * @return bool
|
||||
// */
|
||||
// protected function isPriceValid($price, $currency)
|
||||
// {
|
||||
// $priceValidator = $this->priceValidator;
|
||||
//
|
||||
// /** @var PriceParam $param */
|
||||
// $param = $priceValidator->getParam();
|
||||
// if ($currency == $param->getCurrency()) {
|
||||
// try {
|
||||
// $priceValidator->getParam()->compareTo($price);
|
||||
// } catch(\InvalidArgumentException $e) {
|
||||
// throw new InvalidRuleValueException(get_class(), self::PARAM1_PRICE);
|
||||
// }
|
||||
// } else {
|
||||
// throw new InvalidRuleValueException(get_class(), self::PARAM1_CURRENCY);
|
||||
// }
|
||||
//
|
||||
// return true;
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * Generate current Rule param to be validated from adapter
|
||||
@@ -312,15 +312,15 @@ class AvailableForTotalAmountManager extends CouponRuleAbstract
|
||||
public function getToolTip()
|
||||
{
|
||||
$i18nOperator = Operators::getI18n(
|
||||
$this->translator, $this->priceValidator->getOperator()
|
||||
$this->translator, $this->operators[self::INPUT1]
|
||||
);
|
||||
|
||||
$toolTip = $this->translator->trans(
|
||||
'If cart total amount is <strong>%operator%</strong> %amount% %currency%',
|
||||
array(
|
||||
'%operator%' => $i18nOperator,
|
||||
'%amount%' => $this->priceValidator->getParam()->getPrice(),
|
||||
'%currency%' => $this->priceValidator->getParam()->getCurrency()
|
||||
'%amount%' => $this->values[self::INPUT1],
|
||||
'%currency%' => $this->values[self::INPUT2]
|
||||
),
|
||||
'constraint'
|
||||
);
|
||||
@@ -361,21 +361,7 @@ class AvailableForTotalAmountManager extends CouponRuleAbstract
|
||||
// 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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -63,56 +63,56 @@ class AvailableForXArticlesManager extends CouponRuleAbstract
|
||||
)
|
||||
);
|
||||
|
||||
/** @var QuantityParam Quantity Validator */
|
||||
protected $quantityValidator = null;
|
||||
// /** @var QuantityParam Quantity Validator */
|
||||
// protected $quantityValidator = null;
|
||||
|
||||
/**
|
||||
* Check if backoffice inputs are relevant or not
|
||||
*
|
||||
* @throws InvalidRuleOperatorException if Operator is not allowed
|
||||
* @throws InvalidRuleValueException if Value is not allowed
|
||||
* @return bool
|
||||
*/
|
||||
public function checkBackOfficeInput()
|
||||
{
|
||||
if (!isset($this->validators)
|
||||
|| empty($this->validators)
|
||||
||!isset($this->validators[self::PARAM1_QUANTITY])
|
||||
||!isset($this->validators[self::PARAM1_QUANTITY])
|
||||
) {
|
||||
throw new InvalidRuleValueException(get_class(), self::PARAM1_QUANTITY);
|
||||
}
|
||||
// /**
|
||||
// * Check if backoffice inputs are relevant or not
|
||||
// *
|
||||
// * @throws InvalidRuleOperatorException if Operator is not allowed
|
||||
// * @throws InvalidRuleValueException if Value is not allowed
|
||||
// * @return bool
|
||||
// */
|
||||
// public function checkBackOfficeInput()
|
||||
// {
|
||||
// if (!isset($this->validators)
|
||||
// || empty($this->validators)
|
||||
// ||!isset($this->validators[self::PARAM1_QUANTITY])
|
||||
// ||!isset($this->validators[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];
|
||||
/** @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());
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
// /**
|
||||
// * 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
|
||||
@@ -210,25 +210,25 @@ class AvailableForXArticlesManager extends CouponRuleAbstract
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a quantity is valid
|
||||
*
|
||||
* @param int $quantity Quantity to check
|
||||
*
|
||||
* @throws InvalidRuleValueException if Value is not allowed
|
||||
* @return bool
|
||||
*/
|
||||
protected function isQuantityValid($quantity)
|
||||
{
|
||||
$quantityValidator = $this->quantityValidator;
|
||||
try {
|
||||
$quantityValidator->getParam()->compareTo($quantity);
|
||||
} catch(InvalidArgumentException $e) {
|
||||
throw new InvalidRuleValueException(get_class(), self::PARAM1_QUANTITY);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
// /**
|
||||
// * Check if a quantity is valid
|
||||
// *
|
||||
// * @param int $quantity Quantity to check
|
||||
// *
|
||||
// * @throws InvalidRuleValueException if Value is not allowed
|
||||
// * @return bool
|
||||
// */
|
||||
// protected function isQuantityValid($quantity)
|
||||
// {
|
||||
// $quantityValidator = $this->quantityValidator;
|
||||
// try {
|
||||
// $quantityValidator->getParam()->compareTo($quantity);
|
||||
// } catch(InvalidArgumentException $e) {
|
||||
// throw new InvalidRuleValueException(get_class(), self::PARAM1_QUANTITY);
|
||||
// }
|
||||
//
|
||||
// return true;
|
||||
// }
|
||||
|
||||
/**
|
||||
* Get I18n name
|
||||
@@ -252,14 +252,14 @@ class AvailableForXArticlesManager extends CouponRuleAbstract
|
||||
public function getToolTip()
|
||||
{
|
||||
$i18nOperator = Operators::getI18n(
|
||||
$this->translator, $this->priceValidator->getOperator()
|
||||
$this->translator, $this->operators[self::INPUT1]
|
||||
);
|
||||
|
||||
$toolTip = $this->translator->trans(
|
||||
'If cart products quantity is <strong>%operator%</strong> %quantity%',
|
||||
array(
|
||||
'%operator%' => $i18nOperator,
|
||||
'%quantity%' => $this->quantityValidator->getParam()->getInteger(),
|
||||
'%quantity%' => $this->values[self::INPUT1]
|
||||
),
|
||||
'constraint'
|
||||
);
|
||||
@@ -297,24 +297,4 @@ class AvailableForXArticlesManager extends CouponRuleAbstract
|
||||
// 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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -44,10 +44,10 @@ use Thelia\Exception\InvalidRuleOperatorException;
|
||||
*/
|
||||
abstract class CouponRuleAbstract implements CouponRuleInterface
|
||||
{
|
||||
/** Operator key in $validators */
|
||||
CONST OPERATOR = 'operator';
|
||||
/** Value key in $validators */
|
||||
CONST VALUE = 'value';
|
||||
// /** Operator key in $validators */
|
||||
// CONST OPERATOR = 'operator';
|
||||
// /** Value key in $validators */
|
||||
// CONST VALUE = 'value';
|
||||
|
||||
/** @var string Service Id from Resources/config.xml */
|
||||
protected $serviceId = null;
|
||||
@@ -58,8 +58,8 @@ abstract class CouponRuleAbstract implements CouponRuleInterface
|
||||
/** @var array Parameters validating parameters against */
|
||||
protected $validators = array();
|
||||
|
||||
/** @var array Parameters to be validated */
|
||||
protected $paramsToValidate = array();
|
||||
// /** @var array Parameters to be validated */
|
||||
// protected $paramsToValidate = array();
|
||||
|
||||
/** @var CouponAdapterInterface Provide necessary value from Thelia */
|
||||
protected $adapter = null;
|
||||
@@ -150,25 +150,25 @@ abstract class CouponRuleAbstract implements CouponRuleInterface
|
||||
return $this->availableOperators;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if Operators set for this Rule in the BackOffice are legit
|
||||
*
|
||||
* @throws InvalidRuleOperatorException if Operator is not allowed
|
||||
* @return bool
|
||||
*/
|
||||
protected function checkBackOfficeInputsOperators()
|
||||
{
|
||||
/** @var RuleValidator $param */
|
||||
foreach ($this->validators as $key => $param) {
|
||||
$operator = $param->getOperator();
|
||||
if (!isset($operator)
|
||||
||!in_array($operator, $this->availableOperators)
|
||||
) {
|
||||
throw new InvalidRuleOperatorException(get_class(), $key);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
// /**
|
||||
// * Check if Operators set for this Rule in the BackOffice are legit
|
||||
// *
|
||||
// * @throws InvalidRuleOperatorException if Operator is not allowed
|
||||
// * @return bool
|
||||
// */
|
||||
// protected function checkBackOfficeInputsOperators()
|
||||
// {
|
||||
// /** @var RuleValidator $param */
|
||||
// foreach ($this->validators as $key => $param) {
|
||||
// $operator = $param->getOperator();
|
||||
// if (!isset($operator)
|
||||
// ||!in_array($operator, $this->availableOperators)
|
||||
// ) {
|
||||
// throw new InvalidRuleOperatorException(get_class(), $key);
|
||||
// }
|
||||
// }
|
||||
// return true;
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * Generate current Rule param to be validated from adapter
|
||||
@@ -183,13 +183,15 @@ abstract class CouponRuleAbstract implements CouponRuleInterface
|
||||
|
||||
/**
|
||||
* Return all validators
|
||||
* Serialization purpose
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
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 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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -53,12 +53,12 @@ interface CouponRuleInterface
|
||||
*/
|
||||
public function getServiceId();
|
||||
|
||||
/**
|
||||
* Check if backoffice inputs are relevant or not
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function checkBackOfficeInput();
|
||||
// /**
|
||||
// * Check if backoffice inputs are relevant or not
|
||||
// *
|
||||
// * @return bool
|
||||
// */
|
||||
// public function checkBackOfficeInput();
|
||||
|
||||
// /**
|
||||
// * Check if Checkout inputs are relevant or not
|
||||
@@ -115,7 +115,7 @@ interface CouponRuleInterface
|
||||
public function getToolTip();
|
||||
|
||||
/**
|
||||
* Get validators
|
||||
* Return all validators
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
|
||||
@@ -25,6 +25,7 @@ namespace Thelia\Coupon;
|
||||
|
||||
use Thelia\Constraint\Rule\AvailableForXArticlesManager;
|
||||
use Thelia\Constraint\Rule\Operators;
|
||||
use Thelia\Constraint\Rule\SerializableRule;
|
||||
|
||||
/**
|
||||
* Created by JetBrains PhpStorm.
|
||||
@@ -565,6 +566,101 @@ class AvailableForXArticlesTest extends \PHPUnit_Framework_TestCase
|
||||
$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.
|
||||
|
||||
Reference in New Issue
Block a user