Working : Coupon : Unit testing

This commit is contained in:
gmorel
2013-09-17 11:41:09 +02:00
parent ae4ad0038b
commit 3b249dc21c
6 changed files with 135 additions and 241 deletions

View File

@@ -126,14 +126,11 @@ class AvailableForTotalAmountManager extends CouponRuleAbstract
);
}
$floatType = new FloatType();
if (!$floatType->isValid($priceValue) || $priceValue <= 0) {
throw new InvalidRuleValueException(
get_class(), 'price'
);
}
$this->isPriceValid($priceValue);
$this->IsCurrencyValid($currencyValue);
// @todo check currency is legit or not
$this->operators = array(
self::INPUT1 => $priceOperator,

View File

@@ -31,6 +31,9 @@ use Thelia\Constraint\Validator\ComparableInterface;
use Thelia\Constraint\Validator\RuleValidator;
use Thelia\Exception\InvalidRuleException;
use Thelia\Exception\InvalidRuleOperatorException;
use Thelia\Exception\InvalidRuleValueException;
use Thelia\Model\Currency;
use Thelia\Type\FloatType;
/**
* Created by JetBrains PhpStorm.
@@ -268,4 +271,52 @@ abstract class CouponRuleAbstract implements CouponRuleInterface
return $serializableRule;
}
/**
* Check if currency if valid or not
*
* @param string $currencyValue Currency EUR|USD|..
*
* @return bool
* @throws \Thelia\Exception\InvalidRuleValueException
*/
protected function IsCurrencyValid($currencyValue)
{
$availableCurrencies = $this->adapter->getAvailableCurrencies();
/** @var Currency $currency */
$currencyFound = false;
foreach ($availableCurrencies as $key => $currency) {
if ($currencyValue == $currency->getCode()) {
$currencyFound = true;
}
}
if (!$currencyFound) {
throw new InvalidRuleValueException(
get_class(), 'currency'
);
}
return true;
}
/**
* Check if price is valid
*
* @param float $priceValue Price value to check
*
* @return bool
* @throws \Thelia\Exception\InvalidRuleValueException
*/
protected function isPriceValid($priceValue)
{
$floatType = new FloatType();
if (!$floatType->isValid($priceValue) || $priceValue <= 0) {
throw new InvalidRuleValueException(
get_class(), 'price'
);
}
return true;
}
}

View File

@@ -49,7 +49,9 @@ use Thelia\Form\Exception\FormValidationException;
use Thelia\Log\Tlog;
use Thelia\Model\Coupon;
use Thelia\Model\CouponQuery;
use Thelia\Model\CurrencyQuery;
use Thelia\Model\Lang;
use Thelia\Model\LangQuery;
use Thelia\Tools\I18n;
/**

View File

@@ -169,4 +169,11 @@ interface CouponAdapterInterface
*/
public function getConstraintValidator();
/**
* Return all available currencies
*
* @return array of Currency
*/
public function getAvailableCurrencies();
}

View File

@@ -35,6 +35,8 @@ use Thelia\Model\Coupon;
use Thelia\Model\CouponQuery;
use Thelia\Cart\CartTrait;
use Thelia\Model\Currency;
use Thelia\Model\CurrencyQuery;
use Thelia\Model\LangQuery;
/**
* Created by JetBrains PhpStorm.
@@ -266,4 +268,17 @@ class CouponBaseAdapter implements CouponAdapterInterface
{
return $this->container->get('thelia.constraint.validator');
}
/**
* Return all available currencies
*
* @return array of Currency
*/
public function getAvailableCurrencies()
{
$currencies = CurrencyQuery::create();
return $currencies->find();
}
}

View File

