diff --git a/core/lib/Thelia/Coupon/CouponAdapterInterface.php b/core/lib/Thelia/Coupon/CouponAdapterInterface.php index 9db1160f1..21ff870f6 100644 --- a/core/lib/Thelia/Coupon/CouponAdapterInterface.php +++ b/core/lib/Thelia/Coupon/CouponAdapterInterface.php @@ -23,6 +23,8 @@ namespace Thelia\Coupon; +use Symfony\Component\DependencyInjection\Container; +use Symfony\Component\Translation\TranslatorInterface; use Thelia\Coupon\Type\CouponInterface; use Thelia\Model\Coupon; @@ -70,10 +72,11 @@ interface CouponAdapterInterface /** * Return Products total price + * CartTotalPrice = Checkout total - discount - postage * * @return float */ - public function getCheckoutTotalPriceWithoutDiscountAndPostagePrice(); + public function getCartTotalPrice(); /** * Return Checkout total postage (only) price @@ -87,7 +90,7 @@ interface CouponAdapterInterface * * @return int */ - public function getNbArticlesInTheCart(); + public function getNbArticlesInCart(); /** * Return all Coupon given during the Checkout @@ -114,4 +117,18 @@ interface CouponAdapterInterface */ public function saveCoupon(CouponInterface $coupon); + /** + * Return platform Container + * + * @return Container + */ + public function getContainer(); + + /** + * Return platform TranslatorInterface + * + * @return TranslatorInterface + */ + public function getTranslator(); + } \ No newline at end of file diff --git a/core/lib/Thelia/Coupon/CouponBaseAdapter.php b/core/lib/Thelia/Coupon/CouponBaseAdapter.php index 180ddc3cd..3809612a7 100644 --- a/core/lib/Thelia/Coupon/CouponBaseAdapter.php +++ b/core/lib/Thelia/Coupon/CouponBaseAdapter.php @@ -23,6 +23,8 @@ namespace Thelia\Coupon; +use Symfony\Component\DependencyInjection\Container; +use Symfony\Component\Translation\TranslatorInterface; use Thelia\Coupon\Type\CouponInterface; use Thelia\Model\Coupon; use Thelia\Model\CouponQuery; @@ -94,9 +96,9 @@ class CouponBaseAdapter implements CouponAdapterInterface * * @return float */ - public function getCheckoutTotalPriceWithoutDiscountAndPostagePrice() + public function getCartTotalPrice() { - // TODO: Implement getCheckoutTotalPriceWithoutDiscountAndPostagePrice() method. + // TODO: Implement getCartTotalPrice() method. } /** @@ -104,9 +106,9 @@ class CouponBaseAdapter implements CouponAdapterInterface * * @return int */ - public function getNbArticlesInTheCart() + public function getNbArticlesInCart() { - // TODO: Implement getNbArticlesInTheCart() method. + // TODO: Implement getNbArticlesInCart() method. } /** @@ -158,7 +160,7 @@ class CouponBaseAdapter implements CouponAdapterInterface // $couponModel->setTitle($coupon->getTitle()); // $couponModel->setShortDescription($coupon->getShortDescription()); // $couponModel->setDescription($coupon->getDescription()); -// $couponModel->setAmount($coupon->getEffect()); +// $couponModel->setAmount($coupon->getDiscount()); // $couponModel->setIsUsed(0); // $couponModel->setIsEnabled(1); // $couponModel->set @@ -170,5 +172,23 @@ class CouponBaseAdapter implements CouponAdapterInterface // $couponModel->set } + /** + * Return plateform Container + * + * @return Container + */ + public function getContainer() + { + // TODO: Implement getCheckoutPostagePrice() method. + } -} \ No newline at end of file + /** + * Return platform TranslatorInterface + * + * @return TranslatorInterface + */ + public function getTranslator() + { + return $this->getContainer()->get('thelia.translator'); + } +} diff --git a/core/lib/Thelia/Coupon/CouponFactory.php b/core/lib/Thelia/Coupon/CouponFactory.php index d82bb3430..a77871ffd 100644 --- a/core/lib/Thelia/Coupon/CouponFactory.php +++ b/core/lib/Thelia/Coupon/CouponFactory.php @@ -25,8 +25,8 @@ namespace Thelia\Coupon; use Symfony\Component\Translation\Exception\NotFoundResourceException; use Thelia\Coupon\Type\CouponInterface; -use Thelia\Coupon\Type\RemoveXAmount; use Thelia\Exception\CouponExpiredException; +use Thelia\Exception\InvalidRuleException; use Thelia\Model\Coupon; use Symfony\Component\Serializer\Encoder\JsonEncoder; @@ -79,7 +79,15 @@ class CouponFactory throw new CouponExpiredException($couponCode); } - return $this->buildCouponInterfacFromModel($couponModel); + /** @var CouponInterface $couponInterface */ + $couponInterface = $this->buildCouponInterfacFromModel($couponModel); + if ($couponInterface->getRules()->isEmpty()) { + throw new InvalidRuleException( + get_class($couponInterface) + ); + } + + return $couponInterface; } /** diff --git a/core/lib/Thelia/Coupon/CouponManager.php b/core/lib/Thelia/Coupon/CouponManager.php index c36ad7c40..b913de66d 100644 --- a/core/lib/Thelia/Coupon/CouponManager.php +++ b/core/lib/Thelia/Coupon/CouponManager.php @@ -70,16 +70,20 @@ class CouponManager if (count($this->coupons) > 0) { $couponsKept = $this->sortCoupons($this->coupons); + $isRemovingPostage = $this->isCouponRemovingPostage($couponsKept); + $discount = $this->getEffect($couponsKept); + if ($isRemovingPostage) { $postage = $this->adapter->getCheckoutPostagePrice(); - $discount -= $postage; + $discount += $postage; } // Just In Case test - if ($discount >= $this->adapter->getCheckoutTotalPrice()) { - $discount = 0.00; + $checkoutTotalPrice = $this->adapter->getCartTotalPrice(); + if ($discount >= $checkoutTotalPrice) { + $discount = $checkoutTotalPrice; } } @@ -144,6 +148,34 @@ class CouponManager } } + $coupons = $couponsKept; + $couponsKept = array(); + + /** @var CouponInterface $coupon */ + foreach ($coupons as $coupon) { + if ($coupon->isMatching($this->adapter)) { + $couponsKept[] = $coupon; + } + } + return $couponsKept; } + + /** + * Process given Coupon in order to get their cumulative effects + * + * @param array $coupons CouponInterface to process + * + * @return float discount + */ + protected function getEffect(array $coupons) + { + $discount = 0.00; + /** @var CouponInterface $coupon */ + foreach ($coupons as $coupon) { + $discount += $coupon->getDiscount($this->adapter); + } + + return $discount; + } } \ No newline at end of file diff --git a/core/lib/Thelia/Coupon/CouponRuleCollection.php b/core/lib/Thelia/Coupon/CouponRuleCollection.php index 3146cec56..06f0b15af 100644 --- a/core/lib/Thelia/Coupon/CouponRuleCollection.php +++ b/core/lib/Thelia/Coupon/CouponRuleCollection.php @@ -32,7 +32,7 @@ use Thelia\Exception\InvalidRuleException; * Date: 8/19/13 * Time: 3:24 PM * - * Manage a set of v + * Manage a set of CouponRuleInterface * * @package Coupon * @author Guillaume MOREL @@ -84,5 +84,15 @@ class CouponRuleCollection return $this; } + /** + * Check if there is at least one rule in the collection + * + * @return bool + */ + public function isEmpty() + { + return isEmpty($this->rules); + } + } \ No newline at end of file diff --git a/core/lib/Thelia/Coupon/Rule/AvailableForDate.php b/core/lib/Thelia/Coupon/Rule/AvailableForDate.php index 1b5b27ef5..81cb54a91 100644 --- a/core/lib/Thelia/Coupon/Rule/AvailableForDate.php +++ b/core/lib/Thelia/Coupon/Rule/AvailableForDate.php @@ -23,8 +23,6 @@ namespace Thelia\Coupon\Rule; -use Thelia\Coupon\CouponAdapterInterface; - /** * Created by JetBrains PhpStorm. * Date: 8/19/13 diff --git a/core/lib/Thelia/Coupon/Rule/AvailableForRepeatedPeriod.php b/core/lib/Thelia/Coupon/Rule/AvailableForRepeatedPeriod.php index 8cf30569b..4f67889c2 100644 --- a/core/lib/Thelia/Coupon/Rule/AvailableForRepeatedPeriod.php +++ b/core/lib/Thelia/Coupon/Rule/AvailableForRepeatedPeriod.php @@ -46,9 +46,9 @@ class AvailableForRepeatedPeriod extends AvailableForPeriod * @throws \Symfony\Component\Intl\Exception\NotImplementedException * @return $this */ - protected function setParametersToValidate(CouponAdapterInterface $adapter) + public function setParametersToValidate(CouponAdapterInterface $adapter) { - parent::setParametersToValidate($adapter); // TODO: Change the autogenerated stub + // @todo implement } /** diff --git a/core/lib/Thelia/Coupon/Rule/AvailableForTotalAmount.php b/core/lib/Thelia/Coupon/Rule/AvailableForTotalAmount.php index b01f946a1..aa1e40339 100644 --- a/core/lib/Thelia/Coupon/Rule/AvailableForTotalAmount.php +++ b/core/lib/Thelia/Coupon/Rule/AvailableForTotalAmount.php @@ -25,8 +25,8 @@ namespace Thelia\Coupon\Rule; use Symfony\Component\Intl\Exception\NotImplementedException; use Thelia\Coupon\CouponAdapterInterface; -use Thelia\Coupon\Parameter\PriceParam; -use Thelia\Coupon\Parameter\RuleValidator; +use Thelia\Coupon\Validator\PriceParam; +use Thelia\Coupon\Validator\RuleValidator; use Thelia\Exception\InvalidRuleException; use Thelia\Exception\InvalidRuleOperatorException; use Thelia\Exception\InvalidRuleValueException; @@ -63,13 +63,12 @@ class AvailableForTotalAmount extends CouponRuleAbstract * * @param array $validators Array of RuleValidator * validating $paramsToValidate against - * @param array $validated Parameters to be paramsToValidate * * @throws \Thelia\Exception\InvalidRuleException */ - public function __construct(array $validators, array $validated = null) + public function __construct(array $validators) { - parent::__construct($validators, $validated); + parent::__construct($validators); if (isset($validators[self::PARAM1_PRICE]) && $validators[self::PARAM1_PRICE] instanceof RuleValidator @@ -138,7 +137,7 @@ class AvailableForTotalAmount extends CouponRuleAbstract /** * Check if a price is valid * - * @param int $price Price to check + * @param float $price Price to check * * @throws InvalidRuleValueException if Value is not allowed * @return bool @@ -155,21 +154,6 @@ class AvailableForTotalAmount extends CouponRuleAbstract return true; } - /** - * Generate current Rule validator from adapter - * - * @param CouponAdapterInterface $adapter allowing to gather - * all necessary Thelia variables - * - * @throws \Symfony\Component\Intl\Exception\NotImplementedException - * @return $this - */ - protected function setValidatorsFromAdapter(CouponAdapterInterface $adapter) - { -// $adapter->getRule($this); - // @todo implement - } - /** * Generate current Rule param to be validated from adapter * @@ -181,7 +165,7 @@ class AvailableForTotalAmount extends CouponRuleAbstract protected function setParametersToValidate(CouponAdapterInterface $adapter) { $this->paramsToValidate = array( - self::PARAM1_PRICE => $adapter->getCheckoutTotalPrice() + self::PARAM1_PRICE => $adapter->getCartTotalPrice() ); return $this; diff --git a/core/lib/Thelia/Coupon/Rule/CouponRuleAbstract.php b/core/lib/Thelia/Coupon/Rule/CouponRuleAbstract.php index 4bdd0d57d..7d0b741bb 100644 --- a/core/lib/Thelia/Coupon/Rule/CouponRuleAbstract.php +++ b/core/lib/Thelia/Coupon/Rule/CouponRuleAbstract.php @@ -25,8 +25,8 @@ namespace Thelia\Coupon\Rule; use Symfony\Component\Intl\Exception\NotImplementedException; use Thelia\Coupon\CouponAdapterInterface; -use Thelia\Coupon\Parameter\ComparableInterface; -use Thelia\Coupon\Parameter\RuleValidator; +use Thelia\Coupon\Validator\ComparableInterface; +use Thelia\Coupon\Validator\RuleValidator; use Thelia\Exception\InvalidRuleException; use Thelia\Exception\InvalidRuleOperatorException; @@ -72,14 +72,12 @@ abstract class CouponRuleAbstract implements CouponRuleInterface * * @param array $validators Array of RuleValidator * validating $paramsToValidate against - * @param array $validated Parameters to be paramsToValidate * * @throws InvalidRuleException */ - public function __construct(array $validators, array $validated = null) + public function __construct(array $validators) { $this->setValidators($validators); - $this->paramsToValidate = $validated; } /** @@ -106,10 +104,14 @@ abstract class CouponRuleAbstract implements CouponRuleInterface /** * Check if the current Checkout matches this condition * + * @param CouponAdapterInterface $adapter allowing to gather + * all necessary Thelia variables + * * @return bool */ - public function isMatching() + public function isMatching(CouponAdapterInterface $adapter) { + $this->setParametersToValidate($adapter); $this->checkBackOfficeInput(); $this->checkCheckoutInput(); @@ -118,10 +120,10 @@ abstract class CouponRuleAbstract implements CouponRuleInterface foreach ($this->validators as $param => $validator) { $a = $this->paramsToValidate[$param]; $operator = $validator->getOperator(); - /** @var ComparableInterface, RuleParameterInterface $b */ + /** @var ComparableInterface, RuleParameterAbstract $b */ $b = $validator->getParam(); - if (!Operators::isValidAccordingToOperator($a, $operator, $b)) { + if (!Operators::isValid($a, $operator, $b)) { $isMatching = false; } } @@ -160,35 +162,18 @@ abstract class CouponRuleAbstract implements CouponRuleInterface return true; } - /** - * Generate current Rule validator from adapter - * - * @param CouponAdapterInterface $adapter allowing to gather - * all necessary Thelia variables - * - * @throws \Symfony\Component\Intl\Exception\NotImplementedException - * @return $this - */ - protected function setValidatorsFromAdapter(CouponAdapterInterface $adapter) - { - throw new NotImplementedException( - 'CouponRuleInterface::setValidators needs to be implemented' - ); - } - /** * Generate current Rule param to be validated from adapter * * @param CouponAdapterInterface $adapter allowing to gather * all necessary Thelia variables * - * @throws \Symfony\Component\Intl\Exception\NotImplementedException + * @throws \Thelia\Exception\NotImplementedException * @return $this */ protected function setParametersToValidate(CouponAdapterInterface $adapter) { - throw new NotImplementedException( - 'CouponRuleInterface::setValidators needs to be implemented' - ); + throw new \Thelia\Exception\NotImplementedException(); } + } \ No newline at end of file diff --git a/core/lib/Thelia/Coupon/Rule/CouponRuleInterface.php b/core/lib/Thelia/Coupon/Rule/CouponRuleInterface.php index 22ad31841..6aaca802c 100644 --- a/core/lib/Thelia/Coupon/Rule/CouponRuleInterface.php +++ b/core/lib/Thelia/Coupon/Rule/CouponRuleInterface.php @@ -23,6 +23,8 @@ namespace Thelia\Coupon\Rule; +use Thelia\Coupon\CouponAdapterInterface; + /** * Created by JetBrains PhpStorm. * Date: 8/19/13 @@ -53,9 +55,12 @@ interface CouponRuleInterface /** * Check if the current Checkout matches this condition * + * @param CouponAdapterInterface $adapter allowing to gather + * all necessary Thelia variables + * * @return bool */ - public function isMatching(); + public function isMatching(CouponAdapterInterface $adapter); /** * Return all available Operators for this Rule @@ -64,26 +69,4 @@ interface CouponRuleInterface */ public function getAvailableOperators(); -// /** -// * Generate current Rule validator from adapter -// * Ex : -// * $validator = array( -// * -// * @param CouponAdapterInterface $adapter allowing to gather -// * all necessary Thelia variables -// * -// * @return array Validators : array of ComparableInterface -// */ -// public function getValidators(CouponAdapterInterface $adapter); -// -// /** -// * Retrieve all param to validate from adapter -// * -// * @param CouponAdapterInterface $adapter allowing to gather -// * all necessary Thelia variables -// * -// * @return array Validators : array of ComparableInterface -// */ -// public function getParamToValidate(CouponAdapterInterface $adapter); - -} \ No newline at end of file +} diff --git a/core/lib/Thelia/Coupon/Rule/Operators.php b/core/lib/Thelia/Coupon/Rule/Operators.php index c11198caf..fb188c3cd 100644 --- a/core/lib/Thelia/Coupon/Rule/Operators.php +++ b/core/lib/Thelia/Coupon/Rule/Operators.php @@ -23,7 +23,7 @@ namespace Thelia\Coupon\Rule; -use Thelia\Coupon\Parameter\ComparableInterface; +use Thelia\Coupon\Validator\ComparableInterface; /** * Created by JetBrains PhpStorm. @@ -60,7 +60,7 @@ abstract class Operators * * @return bool */ - public static function isValidAccordingToOperator($a, $operator, ComparableInterface $b) + public static function isValid($a, $operator, ComparableInterface $b) { $ret = false; diff --git a/core/lib/Thelia/Coupon/Type/CouponAbstract.php b/core/lib/Thelia/Coupon/Type/CouponAbstract.php index 8e2f716ea..750c62e5c 100644 --- a/core/lib/Thelia/Coupon/Type/CouponAbstract.php +++ b/core/lib/Thelia/Coupon/Type/CouponAbstract.php @@ -43,9 +43,6 @@ use Thelia\Exception\InvalidRuleException; */ abstract class CouponAbstract implements CouponInterface { - /** @var CouponAdapterInterface Provide necessary value from Thelia*/ - protected $adapter; - /** @var RuleOrganizerInterface */ protected $organizer = null; @@ -85,20 +82,6 @@ abstract class CouponAbstract implements CouponInterface /** @var bool if Coupon is available for Products already on special offers */ protected $isAvailableOnSpecialOffers = false; - /** - * Set Adapter containing all relevant data - * - * @param CouponAdapterInterface $adapter Adapter - * - * @return $this - */ - public function setAdapter($adapter) - { - $this->adapter = $adapter; - - return $this; - } - /** * Set Rule Organizer * @@ -178,12 +161,15 @@ abstract class CouponAbstract implements CouponInterface /** * Return effects generated by the coupon * A negative value - * @ + * + * @param CouponAdapterInterface $adapter allowing to gather + * all necessary Thelia variables + * * @return float Amount removed from the Total Checkout */ - public function getEffect() + public function getDiscount(CouponAdapterInterface $adapter) { - return -$this->amount; + return $this->amount; } /** @@ -196,21 +182,6 @@ abstract class CouponAbstract implements CouponInterface return clone $this->rules; } - /** - * Add a Rule to the Coupon - * - * @param CouponRuleInterface $rule Condition needed to match - * in order to get the Coupon effect - * - * @return $this - */ - public function addRule(CouponRuleInterface $rule) - { - $this->rules->add($rule); - - return $this; - } - /** * Replace the existing Rules by those given in parameter * If one Rule is badly implemented, no Rule will be added @@ -230,17 +201,19 @@ abstract class CouponAbstract implements CouponInterface /** * Check if the current Coupon is matching its conditions (Rules) * Thelia variables are given by the CouponAdapterInterface - * In $this->adapter + * + * @param CouponAdapterInterface $adapter allowing to gather + * all necessary Thelia variables * * @return bool */ - public function isMatching() + public function isMatching(CouponAdapterInterface $adapter) { $isMatching = true; /** @var CouponRuleInterface $rule */ foreach ($this->rules->getRules() as $rule) { - if (!$rule->isMatching()) { + if (!$rule->isMatching($adapter)) { $isMatching = false; } } @@ -300,13 +273,11 @@ abstract class CouponAbstract implements CouponInterface { $ret = true; - if ($this->expirationDate < new \DateTime()) { + $now = new \DateTime(); + if ($this->expirationDate > $now) { $ret = false; } return $ret; } - - - -} \ No newline at end of file +} diff --git a/core/lib/Thelia/Coupon/Type/CouponInterface.php b/core/lib/Thelia/Coupon/Type/CouponInterface.php index 1d470bb69..df1a82f40 100644 --- a/core/lib/Thelia/Coupon/Type/CouponInterface.php +++ b/core/lib/Thelia/Coupon/Type/CouponInterface.php @@ -23,6 +23,7 @@ namespace Thelia\Coupon\Type; +use Thelia\Coupon\CouponAdapterInterface; use Thelia\Coupon\CouponRuleCollection; /** @@ -71,7 +72,7 @@ interface CouponInterface * If is cumulative you can sum Coupon effects * If not cancel all other Coupon and take the last given * - * @return string + * @return bool */ public function isCumulative(); @@ -84,33 +85,37 @@ interface CouponInterface /** * Return effects generated by the coupon - * A negative value + * A positive value * * Effects could also affect something else than the final Checkout price * CouponAdapter could be use to directly pass a Session value * some would wish to modify * Hence affecting a wide variety of Thelia elements - * Ex : $this->adapter->getTheliaInternalValue + * + * @param CouponAdapterInterface $adapter allowing to gather + * all necessary Thelia variables * * @return float Amount removed from the Total Checkout */ - public function getEffect(); + public function getDiscount(CouponAdapterInterface $adapter); /** * Return condition to validate the Coupon or not * - * @return array An array of CouponRuleInterface + * @return CouponRuleCollection A set of CouponRuleInterface */ public function getRules(); /** * Check if the current Coupon is matching its conditions (Rules) * Thelia variables are given by the CouponAdapterInterface - * In $this->adapter + * + * @param CouponAdapterInterface $adapter allowing to gather + * all necessary Thelia variables * * @return bool */ - public function isMatching(); + public function isMatching(CouponAdapterInterface $adapter); /** * Replace the existing Rules by those given in parameter diff --git a/core/lib/Thelia/Coupon/Type/RemoveXPercent.php b/core/lib/Thelia/Coupon/Type/RemoveXPercent.php index 5fd25ae8f..da63a4281 100644 --- a/core/lib/Thelia/Coupon/Type/RemoveXPercent.php +++ b/core/lib/Thelia/Coupon/Type/RemoveXPercent.php @@ -23,6 +23,7 @@ namespace Thelia\Coupon\Type; +use Thelia\Coupon\CouponAdapterInterface; use Thelia\Coupon\Type\CouponAbstract; use Thelia\Exception\MissingAdapterException; @@ -67,26 +68,24 @@ class RemoveXPercent extends CouponAbstract * Return effects generated by the coupon * A negative value * + * @param CouponAdapterInterface $adapter allowing to gather + * all necessary Thelia variables + * * @throws \Thelia\Exception\MissingAdapterException * @throws \InvalidArgumentException * @return float */ - public function getEffect() + public function getDiscount(CouponAdapterInterface $adapter) { - if ($this->adapter === null) { - throw new MissingAdapterException( - 'Cant calculate effect : CouponAdapterInterface is missing.' - ); - } - if ($this->percent >= 100) { throw new \InvalidArgumentException( 'Percentage must be inferior to 100' ); } - $basePrice = $this->adapter - ->getCheckoutTotalPriceWithoutDiscountAndPostagePrice(); + $basePrice =$adapter + ->getCartTotalPrice(); + return $basePrice * (( 100 - $this->percent ) / 100); } diff --git a/core/lib/Thelia/Coupon/Parameter/ComparableInterface.php b/core/lib/Thelia/Coupon/Validator/ComparableInterface.php similarity index 98% rename from core/lib/Thelia/Coupon/Parameter/ComparableInterface.php rename to core/lib/Thelia/Coupon/Validator/ComparableInterface.php index 26bb3ca30..3b70c5e37 100644 --- a/core/lib/Thelia/Coupon/Parameter/ComparableInterface.php +++ b/core/lib/Thelia/Coupon/Validator/ComparableInterface.php @@ -21,7 +21,7 @@ /* */ /**********************************************************************************/ -namespace Thelia\Coupon\Parameter; +namespace Thelia\Coupon\Validator; /** * Comparable interface diff --git a/core/lib/Thelia/Coupon/Parameter/DateParam.php b/core/lib/Thelia/Coupon/Validator/DateParam.php similarity index 95% rename from core/lib/Thelia/Coupon/Parameter/DateParam.php rename to core/lib/Thelia/Coupon/Validator/DateParam.php index 989477210..062c750e5 100644 --- a/core/lib/Thelia/Coupon/Parameter/DateParam.php +++ b/core/lib/Thelia/Coupon/Validator/DateParam.php @@ -21,9 +21,7 @@ /* */ /**********************************************************************************/ -namespace Thelia\Coupon\Parameter; - -use Thelia\Coupon\Parameter\ComparableInterface; +namespace Thelia\Coupon\Validator; /** * Created by JetBrains PhpStorm. @@ -36,7 +34,7 @@ use Thelia\Coupon\Parameter\ComparableInterface; * @author Guillaume MOREL * */ -class DateParam implements ComparableInterface, RuleParameterInterface +class DateParam extends RuleParameterAbstract { /** @var \DateTime Date */ protected $dateTime = null; diff --git a/core/lib/Thelia/Coupon/Parameter/IntegerParam.php b/core/lib/Thelia/Coupon/Validator/IntegerParam.php similarity index 95% rename from core/lib/Thelia/Coupon/Parameter/IntegerParam.php rename to core/lib/Thelia/Coupon/Validator/IntegerParam.php index 14d63417b..7c8303809 100644 --- a/core/lib/Thelia/Coupon/Parameter/IntegerParam.php +++ b/core/lib/Thelia/Coupon/Validator/IntegerParam.php @@ -21,9 +21,7 @@ /* */ /**********************************************************************************/ -namespace Thelia\Coupon\Parameter; - -use Thelia\Coupon\Parameter\ComparableInterface; +namespace Thelia\Coupon\Validator; /** * Created by JetBrains PhpStorm. @@ -36,7 +34,7 @@ use Thelia\Coupon\Parameter\ComparableInterface; * @author Guillaume MOREL * */ -class IntegerParam implements ComparableInterface, RuleParameterInterface +class IntegerParam extends RuleParameterAbstract { /** @var int Integer to compare with */ protected $integer = 0; diff --git a/core/lib/Thelia/Coupon/Parameter/IntervalParam.php b/core/lib/Thelia/Coupon/Validator/IntervalParam.php similarity index 97% rename from core/lib/Thelia/Coupon/Parameter/IntervalParam.php rename to core/lib/Thelia/Coupon/Validator/IntervalParam.php index 3e29d24be..1b310c9d1 100644 --- a/core/lib/Thelia/Coupon/Parameter/IntervalParam.php +++ b/core/lib/Thelia/Coupon/Validator/IntervalParam.php @@ -21,7 +21,7 @@ /* */ /**********************************************************************************/ -namespace Thelia\Coupon\Parameter; +namespace Thelia\Coupon\Validator; /** * Created by JetBrains PhpStorm. @@ -34,7 +34,7 @@ namespace Thelia\Coupon\Parameter; * @author Guillaume MOREL * */ -class IntervalParam implements ComparableInterface, RuleParameterInterface +class IntervalParam extends RuleParameterAbstract { /** @var \DatePeriod Date period */ protected $datePeriod = null; diff --git a/core/lib/Thelia/Coupon/Parameter/PriceParam.php b/core/lib/Thelia/Coupon/Validator/PriceParam.php similarity index 96% rename from core/lib/Thelia/Coupon/Parameter/PriceParam.php rename to core/lib/Thelia/Coupon/Validator/PriceParam.php index 2f3834777..44eae3234 100644 --- a/core/lib/Thelia/Coupon/Parameter/PriceParam.php +++ b/core/lib/Thelia/Coupon/Validator/PriceParam.php @@ -21,9 +21,7 @@ /* */ /**********************************************************************************/ -namespace Thelia\Coupon\Parameter; - -use Thelia\Coupon\Parameter\ComparableInterface; +namespace Thelia\Coupon\Validator; /** * Created by JetBrains PhpStorm. @@ -37,7 +35,7 @@ use Thelia\Coupon\Parameter\ComparableInterface; * @author Guillaume MOREL * */ -class PriceParam implements ComparableInterface, RuleParameterInterface +class PriceParam extends RuleParameterAbstract { /** @var float Positive Float to compare with */ protected $price = null; diff --git a/core/lib/Thelia/Coupon/Parameter/QuantityParam.php b/core/lib/Thelia/Coupon/Validator/QuantityParam.php similarity index 98% rename from core/lib/Thelia/Coupon/Parameter/QuantityParam.php rename to core/lib/Thelia/Coupon/Validator/QuantityParam.php index 526aa4152..dc842605f 100644 --- a/core/lib/Thelia/Coupon/Parameter/QuantityParam.php +++ b/core/lib/Thelia/Coupon/Validator/QuantityParam.php @@ -21,7 +21,7 @@ /* */ /**********************************************************************************/ -namespace Thelia\Coupon\Parameter; +namespace Thelia\Coupon\Validator; /** * Created by JetBrains PhpStorm. diff --git a/core/lib/Thelia/Coupon/Parameter/RepeatedDateParam.php b/core/lib/Thelia/Coupon/Validator/RepeatedDateParam.php similarity index 99% rename from core/lib/Thelia/Coupon/Parameter/RepeatedDateParam.php rename to core/lib/Thelia/Coupon/Validator/RepeatedDateParam.php index 2e99391e6..f0524edc7 100644 --- a/core/lib/Thelia/Coupon/Parameter/RepeatedDateParam.php +++ b/core/lib/Thelia/Coupon/Validator/RepeatedDateParam.php @@ -21,7 +21,7 @@ /* */ /**********************************************************************************/ -namespace Thelia\Coupon\Parameter; +namespace Thelia\Coupon\Validator; /** * Created by JetBrains PhpStorm. diff --git a/core/lib/Thelia/Coupon/Parameter/RepeatedIntervalParam.php b/core/lib/Thelia/Coupon/Validator/RepeatedIntervalParam.php similarity index 99% rename from core/lib/Thelia/Coupon/Parameter/RepeatedIntervalParam.php rename to core/lib/Thelia/Coupon/Validator/RepeatedIntervalParam.php index 3c4de7348..9f61f4db1 100644 --- a/core/lib/Thelia/Coupon/Parameter/RepeatedIntervalParam.php +++ b/core/lib/Thelia/Coupon/Validator/RepeatedIntervalParam.php @@ -21,7 +21,7 @@ /* */ /**********************************************************************************/ -namespace Thelia\Coupon\Parameter; +namespace Thelia\Coupon\Validator; /** * Created by JetBrains PhpStorm. diff --git a/core/lib/Thelia/Coupon/Parameter/RepeatedParam.php b/core/lib/Thelia/Coupon/Validator/RepeatedParam.php similarity index 98% rename from core/lib/Thelia/Coupon/Parameter/RepeatedParam.php rename to core/lib/Thelia/Coupon/Validator/RepeatedParam.php index dba3de0af..675228456 100644 --- a/core/lib/Thelia/Coupon/Parameter/RepeatedParam.php +++ b/core/lib/Thelia/Coupon/Validator/RepeatedParam.php @@ -21,7 +21,7 @@ /* */ /**********************************************************************************/ -namespace Thelia\Coupon\Parameter; +namespace Thelia\Coupon\Validator; use DateInterval; use DatePeriod; @@ -38,7 +38,7 @@ use DateTime; * @author Guillaume MOREL * */ -abstract class RepeatedParam implements ComparableInterface, RuleParameterInterface +abstract class RepeatedParam extends RuleParameterAbstract { /** @var DateTime The start date of the period. */ protected $from = null; diff --git a/core/lib/Thelia/Coupon/Parameter/RuleParameterInterface.php b/core/lib/Thelia/Coupon/Validator/RuleParameterAbstract.php similarity index 89% rename from core/lib/Thelia/Coupon/Parameter/RuleParameterInterface.php rename to core/lib/Thelia/Coupon/Validator/RuleParameterAbstract.php index 4583bd799..3e96682cd 100644 --- a/core/lib/Thelia/Coupon/Parameter/RuleParameterInterface.php +++ b/core/lib/Thelia/Coupon/Validator/RuleParameterAbstract.php @@ -21,7 +21,9 @@ /* */ /**********************************************************************************/ -namespace Thelia\Coupon\Parameter; +namespace Thelia\Coupon\Validator; + +use Thelia\Exception\NotImplementedException; /** * Created by JetBrains PhpStorm. @@ -34,12 +36,15 @@ namespace Thelia\Coupon\Parameter; * @author Guillaume MOREL * */ -interface RuleParameterInterface +abstract class RuleParameterAbstract implements ComparableInterface { /** * Get Parameter value to test against * * @return mixed */ - public function getValue(); + public function getValue() + { + return new NotImplementedException(); + } } \ No newline at end of file diff --git a/core/lib/Thelia/Coupon/Parameter/RuleValidator.php b/core/lib/Thelia/Coupon/Validator/RuleValidator.php similarity index 98% rename from core/lib/Thelia/Coupon/Parameter/RuleValidator.php rename to core/lib/Thelia/Coupon/Validator/RuleValidator.php index f6ffc3b13..064677751 100644 --- a/core/lib/Thelia/Coupon/Parameter/RuleValidator.php +++ b/core/lib/Thelia/Coupon/Validator/RuleValidator.php @@ -21,7 +21,7 @@ /* */ /**********************************************************************************/ -namespace Thelia\Coupon\Parameter; +namespace Thelia\Coupon\Validator; /** * Created by JetBrains PhpStorm. diff --git a/core/lib/Thelia/Exception/NotImplementedException.php b/core/lib/Thelia/Exception/NotImplementedException.php new file mode 100644 index 000000000..991f4d325 --- /dev/null +++ b/core/lib/Thelia/Exception/NotImplementedException.php @@ -0,0 +1,43 @@ +. */ +/* */ +/**********************************************************************************/ + +namespace Thelia\Exception; + +use Symfony\Component\DependencyInjection\Exception\BadMethodCallException; +use Thelia\Log\Tlog; + +/** + * Created by JetBrains PhpStorm. + * Date: 8/19/13 + * Time: 3:24 PM + * + * Thrown when an Abstract method has not been implemented + * + * @package Exception + * @author Guillaume MOREL + * + */ +class NotImplementedException extends BadMethodCallException +{ + +} diff --git a/core/lib/Thelia/Tests/Coupon/CouponFactoryTest.php b/core/lib/Thelia/Tests/Coupon/CouponFactoryTest.php index ed3331f87..c33b2327a 100644 --- a/core/lib/Thelia/Tests/Coupon/CouponFactoryTest.php +++ b/core/lib/Thelia/Tests/Coupon/CouponFactoryTest.php @@ -23,14 +23,16 @@ namespace Thelia\Coupon; -use Thelia\Coupon\Parameter\PriceParam; -use Thelia\Coupon\Parameter\RuleValidator; +use Thelia\Coupon\Validator\PriceParam; +use Thelia\Coupon\Validator\RuleValidator; use Thelia\Coupon\Rule\AvailableForTotalAmount; -use Thelia\Coupon\Rule\CouponRuleInterface; use Thelia\Coupon\Rule\Operators; +use Thelia\Coupon\Type\CouponInterface; use Thelia\Exception\CouponExpiredException; use Thelia\Model\Coupon; +require_once 'CouponManagerTest.php'; + /** * Created by JetBrains PhpStorm. * Date: 8/19/13 @@ -45,89 +47,6 @@ use Thelia\Model\Coupon; class CouponFactoryTest extends \PHPUnit_Framework_TestCase { - CONST VALID_SHORT_DESCRIPTION = 'Coupon for Christmas removing 10€ if your total checkout is more than 40€'; - CONST VALID_DESCRIPTION = '

Lorem ipsum dolor sit amet

Consectetur adipiscing elit. Cras at luctus tellus. Integer turpis mauris, aliquet vitae risus tristique, pellentesque vestibulum urna. Vestibulum sodales laoreet lectus dictum suscipit. Praesent vulputate, sem id varius condimentum, quam magna tempor elit, quis venenatis ligula nulla eget libero. Cras egestas euismod tellus, id pharetra leo suscipit quis. Donec lacinia ac lacus et ultricies. Nunc in porttitor neque. Proin at quam congue, consectetur orci sed, congue nulla. Nulla eleifend nunc ligula, nec pharetra elit tempus quis. Vivamus vel mauris sed est dictum blandit. Maecenas blandit dapibus velit ut sollicitudin. In in euismod mauris, consequat viverra magna. Cras velit velit, sollicitudin commodo tortor gravida, tempus varius nulla. - -Donec rhoncus leo mauris, id porttitor ante luctus tempus. - -Curabitur quis augue feugiat, ullamcorper mauris ac, interdum mi. Quisque aliquam lorem vitae felis lobortis, id interdum turpis mattis. Vestibulum diam massa, ornare congue blandit quis, facilisis at nisl. In tortor metus, venenatis non arcu nec, sollicitudin ornare nisl. Nunc erat risus, varius nec urna at, iaculis lacinia elit. Aenean ut felis tempus, tincidunt odio non, sagittis nisl. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Donec vitae hendrerit elit. Nunc sit amet gravida risus, euismod lobortis massa. Nam a erat mauris. Nam a malesuada lorem. Nulla id accumsan dolor, sed rhoncus tellus. Quisque dictum felis sed leo auctor, at volutpat lectus viverra. Morbi rutrum, est ac aliquam imperdiet, nibh sem sagittis justo, ac mattis magna lacus eu nulla. - -Duis interdum lectus nulla, nec pellentesque sapien condimentum at. Suspendisse potenti. Sed eu purus tellus. Nunc quis rhoncus metus. Fusce vitae tellus enim. Interdum et malesuada fames ac ante ipsum primis in faucibus. Etiam tempor porttitor erat vitae iaculis. Sed est elit, consequat non ornare vitae, vehicula eget lectus. Etiam consequat sapien mauris, eget consectetur magna imperdiet eget. Nunc sollicitudin luctus velit, in commodo nulla adipiscing fermentum. Fusce nisi sapien, posuere vitae metus sit amet, facilisis sollicitudin dui. Fusce ultricies auctor enim sit amet iaculis. Morbi at vestibulum enim, eget adipiscing eros. - -Praesent ligula lorem, faucibus ut metus quis, fermentum iaculis erat. Pellentesque elit erat, lacinia sed semper ac, sagittis vel elit. Nam eu convallis est. Curabitur rhoncus odio vitae consectetur pellentesque. Nam vitae arcu nec ante scelerisque dignissim vel nec neque. Suspendisse augue nulla, mollis eget dui et, tempor facilisis erat. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi ac diam ipsum. Donec convallis dui ultricies velit auctor, non lobortis nulla ultrices. Morbi vitae dignissim ante, sit amet lobortis tortor. Nunc dapibus condimentum augue, in molestie neque congue non. - -Sed facilisis pellentesque nisl, eu tincidunt erat scelerisque a. Nullam malesuada tortor vel erat volutpat tincidunt. In vehicula diam est, a convallis eros scelerisque ut. Donec aliquet venenatis iaculis. Ut a arcu gravida, placerat dui eu, iaculis nisl. Quisque adipiscing orci sit amet dui dignissim lacinia. Sed vulputate lorem non dolor adipiscing ornare. Morbi ornare id nisl id aliquam. Ut fringilla elit ante, nec lacinia enim fermentum sit amet. Aenean rutrum lorem eu convallis pharetra. Cras malesuada varius metus, vitae gravida velit. Nam a varius ipsum, ac commodo dolor. Phasellus nec elementum elit. Etiam vel adipiscing leo.'; - - /** - * Generate valid CouponInterface - * - * @param $code - * @param $type - * @param $title - * @param $shortDescription - * @param $description - * @param $amount - * @param $isUsed - * @param $isEnabled - * @param $expirationDate - * @param $rules - * @param $isCumulative - * @param $isRemovingPostage - * - * @return CouponInterface - */ - public function generateValidCoupon( - $code = 'XMAS1', - $type = '\Thelia\Coupon\Type\RemoveXAmount', - $title = 'Christmas coupon', - $shortDescription = self::VALID_SHORT_DESCRIPTION, - $description = self::VALID_DESCRIPTION, - $amount = 10.00, - $isUsed = 1, - $isEnabled = 1, - $expirationDate = null, - $rules = null, - $isCumulative = 1, - $isRemovingPostage = 0 - ) { - $coupon = new Coupon(); - $coupon->setCode($code); - $coupon->setType($type); - $coupon->setTitle($title); - $coupon->setShortDescription($shortDescription); - $coupon->setDescription($description); - $coupon->setAmount($amount); - $coupon->setIsUsed($isUsed); - $coupon->setIsEnabled($isEnabled); - - if ($expirationDate === null) { - $date = new \DateTime(); - $coupon->setExpirationDate( - $date->setTimestamp(strtotime("today + 2 months")) - ); - } - - if ($rules === null) { - $rules = $this->generateValidRules(); - } - - $couponFactory = new CouponFactory(new CouponBaseAdapter()); - $serializedData = $couponFactory->convertRulesInstancesIntoSerialized( - $rules - ); - - $coupon->setSerializedRulesType($serializedData['rulesType']); - $coupon->setSerializedRulesContent($serializedData['rulesContent']); - - $coupon->setIsCumulative($isCumulative); - $coupon->setIsRemovingPostage($isRemovingPostage); - - return $coupon; - } - - /** * Sets up the fixture, for example, opens a network connection. * This method is called before a test is executed. @@ -136,38 +55,37 @@ Sed facilisis pellentesque nisl, eu tincidunt erat scelerisque a. Nullam malesua { } - - /** * Fake CouponQuery->findByCode * - * @param string $code - * @param string $type - * @param string $title - * @param string $shortDescription - * @param string $description - * @param float $amount - * @param int $isUsed - * @param int $isEnabled - * @param null $expirationDate - * @param null $rules - * @param int $isCumulative - * @param int $isRemovingPostage + * @param string $code Coupon code + * @param string $type Coupon type (object) + * @param string $title Coupon title + * @param string $shortDescription Coupon short description + * @param string $description Coupon description + * @param float $amount Coupon amount + * @param bool $isUsed If Coupon has been used yet + * @param bool $isEnabled If Coupon is enabled + * @param \DateTime $expirationDate When Coupon expires + * @param CouponRuleCollection $rules Coupon rules + * @param bool $isCumulative If Coupon is cumulative + * @param bool $isRemovingPostage If Coupon is removing postage + * * @return Coupon */ public function generateCouponModelMock( - $code, - $type, - $title, - $shortDescription, - $description, - $amount, - $isUsed, - $isEnabled, - $expirationDate, - $rules, - $isCumulative, - $isRemovingPostage + $code = null, + $type = null, + $title = null, + $shortDescription = null, + $description = null, + $amount = null, + $isUsed = null, + $isEnabled = null, + $expirationDate = null, + $rules = null, + $isCumulative = null, + $isRemovingPostage = null ) { $coupon = $this->generateValidCoupon( $code, @@ -200,8 +118,10 @@ Sed facilisis pellentesque nisl, eu tincidunt erat scelerisque a. Nullam malesua /** + * Test if an expired Coupon is build or not (superior) + * * @covers Thelia\Coupon\CouponFactory::buildCouponFromCode - * @expectedException Thelia\Exception\CouponExpiredException + * @expectedException \Thelia\Exception\CouponExpiredException */ public function testBuildCouponFromCodeExpiredDateBefore() { @@ -215,8 +135,10 @@ Sed facilisis pellentesque nisl, eu tincidunt erat scelerisque a. Nullam malesua } /** + * Test if an expired Coupon is build or not (equal) + * * @covers Thelia\Coupon\CouponFactory::buildCouponFromCode - * @expectedException Thelia\Exception\CouponExpiredException + * @expectedException \Thelia\Exception\CouponExpiredException */ public function testBuildCouponFromCodeExpiredDateEquals() { @@ -229,6 +151,22 @@ Sed facilisis pellentesque nisl, eu tincidunt erat scelerisque a. Nullam malesua } /** + * Test if an expired Coupon is build or not (equal) + * + * @covers Thelia\Coupon\CouponFactory::buildCouponFromCode + * @expectedException \Thelia\Exception\InvalidRuleException + */ + public function testBuildCouponFromCodeWithoutRule() + { + /** @var CouponAdapterInterface $mockAdapter */ + $mockAdapter = $this->generateCouponModelMock(null, null, null, null, null, null, null, null, null, new CouponRuleCollection(array())); + $couponFactory = new CouponFactory($mockAdapter); + $coupon = $couponFactory->buildCouponFromCode('XMAS1'); + } + + /** + * Test if a CouponInterface can be built from database + * * @covers Thelia\Coupon\CouponFactory::buildCouponFromCode */ public function testBuildCouponFromCode() @@ -236,15 +174,32 @@ Sed facilisis pellentesque nisl, eu tincidunt erat scelerisque a. Nullam malesua /** @var CouponAdapterInterface $mockAdapter */ $mockAdapter = $this->generateCouponModelMock(); $couponFactory = new CouponFactory($mockAdapter); + /** @var CouponInterface $coupon */ $coupon = $couponFactory->buildCouponFromCode('XMAS1'); - $CouponManager = new CouponManager($mockAdapter) + $this->assertEquals('XMAS1', $coupon->getCode()); + $this->assertEquals('Thelia\Coupon\Type\RemoveXAmount', get_class($coupon)); + $this->assertEquals(CouponManagerTest::VALID_TITLE, $coupon->getTitle()); + $this->assertEquals(CouponManagerTest::VALID_SHORT_DESCRIPTION, $coupon->getShortDescription()); + $this->assertEquals(CouponManagerTest::VALID_DESCRIPTION, $coupon->getDescription()); + $this->assertEquals(10.00, $coupon->getDiscount()); + $this->assertEquals(1, $coupon->isEnabled()); + + $date = new \DateTime(); + $date->setTimestamp(strtotime("today + 2 months")); + $this->assertEquals($date, $coupon->getExpirationDate()); + + $rules = $this->generateValidRules(); + $this->assertEquals($rules, $coupon->getRules()); + + $this->assertEquals(1, $coupon->isCumulative()); + $this->assertEquals(0, $coupon->isRemovingPostage()); } /** * Generate valid CouponRuleInterfaces * - * @return array Array of CouponRuleInterface + * @return CouponRuleCollection Set of CouponRuleInterface */ protected function generateValidRules() { @@ -270,11 +225,111 @@ Sed facilisis pellentesque nisl, eu tincidunt erat scelerisque a. Nullam malesua ) ) ); - $rules = array($rule1, $rule2); + $rules = new CouponRuleCollection(array($rule1, $rule2)); return $rules; } + /** + * Generate valid CouponInterface + * + * @param string $code Coupon code + * @param string $type Coupon type (object) + * @param string $title Coupon title + * @param string $shortDescription Coupon short description + * @param string $description Coupon description + * @param float $amount Coupon amount + * @param bool $isUsed If Coupon has been used yet + * @param bool $isEnabled If Coupon is enabled + * @param \DateTime $expirationDate When Coupon expires + * @param CouponRuleCollection $rules Coupon rules + * @param bool $isCumulative If Coupon is cumulative + * @param bool $isRemovingPostage If Coupon is removing postage + * + * @return Coupon + */ + public function generateValidCoupon( + $code = null, + $type = null, + $title = null, + $shortDescription = null, + $description = null, + $amount = null, + $isUsed = null, + $isEnabled = null, + $expirationDate = null, + $rules = null, + $isCumulative = null, + $isRemovingPostage = null + ) { + $coupon = new Coupon(); + + if ($code === null) { + $code = 'XMAS1'; + } + $coupon->setCode($code); + + if ($type === null) { + $type = 'Thelia\Coupon\Type\RemoveXAmount'; + } + $coupon->setType($type); + + if ($title === null) { + $title = CouponManagerTest::VALID_TITLE; + } + $coupon->setTitle($title); + + if ($shortDescription === null) { + $shortDescription = CouponManagerTest::VALID_SHORT_DESCRIPTION; + } + $coupon->setShortDescription($shortDescription); + + if ($description === null) { + $description = CouponManagerTest::VALID_DESCRIPTION; + } + $coupon->setDescription($description); + + if ($amount === null) { + $amount = 10.00; + } + $coupon->setAmount($amount); + + if ($isUsed === null) { + $isUsed = 1; + } + $coupon->setIsUsed($isUsed); + + if ($isEnabled === null) { + $isEnabled = 1; + } + $coupon->setIsEnabled($isEnabled); + + if ($isCumulative === null) { + $isCumulative = 1; + } + if ($isRemovingPostage === null) { + $isRemovingPostage = 0; + } + + if ($expirationDate === null) { + $date = new \DateTime(); + $coupon->setExpirationDate( + $date->setTimestamp(strtotime("today + 2 months")) + ); + } + + if ($rules === null) { + $rules = $this->generateValidRules(); + } + + $coupon->setSerializedRules(base64_encode(serialize($rules))); + + $coupon->setIsCumulative($isCumulative); + $coupon->setIsRemovingPostage($isRemovingPostage); + + return $coupon; + } + /** * Tears down the fixture, for example, closes a network connection. * This method is called after a test is executed. diff --git a/core/lib/Thelia/Tests/Coupon/CouponManagerTest.php b/core/lib/Thelia/Tests/Coupon/CouponManagerTest.php index 9234642c9..b5b9cefbe 100644 --- a/core/lib/Thelia/Tests/Coupon/CouponManagerTest.php +++ b/core/lib/Thelia/Tests/Coupon/CouponManagerTest.php @@ -23,12 +23,12 @@ namespace Thelia\Coupon; -use Thelia\Coupon\Parameter\PriceParam; -use Thelia\Coupon\Parameter\RuleValidator; +use Thelia\Coupon\Validator\PriceParam; +use Thelia\Coupon\Validator\RuleValidator; use Thelia\Coupon\Rule\AvailableForTotalAmount; use Thelia\Coupon\Rule\Operators; +use Thelia\Coupon\Type\CouponInterface; use Thelia\Coupon\Type\RemoveXAmount; -use Thelia\Model\Coupon; use Thelia\Tools\PhpUnitUtils; /** @@ -44,7 +44,8 @@ use Thelia\Tools\PhpUnitUtils; */ class CouponManagerTest extends \PHPUnit_Framework_TestCase { - + CONST VALID_CODE = 'XMAS'; + CONST VALID_TITLE = 'XMAS coupon'; CONST VALID_SHORT_DESCRIPTION = 'Coupon for Christmas removing 10€ if your total checkout is more than 40€'; CONST VALID_DESCRIPTION = '

Lorem ipsum dolor sit amet

Consectetur adipiscing elit. Cras at luctus tellus. Integer turpis mauris, aliquet vitae risus tristique, pellentesque vestibulum urna. Vestibulum sodales laoreet lectus dictum suscipit. Praesent vulputate, sem id varius condimentum, quam magna tempor elit, quis venenatis ligula nulla eget libero. Cras egestas euismod tellus, id pharetra leo suscipit quis. Donec lacinia ac lacus et ultricies. Nunc in porttitor neque. Proin at quam congue, consectetur orci sed, congue nulla. Nulla eleifend nunc ligula, nec pharetra elit tempus quis. Vivamus vel mauris sed est dictum blandit. Maecenas blandit dapibus velit ut sollicitudin. In in euismod mauris, consequat viverra magna. Cras velit velit, sollicitudin commodo tortor gravida, tempus varius nulla. @@ -68,167 +69,171 @@ Sed facilisis pellentesque nisl, eu tincidunt erat scelerisque a. Nullam malesua { } - /** - * Generate valid CouponInterface + * Test getDiscount() behaviour + * Entering : 1 valid Coupon (If 40 < total amount 400) - 10euros * - * @param string $code - * @param string $title - * @param string $shortDescription - * @param string $description - * @param float $amount - * @param bool $isEnabled - * @param $expirationDate - * @param $rules - * @param bool $isCumulative - * @param bool $isRemovingPostage - * @param bool $isAvailableOnSpecialOffers - * @param int $maxUsage - * - * @return CouponInterface + * @covers Thelia\Coupon\CouponManager::getDiscount */ - public function generateValidCoupon( - $code = 'XMAS1', - $title = 'Christmas coupon', - $shortDescription = self::VALID_SHORT_DESCRIPTION, - $description = self::VALID_DESCRIPTION, - $amount = 10.00, - $isEnabled = true, - $expirationDate = null, - $rules = null, - $isCumulative = true, - $isRemovingPostage = false, - $isAvailableOnSpecialOffers = true, - $maxUsage = 40 - ) { - if ($expirationDate === null) { - $expirationDate = new \DateTime(); - $expirationDate->setTimestamp(strtotime("today + 2 months")); - } + public function testGetDiscountOneCoupon() + { + $cartTotalPrice = 100.00; + $checkoutTotalPrice = 120.00; - $coupon = new RemoveXAmount($code, $title, $shortDescription, $description, $amount, $isCumulative, $isRemovingPostage, $isAvailableOnSpecialOffers, $isEnabled, $maxUsage, $expirationDate); + /** @var CouponInterface $coupon */ + $coupon = self::generateValidCoupon(); - if ($rules === null) { - $rules = $this->generateValidRules(); - } + /** @var CouponAdapterInterface $stubCouponBaseAdapter */ + $stubCouponBaseAdapter = $this->generateFakeAdapter(array($coupon), $cartTotalPrice, $checkoutTotalPrice); - $coupon->setRules($rules); + $couponManager = new CouponManager($stubCouponBaseAdapter); + $discount = $couponManager->getDiscount(); - return $coupon; + $expected = 10.00; + $actual = $discount; + $this->assertEquals($expected, $actual); } -// /** -// * @covers Thelia\Coupon\CouponManager::getDiscount -// * @todo Implement testGetDiscount(). -// */ -// public function testGetDiscountOneCoupon() -// { -// $this->markTestIncomplete( -// 'This test has not been implemented yet.' -// ); -// /** @var CouponInterface $coupon */ -// $coupon = $this->generateValidCoupon(); -// -// -// /** @var CouponAdapterInterface $stubCouponBaseAdapter */ -// $stubCouponBaseAdapter = $this->getMock( -// 'Thelia\Coupon\CouponBaseAdapter', -// array( -// 'getCurrentCoupons', -// 'getCheckoutTotalPriceWithoutDiscountAndPostagePrice' -// ), -// array() -// ); -// -// // Returns -10euros not removing postage Coupon -// // If 40 < total amount 400 -// $stubCouponBaseAdapter->expects($this->any()) -// ->method('getCurrentCoupons') -// ->will($this->returnValue(array($coupon))); -// -// // Return Checkout product amoun = 100euros -// $stubCouponBaseAdapter->expects($this->any()) -// ->method('getCheckoutTotalPriceWithoutDiscountAndPostagePrice') -// ->will($this->returnValue(100.00)); -// -// $couponManager = new CouponManager($stubCouponBaseAdapter); -// $discount = $couponManager->getDiscount(); -// -// $expected = 10.00; -// $actual = $discount; -// $this->assertEquals($expected, $actual); -// } -// -// /** -// * @covers Thelia\Coupon\CouponManager::getDiscount -// * @todo Implement testGetDiscount(). -// */ -// public function testGetDiscountAlwaysInferiorToPrice() -// { -// // Remove the following lines when you implement this test. -// $this->markTestIncomplete( -// 'This test has not been implemented yet.' -// ); -// } -// -// /** -// * @covers Thelia\Coupon\CouponManager::getDiscount -// * @covers Thelia\Coupon\CouponManager::sortCoupons -// * @todo Implement testGetDiscount(). -// */ -// public function testGetDiscountCouponNotCumulativeCancelOthers() -// { -// // Remove the following lines when you implement this test. -// $this->markTestIncomplete( -// 'This test has not been implemented yet.' -// ); -// } -// -// /** -// * @covers Thelia\Coupon\CouponManager::getDiscount -// * @covers Thelia\Coupon\CouponManager::sortCoupons -// * @todo Implement testGetDiscount(). -// */ -// public function testGetDiscountCouponCumulativeCumulatesWithOthers() -// { -// // Remove the following lines when you implement this test. -// $this->markTestIncomplete( -// 'This test has not been implemented yet.' -// ); -// } -// -// /** -// * @covers Thelia\Coupon\CouponManager::isCouponRemovingPostage -// * @covers Thelia\Coupon\CouponManager::sortCoupons -// * @todo Implement testGetDiscount(). -// */ -// public function testIsCouponRemovingPostage() -// { -// // Remove the following lines when you implement this test. -// $this->markTestIncomplete( -// 'This test has not been implemented yet.' -// ); -// } + /** + * Test getDiscount() behaviour + * Entering : 1 valid Coupon (If 40 < total amount 400) - 10euros + * 1 valid Coupon (If total amount > 20) - 15euros + * + * @covers Thelia\Coupon\CouponManager::getDiscount + */ + public function testGetDiscountTwoCoupon() + { + $cartTotalPrice = 100.00; + $checkoutTotalPrice = 120.00; + + /** @var CouponInterface $coupon1 */ + $coupon1 = self::generateValidCoupon(); + $rule1 = new AvailableForTotalAmount( + array( + AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( + Operators::SUPERIOR, + new PriceParam( + 40.00, + 'EUR' + ) + ) + ) + ); + $rules = new CouponRuleCollection(array($rule1)); + /** @var CouponInterface $coupon2 */ + $coupon2 = $this->generateValidCoupon('XMAS2', null, null, null, 15.00, null, null, $rules); + + /** @var CouponAdapterInterface $stubCouponBaseAdapter */ + $stubCouponBaseAdapter = $this->generateFakeAdapter(array($coupon1, $coupon2), $cartTotalPrice, $checkoutTotalPrice); + + $couponManager = new CouponManager($stubCouponBaseAdapter); + $discount = $couponManager->getDiscount(); + + $expected = 25.00; + $actual = $discount; + $this->assertEquals($expected, $actual); + } /** + * Test getDiscount() behaviour + * For a Cart of 21euros + * Entering : 1 valid Coupon (If total amount > 20) - 30euros + * + * @covers Thelia\Coupon\CouponManager::getDiscount + */ + public function testGetDiscountAlwaysInferiorToPrice() + { + $cartTotalPrice = 21.00; + $checkoutTotalPrice = 26.00; + + $rule1 = new AvailableForTotalAmount( + array( + AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( + Operators::SUPERIOR, + new PriceParam( + 20.00, + 'EUR' + ) + ) + ) + ); + $rules = new CouponRuleCollection(array($rule1)); + /** @var CouponInterface $coupon */ + $coupon = $this->generateValidCoupon('XMAS2', null, null, null, 30.00, null, null, $rules); + + /** @var CouponAdapterInterface $stubCouponBaseAdapter */ + $stubCouponBaseAdapter = $this->generateFakeAdapter(array($coupon), $cartTotalPrice, $checkoutTotalPrice); + + $couponManager = new CouponManager($stubCouponBaseAdapter); + $discount = $couponManager->getDiscount(); + + $expected = 21.00; + $actual = $discount; + $this->assertEquals($expected, $actual); + } + + + /** + * Check if removing postage on discout is working + * @covers Thelia\Coupon\CouponManager::isCouponRemovingPostage + * @covers Thelia\Coupon\CouponManager::sortCoupons + */ + public function testIsCouponRemovingPostage() + { + $cartTotalPrice = 21.00; + $checkoutTotalPrice = 27.00; + + $rule1 = new AvailableForTotalAmount( + array( + AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( + Operators::SUPERIOR, + new PriceParam( + 20.00, + 'EUR' + ) + ) + ) + ); + $rules = new CouponRuleCollection(array($rule1)); + /** @var CouponInterface $coupon */ + $coupon = $this->generateValidCoupon('XMAS2', null, null, null, 30.00, null, null, $rules, null, true); + + /** @var CouponAdapterInterface $stubCouponBaseAdapter */ + $stubCouponBaseAdapter = $this->generateFakeAdapter(array($coupon), $cartTotalPrice, $checkoutTotalPrice); + + $couponManager = new CouponManager($stubCouponBaseAdapter); + $discount = $couponManager->getDiscount(); + + $expected = 21.00; + $actual = $discount; + $this->assertEquals($expected, $actual); + } + + /** + * Testing how multiple Coupon behaviour + * Entering 1 Coupon not cumulative + * * @covers Thelia\Coupon\CouponManager::sortCoupons */ public function testCouponCumulationOneCouponNotCumulative() { + $cartTotalPrice = 100.00; + $checkoutTotalPrice = 120.00; + // Given /** @var CouponInterface $coupon */ - $couponCumulative1 = $this->generateValidCoupon(null, null, null, null, null, null, null, null, true); - $couponCumulative2 = $this->generateValidCoupon(null, null, null, null, null, null, null, null, true); - $couponNotCumulative1 = $this->generateValidCoupon(null, null, null, null, null, null, null, null, false); - $couponNotCumulative2 = $this->generateValidCoupon(null, null, null, null, null, null, null, null, false); + $couponCumulative1 = $this->generateValidCoupon(null, null, null, null, null, null, null, null, false); $coupons = array($couponCumulative1); + /** @var CouponAdapterInterface $stubCouponBaseAdapter */ + $stubCouponBaseAdapter = $this->generateFakeAdapter($coupons, $cartTotalPrice, $checkoutTotalPrice); + // When $sortedCoupons = PhpUnitUtils::callMethod( - new CouponManager(new CouponBaseAdapter()), + new CouponManager($stubCouponBaseAdapter), 'sortCoupons', - $coupons + array($coupons) ); // Then @@ -238,13 +243,402 @@ Sed facilisis pellentesque nisl, eu tincidunt erat scelerisque a. Nullam malesua $this->assertSame($expected, $actual, 'Array Sorted despite there is only once'); } + /** + * Testing how multiple Coupon behaviour + * Entering 1 Coupon cumulative + * + * @covers Thelia\Coupon\CouponManager::sortCoupons + */ + public function testCouponCumulationOneCouponCumulative() + { + $cartTotalPrice = 100.00; + $checkoutTotalPrice = 120.00; + + // Given + /** @var CouponInterface $coupon */ + $couponCumulative1 = $this->generateValidCoupon(null, null, null, null, null, null, null, null, true); + + $coupons = array($couponCumulative1); + /** @var CouponAdapterInterface $stubCouponBaseAdapter */ + $stubCouponBaseAdapter = $this->generateFakeAdapter($coupons, $cartTotalPrice, $checkoutTotalPrice); + + // When + $sortedCoupons = PhpUnitUtils::callMethod( + new CouponManager($stubCouponBaseAdapter), + 'sortCoupons', + array($coupons) + ); + + // Then + $expected = $coupons; + $actual = $sortedCoupons; + + $this->assertSame($expected, $actual, 'Array Sorted despite there is only once'); + } + + /** + * Testing how multiple Coupon behaviour + * Entering 1 Coupon cumulative + * 1 Coupon cumulative + * + * @covers Thelia\Coupon\CouponManager::sortCoupons + */ + public function testCouponCumulationTwoCouponCumulative() + { + $cartTotalPrice = 100.00; + $checkoutTotalPrice = 120.00; + + // Given + /** @var CouponInterface $coupon */ + $couponCumulative1 = $this->generateValidCoupon('XMAS1', null, null, null, null, null, null, null, true); + $couponCumulative2 = $this->generateValidCoupon('XMAS2', null, null, null, null, null, null, null, true); + + $coupons = array($couponCumulative1, $couponCumulative2); + /** @var CouponAdapterInterface $stubCouponBaseAdapter */ + $stubCouponBaseAdapter = $this->generateFakeAdapter($coupons, $cartTotalPrice, $checkoutTotalPrice); + + // When + $sortedCoupons = PhpUnitUtils::callMethod( + new CouponManager($stubCouponBaseAdapter), + 'sortCoupons', + array($coupons) + ); + + // Then + $expected = $coupons; + $actual = $sortedCoupons; + + $this->assertSame($expected, $actual, 'Array Sorted despite both Coupon can be accumulated'); + } + + /** + * Testing how multiple Coupon behaviour + * Entering 1 Coupon cumulative + * 1 Coupon non cumulative + * + * @covers Thelia\Coupon\CouponManager::sortCoupons + */ + public function testCouponCumulationOneCouponCumulativeOneNonCumulative() + { + $cartTotalPrice = 100.00; + $checkoutTotalPrice = 120.00; + + // Given + /** @var CouponInterface $coupon */ + $couponCumulative1 = $this->generateValidCoupon('XMAS1', null, null, null, null, null, null, null, true); + $couponCumulative2 = $this->generateValidCoupon('XMAS2', null, null, null, null, null, null, null, false); + + $coupons = array($couponCumulative1, $couponCumulative2); + /** @var CouponAdapterInterface $stubCouponBaseAdapter */ + $stubCouponBaseAdapter = $this->generateFakeAdapter($coupons, $cartTotalPrice, $checkoutTotalPrice); + + // When + $sortedCoupons = PhpUnitUtils::callMethod( + new CouponManager($stubCouponBaseAdapter), + 'sortCoupons', + array($coupons) + ); + + // Then + $expected = array($couponCumulative2); + $actual = $sortedCoupons; + + $this->assertSame($expected, $actual, 'Array Sorted despite both Coupon can be accumulated'); + } + + /** + * Testing how multiple Coupon behaviour + * Entering 1 Coupon non cumulative + * 1 Coupon cumulative + * + * @covers Thelia\Coupon\CouponManager::sortCoupons + */ + public function testCouponCumulationOneCouponNonCumulativeOneCumulative() + { + $cartTotalPrice = 100.00; + $checkoutTotalPrice = 120.00; + + // Given + /** @var CouponInterface $coupon */ + $couponCumulative1 = $this->generateValidCoupon('XMAS1', null, null, null, null, null, null, null, false); + $couponCumulative2 = $this->generateValidCoupon('XMAS2', null, null, null, null, null, null, null, true); + + $coupons = array($couponCumulative1, $couponCumulative2); + /** @var CouponAdapterInterface $stubCouponBaseAdapter */ + $stubCouponBaseAdapter = $this->generateFakeAdapter($coupons, $cartTotalPrice, $checkoutTotalPrice); + + // When + $sortedCoupons = PhpUnitUtils::callMethod( + new CouponManager($stubCouponBaseAdapter), + 'sortCoupons', + array($coupons) + ); + + // Then + $expected = array($couponCumulative2); + $actual = $sortedCoupons; + + $this->assertSame($expected, $actual, 'Array Sorted despite both Coupon can be accumulated'); + } + + /** + * Testing how multiple Coupon behaviour + * Entering 1 Coupon non cumulative + * 1 Coupon non cumulative + * + * @covers Thelia\Coupon\CouponManager::sortCoupons + */ + public function testCouponCumulationTwoCouponNonCumulative() + { + $cartTotalPrice = 100.00; + $checkoutTotalPrice = 120.00; + + // Given + /** @var CouponInterface $coupon */ + $couponCumulative1 = $this->generateValidCoupon('XMAS1', null, null, null, null, null, null, null, false); + $couponCumulative2 = $this->generateValidCoupon('XMAS2', null, null, null, null, null, null, null, false); + + $coupons = array($couponCumulative1, $couponCumulative2); + /** @var CouponAdapterInterface $stubCouponBaseAdapter */ + $stubCouponBaseAdapter = $this->generateFakeAdapter($coupons, $cartTotalPrice, $checkoutTotalPrice); + + // When + $sortedCoupons = PhpUnitUtils::callMethod( + new CouponManager($stubCouponBaseAdapter), + 'sortCoupons', + array($coupons) + ); + + // Then + $expected = array($couponCumulative2); + $actual = $sortedCoupons; + + $this->assertSame($expected, $actual, 'Array Sorted despite both Coupon can be accumulated'); + } + + /** + * Testing how multiple Coupon behaviour + * Entering 1 Coupon cumulative expired + * + * @covers Thelia\Coupon\CouponManager::sortCoupons + */ + public function testCouponCumulationOneCouponCumulativeExpired() + { + $cartTotalPrice = 100.00; + $checkoutTotalPrice = 120.00; + + // Given + /** @var CouponInterface $coupon */ + $couponCumulative1 = $this->generateValidCoupon('XMAS1', null, null, null, null, null, new \DateTime(), null, true); + + $coupons = array($couponCumulative1); + /** @var CouponAdapterInterface $stubCouponBaseAdapter */ + $stubCouponBaseAdapter = $this->generateFakeAdapter($coupons, $cartTotalPrice, $checkoutTotalPrice); + + // When + $sortedCoupons = PhpUnitUtils::callMethod( + new CouponManager($stubCouponBaseAdapter), + 'sortCoupons', + array($coupons) + ); + + // Then + $expected = array(); + $actual = $sortedCoupons; + + $this->assertSame($expected, $actual, 'Coupon expired ignored'); + } + + /** + * Testing how multiple Coupon behaviour + * Entering 1 Coupon cumulative expired + * 1 Coupon cumulative expired + * + * @covers Thelia\Coupon\CouponManager::sortCoupons + */ + public function testCouponCumulationTwoCouponCumulativeExpired() + { + $cartTotalPrice = 100.00; + $checkoutTotalPrice = 120.00; + + // Given + /** @var CouponInterface $coupon */ + $couponCumulative1 = $this->generateValidCoupon('XMAS1', null, null, null, null, null, new \DateTime(), null, true); + $couponCumulative2 = $this->generateValidCoupon('XMAS2', null, null, null, null, null, new \DateTime(), null, true); + + $coupons = array($couponCumulative1, $couponCumulative2); + /** @var CouponAdapterInterface $stubCouponBaseAdapter */ + $stubCouponBaseAdapter = $this->generateFakeAdapter($coupons, $cartTotalPrice, $checkoutTotalPrice); + + // When + $sortedCoupons = PhpUnitUtils::callMethod( + new CouponManager($stubCouponBaseAdapter), + 'sortCoupons', + array($coupons) + ); + + // Then + $expected = array(); + $actual = $sortedCoupons; + + $this->assertSame($expected, $actual, 'Coupon expired ignored'); + } + + /** + * Testing how multiple Coupon behaviour + * Entering 1 Coupon cumulative expired + * 1 Coupon cumulative valid + * + * @covers Thelia\Coupon\CouponManager::sortCoupons + */ + public function testCouponCumulationOneCouponCumulativeExpiredOneNonExpired() + { + $cartTotalPrice = 100.00; + $checkoutTotalPrice = 120.00; + + // Given + /** @var CouponInterface $coupon */ + $couponCumulative1 = $this->generateValidCoupon('XMAS1', null, null, null, null, null, new \DateTime(), null, true); + $couponCumulative2 = $this->generateValidCoupon('XMAS2', null, null, null, null, null, null, null, true); + + $coupons = array($couponCumulative1, $couponCumulative2); + /** @var CouponAdapterInterface $stubCouponBaseAdapter */ + $stubCouponBaseAdapter = $this->generateFakeAdapter($coupons, $cartTotalPrice, $checkoutTotalPrice); + + // When + $sortedCoupons = PhpUnitUtils::callMethod( + new CouponManager($stubCouponBaseAdapter), + 'sortCoupons', + array($coupons) + ); + + // Then + $expected = array($couponCumulative2); + $actual = $sortedCoupons; + + $this->assertSame($expected, $actual, 'Coupon expired ignored'); + } + + /** + * Testing how multiple Coupon behaviour + * Entering 1 Coupon cumulative valid + * 1 Coupon cumulative expired + * + * @covers Thelia\Coupon\CouponManager::sortCoupons + */ + public function testCouponCumulationOneCouponCumulativeNonExpiredOneExpired() + { + $cartTotalPrice = 100.00; + $checkoutTotalPrice = 120.00; + + // Given + /** @var CouponInterface $coupon */ + $couponCumulative1 = $this->generateValidCoupon('XMAS1', null, null, null, null, null, null, null, true); + $couponCumulative2 = $this->generateValidCoupon('XMAS2', null, null, null, null, null, new \DateTime(), null, true); + + $coupons = array($couponCumulative1, $couponCumulative2); + /** @var CouponAdapterInterface $stubCouponBaseAdapter */ + $stubCouponBaseAdapter = $this->generateFakeAdapter($coupons, $cartTotalPrice, $checkoutTotalPrice); + + // When + $sortedCoupons = PhpUnitUtils::callMethod( + new CouponManager($stubCouponBaseAdapter), + 'sortCoupons', + array($coupons) + ); + + // Then + $expected = array($couponCumulative1); + $actual = $sortedCoupons; + + $this->assertSame($expected, $actual, 'Coupon expired ignored'); + } + + /** + * Testing how multiple Coupon behaviour + * Entering 1 Coupon cumulative valid + * 1 Coupon cumulative valid + * 1 Coupon cumulative valid + * 1 Coupon cumulative valid + * + * @covers Thelia\Coupon\CouponManager::sortCoupons + */ + public function testCouponCumulationFourCouponCumulative() + { + $cartTotalPrice = 100.00; + $checkoutTotalPrice = 120.00; + + // Given + /** @var CouponInterface $coupon */ + $couponCumulative1 = $this->generateValidCoupon('XMAS1', null, null, null, null, null, null, null, true); + $couponCumulative2 = $this->generateValidCoupon('XMAS2', null, null, null, null, null, null, null, true); + $couponCumulative3 = $this->generateValidCoupon('XMAS3', null, null, null, null, null, null, null, true); + $couponCumulative4 = $this->generateValidCoupon('XMAS4', null, null, null, null, null, null, null, true); + + $coupons = array($couponCumulative1, $couponCumulative2, $couponCumulative3, $couponCumulative4); + /** @var CouponAdapterInterface $stubCouponBaseAdapter */ + $stubCouponBaseAdapter = $this->generateFakeAdapter($coupons, $cartTotalPrice, $checkoutTotalPrice); + + // When + $sortedCoupons = PhpUnitUtils::callMethod( + new CouponManager($stubCouponBaseAdapter), + 'sortCoupons', + array($coupons) + ); + + // Then + $expected = $coupons; + $actual = $sortedCoupons; + + $this->assertSame($expected, $actual, 'Coupon cumulative ignored'); + } + + /** + * Testing how multiple Coupon behaviour + * Entering 1 Coupon cumulative valid + * 1 Coupon cumulative valid + * 1 Coupon cumulative valid + * 1 Coupon non cumulative valid + * + * @covers Thelia\Coupon\CouponManager::sortCoupons + */ + public function testCouponCumulationThreeCouponCumulativeOneNonCumulative() + { + $cartTotalPrice = 100.00; + $checkoutTotalPrice = 120.00; + + // Given + /** @var CouponInterface $coupon */ + $couponCumulative1 = $this->generateValidCoupon('XMAS1', null, null, null, null, null, null, null, true); + $couponCumulative2 = $this->generateValidCoupon('XMAS2', null, null, null, null, null, null, null, true); + $couponCumulative3 = $this->generateValidCoupon('XMAS3', null, null, null, null, null, null, null, true); + $couponCumulative4 = $this->generateValidCoupon('XMAS4', null, null, null, null, null, null, null, false); + + $coupons = array($couponCumulative1, $couponCumulative2, $couponCumulative3, $couponCumulative4); + /** @var CouponAdapterInterface $stubCouponBaseAdapter */ + $stubCouponBaseAdapter = $this->generateFakeAdapter($coupons, $cartTotalPrice, $checkoutTotalPrice); + + // When + $sortedCoupons = PhpUnitUtils::callMethod( + new CouponManager($stubCouponBaseAdapter), + 'sortCoupons', + array($coupons) + ); + + // Then + $expected = array($couponCumulative4); + $actual = $sortedCoupons; + + $this->assertSame($expected, $actual, 'Coupon cumulative ignored'); + } + /** * Generate valid CouponRuleInterfaces * * @return array Array of CouponRuleInterface */ - protected function generateValidRules() + public static function generateValidRules() { $rule1 = new AvailableForTotalAmount( array( @@ -281,4 +675,130 @@ Sed facilisis pellentesque nisl, eu tincidunt erat scelerisque a. Nullam malesua { } + /** + * 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; + } + + /** + * Generate valid CouponInterface + * + * @param string $code Coupon Code + * @param string $title Coupon Title + * @param string $shortDescription Coupon short + * description + * @param string $description Coupon description + * @param float $amount Coupon discount + * @param bool $isEnabled Is Coupon enabled + * @param \DateTime $expirationDate Coupon expiration date + * @param CouponRuleCollection $rules Coupon rules + * @param bool $isCumulative If is cumulative + * @param bool $isRemovingPostage If is removing postage + * @param bool $isAvailableOnSpecialOffers If is available on + * special offers or not + * @param int $maxUsage How many time a Coupon + * can be used + * + * @return CouponInterface + */ + public static function generateValidCoupon( + $code = null, + $title = null, + $shortDescription = null, + $description = null, + $amount = null, + $isEnabled = null, + $expirationDate = null, + $rules = null, + $isCumulative = null, + $isRemovingPostage = null, + $isAvailableOnSpecialOffers = null, + $maxUsage = null + ) { + if ($code === null) { + $code = self::VALID_CODE; + } + if ($title === null) { + $title = self::VALID_TITLE; + } + if ($shortDescription === null) { + $shortDescription = self::VALID_SHORT_DESCRIPTION; + } + if ($description === null) { + $description = self::VALID_DESCRIPTION; + } + if ($amount === null) { + $amount = 10.00; + } + if ($isEnabled === null) { + $isEnabled = true; + } + if ($isCumulative === null) { + $isCumulative = true; + } + if ($isRemovingPostage === null) { + $isRemovingPostage = false; + } + if ($isAvailableOnSpecialOffers === null) { + $isAvailableOnSpecialOffers = true; + } + if ($maxUsage === null) { + $maxUsage = 40; + } + + if ($expirationDate === null) { + $expirationDate = new \DateTime(); + $expirationDate->setTimestamp(strtotime("today + 2 months")); + } + + $coupon = new RemoveXAmount($code, $title, $shortDescription, $description, $amount, $isCumulative, $isRemovingPostage, $isAvailableOnSpecialOffers, $isEnabled, $maxUsage, $expirationDate); + + if ($rules === null) { + $rules = self::generateValidRules(); + } + + $coupon->setRules($rules); + + return $coupon; + } + } diff --git a/core/lib/Thelia/Tests/Coupon/CouponRuleCollectionTest.php b/core/lib/Thelia/Tests/Coupon/CouponRuleCollectionTest.php index 081aee2dd..e399b6f37 100644 --- a/core/lib/Thelia/Tests/Coupon/CouponRuleCollectionTest.php +++ b/core/lib/Thelia/Tests/Coupon/CouponRuleCollectionTest.php @@ -23,8 +23,8 @@ namespace Thelia\Coupon; -use Thelia\Coupon\Parameter\PriceParam; -use Thelia\Coupon\Parameter\RuleValidator; +use Thelia\Coupon\Validator\PriceParam; +use Thelia\Coupon\Validator\RuleValidator; use Thelia\Coupon\Rule\AvailableForTotalAmount; use Thelia\Coupon\Rule\Operators; diff --git a/core/lib/Thelia/Tests/Coupon/Parameter/DateParamTest.php b/core/lib/Thelia/Tests/Coupon/Parameter/DateParamTest.php index 4c26bfa2f..bbfe4015a 100644 --- a/core/lib/Thelia/Tests/Coupon/Parameter/DateParamTest.php +++ b/core/lib/Thelia/Tests/Coupon/Parameter/DateParamTest.php @@ -24,7 +24,7 @@ namespace Thelia\Coupon; use InvalidArgumentException; -use Thelia\Coupon\Parameter\DateParam; +use Thelia\Coupon\Validator\DateParam; /** * Created by JetBrains PhpStorm. diff --git a/core/lib/Thelia/Tests/Coupon/Parameter/IntegerParamTest.php b/core/lib/Thelia/Tests/Coupon/Parameter/IntegerParamTest.php index 05f19955d..b8938d102 100644 --- a/core/lib/Thelia/Tests/Coupon/Parameter/IntegerParamTest.php +++ b/core/lib/Thelia/Tests/Coupon/Parameter/IntegerParamTest.php @@ -24,7 +24,7 @@ namespace Thelia\Coupon; use InvalidArgumentException; -use Thelia\Coupon\Parameter\IntegerParam; +use Thelia\Coupon\Validator\IntegerParam; /** * Created by JetBrains PhpStorm. diff --git a/core/lib/Thelia/Tests/Coupon/Parameter/IntervalParamTest.php b/core/lib/Thelia/Tests/Coupon/Parameter/IntervalParamTest.php index 73571ba5b..c068dfc01 100644 --- a/core/lib/Thelia/Tests/Coupon/Parameter/IntervalParamTest.php +++ b/core/lib/Thelia/Tests/Coupon/Parameter/IntervalParamTest.php @@ -24,7 +24,7 @@ namespace Thelia\Coupon; use InvalidArgumentException; -use Thelia\Coupon\Parameter\IntervalParam; +use Thelia\Coupon\Validator\IntervalParam; /** * Created by JetBrains PhpStorm. diff --git a/core/lib/Thelia/Tests/Coupon/Parameter/PriceParamTest.php b/core/lib/Thelia/Tests/Coupon/Parameter/PriceParamTest.php index 62d552e0b..19f19fd4e 100644 --- a/core/lib/Thelia/Tests/Coupon/Parameter/PriceParamTest.php +++ b/core/lib/Thelia/Tests/Coupon/Parameter/PriceParamTest.php @@ -24,7 +24,7 @@ namespace Thelia\Coupon; use InvalidArgumentException; -use Thelia\Coupon\Parameter\PriceParam; +use Thelia\Coupon\Validator\PriceParam; /** * Created by JetBrains PhpStorm. diff --git a/core/lib/Thelia/Tests/Coupon/Parameter/QuantityParamTest.php b/core/lib/Thelia/Tests/Coupon/Parameter/QuantityParamTest.php index 6a9d9d737..17343121c 100644 --- a/core/lib/Thelia/Tests/Coupon/Parameter/QuantityParamTest.php +++ b/core/lib/Thelia/Tests/Coupon/Parameter/QuantityParamTest.php @@ -24,7 +24,7 @@ namespace Thelia\Coupon; use InvalidArgumentException; -use Thelia\Coupon\Parameter\QuantityParam; +use Thelia\Coupon\Validator\QuantityParam; /** * Created by JetBrains PhpStorm. diff --git a/core/lib/Thelia/Tests/Coupon/Parameter/RepeatedDateParamTest.php b/core/lib/Thelia/Tests/Coupon/Parameter/RepeatedDateParamTest.php index f9a6f0d18..66c970a4f 100644 --- a/core/lib/Thelia/Tests/Coupon/Parameter/RepeatedDateParamTest.php +++ b/core/lib/Thelia/Tests/Coupon/Parameter/RepeatedDateParamTest.php @@ -25,7 +25,7 @@ namespace Thelia\Coupon; use InvalidArgumentException; use Symfony\Component\Intl\Exception\NotImplementedException; -use Thelia\Coupon\Parameter\RepeatedDateParam; +use Thelia\Coupon\Validator\RepeatedDateParam; /** * Created by JetBrains PhpStorm. diff --git a/core/lib/Thelia/Tests/Coupon/Parameter/RepeatedIntervalParamTest.php b/core/lib/Thelia/Tests/Coupon/Parameter/RepeatedIntervalParamTest.php index fe5866b0f..dc7a1335d 100644 --- a/core/lib/Thelia/Tests/Coupon/Parameter/RepeatedIntervalParamTest.php +++ b/core/lib/Thelia/Tests/Coupon/Parameter/RepeatedIntervalParamTest.php @@ -24,7 +24,7 @@ namespace Thelia\Coupon; use Symfony\Component\Intl\Exception\NotImplementedException; -use Thelia\Coupon\Parameter\RepeatedIntervalParam; +use Thelia\Coupon\Validator\RepeatedIntervalParam; /** * Created by JetBrains PhpStorm. @@ -75,7 +75,7 @@ class RepeatedIntervalParamTest extends \PHPUnit_Framework_TestCase * @covers Thelia\Coupon\Parameter\RepeatedIntervalParam::compareTo * */ - public function testEqualsDateRepeatEveryMonthOneTimeFirstPeriodBegining() + public function testEqualsDateRepeatEveryMonthOneTimeFirstPeriodBeginning() { $startDateValidator = new \DateTime("2012-07-08"); $dateToValidate = new \DateTime("2012-07-08"); @@ -138,7 +138,7 @@ class RepeatedIntervalParamTest extends \PHPUnit_Framework_TestCase * @covers Thelia\Coupon\Parameter\RepeatedIntervalParam::compareTo * */ - public function testEqualsDateRepeatEveryMonthOneTimeSecondPeriodBegining() + public function testEqualsDateRepeatEveryMonthOneTimeSecondPeriodBeginning() { $startDateValidator = new \DateTime("2012-08-08"); $dateToValidate = new \DateTime("2012-08-08"); @@ -264,7 +264,7 @@ class RepeatedIntervalParamTest extends \PHPUnit_Framework_TestCase * @covers Thelia\Coupon\Parameter\RepeatedIntervalParam::compareTo * */ - public function testNotEqualsDateRepeatEveryMonthFourTimeInTheBegining() + public function testNotEqualsDateRepeatEveryMonthFourTimeInTheBeginning() { $startDateValidator = new \DateTime("2012-10-08"); $dateToValidate = new \DateTime("2012-07-19"); @@ -348,7 +348,7 @@ class RepeatedIntervalParamTest extends \PHPUnit_Framework_TestCase /** * @covers Thelia\Coupon\Parameter\DateParam::compareTo - * @expectedException InvalidArgumentException + * @expectedException \InvalidArgumentException */ public function testInvalidArgumentException() { diff --git a/core/lib/Thelia/Tests/Coupon/Rule/AvailableForTotalAmountTest.php b/core/lib/Thelia/Tests/Coupon/Rule/AvailableForTotalAmountTest.php index 15c07ba1a..63d78615f 100644 --- a/core/lib/Thelia/Tests/Coupon/Rule/AvailableForTotalAmountTest.php +++ b/core/lib/Thelia/Tests/Coupon/Rule/AvailableForTotalAmountTest.php @@ -23,8 +23,8 @@ namespace Thelia\Coupon; -use Thelia\Coupon\Parameter\PriceParam; -use Thelia\Coupon\Parameter\RuleValidator; +use Thelia\Coupon\Validator\PriceParam; +use Thelia\Coupon\Validator\RuleValidator; use Thelia\Coupon\Rule\AvailableForTotalAmount; use Thelia\Coupon\Rule\Operators; use Thelia\Exception\InvalidRuleOperatorException; @@ -52,6 +52,11 @@ class AvailableForTotalAmountTest extends \PHPUnit_Framework_TestCase { } + /** + * Generate valid CouponBaseAdapter + * + * @return CouponAdapterInterface + */ protected function generateValidCouponBaseAdapterMock() { /** @var CouponAdapterInterface $stubTheliaAdapter */ @@ -130,7 +135,7 @@ class AvailableForTotalAmountTest extends \PHPUnit_Framework_TestCase /** * * @covers Thelia\Coupon\Rule\AvailableForTotalAmount::checkBackOfficeInput - * @expectedException ErrorException + * @expectedException \ErrorException * */ public function testInValidBackOfficeInputValue() diff --git a/core/lib/Thelia/Tests/Coupon/Rule/AvailableForXArticlesTest.php b/core/lib/Thelia/Tests/Coupon/Rule/AvailableForXArticlesTest.php index 8bffc05a9..3579eacf3 100644 --- a/core/lib/Thelia/Tests/Coupon/Rule/AvailableForXArticlesTest.php +++ b/core/lib/Thelia/Tests/Coupon/Rule/AvailableForXArticlesTest.php @@ -52,11 +52,11 @@ class AvailableForXArticlesTest extends \PHPUnit_Framework_TestCase /** @var CouponAdapterInterface $stubTheliaAdapter */ $stubTheliaAdapter = $this->getMock( 'CouponBaseAdapter', - array('getNbArticlesInTheCart'), + array('getNbArticlesInCart'), array() ); $stubTheliaAdapter->expects($this->any()) - ->method('getNbArticlesInTheCart') + ->method('getNbArticlesInCart') ->will($this->returnValue(4)); return $stubTheliaAdapter; @@ -73,7 +73,7 @@ class AvailableForXArticlesTest extends \PHPUnit_Framework_TestCase $stubTheliaAdapter = $this->generateValidCouponBaseAdapterMock(); $validators = array(4); - $validated = array($stubTheliaAdapter->getNbArticlesInTheCart()); + $validated = array($stubTheliaAdapter->getNbArticlesInCart()); $rule = new AvailableForXArticles($validators, $validated); $expected = true; @@ -92,7 +92,7 @@ class AvailableForXArticlesTest extends \PHPUnit_Framework_TestCase $stubTheliaAdapter = $this->generateValidCouponBaseAdapterMock(); $validators = array(4.5); - $validated = array($stubTheliaAdapter->getNbArticlesInTheCart()); + $validated = array($stubTheliaAdapter->getNbArticlesInCart()); $rule = new AvailableForXArticles($validators, $validated); $expected = false; @@ -100,7 +100,7 @@ class AvailableForXArticlesTest extends \PHPUnit_Framework_TestCase $this->assertEquals($expected, $actual); $validators = array(-1); - $validated = array($stubTheliaAdapter->getNbArticlesInTheCart()); + $validated = array($stubTheliaAdapter->getNbArticlesInCart()); $rule = new AvailableForXArticles($validators, $validated); $expected = false; @@ -108,7 +108,7 @@ class AvailableForXArticlesTest extends \PHPUnit_Framework_TestCase $this->assertEquals($expected, $actual); $validators = array('bad'); - $validated = array($stubTheliaAdapter->getNbArticlesInTheCart()); + $validated = array($stubTheliaAdapter->getNbArticlesInCart()); $rule = new AvailableForXArticles($validators, $validated); $expected = false; @@ -129,7 +129,7 @@ class AvailableForXArticlesTest extends \PHPUnit_Framework_TestCase $stubTheliaAdapter = $this->generateValidCouponBaseAdapterMock(); $validators = array(4); - $validated = array($stubTheliaAdapter->getNbArticlesInTheCart()); + $validated = array($stubTheliaAdapter->getNbArticlesInCart()); $rule = new AvailableForXArticles($validators, $validated); $expected = true; @@ -148,7 +148,7 @@ class AvailableForXArticlesTest extends \PHPUnit_Framework_TestCase $stubTheliaAdapter = $this->generateValidCouponBaseAdapterMock(); $validators = array(4.5); - $validated = array($stubTheliaAdapter->getNbArticlesInTheCart()); + $validated = array($stubTheliaAdapter->getNbArticlesInCart()); $rule = new AvailableForXArticles($validators, $validated); $expected = false; @@ -156,7 +156,7 @@ class AvailableForXArticlesTest extends \PHPUnit_Framework_TestCase $this->assertEquals($expected, $actual); $validators = array(-1); - $validated = array($stubTheliaAdapter->getNbArticlesInTheCart()); + $validated = array($stubTheliaAdapter->getNbArticlesInCart()); $rule = new AvailableForXArticles($validators, $validated); $expected = false; @@ -164,7 +164,7 @@ class AvailableForXArticlesTest extends \PHPUnit_Framework_TestCase $this->assertEquals($expected, $actual); $validators = array('bad'); - $validated = array($stubTheliaAdapter->getNbArticlesInTheCart()); + $validated = array($stubTheliaAdapter->getNbArticlesInCart()); $rule = new AvailableForXArticles($validators, $validated); $expected = false; @@ -183,7 +183,7 @@ class AvailableForXArticlesTest extends \PHPUnit_Framework_TestCase $stubTheliaAdapter = $this->generateValidCouponBaseAdapterMock(); $validators = array(4); - $validated = array($stubTheliaAdapter->getNbArticlesInTheCart()); + $validated = array($stubTheliaAdapter->getNbArticlesInCart()); $rule = new AvailableForXArticles($validators, $validated); $expected = true; @@ -202,7 +202,7 @@ class AvailableForXArticlesTest extends \PHPUnit_Framework_TestCase $stubTheliaAdapter = $this->generateValidCouponBaseAdapterMock(); $validators = array(5); - $validated = array($stubTheliaAdapter->getNbArticlesInTheCart()); + $validated = array($stubTheliaAdapter->getNbArticlesInCart()); $rule = new AvailableForXArticles($validators, $validated); $expected = true; @@ -221,7 +221,7 @@ class AvailableForXArticlesTest extends \PHPUnit_Framework_TestCase $stubTheliaAdapter = $this->generateValidCouponBaseAdapterMock(); $validators = array(3); - $validated = array($stubTheliaAdapter->getNbArticlesInTheCart()); + $validated = array($stubTheliaAdapter->getNbArticlesInCart()); $rule = new AvailableForXArticles($validators, $validated); $expected = false; diff --git a/core/lib/Thelia/Tests/Coupon/Rule/OperatorsTest.php b/core/lib/Thelia/Tests/Coupon/Rule/OperatorsTest.php index bf4fbc75d..22a83dc8b 100644 --- a/core/lib/Thelia/Tests/Coupon/Rule/OperatorsTest.php +++ b/core/lib/Thelia/Tests/Coupon/Rule/OperatorsTest.php @@ -23,7 +23,7 @@ namespace Thelia\Coupon; -use Thelia\Coupon\Parameter\QuantityParam; +use Thelia\Coupon\Validator\QuantityParam; use Thelia\Coupon\Rule\Operators; /** @@ -61,7 +61,7 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase $b = new QuantityParam(12); // When - $actual = Operators::isValidAccordingToOperator($a, $operator, $b); + $actual = Operators::isValid($a, $operator, $b); // Then $this->assertTrue($actual); @@ -80,7 +80,7 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase $b = new QuantityParam(12); // When - $actual = Operators::isValidAccordingToOperator($a, $operator, $b); + $actual = Operators::isValid($a, $operator, $b); // Then $this->assertFalse($actual); @@ -99,7 +99,7 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase $b = new QuantityParam(12); // When - $actual = Operators::isValidAccordingToOperator($a, $operator, $b); + $actual = Operators::isValid($a, $operator, $b); // Then $this->assertFalse($actual); @@ -118,7 +118,7 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase $b = new QuantityParam(11); // When - $actual = Operators::isValidAccordingToOperator($a, $operator, $b); + $actual = Operators::isValid($a, $operator, $b); // Then $this->assertTrue($actual); @@ -137,7 +137,7 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase $b = new QuantityParam(11); // When - $actual = Operators::isValidAccordingToOperator($a, $operator, $b); + $actual = Operators::isValid($a, $operator, $b); // Then $this->assertTrue($actual); @@ -156,7 +156,7 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase $b = new QuantityParam(11); // When - $actual = Operators::isValidAccordingToOperator($a, $operator, $b); + $actual = Operators::isValid($a, $operator, $b); // Then $this->assertFalse($actual); @@ -175,7 +175,7 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase $b = new QuantityParam(12); // When - $actual = Operators::isValidAccordingToOperator($a, $operator, $b); + $actual = Operators::isValid($a, $operator, $b); // Then $this->assertTrue($actual); @@ -194,7 +194,7 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase $b = new QuantityParam(12); // When - $actual = Operators::isValidAccordingToOperator($a, $operator, $b); + $actual = Operators::isValid($a, $operator, $b); // Then $this->assertFalse($actual); @@ -213,7 +213,7 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase $b = new QuantityParam(12); // When - $actual = Operators::isValidAccordingToOperator($a, $operator, $b); + $actual = Operators::isValid($a, $operator, $b); // Then $this->assertFalse($actual); @@ -232,7 +232,7 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase $b = new QuantityParam(13); // When - $actual = Operators::isValidAccordingToOperator($a, $operator, $b); + $actual = Operators::isValid($a, $operator, $b); // Then $this->assertTrue($actual); @@ -251,7 +251,7 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase $b = new QuantityParam(13); // When - $actual = Operators::isValidAccordingToOperator($a, $operator, $b); + $actual = Operators::isValid($a, $operator, $b); // Then $this->assertTrue($actual); @@ -270,7 +270,7 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase $b = new QuantityParam(13); // When - $actual = Operators::isValidAccordingToOperator($a, $operator, $b); + $actual = Operators::isValid($a, $operator, $b); // Then $this->assertFalse($actual); @@ -289,7 +289,7 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase $b = new QuantityParam(12); // When - $actual = Operators::isValidAccordingToOperator($a, $operator, $b); + $actual = Operators::isValid($a, $operator, $b); // Then $this->assertTrue($actual); @@ -308,7 +308,7 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase $b = new QuantityParam(12); // When - $actual = Operators::isValidAccordingToOperator($a, $operator, $b); + $actual = Operators::isValid($a, $operator, $b); // Then $this->assertFalse($actual); @@ -327,7 +327,7 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase $b = new QuantityParam(12); // When - $actual = Operators::isValidAccordingToOperator($a, $operator, $b); + $actual = Operators::isValid($a, $operator, $b); // Then $this->assertFalse($actual); @@ -346,7 +346,7 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase $b = new QuantityParam(11); // When - $actual = Operators::isValidAccordingToOperator($a, $operator, $b); + $actual = Operators::isValid($a, $operator, $b); // Then $this->assertTrue($actual); @@ -365,7 +365,7 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase $b = new QuantityParam(11); // When - $actual = Operators::isValidAccordingToOperator($a, $operator, $b); + $actual = Operators::isValid($a, $operator, $b); // Then $this->assertFalse($actual); @@ -384,7 +384,7 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase $b = new QuantityParam(11); // When - $actual = Operators::isValidAccordingToOperator($a, $operator, $b); + $actual = Operators::isValid($a, $operator, $b); // Then $this->assertFalse($actual); diff --git a/core/lib/Thelia/Tests/Coupon/Type/RemoveXAmountTest.php b/core/lib/Thelia/Tests/Coupon/Type/RemoveXAmountTest.php index f7287bb45..72313f9ec 100644 --- a/core/lib/Thelia/Tests/Coupon/Type/RemoveXAmountTest.php +++ b/core/lib/Thelia/Tests/Coupon/Type/RemoveXAmountTest.php @@ -23,11 +23,14 @@ namespace Thelia\Coupon; -use Thelia\Coupon\Parameter\PriceParam; +use Thelia\Coupon\Validator\PriceParam; +use Thelia\Coupon\Validator\RuleValidator; use Thelia\Coupon\Rule\AvailableForTotalAmount; use Thelia\Coupon\Rule\Operators; use Thelia\Coupon\Type\RemoveXAmount; +require_once '../CouponManagerTest.php'; + /** * Created by JetBrains PhpStorm. * Date: 8/19/13 @@ -41,11 +44,6 @@ use Thelia\Coupon\Type\RemoveXAmount; */ class RemoveXAmountTest extends \PHPUnit_Framework_TestCase { - - CONST VALID_COUPON_CODE = 'XMAS'; - CONST VALID_COUPON_TITLE = 'XMAS Coupon'; - CONST VALID_COUPON_SHORT_DESCRIPTION = 'Coupon for christmas'; - CONST VALID_COUPON_DESCRIPTION = '

Lorem

ipsum'; /** * Sets up the fixture, for example, opens a network connection. * This method is called before a test is executed. @@ -55,37 +53,6 @@ class RemoveXAmountTest extends \PHPUnit_Framework_TestCase } - - protected function generateValidCumulativeRemovingPostageCoupon() - { - $coupon = new RemoveXAmount( - self::VALID_COUPON_CODE, - self::VALID_COUPON_TITLE, - self::VALID_COUPON_SHORT_DESCRIPTION, - self::VALID_COUPON_DESCRIPTION, - 30.00, - true, - true - ); - - return $coupon; - } - - protected function generateValidNonCumulativeNonRemovingPostageCoupon() - { - $coupon = new RemoveXAmount( - self::VALID_COUPON_CODE, - self::VALID_COUPON_TITLE, - self::VALID_COUPON_SHORT_DESCRIPTION, - self::VALID_COUPON_DESCRIPTION, - 30.00, - false, - false - ); - - return $coupon; - } - /** * * @covers Thelia\Coupon\type\RemoveXAmount::getCode @@ -96,28 +63,25 @@ class RemoveXAmountTest extends \PHPUnit_Framework_TestCase */ public function testDisplay() { + $coupon = CouponManagerTest::generateValidCoupon(null, null, null, null, null, null, null, null, true, true); - $coupon = $this->generateValidCumulativeRemovingPostageCoupon(); - - $expected = self::VALID_COUPON_CODE; + $expected = CouponManagerTest::VALID_CODE; $actual = $coupon->getCode(); $this->assertEquals($expected, $actual); - $expected = self::VALID_COUPON_TITLE; + $expected = CouponManagerTest::VALID_TITLE; $actual = $coupon->getTitle(); $this->assertEquals($expected, $actual); - $expected = self::VALID_COUPON_SHORT_DESCRIPTION; + $expected = CouponManagerTest::VALID_SHORT_DESCRIPTION; $actual = $coupon->getShortDescription(); $this->assertEquals($expected, $actual); - $expected = self::VALID_COUPON_DESCRIPTION; + $expected = CouponManagerTest::VALID_DESCRIPTION; $actual = $coupon->getDescription(); $this->assertEquals($expected, $actual); - } - /** * * @covers Thelia\Coupon\type\RemoveXAmount::isCumulative @@ -126,7 +90,7 @@ class RemoveXAmountTest extends \PHPUnit_Framework_TestCase public function testIsCumulative() { - $coupon = $this->generateValidCumulativeRemovingPostageCoupon(); + $coupon = CouponManagerTest::generateValidCoupon(null, null, null, null, null, null, null, null, true, true); $actual = $coupon->isCumulative(); $this->assertTrue($actual); @@ -140,7 +104,7 @@ class RemoveXAmountTest extends \PHPUnit_Framework_TestCase public function testIsNotCumulative() { - $coupon = $this->generateValidNonCumulativeNonRemovingPostageCoupon(); + $coupon = CouponManagerTest::generateValidCoupon(null, null, null, null, null, null, null, null, false, false); $actual = $coupon->isCumulative(); $this->assertFalse($actual); @@ -154,8 +118,7 @@ class RemoveXAmountTest extends \PHPUnit_Framework_TestCase */ public function testIsRemovingPostage() { - - $coupon = $this->generateValidCumulativeRemovingPostageCoupon(); + $coupon = CouponManagerTest::generateValidCoupon(null, null, null, null, null, null, null, null, true, true); $actual = $coupon->isRemovingPostage(); $this->assertTrue($actual); @@ -169,7 +132,7 @@ class RemoveXAmountTest extends \PHPUnit_Framework_TestCase public function testIsNotRemovingPostage() { - $coupon = $this->generateValidNonCumulativeNonRemovingPostageCoupon(); + $coupon = CouponManagerTest::generateValidCoupon(null, null, null, null, null, null, null, null, false, false); $actual = $coupon->isRemovingPostage(); $this->assertFalse($actual); @@ -184,43 +147,13 @@ class RemoveXAmountTest extends \PHPUnit_Framework_TestCase public function testGetEffect() { - $coupon = $this->generateValidNonCumulativeNonRemovingPostageCoupon(); + $coupon = CouponManagerTest::generateValidCoupon(null, null, null, null, null, null, null, null, false, false); - $expected = -30.00; - $actual = $coupon->getEffect(); + $expected = 10; + $actual = $coupon->getDiscount(); $this->assertEquals($expected, $actual); } - /** - * - * @covers Thelia\Coupon\type\RemoveXAmount::addRule - * @covers Thelia\Coupon\type\RemoveXAmount::getRules - * - */ - public function testAddRuleValid() - { - // Given - $rule1 = $this->generateValideRuleAvailableForTotalAmountOperatorTo( - Operators::INFERIOR, - 100.23 - ); - $rule2 = $this->generateValideRuleAvailableForTotalAmountOperatorTo( - Operators::SUPERIOR, - 421.23 - ); - - $coupon = $this->generateValidNonCumulativeNonRemovingPostageCoupon(); - - // When - $coupon->addRule($rule1) - ->addRule($rule2); - - // Then - $expected = 2; - $this->assertCount($expected, $coupon->getRules()); - } - - /** * * @covers Thelia\Coupon\type\RemoveXAmount::setRules @@ -230,29 +163,27 @@ class RemoveXAmountTest extends \PHPUnit_Framework_TestCase public function testSetRulesValid() { // Given - $rule0 = $this->generateValideRuleAvailableForTotalAmountOperatorTo( + $rule0 = $this->generateValidRuleAvailableForTotalAmountOperatorTo( Operators::EQUAL, 20.00 ); - $rule1 = $this->generateValideRuleAvailableForTotalAmountOperatorTo( + $rule1 = $this->generateValidRuleAvailableForTotalAmountOperatorTo( Operators::INFERIOR, 100.23 ); - $rule2 = $this->generateValideRuleAvailableForTotalAmountOperatorTo( + $rule2 = $this->generateValidRuleAvailableForTotalAmountOperatorTo( Operators::SUPERIOR, 421.23 ); - $coupon = $this->generateValidNonCumulativeNonRemovingPostageCoupon(); + $coupon = CouponManagerTest::generateValidCoupon(null, null, null, null, null, null, null, null, false, false); // When - $coupon->addRule($rule0) - ->setRules(array($rule1, $rule2)); - + $coupon->setRules(new CouponRuleCollection(array($rule0, $rule1, $rule2))); // Then - $expected = 2; - $this->assertCount($expected, $coupon->getRules()); + $expected = 3; + $this->assertCount($expected, $coupon->getRules()->getRules()); } /** @@ -264,21 +195,20 @@ class RemoveXAmountTest extends \PHPUnit_Framework_TestCase public function testSetRulesInvalid() { // Given - $rule0 = $this->generateValideRuleAvailableForTotalAmountOperatorTo( + $rule0 = $this->generateValidRuleAvailableForTotalAmountOperatorTo( Operators::EQUAL, 20.00 ); - $rule1 = $this->generateValideRuleAvailableForTotalAmountOperatorTo( + $rule1 = $this->generateValidRuleAvailableForTotalAmountOperatorTo( Operators::INFERIOR, 100.23 ); $rule2 = $this; - $coupon = $this->generateValidNonCumulativeNonRemovingPostageCoupon(); + $coupon = CouponManagerTest::generateValidCoupon(null, null, null, null, null, null, null, null, false, false); // When - $coupon->addRule($rule0) - ->setRules(array($rule1, $rule2)); + $coupon->setRules(new CouponRuleCollection(array($rule0, $rule1, $rule2))); } /** @@ -289,18 +219,18 @@ class RemoveXAmountTest extends \PHPUnit_Framework_TestCase public function testGetEffectIfTotalAmountInferiorTo400Valid() { // Given - $rule0 = $this->generateValideRuleAvailableForTotalAmountOperatorTo( + $rule0 = $this->generateValidRuleAvailableForTotalAmountOperatorTo( Operators::INFERIOR, 400.00 ); - $coupon = $this->generateValidNonCumulativeNonRemovingPostageCoupon(); + $coupon = CouponManagerTest::generateValidCoupon(null, null, null, null, null, null, null, null, false, false); // When - $coupon->addRule($rule0); + $coupon->setRules(new CouponRuleCollection(array($rule0))); // Then - $expected = -30.00; - $actual = $coupon->getEffect(); + $expected = 10.00; + $actual = $coupon->getDiscount(); $this->assertEquals($expected, $actual); } @@ -312,18 +242,18 @@ class RemoveXAmountTest extends \PHPUnit_Framework_TestCase public function testGetEffectIfTotalAmountInferiorOrEqualTo400Valid() { // Given - $rule0 = $this->generateValideRuleAvailableForTotalAmountOperatorTo( + $rule0 = $this->generateValidRuleAvailableForTotalAmountOperatorTo( Operators::INFERIOR_OR_EQUAL, 400.00 ); - $coupon = $this->generateValidNonCumulativeNonRemovingPostageCoupon(); + $coupon = CouponManagerTest::generateValidCoupon(null, null, null, null, null, null, null, null, false, false); // When - $coupon->addRule($rule0); + $coupon->setRules(new CouponRuleCollection(array($rule0))); // Then - $expected = -30.00; - $actual = $coupon->getEffect(); + $expected = 10.00; + $actual = $coupon->getDiscount(); $this->assertEquals($expected, $actual); } @@ -335,18 +265,18 @@ class RemoveXAmountTest extends \PHPUnit_Framework_TestCase public function testGetEffectIfTotalAmountEqualTo400Valid() { // Given - $rule0 = $this->generateValideRuleAvailableForTotalAmountOperatorTo( + $rule0 = $this->generateValidRuleAvailableForTotalAmountOperatorTo( Operators::EQUAL, 400.00 ); - $coupon = $this->generateValidNonCumulativeNonRemovingPostageCoupon(); + $coupon = CouponManagerTest::generateValidCoupon(null, null, null, null, null, null, null, null, false, false); // When - $coupon->addRule($rule0); + $coupon->setRules(new CouponRuleCollection(array($rule0))); // Then - $expected = -30.00; - $actual = $coupon->getEffect(); + $expected = 10.00; + $actual = $coupon->getDiscount(); $this->assertEquals($expected, $actual); } @@ -358,18 +288,18 @@ class RemoveXAmountTest extends \PHPUnit_Framework_TestCase public function testGetEffectIfTotalAmountSuperiorOrEqualTo400Valid() { // Given - $rule0 = $this->generateValideRuleAvailableForTotalAmountOperatorTo( + $rule0 = $this->generateValidRuleAvailableForTotalAmountOperatorTo( Operators::SUPERIOR_OR_EQUAL, 400.00 ); - $coupon = $this->generateValidNonCumulativeNonRemovingPostageCoupon(); + $coupon = CouponManagerTest::generateValidCoupon(null, null, null, null, null, null, null, null, false, false); // When - $coupon->addRule($rule0); + $coupon->setRules(new CouponRuleCollection(array($rule0))); // Then - $expected = -30.00; - $actual = $coupon->getEffect(); + $expected = 10.00; + $actual = $coupon->getDiscount(); $this->assertEquals($expected, $actual); } @@ -381,18 +311,18 @@ class RemoveXAmountTest extends \PHPUnit_Framework_TestCase public function testGetEffectIfTotalAmountSuperiorTo400Valid() { // Given - $rule0 = $this->generateValideRuleAvailableForTotalAmountOperatorTo( + $rule0 = $this->generateValidRuleAvailableForTotalAmountOperatorTo( Operators::SUPERIOR, 400.00 ); - $coupon = $this->generateValidNonCumulativeNonRemovingPostageCoupon(); + $coupon = CouponManagerTest::generateValidCoupon(null, null, null, null, null, null, null, null, false, false); // When - $coupon->addRule($rule0); + $coupon->setRules(new CouponRuleCollection(array($rule0))); // Then - $expected = -30.00; - $actual = $coupon->getEffect(); + $expected = 10.00; + $actual = $coupon->getDiscount(); $this->assertEquals($expected, $actual); } @@ -415,12 +345,15 @@ class RemoveXAmountTest extends \PHPUnit_Framework_TestCase * * @return AvailableForTotalAmount */ - protected function generateValideRuleAvailableForTotalAmountOperatorTo($operator, $amount) + protected function generateValidRuleAvailableForTotalAmountOperatorTo($operator, $amount) { $validators = array( - AvailableForTotalAmount::PARAM1_PRICE => array( - AvailableForTotalAmount::OPERATOR => $operator, - AvailableForTotalAmount::VALUE => new PriceParam($amount, 'EUR') + AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( + $operator, + new PriceParam( + $amount, + 'EUR' + ) ) ); diff --git a/core/lib/Thelia/Tests/Coupon/Type/RemoveXPercentForCategoryYTest.php b/core/lib/Thelia/Tests/Coupon/Type/RemoveXPercentForCategoryYTest.php index caa90f4ae..24388104d 100644 --- a/core/lib/Thelia/Tests/Coupon/Type/RemoveXPercentForCategoryYTest.php +++ b/core/lib/Thelia/Tests/Coupon/Type/RemoveXPercentForCategoryYTest.php @@ -28,13 +28,13 @@ namespace Thelia\Coupon; * Date: 8/19/13 * Time: 3:24 PM * - * Unit Test RemoveXPercenForCategoryY Class + * Unit Test RemoveXPercentForCategoryY Class * * @package Coupon * @author Guillaume MOREL * */ -class RemoveXPercenForCategoryYTest extends \PHPUnit_Framework_TestCase +class RemoveXPercentForCategoryYTest extends \PHPUnit_Framework_TestCase { /** diff --git a/core/lib/Thelia/Tests/Coupon/Type/RemoveXPercentTest.php b/core/lib/Thelia/Tests/Coupon/Type/RemoveXPercentTest.php index 7dfca8dd2..5d110d3df 100644 --- a/core/lib/Thelia/Tests/Coupon/Type/RemoveXPercentTest.php +++ b/core/lib/Thelia/Tests/Coupon/Type/RemoveXPercentTest.php @@ -171,7 +171,7 @@ class RemoveXPercentTest extends \PHPUnit_Framework_TestCase /** * - * @covers Thelia\Coupon\Type\RemoveXPercent::getEffect + * @covers Thelia\Coupon\Type\RemoveXPercent::getDiscount * */ public function testGetEffect() @@ -180,7 +180,7 @@ class RemoveXPercentTest extends \PHPUnit_Framework_TestCase $coupon = $this->generateValidNonCumulativeNonRemovingPostageCoupon(); $expected = -30.00; - $actual = $coupon->getEffect(); + $actual = $coupon->getDiscount(); $this->assertEquals($expected, $actual); } diff --git a/core/lib/Thelia/Tools/PhpUnitUtils.php b/core/lib/Thelia/Tools/PhpUnitUtils.php index e8a2d41ab..89bd3b148 100644 --- a/core/lib/Thelia/Tools/PhpUnitUtils.php +++ b/core/lib/Thelia/Tools/PhpUnitUtils.php @@ -39,11 +39,11 @@ class PhpUnitUtils /** * Allow to call a protected methods * - * @param string $obj Class name + namespace + * @param Object $obj Class name + namespace * @param string $name Method name * @param array $args Method arguments * - * @return protected method result + * @return mixed protected method result */ public static function callMethod($obj, $name, array $args) {