WIP Coupon

Refactor : creating dedicated reusable module for Constraints
Adding ConstraintManager
Secured :
- Effects : RemoveXPercent + RemoveXAmount
- Validators : all  except ModelParam (need CustomerModelParam, AreaModelParam, CountryModelParam ?)
- Conditions : AvailableForTotalAmount
This commit is contained in:
gmorel
2013-08-28 09:33:33 +02:00
parent c24e82758f
commit 50d50d3b91
19 changed files with 350 additions and 265 deletions

View File

@@ -61,14 +61,16 @@ class AvailableForTotalAmount extends CouponRuleAbstract
/** /**
* Constructor * Constructor
* *
* @param array $validators Array of RuleValidator * @param CouponAdapterInterface $adapter allowing to gather
* validating $paramsToValidate against * all necessary Thelia variables
* @param array $validators Array of RuleValidator
* validating $paramsToValidate against
* *
* @throws \Thelia\Exception\InvalidRuleException * @throws \Thelia\Exception\InvalidRuleException
*/ */
public function __construct(array $validators) public function __construct(CouponAdapterInterface $adapter, array $validators)
{ {
parent::__construct($validators); parent::__construct($adapter, $validators);
if (isset($validators[self::PARAM1_PRICE]) if (isset($validators[self::PARAM1_PRICE])
&& $validators[self::PARAM1_PRICE] instanceof RuleValidator && $validators[self::PARAM1_PRICE] instanceof RuleValidator
@@ -77,7 +79,6 @@ class AvailableForTotalAmount extends CouponRuleAbstract
} else { } else {
throw new InvalidRuleException(get_class()); throw new InvalidRuleException(get_class());
} }
} }
@@ -109,8 +110,6 @@ class AvailableForTotalAmount extends CouponRuleAbstract
$this->checkBackOfficeInputsOperators(); $this->checkBackOfficeInputsOperators();
return $this->isPriceValid($price->getPrice()); return $this->isPriceValid($price->getPrice());
} }
@@ -129,9 +128,9 @@ class AvailableForTotalAmount extends CouponRuleAbstract
throw new InvalidRuleValueException(get_class(), self::PARAM1_PRICE); throw new InvalidRuleValueException(get_class(), self::PARAM1_PRICE);
} }
$quantity = $this->paramsToValidate[self::PARAM1_PRICE]; $price = $this->paramsToValidate[self::PARAM1_PRICE];
return $this->isPriceValid($quantity); return $this->isPriceValid($price);
} }
/** /**
@@ -157,15 +156,12 @@ class AvailableForTotalAmount extends CouponRuleAbstract
/** /**
* Generate current Rule param to be validated from adapter * Generate current Rule param to be validated from adapter
* *
* @param CouponAdapterInterface $adapter allowing to gather
* all necessary Thelia variables
*
* @return $this * @return $this
*/ */
protected function setParametersToValidate(CouponAdapterInterface $adapter) protected function setParametersToValidate()
{ {
$this->paramsToValidate = array( $this->paramsToValidate = array(
self::PARAM1_PRICE => $adapter->getCartTotalPrice() self::PARAM1_PRICE => $this->adapter->getCartTotalPrice()
); );
return $this; return $this;

View File

@@ -23,6 +23,11 @@
namespace Thelia\Constraint\Rule; namespace Thelia\Constraint\Rule;
use Thelia\Constraint\Validator\QuantityParam;
use Thelia\Constraint\Validator\RuleValidator;
use Thelia\Coupon\CouponAdapterInterface;
use Thelia\Exception\InvalidRuleValueException;
/** /**
* Created by JetBrains PhpStorm. * Created by JetBrains PhpStorm.
* Date: 8/19/13 * Date: 8/19/13
@@ -36,15 +41,90 @@ namespace Thelia\Constraint\Rule;
*/ */
class AvailableForXArticles extends CouponRuleAbstract class AvailableForXArticles extends CouponRuleAbstract
{ {
/** Rule 1st parameter : price */
CONST PARAM1_QUANTITY = 'quantity';
/** @var array Available Operators (Operators::CONST) */
protected $availableOperators = array(
Operators::INFERIOR,
Operators::EQUAL,
Operators::SUPERIOR,
);
/** @var RuleValidator Quantity Validator */
protected $quantityValidator = null;
/**
* Constructor
*
* @param CouponAdapterInterface $adapter allowing to gather
* all necessary Thelia variables
* @param array $validators Array of RuleValidator
* validating $paramsToValidate against
*
* @throws InvalidRuleException
*/
public function __construct(CouponAdapterInterface $adapter, array $validators)
{
parent::__construct($adapter, $validators);
if (isset($validators[self::PARAM1_QUANTITY])
&& $validators[self::PARAM1_QUANTITY] instanceof RuleValidator
) {
$this->quantityValidator = $validators[self::PARAM1_QUANTITY];
} else {
throw new InvalidRuleException(get_class());
}
$this->adapter = $adapter;
}
/** /**
* Check if backoffice inputs are relevant or not * Check if backoffice inputs are relevant or not
* *
* @throws InvalidRuleOperatorException if Operator is not allowed
* @throws InvalidRuleValueException if Value is not allowed
* @return bool * @return bool
*/ */
public function checkBackOfficeInput() public function checkBackOfficeInput()
{ {
// TODO: Implement checkBackOfficeInput() method. 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());
}
/**
* 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->getCartTotalPrice()
);
return $this;
} }
/** /**
@@ -57,4 +137,61 @@ class AvailableForXArticles extends CouponRuleAbstract
// TODO: Implement checkCheckoutInput() method. // TODO: Implement checkCheckoutInput() method.
} }
/**
* Check if a quantity is valid
*
* @param float $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
*
* @return string
*/
public function getName()
{
return $this->adapter
->getTranslator()
->trans('Number of articles in cart', null, 'constraint');
}
/**
* Get I18n tooltip
*
* @return string
*/
public function getToolTip()
{
$i18nOperator = Operators::getI18n(
$this->adapter, $this->priceValidator->getOperator()
);
$toolTip = $this->adapter
->getTranslator()
->trans(
'If cart products quantity is %operator% %quantity%',
array(
'%operator%' => $i18nOperator,
'%quantity%' => $this->quantityValidator->getParam()->getInteger(),
),
'constraint'
);
return $toolTip;
}
} }

View File

@@ -57,6 +57,9 @@ abstract class CouponRuleAbstract implements CouponRuleInterface
/** @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 */
protected $adapter = null;
/** /**
* Constructor * Constructor
* Ex: * Ex:
@@ -70,14 +73,16 @@ abstract class CouponRuleAbstract implements CouponRuleInterface
* Param 2 : * Param 2 :
* $paramsToValidate[AvailableForTotalAmount::PARAM1_PRICE] = 9 * $paramsToValidate[AvailableForTotalAmount::PARAM1_PRICE] = 9
* *
* @param array $validators Array of RuleValidator * @param CouponAdapterInterface $adapter allowing to gather
* validating $paramsToValidate against * all necessary Thelia variables
* * @param array $validators Array of RuleValidator
* @throws InvalidRuleException * validating $paramsToValidate against
*/ */
public function __construct(array $validators) public function __construct(CouponAdapterInterface $adapter, array $validators)
{ {
$this->setValidators($validators); $this->setValidators($validators);
$this->adapter = $adapter;
$this->setParametersToValidate($this->adapter);
} }
/** /**
@@ -104,14 +109,10 @@ abstract class CouponRuleAbstract implements CouponRuleInterface
/** /**
* Check if the current Checkout matches this condition * Check if the current Checkout matches this condition
* *
* @param CouponAdapterInterface $adapter allowing to gather
* all necessary Thelia variables
*
* @return bool * @return bool
*/ */
public function isMatching(CouponAdapterInterface $adapter) public function isMatching()
{ {
$this->setParametersToValidate($adapter);
$this->checkBackOfficeInput(); $this->checkBackOfficeInput();
$this->checkCheckoutInput(); $this->checkCheckoutInput();
@@ -165,13 +166,10 @@ abstract class CouponRuleAbstract implements CouponRuleInterface
/** /**
* Generate current Rule param to be validated from adapter * Generate current Rule param to be validated from adapter
* *
* @param CouponAdapterInterface $adapter allowing to gather
* all necessary Thelia variables
*
* @throws \Thelia\Exception\NotImplementedException * @throws \Thelia\Exception\NotImplementedException
* @return $this * @return $this
*/ */
protected function setParametersToValidate(CouponAdapterInterface $adapter) protected function setParametersToValidate()
{ {
throw new \Thelia\Exception\NotImplementedException(); throw new \Thelia\Exception\NotImplementedException();
} }

View File

@@ -55,12 +55,9 @@ interface CouponRuleInterface
/** /**
* Check if the current Checkout matches this condition * Check if the current Checkout matches this condition
* *
* @param CouponAdapterInterface $adapter allowing to gather
* all necessary Thelia variables
*
* @return bool * @return bool
*/ */
public function isMatching(CouponAdapterInterface $adapter); public function isMatching();
/** /**
* Return all available Operators for this Rule * Return all available Operators for this Rule

View File

@@ -168,12 +168,9 @@ abstract class CouponAbstract implements CouponInterface
* Return effects generated by the coupon * Return effects generated by the coupon
* A negative value * A negative value
* *
* @param CouponAdapterInterface $adapter allowing to gather
* all necessary Thelia variables
*
* @return float Amount removed from the Total Checkout * @return float Amount removed from the Total Checkout
*/ */
public function getDiscount(CouponAdapterInterface $adapter) public function getDiscount()
{ {
return $this->amount; return $this->amount;
} }

View File

@@ -88,16 +88,13 @@ interface CouponInterface
* A positive value * A positive value
* *
* Effects could also affect something else than the final Checkout price * Effects could also affect something else than the final Checkout price
* CouponAdapter could be use to directly pass a Session value * CouponAdapter $adapter could be use to directly pass a Session value
* some would wish to modify * some would wish to modify
* Hence affecting a wide variety of Thelia elements * Hence affecting a wide variety of Thelia elements
* *
* @param CouponAdapterInterface $adapter allowing to gather
* all necessary Thelia variables
*
* @return float Amount removed from the Total Checkout * @return float Amount removed from the Total Checkout
*/ */
public function getDiscount(CouponAdapterInterface $adapter); public function getDiscount();
/** /**
* Return condition to validate the Coupon or not * Return condition to validate the Coupon or not

View File

@@ -92,14 +92,11 @@ class RemoveXPercent extends CouponAbstract
* Return effects generated by the coupon * Return effects generated by the coupon
* A negative value * A negative value
* *
* @param CouponAdapterInterface $adapter allowing to gather
* all necessary Thelia variables
*
* @throws \Thelia\Exception\MissingAdapterException * @throws \Thelia\Exception\MissingAdapterException
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
* @return float * @return float
*/ */
public function getDiscount(CouponAdapterInterface $adapter) public function getDiscount()
{ {
if ($this->percent >= 100) { if ($this->percent >= 100) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(
@@ -107,7 +104,7 @@ class RemoveXPercent extends CouponAbstract
); );
} }
$basePrice = $adapter->getCartTotalPrice(); $basePrice = $this->adapter->getCartTotalPrice();
return $basePrice * (( $this->percent ) / 100); return $basePrice * (( $this->percent ) / 100);
} }

View File

@@ -56,36 +56,11 @@ class ConstraintManagerTest extends \PHPUnit_Framework_TestCase
/** public function incompleteTest()
* Generate valid CouponRuleInterfaces
*
* @return array Array of CouponRuleInterface
*/
public static function generateValidRules()
{ {
$rule1 = new AvailableForTotalAmount( $this->markTestIncomplete(
array( 'This test has not been implemented yet.'
AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator(
Operators::SUPERIOR,
new PriceParam(
, 40.00, 'EUR'
)
)
)
); );
$rule2 = new AvailableForTotalAmount(
array(
AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator(
Operators::INFERIOR,
new PriceParam(
, 400.00, 'EUR'
)
)
)
);
$rules = new CouponRuleCollection(array($rule1, $rule2));
return $rules;
} }
/** /**
@@ -95,48 +70,4 @@ class ConstraintManagerTest extends \PHPUnit_Framework_TestCase
protected function tearDown() protected function tearDown()
{ {
} }
/**
* Generate a fake Adapter
*
* @param array $coupons Coupons
* @param float $cartTotalPrice Cart total price
* @param float $checkoutTotalPrice Checkout total price
* @param float $postagePrice Checkout postage price
*
* @return \PHPUnit_Framework_MockObject_MockObject
*/
public function generateFakeAdapter(array $coupons, $cartTotalPrice, $checkoutTotalPrice, $postagePrice = 6.00)
{
$stubCouponBaseAdapter = $this->getMock(
'Thelia\Coupon\CouponBaseAdapter',
array(
'getCurrentCoupons',
'getCartTotalPrice',
'getCheckoutTotalPrice',
'getCheckoutPostagePrice'
),
array()
);
$stubCouponBaseAdapter->expects($this->any())
->method('getCurrentCoupons')
->will($this->returnValue(($coupons)));
// Return Cart product amount = $cartTotalPrice euros
$stubCouponBaseAdapter->expects($this->any())
->method('getCartTotalPrice')
->will($this->returnValue($cartTotalPrice));
// Return Checkout amount = $checkoutTotalPrice euros
$stubCouponBaseAdapter->expects($this->any())
->method('getCheckoutTotalPrice')
->will($this->returnValue($checkoutTotalPrice));
$stubCouponBaseAdapter->expects($this->any())
->method('getCheckoutPostagePrice')
->will($this->returnValue($postagePrice));
return $stubCouponBaseAdapter;
}
} }

View File

@@ -50,6 +50,8 @@ class AvailableForTotalAmountTest extends \PHPUnit_Framework_TestCase
*/ */
protected function setUp() protected function setUp()
{ {
/** @var CouponAdapterInterface $stubTheliaAdapter */
$stubTheliaAdapter = $this->generateValidCouponBaseAdapterMock();
} }
/** /**
@@ -62,11 +64,11 @@ class AvailableForTotalAmountTest extends \PHPUnit_Framework_TestCase
/** @var CouponAdapterInterface $stubTheliaAdapter */ /** @var CouponAdapterInterface $stubTheliaAdapter */
$stubTheliaAdapter = $this->getMock( $stubTheliaAdapter = $this->getMock(
'CouponBaseAdapter', 'CouponBaseAdapter',
array('getCheckoutTotalPrice'), array('getCartTotalPrice'),
array() array()
); );
$stubTheliaAdapter->expects($this->any()) $stubTheliaAdapter->expects($this->any())
->method('getCheckoutTotalPrice') ->method('getCartTotalPrice')
->will($this->returnValue(421.23)); ->will($this->returnValue(421.23));
return $stubTheliaAdapter; return $stubTheliaAdapter;
@@ -79,21 +81,20 @@ class AvailableForTotalAmountTest extends \PHPUnit_Framework_TestCase
*/ */
public function testValidBackOfficeInput() public function testValidBackOfficeInput()
{ {
/** @var CouponAdapterInterface $stubTheliaAdapter */ $adapter = new CouponBaseAdapter();
$stubTheliaAdapter = $this->generateValidCouponBaseAdapterMock();
$validators = array( $validators = array(
AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator(
Operators::SUPERIOR, Operators::SUPERIOR,
new PriceParam( new PriceParam(
, 421.23, 'EUR' $adapter, 421.23, 'EUR'
) )
) )
); );
$validated = array( $validated = array(
AvailableForTotalAmount::PARAM1_PRICE => $stubTheliaAdapter->getCheckoutTotalPrice() AvailableForTotalAmount::PARAM1_PRICE => $adapter->getCartTotalPrice()
); );
$rule = new AvailableForTotalAmount($validators, $validated); $rule = new AvailableForTotalAmount($adapter, $validators, $validated);
$expected = true; $expected = true;
$actual = $rule->checkBackOfficeInput(); $actual = $rule->checkBackOfficeInput();
@@ -108,22 +109,21 @@ class AvailableForTotalAmountTest extends \PHPUnit_Framework_TestCase
*/ */
public function testInValidBackOfficeInputOperator() public function testInValidBackOfficeInputOperator()
{ {
/** @var CouponAdapterInterface $stubTheliaAdapter */ $adapter = new CouponBaseAdapter();
$stubTheliaAdapter = $this->generateValidCouponBaseAdapterMock();
$validators = array( $validators = array(
AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator(
'X', 'X',
new PriceParam( new PriceParam(
, 421.23, 'EUR' $adapter, 421.23, 'EUR'
) )
) )
); );
$validated = array( $validated = array(
AvailableForTotalAmount::PARAM1_PRICE => $stubTheliaAdapter->getCheckoutTotalPrice() AvailableForTotalAmount::PARAM1_PRICE => $adapter->getCartTotalPrice()
); );
$rule = new AvailableForTotalAmount($validators, $validated); $rule = new AvailableForTotalAmount($adapter, $validators, $validated);
$expected = false; $expected = false;
$actual = $rule->checkBackOfficeInput(); $actual = $rule->checkBackOfficeInput();
@@ -138,8 +138,7 @@ class AvailableForTotalAmountTest extends \PHPUnit_Framework_TestCase
*/ */
public function testInValidBackOfficeInputValue() public function testInValidBackOfficeInputValue()
{ {
/** @var CouponAdapterInterface $stubTheliaAdapter */ $adapter = new CouponBaseAdapter();
$stubTheliaAdapter = $this->generateValidCouponBaseAdapterMock();
$validators = array( $validators = array(
AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator(
@@ -149,9 +148,9 @@ class AvailableForTotalAmountTest extends \PHPUnit_Framework_TestCase
); );
$validated = array( $validated = array(
AvailableForTotalAmount::PARAM1_PRICE => $stubTheliaAdapter->getCheckoutTotalPrice() AvailableForTotalAmount::PARAM1_PRICE => $adapter->getCartTotalPrice()
); );
$rule = new AvailableForTotalAmount($validators, $validated); $rule = new AvailableForTotalAmount($adapter, $validators, $validated);
$expected = false; $expected = false;
$actual = $rule->checkBackOfficeInput(); $actual = $rule->checkBackOfficeInput();
@@ -167,22 +166,21 @@ class AvailableForTotalAmountTest extends \PHPUnit_Framework_TestCase
*/ */
public function testValidCheckoutInput() public function testValidCheckoutInput()
{ {
/** @var CouponAdapterInterface $stubTheliaAdapter */ $adapter = new CouponBaseAdapter();
$stubTheliaAdapter = $this->generateValidCouponBaseAdapterMock();
$validators = array( $validators = array(
AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator(
Operators::SUPERIOR, Operators::SUPERIOR,
new PriceParam( new PriceParam(
, 421.23, 'EUR' $adapter, 421.23, 'EUR'
) )
) )
); );
$validated = array( $validated = array(
AvailableForTotalAmount::PARAM1_PRICE => $stubTheliaAdapter->getCheckoutTotalPrice() AvailableForTotalAmount::PARAM1_PRICE => $adapter->getCartTotalPrice()
); );
$rule = new AvailableForTotalAmount($validators, $validated); $rule = new AvailableForTotalAmount($adapter, $validators, $validated);
$expected = true; $expected = true;
$actual = $rule->checkCheckoutInput(); $actual = $rule->checkCheckoutInput();
@@ -197,11 +195,13 @@ class AvailableForTotalAmountTest extends \PHPUnit_Framework_TestCase
*/ */
public function testInValidCheckoutInputValue() public function testInValidCheckoutInputValue()
{ {
$adapter = new CouponBaseAdapter();
$validators = array( $validators = array(
AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator(
Operators::SUPERIOR, Operators::SUPERIOR,
new PriceParam( new PriceParam(
, 421.23, 'EUR' $adapter, 421.23, 'EUR'
) )
) )
); );
@@ -209,7 +209,7 @@ class AvailableForTotalAmountTest extends \PHPUnit_Framework_TestCase
$validated = array( $validated = array(
AvailableForTotalAmount::PARAM1_PRICE => 421 AvailableForTotalAmount::PARAM1_PRICE => 421
); );
$rule = new AvailableForTotalAmount($validators, $validated); $rule = new AvailableForTotalAmount($adapter, $validators, $validated);
$expected = false; $expected = false;
$actual = $rule->checkCheckoutInput(); $actual = $rule->checkCheckoutInput();
@@ -224,11 +224,13 @@ class AvailableForTotalAmountTest extends \PHPUnit_Framework_TestCase
*/ */
public function testInValidCheckoutInputType() public function testInValidCheckoutInputType()
{ {
$adapter = new CouponBaseAdapter();
$validators = array( $validators = array(
AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator(
Operators::SUPERIOR, Operators::SUPERIOR,
new PriceParam( new PriceParam(
, 421.23, 'EUR' $adapter, 421.23, 'EUR'
) )
) )
); );
@@ -236,7 +238,7 @@ class AvailableForTotalAmountTest extends \PHPUnit_Framework_TestCase
$validated = array( $validated = array(
AvailableForTotalAmount::PARAM1_PRICE => 421 AvailableForTotalAmount::PARAM1_PRICE => 421
); );
$rule = new AvailableForTotalAmount($validators, $validated); $rule = new AvailableForTotalAmount($adapter, $validators, $validated);
$expected = false; $expected = false;
$actual = $rule->checkCheckoutInput(); $actual = $rule->checkCheckoutInput();
@@ -250,11 +252,13 @@ class AvailableForTotalAmountTest extends \PHPUnit_Framework_TestCase
*/ */
public function testMatchingRuleInferior() public function testMatchingRuleInferior()
{ {
$adapter = new CouponBaseAdapter();
$validators = array( $validators = array(
AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator(
Operators::INFERIOR, Operators::INFERIOR,
new PriceParam( new PriceParam(
, 421.23, 'EUR' $adapter, 421.23, 'EUR'
) )
) )
); );
@@ -262,7 +266,7 @@ class AvailableForTotalAmountTest extends \PHPUnit_Framework_TestCase
$validated = array( $validated = array(
AvailableForTotalAmount::PARAM1_PRICE => 421.22 AvailableForTotalAmount::PARAM1_PRICE => 421.22
); );
$rule = new AvailableForTotalAmount($validators, $validated); $rule = new AvailableForTotalAmount($adapter, $validators, $validated);
$expected = true; $expected = true;
$actual = $rule->isMatching(); $actual = $rule->isMatching();
@@ -276,11 +280,13 @@ class AvailableForTotalAmountTest extends \PHPUnit_Framework_TestCase
*/ */
public function testNotMatchingRuleInferior() public function testNotMatchingRuleInferior()
{ {
$adapter = new CouponBaseAdapter();
$validators = array( $validators = array(
AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator(
Operators::INFERIOR, Operators::INFERIOR,
new PriceParam( new PriceParam(
, 421.23, 'EUR' $adapter, 421.23, 'EUR'
) )
) )
); );
@@ -288,7 +294,7 @@ class AvailableForTotalAmountTest extends \PHPUnit_Framework_TestCase
$validated = array( $validated = array(
AvailableForTotalAmount::PARAM1_PRICE => 421.23 AvailableForTotalAmount::PARAM1_PRICE => 421.23
); );
$rule = new AvailableForTotalAmount($validators, $validated); $rule = new AvailableForTotalAmount($adapter, $validators, $validated);
$expected = false; $expected = false;
$actual = $rule->isMatching(); $actual = $rule->isMatching();
@@ -302,22 +308,21 @@ class AvailableForTotalAmountTest extends \PHPUnit_Framework_TestCase
*/ */
public function testMatchingRuleEqual() public function testMatchingRuleEqual()
{ {
/** @var CouponAdapterInterface $stubTheliaAdapter */ $adapter = new CouponBaseAdapter();
$stubTheliaAdapter = $this->generateValidCouponBaseAdapterMock();
$validators = array( $validators = array(
AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator(
Operators::EQUAL, Operators::EQUAL,
new PriceParam( new PriceParam(
, 421.23, 'EUR' $adapter, 421.23, 'EUR'
) )
) )
); );
$validated = array( $validated = array(
AvailableForTotalAmount::PARAM1_PRICE => $stubTheliaAdapter->getCheckoutTotalPrice() AvailableForTotalAmount::PARAM1_PRICE => $adapter->getCartTotalPrice()
); );
$rule = new AvailableForTotalAmount($validators, $validated); $rule = new AvailableForTotalAmount($adapter, $validators, $validated);
$expected = true; $expected = true;
$actual = $rule->isMatching(); $actual = $rule->isMatching();
@@ -331,11 +336,13 @@ class AvailableForTotalAmountTest extends \PHPUnit_Framework_TestCase
*/ */
public function testNotMatchingRuleEqual() public function testNotMatchingRuleEqual()
{ {
$adapter = new CouponBaseAdapter();
$validators = array( $validators = array(
AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator(
Operators::EQUAL, Operators::EQUAL,
new PriceParam( new PriceParam(
, 421.23, 'EUR' $adapter, 421.23, 'EUR'
) )
) )
); );
@@ -343,7 +350,7 @@ class AvailableForTotalAmountTest extends \PHPUnit_Framework_TestCase
$validated = array( $validated = array(
AvailableForTotalAmount::PARAM1_PRICE => 421.22 AvailableForTotalAmount::PARAM1_PRICE => 421.22
); );
$rule = new AvailableForTotalAmount($validators, $validated); $rule = new AvailableForTotalAmount($adapter, $validators, $validated);
$expected = false; $expected = false;
$actual = $rule->isMatching(); $actual = $rule->isMatching();
@@ -357,11 +364,13 @@ class AvailableForTotalAmountTest extends \PHPUnit_Framework_TestCase
*/ */
public function testMatchingRuleSuperior() public function testMatchingRuleSuperior()
{ {
$adapter = new CouponBaseAdapter();
$validators = array( $validators = array(
AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator(
Operators::SUPERIOR, Operators::SUPERIOR,
new PriceParam( new PriceParam(
, 421.23, 'EUR' $adapter, 421.23, 'EUR'
) )
) )
); );
@@ -369,7 +378,7 @@ class AvailableForTotalAmountTest extends \PHPUnit_Framework_TestCase
$validated = array( $validated = array(
AvailableForTotalAmount::PARAM1_PRICE => 421.24 AvailableForTotalAmount::PARAM1_PRICE => 421.24
); );
$rule = new AvailableForTotalAmount($validators, $validated); $rule = new AvailableForTotalAmount($adapter, $validators, $validated);
$expected = true; $expected = true;
$actual = $rule->isMatching(); $actual = $rule->isMatching();
@@ -383,11 +392,13 @@ class AvailableForTotalAmountTest extends \PHPUnit_Framework_TestCase
*/ */
public function testNotMatchingRuleSuperior() public function testNotMatchingRuleSuperior()
{ {
$adapter = new CouponBaseAdapter();
$validators = array( $validators = array(
AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator(
Operators::SUPERIOR, Operators::SUPERIOR,
new PriceParam( new PriceParam(
, 421.23, 'EUR' $adapter, 421.23, 'EUR'
) )
) )
); );
@@ -395,7 +406,7 @@ class AvailableForTotalAmountTest extends \PHPUnit_Framework_TestCase
$validated = array( $validated = array(
AvailableForTotalAmount::PARAM1_PRICE => 421.23 AvailableForTotalAmount::PARAM1_PRICE => 421.23
); );
$rule = new AvailableForTotalAmount($validators, $validated); $rule = new AvailableForTotalAmount($adapter, $validators, $validated);
$expected = false; $expected = false;
$actual = $rule->isMatching(); $actual = $rule->isMatching();

View File

@@ -45,16 +45,23 @@ class AvailableForXArticlesTest extends \PHPUnit_Framework_TestCase
*/ */
protected function setUp() protected function setUp()
{ {
/** @var CouponAdapterInterface $stubTheliaAdapter */
$stubTheliaAdapter = $this->generateValidCouponBaseAdapterMock();
} }
/**
* Return an Adapter Mock wick 4 products int the Cart
*
* @return CouponAdapterInterface
*/
protected function generateValidCouponBaseAdapterMock() protected function generateValidCouponBaseAdapterMock()
{ {
/** @var CouponAdapterInterface $stubTheliaAdapter */ /** @var CouponAdapterInterface $stubTheliaAdapter */
$stubTheliaAdapter = $this->getMock( $stubTheliaAdapter = $this->getMock(
'CouponBaseAdapter', 'CouponBaseAdapter',
array('getNbArticlesInCart'), array('getNbArticlesInCart'),
array() array()
); );
$stubTheliaAdapter->expects($this->any()) $stubTheliaAdapter->expects($this->any())
->method('getNbArticlesInCart') ->method('getNbArticlesInCart')
->will($this->returnValue(4)); ->will($this->returnValue(4));
@@ -69,12 +76,11 @@ class AvailableForXArticlesTest extends \PHPUnit_Framework_TestCase
*/ */
public function testValidBackOfficeInput() public function testValidBackOfficeInput()
{ {
/** @var CouponAdapterInterface $stubTheliaAdapter */ $adapter = new CouponBaseAdapter();
$stubTheliaAdapter = $this->generateValidCouponBaseAdapterMock();
$validators = array(4); $validators = array(4);
$validated = array($stubTheliaAdapter->getNbArticlesInCart()); $validated = array($adapter->getNbArticlesInCart());
$rule = new AvailableForXArticles($validators, $validated); $rule = new AvailableForXArticles($adapter, $validators, $validated);
$expected = true; $expected = true;
$actual = $rule->checkBackOfficeInput(); $actual = $rule->checkBackOfficeInput();
@@ -88,28 +94,27 @@ class AvailableForXArticlesTest extends \PHPUnit_Framework_TestCase
*/ */
public function testInValidBackOfficeInput() public function testInValidBackOfficeInput()
{ {
/** @var CouponAdapterInterface $stubTheliaAdapter */ $adapter = new CouponBaseAdapter();
$stubTheliaAdapter = $this->generateValidCouponBaseAdapterMock();
$validators = array(4.5); $validators = array(4.5);
$validated = array($stubTheliaAdapter->getNbArticlesInCart()); $validated = array($adapter->getNbArticlesInCart());
$rule = new AvailableForXArticles($validators, $validated); $rule = new AvailableForXArticles($adapter, $validators, $validated);
$expected = false; $expected = false;
$actual = $rule->checkBackOfficeInput(); $actual = $rule->checkBackOfficeInput();
$this->assertEquals($expected, $actual); $this->assertEquals($expected, $actual);
$validators = array(-1); $validators = array(-1);
$validated = array($stubTheliaAdapter->getNbArticlesInCart()); $validated = array($adapter->getNbArticlesInCart());
$rule = new AvailableForXArticles($validators, $validated); $rule = new AvailableForXArticles($adapter, $validators, $validated);
$expected = false; $expected = false;
$actual = $rule->checkBackOfficeInput(); $actual = $rule->checkBackOfficeInput();
$this->assertEquals($expected, $actual); $this->assertEquals($expected, $actual);
$validators = array('bad'); $validators = array('bad');
$validated = array($stubTheliaAdapter->getNbArticlesInCart()); $validated = array($adapter->getNbArticlesInCart());
$rule = new AvailableForXArticles($validators, $validated); $rule = new AvailableForXArticles($adapter, $validators, $validated);
$expected = false; $expected = false;
$actual = $rule->checkBackOfficeInput(); $actual = $rule->checkBackOfficeInput();
@@ -125,12 +130,10 @@ class AvailableForXArticlesTest extends \PHPUnit_Framework_TestCase
*/ */
public function testValidCheckoutInput() public function testValidCheckoutInput()
{ {
/** @var CouponAdapterInterface $stubTheliaAdapter */ $adapter = new CouponBaseAdapter();
$stubTheliaAdapter = $this->generateValidCouponBaseAdapterMock();
$validators = array(4); $validators = array(4);
$validated = array($stubTheliaAdapter->getNbArticlesInCart()); $validated = array($adapter->getNbArticlesInCart());
$rule = new AvailableForXArticles($validators, $validated); $rule = new AvailableForXArticles($adapter, $validators, $validated);
$expected = true; $expected = true;
$actual = $rule->checkCheckoutInput(); $actual = $rule->checkCheckoutInput();
@@ -144,28 +147,26 @@ class AvailableForXArticlesTest extends \PHPUnit_Framework_TestCase
*/ */
public function testInValidCheckoutInput() public function testInValidCheckoutInput()
{ {
/** @var CouponAdapterInterface $stubTheliaAdapter */ $adapter = new CouponBaseAdapter();
$stubTheliaAdapter = $this->generateValidCouponBaseAdapterMock();
$validators = array(4.5); $validators = array(4.5);
$validated = array($stubTheliaAdapter->getNbArticlesInCart()); $validated = array($adapter->getNbArticlesInCart());
$rule = new AvailableForXArticles($validators, $validated); $rule = new AvailableForXArticles($adapter, $validators, $validated);
$expected = false; $expected = false;
$actual = $rule->checkCheckoutInput(); $actual = $rule->checkCheckoutInput();
$this->assertEquals($expected, $actual); $this->assertEquals($expected, $actual);
$validators = array(-1); $validators = array(-1);
$validated = array($stubTheliaAdapter->getNbArticlesInCart()); $validated = array($adapter->getNbArticlesInCart());
$rule = new AvailableForXArticles($validators, $validated); $rule = new AvailableForXArticles($adapter, $validators, $validated);
$expected = false; $expected = false;
$actual = $rule->checkCheckoutInput(); $actual = $rule->checkCheckoutInput();
$this->assertEquals($expected, $actual); $this->assertEquals($expected, $actual);
$validators = array('bad'); $validators = array('bad');
$validated = array($stubTheliaAdapter->getNbArticlesInCart()); $validated = array($adapter->getNbArticlesInCart());
$rule = new AvailableForXArticles($validators, $validated); $rule = new AvailableForXArticles($adapter, $validators, $validated);
$expected = false; $expected = false;
$actual = $rule->checkCheckoutInput(); $actual = $rule->checkCheckoutInput();
@@ -179,12 +180,10 @@ class AvailableForXArticlesTest extends \PHPUnit_Framework_TestCase
*/ */
public function testMatchingRuleEqual() public function testMatchingRuleEqual()
{ {
/** @var CouponAdapterInterface $stubTheliaAdapter */ $adapter = new CouponBaseAdapter();
$stubTheliaAdapter = $this->generateValidCouponBaseAdapterMock();
$validators = array(4); $validators = array(4);
$validated = array($stubTheliaAdapter->getNbArticlesInCart()); $validated = array($adapter->getNbArticlesInCart());
$rule = new AvailableForXArticles($validators, $validated); $rule = new AvailableForXArticles($adapter, $validators, $validated);
$expected = true; $expected = true;
$actual = $rule->isMatching(); $actual = $rule->isMatching();
@@ -198,12 +197,10 @@ class AvailableForXArticlesTest extends \PHPUnit_Framework_TestCase
*/ */
public function testMatchingRuleSuperior() public function testMatchingRuleSuperior()
{ {
/** @var CouponAdapterInterface $stubTheliaAdapter */ $adapter = new CouponBaseAdapter();
$stubTheliaAdapter = $this->generateValidCouponBaseAdapterMock();
$validators = array(5); $validators = array(5);
$validated = array($stubTheliaAdapter->getNbArticlesInCart()); $validated = array($adapter->getNbArticlesInCart());
$rule = new AvailableForXArticles($validators, $validated); $rule = new AvailableForXArticles($adapter, $validators, $validated);
$expected = true; $expected = true;
$actual = $rule->isMatching(); $actual = $rule->isMatching();
@@ -217,12 +214,10 @@ class AvailableForXArticlesTest extends \PHPUnit_Framework_TestCase
*/ */
public function testNotMatchingRule() public function testNotMatchingRule()
{ {
/** @var CouponAdapterInterface $stubTheliaAdapter */ $adapter = new CouponBaseAdapter();
$stubTheliaAdapter = $this->generateValidCouponBaseAdapterMock();
$validators = array(3); $validators = array(3);
$validated = array($stubTheliaAdapter->getNbArticlesInCart()); $validated = array($adapter->getNbArticlesInCart());
$rule = new AvailableForXArticles($validators, $validated); $rule = new AvailableForXArticles($adapter, $validators, $validated);
$expected = false; $expected = false;
$actual = $rule->isMatching(); $actual = $rule->isMatching();

View File

@@ -55,10 +55,11 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase
*/ */
public function testOperatorInferiorValidBefore() public function testOperatorInferiorValidBefore()
{ {
$adapter = new CouponBaseAdapter();
// Given // Given
$a = 11; $a = 11;
$operator = Operators::INFERIOR; $operator = Operators::INFERIOR;
$b = new QuantityParam(, 12); $b = new QuantityParam($adapter, 12);
// When // When
$actual = Operators::isValid($a, $operator, $b); $actual = Operators::isValid($a, $operator, $b);
@@ -75,9 +76,10 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase
public function testOperatorInferiorInvalidEquals() public function testOperatorInferiorInvalidEquals()
{ {
// Given // Given
$adapter = new CouponBaseAdapter();
$a = 12; $a = 12;
$operator = Operators::INFERIOR; $operator = Operators::INFERIOR;
$b = new QuantityParam(, 12); $b = new QuantityParam($adapter, 12);
// When // When
$actual = Operators::isValid($a, $operator, $b); $actual = Operators::isValid($a, $operator, $b);
@@ -94,9 +96,10 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase
public function testOperatorInferiorInvalidAfter() public function testOperatorInferiorInvalidAfter()
{ {
// Given // Given
$adapter = new CouponBaseAdapter();
$a = 13; $a = 13;
$operator = Operators::INFERIOR; $operator = Operators::INFERIOR;
$b = new QuantityParam(, 12); $b = new QuantityParam($adapter, 12);
// When // When
$actual = Operators::isValid($a, $operator, $b); $actual = Operators::isValid($a, $operator, $b);
@@ -113,9 +116,10 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase
public function testOperatorInferiorOrEqualValidEqual() public function testOperatorInferiorOrEqualValidEqual()
{ {
// Given // Given
$adapter = new CouponBaseAdapter();
$a = 11; $a = 11;
$operator = Operators::INFERIOR_OR_EQUAL; $operator = Operators::INFERIOR_OR_EQUAL;
$b = new QuantityParam(, 11); $b = new QuantityParam($adapter, 11);
// When // When
$actual = Operators::isValid($a, $operator, $b); $actual = Operators::isValid($a, $operator, $b);
@@ -132,9 +136,10 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase
public function testOperatorInferiorOrEqualValidBefore() public function testOperatorInferiorOrEqualValidBefore()
{ {
// Given // Given
$adapter = new CouponBaseAdapter();
$a = 10; $a = 10;
$operator = Operators::INFERIOR_OR_EQUAL; $operator = Operators::INFERIOR_OR_EQUAL;
$b = new QuantityParam(, 11); $b = new QuantityParam($adapter, 11);
// When // When
$actual = Operators::isValid($a, $operator, $b); $actual = Operators::isValid($a, $operator, $b);
@@ -151,9 +156,10 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase
public function testOperatorInferiorOrEqualInValidAfter() public function testOperatorInferiorOrEqualInValidAfter()
{ {
// Given // Given
$adapter = new CouponBaseAdapter();
$a = 12; $a = 12;
$operator = Operators::INFERIOR_OR_EQUAL; $operator = Operators::INFERIOR_OR_EQUAL;
$b = new QuantityParam(, 11); $b = new QuantityParam($adapter, 11);
// When // When
$actual = Operators::isValid($a, $operator, $b); $actual = Operators::isValid($a, $operator, $b);
@@ -170,9 +176,10 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase
public function testOperatorEqualValidEqual() public function testOperatorEqualValidEqual()
{ {
// Given // Given
$adapter = new CouponBaseAdapter();
$a = 12; $a = 12;
$operator = Operators::EQUAL; $operator = Operators::EQUAL;
$b = new QuantityParam(, 12); $b = new QuantityParam($adapter, 12);
// When // When
$actual = Operators::isValid($a, $operator, $b); $actual = Operators::isValid($a, $operator, $b);
@@ -189,9 +196,10 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase
public function testOperatorEqualInValidBefore() public function testOperatorEqualInValidBefore()
{ {
// Given // Given
$adapter = new CouponBaseAdapter();
$a = 11; $a = 11;
$operator = Operators::EQUAL; $operator = Operators::EQUAL;
$b = new QuantityParam(, 12); $b = new QuantityParam($adapter, 12);
// When // When
$actual = Operators::isValid($a, $operator, $b); $actual = Operators::isValid($a, $operator, $b);
@@ -208,9 +216,10 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase
public function testOperatorEqualInValidAfter() public function testOperatorEqualInValidAfter()
{ {
// Given // Given
$adapter = new CouponBaseAdapter();
$a = 13; $a = 13;
$operator = Operators::EQUAL; $operator = Operators::EQUAL;
$b = new QuantityParam(, 12); $b = new QuantityParam($adapter, 12);
// When // When
$actual = Operators::isValid($a, $operator, $b); $actual = Operators::isValid($a, $operator, $b);
@@ -227,9 +236,10 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase
public function testOperatorSuperiorOrEqualValidEqual() public function testOperatorSuperiorOrEqualValidEqual()
{ {
// Given // Given
$adapter = new CouponBaseAdapter();
$a = 13; $a = 13;
$operator = Operators::SUPERIOR_OR_EQUAL; $operator = Operators::SUPERIOR_OR_EQUAL;
$b = new QuantityParam(, 13); $b = new QuantityParam($adapter, 13);
// When // When
$actual = Operators::isValid($a, $operator, $b); $actual = Operators::isValid($a, $operator, $b);
@@ -246,9 +256,10 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase
public function testOperatorSuperiorOrEqualAfter() public function testOperatorSuperiorOrEqualAfter()
{ {
// Given // Given
$adapter = new CouponBaseAdapter();
$a = 14; $a = 14;
$operator = Operators::SUPERIOR_OR_EQUAL; $operator = Operators::SUPERIOR_OR_EQUAL;
$b = new QuantityParam(, 13); $b = new QuantityParam($adapter, 13);
// When // When
$actual = Operators::isValid($a, $operator, $b); $actual = Operators::isValid($a, $operator, $b);
@@ -265,9 +276,10 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase
public function testOperatorSuperiorOrEqualInvalidBefore() public function testOperatorSuperiorOrEqualInvalidBefore()
{ {
// Given // Given
$adapter = new CouponBaseAdapter();
$a = 12; $a = 12;
$operator = Operators::SUPERIOR_OR_EQUAL; $operator = Operators::SUPERIOR_OR_EQUAL;
$b = new QuantityParam(, 13); $b = new QuantityParam($adapter, 13);
// When // When
$actual = Operators::isValid($a, $operator, $b); $actual = Operators::isValid($a, $operator, $b);
@@ -284,9 +296,10 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase
public function testOperatorSuperiorValidAfter() public function testOperatorSuperiorValidAfter()
{ {
// Given // Given
$adapter = new CouponBaseAdapter();
$a = 13; $a = 13;
$operator = Operators::SUPERIOR; $operator = Operators::SUPERIOR;
$b = new QuantityParam(, 12); $b = new QuantityParam($adapter, 12);
// When // When
$actual = Operators::isValid($a, $operator, $b); $actual = Operators::isValid($a, $operator, $b);
@@ -303,9 +316,10 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase
public function testOperatorSuperiorInvalidEqual() public function testOperatorSuperiorInvalidEqual()
{ {
// Given // Given
$adapter = new CouponBaseAdapter();
$a = 12; $a = 12;
$operator = Operators::SUPERIOR; $operator = Operators::SUPERIOR;
$b = new QuantityParam(, 12); $b = new QuantityParam($adapter, 12);
// When // When
$actual = Operators::isValid($a, $operator, $b); $actual = Operators::isValid($a, $operator, $b);
@@ -322,9 +336,10 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase
public function testOperatorSuperiorInvalidBefore() public function testOperatorSuperiorInvalidBefore()
{ {
// Given // Given
$adapter = new CouponBaseAdapter();
$a = 11; $a = 11;
$operator = Operators::SUPERIOR; $operator = Operators::SUPERIOR;
$b = new QuantityParam(, 12); $b = new QuantityParam($adapter, 12);
// When // When
$actual = Operators::isValid($a, $operator, $b); $actual = Operators::isValid($a, $operator, $b);
@@ -341,9 +356,10 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase
public function testOperatorDifferentValid() public function testOperatorDifferentValid()
{ {
// Given // Given
$adapter = new CouponBaseAdapter();
$a = 12; $a = 12;
$operator = Operators::DIFFERENT; $operator = Operators::DIFFERENT;
$b = new QuantityParam(, 11); $b = new QuantityParam($adapter, 11);
// When // When
$actual = Operators::isValid($a, $operator, $b); $actual = Operators::isValid($a, $operator, $b);
@@ -360,9 +376,10 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase
public function testOperatorDifferentInvalidEquals() public function testOperatorDifferentInvalidEquals()
{ {
// Given // Given
$adapter = new CouponBaseAdapter();
$a = 11; $a = 11;
$operator = Operators::DIFFERENT; $operator = Operators::DIFFERENT;
$b = new QuantityParam(, 11); $b = new QuantityParam($adapter, 11);
// When // When
$actual = Operators::isValid($a, $operator, $b); $actual = Operators::isValid($a, $operator, $b);
@@ -379,9 +396,10 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase
public function testOperatorInValid() public function testOperatorInValid()
{ {
// Given // Given
$adapter = new CouponBaseAdapter();
$a = 12; $a = 12;
$operator = 'X'; $operator = 'X';
$b = new QuantityParam(, 11); $b = new QuantityParam($adapter, 11);
// When // When
$actual = Operators::isValid($a, $operator, $b); $actual = Operators::isValid($a, $operator, $b);

View File

@@ -24,6 +24,7 @@
namespace Thelia\Coupon; namespace Thelia\Coupon;
use InvalidArgumentException; use InvalidArgumentException;
use Thelia\Constraint\Validator\PriceParam;
use Thelia\Constraint\Validator\QuantityParam; use Thelia\Constraint\Validator\QuantityParam;
/** /**
@@ -173,7 +174,7 @@ class QuantityParamTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($param->getValue(), $unserialized->getValue()); $this->assertEquals($param->getValue(), $unserialized->getValue());
$this->assertEquals($param->getInteger(), $unserialized->getInteger()); $this->assertEquals($param->getInteger(), $unserialized->getInteger());
$new = new PriceParam($adapter, $unserialized->getInteger()); $new = new QuantityParam($adapter, $unserialized->getInteger());
$this->assertEquals($param->getInteger(), $new->getInteger()); $this->assertEquals($param->getInteger(), $new->getInteger());
} }

View File

@@ -204,7 +204,7 @@ class CouponFactoryTest extends \PHPUnit_Framework_TestCase
protected function generateValidRules() protected function generateValidRules()
{ {
$rule1 = new AvailableForTotalAmount( $rule1 = new AvailableForTotalAmount(
array( , array(
AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator(
Operators::SUPERIOR, Operators::SUPERIOR,
new PriceParam( new PriceParam(
@@ -214,7 +214,7 @@ class CouponFactoryTest extends \PHPUnit_Framework_TestCase
) )
); );
$rule2 = new AvailableForTotalAmount( $rule2 = new AvailableForTotalAmount(
array( , array(
AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator(
Operators::INFERIOR, Operators::INFERIOR,
new PriceParam( new PriceParam(

View File

@@ -110,7 +110,7 @@ Sed facilisis pellentesque nisl, eu tincidunt erat scelerisque a. Nullam malesua
/** @var CouponInterface $coupon1 */ /** @var CouponInterface $coupon1 */
$coupon1 = self::generateValidCoupon(); $coupon1 = self::generateValidCoupon();
$rule1 = new AvailableForTotalAmount( $rule1 = new AvailableForTotalAmount(
array( $adapter, array(
AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator(
Operators::SUPERIOR, Operators::SUPERIOR,
new PriceParam( new PriceParam(
@@ -148,7 +148,7 @@ Sed facilisis pellentesque nisl, eu tincidunt erat scelerisque a. Nullam malesua
$checkoutTotalPrice = 26.00; $checkoutTotalPrice = 26.00;
$rule1 = new AvailableForTotalAmount( $rule1 = new AvailableForTotalAmount(
array( $adapter, array(
AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator(
Operators::SUPERIOR, Operators::SUPERIOR,
new PriceParam( new PriceParam(
@@ -185,7 +185,7 @@ Sed facilisis pellentesque nisl, eu tincidunt erat scelerisque a. Nullam malesua
$checkoutTotalPrice = 27.00; $checkoutTotalPrice = 27.00;
$rule1 = new AvailableForTotalAmount( $rule1 = new AvailableForTotalAmount(
array( $adapter, array(
AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator(
Operators::SUPERIOR, Operators::SUPERIOR,
new PriceParam( new PriceParam(
@@ -642,7 +642,7 @@ Sed facilisis pellentesque nisl, eu tincidunt erat scelerisque a. Nullam malesua
{ {
$adapter = new CouponBaseAdapter(); $adapter = new CouponBaseAdapter();
$rule1 = new AvailableForTotalAmount( $rule1 = new AvailableForTotalAmount(
array( $adapter, array(
AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator(
Operators::SUPERIOR, Operators::SUPERIOR,
new PriceParam( new PriceParam(
@@ -652,7 +652,7 @@ Sed facilisis pellentesque nisl, eu tincidunt erat scelerisque a. Nullam malesua
) )
); );
$rule2 = new AvailableForTotalAmount( $rule2 = new AvailableForTotalAmount(
array( $adapter, array(
AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator(
Operators::INFERIOR, Operators::INFERIOR,
new PriceParam( new PriceParam(

View File

@@ -47,7 +47,7 @@ class CouponRuleCollectionTest extends \PHPUnit_Framework_TestCase
public function testRuleSerialisation() public function testRuleSerialisation()
{ {
$rule1 = new AvailableForTotalAmount( $rule1 = new AvailableForTotalAmount(
array( , array(
AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator(
Operators::SUPERIOR, Operators::SUPERIOR,
new PriceParam( new PriceParam(
@@ -57,7 +57,7 @@ class CouponRuleCollectionTest extends \PHPUnit_Framework_TestCase
) )
); );
$rule2 = new AvailableForTotalAmount( $rule2 = new AvailableForTotalAmount(
array( , array(
AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator(
Operators::INFERIOR, Operators::INFERIOR,
new PriceParam( new PriceParam(

View File

@@ -45,6 +45,13 @@ class RemoveXAmountForCategoryYTest extends \PHPUnit_Framework_TestCase
{ {
} }
public function incompleteTest()
{
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
/** /**
* Tears down the fixture, for example, closes a network connection. * Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed. * This method is called after a test is executed.

View File

@@ -151,7 +151,7 @@ class RemoveXAmountTest extends \PHPUnit_Framework_TestCase
$coupon = CouponManagerTest::generateValidCoupon(null, null, null, null, null, null, null, null, false, false); $coupon = CouponManagerTest::generateValidCoupon(null, null, null, null, null, null, null, null, false, false);
$expected = 10; $expected = 10;
$actual = $coupon->getDiscount($adapter); $actual = $coupon->getDiscount();
$this->assertEquals($expected, $actual); $this->assertEquals($expected, $actual);
} }
@@ -234,7 +234,7 @@ class RemoveXAmountTest extends \PHPUnit_Framework_TestCase
// Then // Then
$expected = 10.00; $expected = 10.00;
$actual = $coupon->getDiscount($adapter); $actual = $coupon->getDiscount();
$this->assertEquals($expected, $actual); $this->assertEquals($expected, $actual);
} }
@@ -259,7 +259,7 @@ class RemoveXAmountTest extends \PHPUnit_Framework_TestCase
// Then // Then
$expected = 10.00; $expected = 10.00;
$actual = $coupon->getDiscount($adapter); $actual = $coupon->getDiscount();
$this->assertEquals($expected, $actual); $this->assertEquals($expected, $actual);
} }
@@ -284,7 +284,7 @@ class RemoveXAmountTest extends \PHPUnit_Framework_TestCase
// Then // Then
$expected = 10.00; $expected = 10.00;
$actual = $coupon->getDiscount($adapter); $actual = $coupon->getDiscount();
$this->assertEquals($expected, $actual); $this->assertEquals($expected, $actual);
} }
@@ -309,7 +309,7 @@ class RemoveXAmountTest extends \PHPUnit_Framework_TestCase
// Then // Then
$expected = 10.00; $expected = 10.00;
$actual = $coupon->getDiscount($adapter); $actual = $coupon->getDiscount();
$this->assertEquals($expected, $actual); $this->assertEquals($expected, $actual);
} }
@@ -334,7 +334,7 @@ class RemoveXAmountTest extends \PHPUnit_Framework_TestCase
// Then // Then
$expected = 10.00; $expected = 10.00;
$actual = $coupon->getDiscount($adapter); $actual = $coupon->getDiscount();
$this->assertEquals($expected, $actual); $this->assertEquals($expected, $actual);
} }
@@ -371,7 +371,7 @@ class RemoveXAmountTest extends \PHPUnit_Framework_TestCase
) )
); );
return new AvailableForTotalAmount($validators); return new AvailableForTotalAmount($adapter, $validators);
} }
} }

View File

@@ -45,6 +45,13 @@ class RemoveXPercentForCategoryYTest extends \PHPUnit_Framework_TestCase
{ {
} }
public function incompleteTest()
{
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
/** /**
* Tears down the fixture, for example, closes a network connection. * Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed. * This method is called after a test is executed.

View File

@@ -63,7 +63,7 @@ class RemoveXPercentTest extends \PHPUnit_Framework_TestCase
*/ */
public function testIsCumulative() public function testIsCumulative()
{ {
$coupon = self::generateValidCoupon(null, null, null, null, null, null, null, null, true, true); $coupon = $this->generateValidCoupon(null, null, null, null, null, null, null, null, true, true);
$actual = $coupon->isCumulative(); $actual = $coupon->isCumulative();
$this->assertTrue($actual); $this->assertTrue($actual);
@@ -77,7 +77,7 @@ class RemoveXPercentTest extends \PHPUnit_Framework_TestCase
*/ */
public function testIsNotCumulative() public function testIsNotCumulative()
{ {
$coupon = self::generateValidCoupon(null, null, null, null, null, null, null, null, false, false); $coupon = $this->generateValidCoupon(null, null, null, null, null, null, null, null, false, false);
$actual = $coupon->isCumulative(); $actual = $coupon->isCumulative();
$this->assertFalse($actual); $this->assertFalse($actual);
@@ -92,7 +92,7 @@ class RemoveXPercentTest extends \PHPUnit_Framework_TestCase
*/ */
public function testIsRemovingPostage() public function testIsRemovingPostage()
{ {
$coupon = self::generateValidCoupon(null, null, null, null, null, null, null, null, true, true); $coupon = $this->generateValidCoupon(null, null, null, null, null, null, null, null, true, true);
$actual = $coupon->isRemovingPostage(); $actual = $coupon->isRemovingPostage();
$this->assertTrue($actual); $this->assertTrue($actual);
@@ -105,7 +105,7 @@ class RemoveXPercentTest extends \PHPUnit_Framework_TestCase
*/ */
public function testIsNotRemovingPostage() public function testIsNotRemovingPostage()
{ {
$coupon = self::generateValidCoupon(null, null, null, null, null, null, null, null, false, false); $coupon = $this->generateValidCoupon(null, null, null, null, null, null, null, null, false, false);
$actual = $coupon->isRemovingPostage(); $actual = $coupon->isRemovingPostage();
$this->assertFalse($actual); $this->assertFalse($actual);
@@ -120,7 +120,7 @@ class RemoveXPercentTest extends \PHPUnit_Framework_TestCase
public function testGetEffect() public function testGetEffect()
{ {
$adapter = $this->generateFakeAdapter(245); $adapter = $this->generateFakeAdapter(245);
$coupon = self::generateValidCoupon(null, null, null, null, null, null, null, null, false, false); $coupon = $this->generateValidCoupon(null, null, null, null, null, null, null, null, false, false);
$expected = 24.50; $expected = 24.50;
$actual = $coupon->getDiscount($adapter); $actual = $coupon->getDiscount($adapter);
@@ -149,7 +149,7 @@ class RemoveXPercentTest extends \PHPUnit_Framework_TestCase
421.23 421.23
); );
$coupon = self::generateValidCoupon(null, null, null, null, null, null, null, null, false, false); $coupon = $this->generateValidCoupon(null, null, null, null, null, null, null, null, false, false);
// When // When
$coupon->setRules(new CouponRuleCollection(array($rule0, $rule1, $rule2))); $coupon->setRules(new CouponRuleCollection(array($rule0, $rule1, $rule2)));
@@ -179,7 +179,7 @@ class RemoveXPercentTest extends \PHPUnit_Framework_TestCase
); );
$rule2 = $this; $rule2 = $this;
$coupon = self::generateValidCoupon(null, null, null, null, null, null, null, null, false, false); $coupon = $this->generateValidCoupon(null, null, null, null, null, null, null, null, false, false);
// When // When
$coupon->setRules(new CouponRuleCollection(array($rule0, $rule1, $rule2))); $coupon->setRules(new CouponRuleCollection(array($rule0, $rule1, $rule2)));
@@ -194,19 +194,18 @@ class RemoveXPercentTest extends \PHPUnit_Framework_TestCase
public function testGetEffectIfTotalAmountInferiorTo400Valid() public function testGetEffectIfTotalAmountInferiorTo400Valid()
{ {
// Given // Given
$adapter = $this->generateFakeAdapter(245);
$rule0 = $this->generateValidRuleAvailableForTotalAmountOperatorTo( $rule0 = $this->generateValidRuleAvailableForTotalAmountOperatorTo(
Operators::INFERIOR, Operators::INFERIOR,
400.00 400.00
); );
$coupon = self::generateValidCoupon(null, null, null, null, null, null, null, null, false, false); $coupon = $this->generateValidCoupon(null, null, null, null, null, null, null, null, false, false);
// When // When
$coupon->setRules(new CouponRuleCollection(array($rule0))); $coupon->setRules(new CouponRuleCollection(array($rule0)));
// Then // Then
$expected = 24.50; $expected = 24.50;
$actual = $coupon->getDiscount($adapter); $actual = $coupon->getDiscount();
$this->assertEquals($expected, $actual); $this->assertEquals($expected, $actual);
} }
@@ -219,19 +218,18 @@ class RemoveXPercentTest extends \PHPUnit_Framework_TestCase
public function testGetEffectIfTotalAmountInferiorOrEqualTo400Valid() public function testGetEffectIfTotalAmountInferiorOrEqualTo400Valid()
{ {
// Given // Given
$adapter = $this->generateFakeAdapter(245);
$rule0 = $this->generateValidRuleAvailableForTotalAmountOperatorTo( $rule0 = $this->generateValidRuleAvailableForTotalAmountOperatorTo(
Operators::INFERIOR_OR_EQUAL, Operators::INFERIOR_OR_EQUAL,
400.00 400.00
); );
$coupon = self::generateValidCoupon(null, null, null, null, null, null, null, null, false, false); $coupon = $this->generateValidCoupon(null, null, null, null, null, null, null, null, false, false);
// When // When
$coupon->setRules(new CouponRuleCollection(array($rule0))); $coupon->setRules(new CouponRuleCollection(array($rule0)));
// Then // Then
$expected = 24.50; $expected = 24.50;
$actual = $coupon->getDiscount($adapter); $actual = $coupon->getDiscount();
$this->assertEquals($expected, $actual); $this->assertEquals($expected, $actual);
} }
@@ -244,19 +242,18 @@ class RemoveXPercentTest extends \PHPUnit_Framework_TestCase
public function testGetEffectIfTotalAmountEqualTo400Valid() public function testGetEffectIfTotalAmountEqualTo400Valid()
{ {
// Given // Given
$adapter = $this->generateFakeAdapter(245);
$rule0 = $this->generateValidRuleAvailableForTotalAmountOperatorTo( $rule0 = $this->generateValidRuleAvailableForTotalAmountOperatorTo(
Operators::EQUAL, Operators::EQUAL,
400.00 400.00
); );
$coupon = self::generateValidCoupon(null, null, null, null, null, null, null, null, false, false); $coupon = $this->generateValidCoupon(null, null, null, null, null, null, null, null, false, false);
// When // When
$coupon->setRules(new CouponRuleCollection(array($rule0))); $coupon->setRules(new CouponRuleCollection(array($rule0)));
// Then // Then
$expected = 24.50; $expected = 24.50;
$actual = $coupon->getDiscount($adapter); $actual = $coupon->getDiscount();
$this->assertEquals($expected, $actual); $this->assertEquals($expected, $actual);
} }
@@ -269,19 +266,18 @@ class RemoveXPercentTest extends \PHPUnit_Framework_TestCase
public function testGetEffectIfTotalAmountSuperiorOrEqualTo400Valid() public function testGetEffectIfTotalAmountSuperiorOrEqualTo400Valid()
{ {
// Given // Given
$adapter = $this->generateFakeAdapter(245);
$rule0 = $this->generateValidRuleAvailableForTotalAmountOperatorTo( $rule0 = $this->generateValidRuleAvailableForTotalAmountOperatorTo(
Operators::SUPERIOR_OR_EQUAL, Operators::SUPERIOR_OR_EQUAL,
400.00 400.00
); );
$coupon = self::generateValidCoupon(null, null, null, null, null, null, null, null, false, false); $coupon = $this->generateValidCoupon(null, null, null, null, null, null, null, null, false, false);
// When // When
$coupon->setRules(new CouponRuleCollection(array($rule0))); $coupon->setRules(new CouponRuleCollection(array($rule0)));
// Then // Then
$expected = 24.50; $expected = 24.50;
$actual = $coupon->getDiscount($adapter); $actual = $coupon->getDiscount();
$this->assertEquals($expected, $actual); $this->assertEquals($expected, $actual);
} }
@@ -294,19 +290,18 @@ class RemoveXPercentTest extends \PHPUnit_Framework_TestCase
public function testGetEffectIfTotalAmountSuperiorTo400Valid() public function testGetEffectIfTotalAmountSuperiorTo400Valid()
{ {
// Given // Given
$adapter = $this->generateFakeAdapter(245);
$rule0 = $this->generateValidRuleAvailableForTotalAmountOperatorTo( $rule0 = $this->generateValidRuleAvailableForTotalAmountOperatorTo(
Operators::SUPERIOR, Operators::SUPERIOR,
400.00 400.00
); );
$coupon = self::generateValidCoupon(null, null, null, null, null, null, null, null, false, false); $coupon = $this->generateValidCoupon(null, null, null, null, null, null, null, null, false, false);
// When // When
$coupon->setRules(new CouponRuleCollection(array($rule0))); $coupon->setRules(new CouponRuleCollection(array($rule0)));
// Then // Then
$expected = 24.50; $expected = 24.50;
$actual = $coupon->getDiscount($adapter); $actual = $coupon->getDiscount();
$this->assertEquals($expected, $actual); $this->assertEquals($expected, $actual);
} }
@@ -331,7 +326,7 @@ class RemoveXPercentTest extends \PHPUnit_Framework_TestCase
* *
* @return CouponInterface * @return CouponInterface
*/ */
public static function generateValidCoupon( public function generateValidCoupon(
$code = null, $code = null,
$title = null, $title = null,
$shortDescription = null, $shortDescription = null,
@@ -345,7 +340,8 @@ class RemoveXPercentTest extends \PHPUnit_Framework_TestCase
$isAvailableOnSpecialOffers = null, $isAvailableOnSpecialOffers = null,
$maxUsage = null $maxUsage = null
) { ) {
$adapter = new CouponBaseAdapter(); $adapter = $this->generateFakeAdapter(245);
if ($code === null) { if ($code === null) {
$code = CouponManagerTest::VALID_CODE; $code = CouponManagerTest::VALID_CODE;
} }
@@ -417,7 +413,7 @@ class RemoveXPercentTest extends \PHPUnit_Framework_TestCase
) )
); );
return new AvailableForTotalAmount($validators); return new AvailableForTotalAmount($adapter, $validators);
} }
/** /**