@@ -27,6 +27,7 @@ use Thelia\Constraint\ConstraintValidator;
use Thelia\Constraint\Rule\AvailableForTotalAmountManager;
use Thelia\Constraint\Rule\Operators;
use Thelia\Exception\InvalidRuleValueException;
use Thelia\Model\Currency;
/**
* Created by JetBrains PhpStorm.
@@ -44,6 +45,43 @@ class AvailableForTotalAmountManagerTest extends \PHPUnit_Framework_TestCase
/** @var CouponAdapterInterface $stubTheliaAdapter */
protected $stubTheliaAdapter = null;
/**
* Generate adapter stub
*
* @param int $cartTotalPrice Cart total price
* @param string $checkoutCurrency Checkout currency
*
* @return \PHPUnit_Framework_MockObject_MockObject
*/
public function generateAdapterStub($cartTotalPrice = 400, $checkoutCurrency = 'EUR')
{
$stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter')
->disableOriginalConstructor()
->getMock();
$stubAdapter->expects($this->any())
->method('getCartTotalPrice')
->will($this->returnValue($cartTotalPrice));
$stubAdapter->expects($this->any())
->method('getCheckoutCurrency')
->will($this->returnValue($checkoutCurrency));
$stubAdapter->expects($this->any())
->method('getConstraintValidator')
->will($this->returnValue(new ConstraintValidator()));
$currency1 = new Currency();
$currency1->setCode('EUR');
$currency2 = new Currency();
$currency2->setCode('USD');
$stubAdapter->expects($this->any())
->method('getAvailableCurrencies')
->will($this->returnValue(array($currency1, $currency2)));
return $stubAdapter;
}
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
@@ -62,19 +100,7 @@ class AvailableForTotalAmountManagerTest extends \PHPUnit_Framework_TestCase
*/
public function testInValidBackOfficeInputOperator()
{
$stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter')
->disableOriginalConstructor()
->getMock();
$stubAdapter->expects($this->any())
->method('getCartTotalPrice')
->will($this->returnValue(399));
$stubAdapter->expects($this->any())
->method('getCheckoutCurrency')
->will($this->returnValue('EUR'));
$stubAdapter->expects($this->any())
->method('getConstraintValidator')
->will($this->returnValue(new ConstraintValidator()));
$stubAdapter = $this->generateAdapterStub(399, 'EUR');
$rule1 = new AvailableForTotalAmountManager($stubAdapter);
$operators = array(
@@ -102,19 +128,7 @@ class AvailableForTotalAmountManagerTest extends \PHPUnit_Framework_TestCase
*/
public function testInValidBackOfficeInputOperator2()
{
$stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter')
->disableOriginalConstructor()
->getMock();
$stubAdapter->expects($this->any())
->method('getCartTotalPrice')
->will($this->returnValue(399));
$stubAdapter->expects($this->any())
->method('getCheckoutCurrency')
->will($this->returnValue('EUR'));
$stubAdapter->expects($this->any())
->method('getConstraintValidator')
->will($this->returnValue(new ConstraintValidator()));
$stubAdapter = $this->generateAdapterStub(399, 'EUR');
$rule1 = new AvailableForTotalAmountManager($stubAdapter);
$operators = array(
@@ -142,19 +156,7 @@ class AvailableForTotalAmountManagerTest extends \PHPUnit_Framework_TestCase
*/
public function testInValidBackOfficeInputValue()
{
$stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter')
->disableOriginalConstructor()
->getMock();
$stubAdapter->expects($this->any())
->method('getCartTotalPrice')
->will($this->returnValue(399));
$stubAdapter->expects($this->any())
->method('getCheckoutCurrency')
->will($this->returnValue('EUR'));
$stubAdapter->expects($this->any())
->method('getConstraintValidator')
->will($this->returnValue(new ConstraintValidator()));
$stubAdapter = $this->generateAdapterStub(399, 'EUR');
$rule1 = new AvailableForTotalAmountManager($stubAdapter);
$operators = array(
@@ -182,19 +184,7 @@ class AvailableForTotalAmountManagerTest extends \PHPUnit_Framework_TestCase
*/
public function testInValidBackOfficeInputValue2()
{
$stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter')
->disableOriginalConstructor()
->getMock();
$stubAdapter->expects($this->any())
->method('getCartTotalPrice')
->will($this->returnValue(399));
$stubAdapter->expects($this->any())
->method('getCheckoutCurrency')
->will($this->returnValue('EUR'));
$stubAdapter->expects($this->any())
->method('getConstraintValidator')
->will($this->returnValue(new ConstraintValidator()));
$stubAdapter = $this->generateAdapterStub(399, 'EUR');
$rule1 = new AvailableForTotalAmountManager($stubAdapter);
$operators = array(
@@ -221,19 +211,7 @@ class AvailableForTotalAmountManagerTest extends \PHPUnit_Framework_TestCase
*/
public function testMatchingRuleInferior()
{
$stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter')
->disableOriginalConstructor()
->getMock();
$stubAdapter->expects($this->any())
->method('getCartTotalPrice')
->will($this->returnValue(399));
$stubAdapter->expects($this->any())
->method('getCheckoutCurrency')
->will($this->returnValue('EUR'));
$stubAdapter->expects($this->any())
->method('getConstraintValidator')
->will($this->returnValue(new ConstraintValidator()));
$stubAdapter = $this->generateAdapterStub(399, 'EUR');
$rule1 = new AvailableForTotalAmountManager($stubAdapter);
$operators = array(
@@ -260,19 +238,7 @@ class AvailableForTotalAmountManagerTest extends \PHPUnit_Framework_TestCase
*/
public function testNotMatchingRuleInferior()
{
$stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter')
->disableOriginalConstructor()
->getMock();
$stubAdapter->expects($this->any())
->method('getCartTotalPrice')
->will($this->returnValue(400));
$stubAdapter->expects($this->any())
->method('getCheckoutCurrency')
->will($this->returnValue('EUR'));
$stubAdapter->expects($this->any())
->method('getConstraintValidator')
->will($this->returnValue(new ConstraintValidator()));
$stubAdapter = $this->generateAdapterStub(400, 'EUR');
$rule1 = new AvailableForTotalAmountManager($stubAdapter);
$operators = array(
@@ -299,19 +265,7 @@ class AvailableForTotalAmountManagerTest extends \PHPUnit_Framework_TestCase
*/
public function testMatchingRuleInferiorEquals()
{
$stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter')
->disableOriginalConstructor()
->getMock();
$stubAdapter->expects($this->any())
->method('getCartTotalPrice')
->will($this->returnValue(400));
$stubAdapter->expects($this->any())
->method('getCheckoutCurrency')
->will($this->returnValue('EUR'));
$stubAdapter->expects($this->any())
->method('getConstraintValidator')
->will($this->returnValue(new ConstraintValidator()));
$stubAdapter = $this->generateAdapterStub(400, 'EUR');
$rule1 = new AvailableForTotalAmountManager($stubAdapter);
$operators = array(
@@ -338,19 +292,7 @@ class AvailableForTotalAmountManagerTest extends \PHPUnit_Framework_TestCase
*/
public function testMatchingRuleInferiorEquals2()
{
$stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter')
->disableOriginalConstructor()
->getMock();
$stubAdapter->expects($this->any())
->method('getCartTotalPrice')
->will($this->returnValue(399));
$stubAdapter->expects($this->any())
->method('getCheckoutCurrency')
->will($this->returnValue('EUR'));
$stubAdapter->expects($this->any())
->method('getConstraintValidator')
->will($this->returnValue(new ConstraintValidator()));
$stubAdapter = $this->generateAdapterStub(399, 'EUR');
$rule1 = new AvailableForTotalAmountManager($stubAdapter);
$operators = array(
@@ -377,19 +319,7 @@ class AvailableForTotalAmountManagerTest extends \PHPUnit_Framework_TestCase
*/
public function testNotMatchingRuleInferiorEquals()
{
$stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter')
->disableOriginalConstructor()
->getMock();
$stubAdapter->expects($this->any())
->method('getCartTotalPrice')
->will($this->returnValue(401));
$stubAdapter->expects($this->any())
->method('getCheckoutCurrency')
->will($this->returnValue('EUR'));
$stubAdapter->expects($this->any())
->method('getConstraintValidator')
->will($this->returnValue(new ConstraintValidator()));
$stubAdapter = $this->generateAdapterStub(401, 'EUR');
$rule1 = new AvailableForTotalAmountManager($stubAdapter);
$operators = array(
@@ -416,19 +346,7 @@ class AvailableForTotalAmountManagerTest extends \PHPUnit_Framework_TestCase
*/
public function testMatchingRuleEqual()
{
$stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter')
->disableOriginalConstructor()
->getMock();
$stubAdapter->expects($this->any())
->method('getCartTotalPrice')
->will($this->returnValue(400));
$stubAdapter->expects($this->any())
->method('getCheckoutCurrency')
->will($this->returnValue('EUR'));
$stubAdapter->expects($this->any())
->method('getConstraintValidator')
->will($this->returnValue(new ConstraintValidator()));
$stubAdapter = $this->generateAdapterStub(400, 'EUR');
$rule1 = new AvailableForTotalAmountManager($stubAdapter);
$operators = array(
@@ -455,19 +373,7 @@ class AvailableForTotalAmountManagerTest extends \PHPUnit_Framework_TestCase
*/
public function testNotMatchingRuleEqual()
{
$stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter')
->disableOriginalConstructor()
->getMock();
$stubAdapter->expects($this->any())
->method('getCartTotalPrice')
->will($this->returnValue(399));
$stubAdapter->expects($this->any())
->method('getCheckoutCurrency')
->will($this->returnValue('EUR'));
$stubAdapter->expects($this->any())
->method('getConstraintValidator')
->will($this->returnValue(new ConstraintValidator()));
$stubAdapter = $this->generateAdapterStub(399, 'EUR');
$rule1 = new AvailableForTotalAmountManager($stubAdapter);
$operators = array(
@@ -494,19 +400,7 @@ class AvailableForTotalAmountManagerTest extends \PHPUnit_Framework_TestCase
*/
public function testMatchingRuleSuperiorEquals()
{
$stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter')
->disableOriginalConstructor()
->getMock();
$stubAdapter->expects($this->any())
->method('getCartTotalPrice')
->will($this->returnValue(401));
$stubAdapter->expects($this->any())
->method('getCheckoutCurrency')
->will($this->returnValue('EUR'));
$stubAdapter->expects($this->any())
->method('getConstraintValidator')
->will($this->returnValue(new ConstraintValidator()));
$stubAdapter = $this->generateAdapterStub(401, 'EUR');
$rule1 = new AvailableForTotalAmountManager($stubAdapter);
$operators = array(
@@ -533,19 +427,7 @@ class AvailableForTotalAmountManagerTest extends \PHPUnit_Framework_TestCase
*/
public function testMatchingRuleSuperiorEquals2()
{
$stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter')
->disableOriginalConstructor()
->getMock();
$stubAdapter->expects($this->any())
->method('getCartTotalPrice')
->will($this->returnValue(400));
$stubAdapter->expects($this->any())
->method('getCheckoutCurrency')
->will($this->returnValue('EUR'));
$stubAdapter->expects($this->any())
->method('getConstraintValidator')
->will($this->returnValue(new ConstraintValidator()));
$stubAdapter = $this->generateAdapterStub(400, 'EUR');
$rule1 = new AvailableForTotalAmountManager($stubAdapter);
$operators = array(
@@ -572,19 +454,7 @@ class AvailableForTotalAmountManagerTest extends \PHPUnit_Framework_TestCase
*/
public function testNotMatchingRuleSuperiorEquals()
{
$stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter')
->disableOriginalConstructor()
->getMock();
$stubAdapter->expects($this->any())
->method('getCartTotalPrice')
->will($this->returnValue(399.00));
$stubAdapter->expects($this->any())
->method('getCheckoutCurrency')
->will($this->returnValue('EUR'));
$stubAdapter->expects($this->any())
->method('getConstraintValidator')
->will($this->returnValue(new ConstraintValidator()));
$stubAdapter = $this->generateAdapterStub(399, 'EUR');
$rule1 = new AvailableForTotalAmountManager($stubAdapter);
$operators = array(
@@ -611,19 +481,7 @@ class AvailableForTotalAmountManagerTest extends \PHPUnit_Framework_TestCase
*/
public function testMatchingRuleSuperior()
{
$stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter')
->disableOriginalConstructor()
->getMock();
$stubAdapter->expects($this->any())
->method('getCartTotalPrice')
->will($this->returnValue(401));
$stubAdapter->expects($this->any())
->method('getCheckoutCurrency')
->will($this->returnValue('EUR'));
$stubAdapter->expects($this->any())
->method('getConstraintValidator')
->will($this->returnValue(new ConstraintValidator()));
$stubAdapter = $this->generateAdapterStub(401, 'EUR');
$rule1 = new AvailableForTotalAmountManager($stubAdapter);
$operators = array(
@@ -650,19 +508,7 @@ class AvailableForTotalAmountManagerTest extends \PHPUnit_Framework_TestCase
*/
public function testNotMatchingRuleSuperior()
{
$stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter')
->disableOriginalConstructor()
->getMock();
$stubAdapter->expects($this->any())
->method('getCartTotalPrice')
->will($this->returnValue(399.00));
$stubAdapter->expects($this->any())
->method('getCheckoutCurrency')
->will($this->returnValue('EUR'));
$stubAdapter->expects($this->any())
->method('getConstraintValidator')
->will($this->returnValue(new ConstraintValidator()));
$stubAdapter = $this->generateAdapterStub(399, 'EUR');
$rule1 = new AvailableForTotalAmountManager($stubAdapter);
$operators = array(
@@ -689,19 +535,7 @@ class AvailableForTotalAmountManagerTest extends \PHPUnit_Framework_TestCase
*/
public function testMatchingRuleCurrency()
{
$stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter')
->disableOriginalConstructor()
->getMock();
$stubAdapter->expects($this->any())
->method('getCartTotalPrice')
->will($this->returnValue(400.00));
$stubAdapter->expects($this->any())
->method('getCheckoutCurrency')
->will($this->returnValue('EUR'));
$stubAdapter->expects($this->any())
->method('getConstraintValidator')
->will($this->returnValue(new ConstraintValidator()));
$stubAdapter = $this->generateAdapterStub(400, 'EUR');
$rule1 = new AvailableForTotalAmountManager($stubAdapter);
$operators = array(
@@ -728,19 +562,7 @@ class AvailableForTotalAmountManagerTest extends \PHPUnit_Framework_TestCase
*/
public function testNotMatchingRuleCurrency()
{
$stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter')
->disableOriginalConstructor()
->getMock();
$stubAdapter->expects($this->any())
->method('getCartTotalPrice')
->will($this->returnValue(400.00));
$stubAdapter->expects($this->any())
->method('getCheckoutCurrency')
->will($this->returnValue('EUR'));
$stubAdapter->expects($this->any())
->method('getConstraintValidator')
->will($this->returnValue(new ConstraintValidator()));
$stubAdapter = $this->generateAdapterStub(400.00, 'EUR');
$rule1 = new AvailableForTotalAmountManager($stubAdapter);
$operators = array(