WIP : Coupon : unit tests on Rules

This commit is contained in:
gmorel
2013-09-16 23:51:47 +02:00
parent 73c0a96979
commit ad48823b48
3 changed files with 236 additions and 149 deletions

View File

@@ -26,19 +26,20 @@ namespace Thelia\Coupon;
use Thelia\Constraint\ConstraintValidator;
use Thelia\Constraint\Rule\AvailableForTotalAmountManager;
use Thelia\Constraint\Rule\Operators;
use Thelia\Exception\InvalidRuleValueException;
/**
* Created by JetBrains PhpStorm.
* Date: 8/19/13
* Time: 3:24 PM
*
* Unit Test AvailableForTotalAmount Class
* Unit Test AvailableForTotalAmountManager Class
*
* @package Constraint
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
*/
class AvailableForTotalAmountTest extends \PHPUnit_Framework_TestCase
class AvailableForTotalAmountManagerTest extends \PHPUnit_Framework_TestCase
{
/** @var CouponAdapterInterface $stubTheliaAdapter */
protected $stubTheliaAdapter = null;
@@ -49,108 +50,168 @@ class AvailableForTotalAmountTest extends \PHPUnit_Framework_TestCase
*/
protected function setUp()
{
// /** @var CouponAdapterInterface $stubTheliaAdapter */
// $this->stubTheliaAdapter = $this->generateValidCouponBaseAdapterMock();
}
// /**
// * Generate valid CouponBaseAdapter
// *
// * @param float $cartTotalPrice Total amount of the current Cart
// *
// * @return CouponAdapterInterface
// */
// protected function generateValidCouponBaseAdapterMock($cartTotalPrice = 421.23)
// {
// /** @var CouponAdapterInterface $stubTheliaAdapter */
// $stubTheliaAdapter = $this->getMock(
// 'Thelia\Coupon\CouponBaseAdapter',
// array('getCartTotalPrice'),
// array()
// );
// $stubTheliaAdapter->expects($this->any())
// ->method('getCartTotalPrice')
// ->will($this->returnValue($cartTotalPrice));
//
// return $stubTheliaAdapter;
// }
/**
* Check if validity test on BackOffice inputs are working
*
* @covers Thelia\Coupon\Rule\AvailableForTotalAmountManager::setValidators
* @expectedException \Thelia\Exception\InvalidRuleOperatorException
*
*/
public function testInValidBackOfficeInputOperator()
{
$stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter')
->disableOriginalConstructor()
->getMock();
// /**
// * Check if validity test on BackOffice inputs are working
// *
// * @covers Thelia\Coupon\Rule\AvailableForTotalAmount::checkBackOfficeInput
// *
// */
// public function testValidBackOfficeInput()
// {
// $adapter = new CouponBaseAdapter();
//
// $validators = array(
// AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator(
// Operators::SUPERIOR,
// new PriceParam(
// $adapter, 421.23, 'EUR'
// )
// )
// );
// $rule = new AvailableForTotalAmount($adapter, $validators);
//
// $expected = true;
// $actual = $rule->checkBackOfficeInput();
// $this->assertEquals($expected, $actual);
// }
$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()));
// /**
// * Check if validity test on BackOffice inputs are working
// *
// * @covers Thelia\Coupon\Rule\AvailableForTotalAmount::checkBackOfficeInput
// * @expectedException \Thelia\Exception\InvalidRuleOperatorException
// *
// */
// public function testInValidBackOfficeInputOperator()
// {
// $adapter = new CouponBaseAdapter();
//
// $validators = array(
// AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator(
// 'X',
// new PriceParam(
// $adapter, 421.23, 'EUR'
// )
// )
// );
//
// $rule = new AvailableForTotalAmount($adapter, $validators);
//
// $expected = false;
// $actual = $rule->checkBackOfficeInput();
// $this->assertEquals($expected, $actual);
// }
$rule1 = new AvailableForTotalAmountManager($stubAdapter);
$operators = array(
AvailableForTotalAmountManager::INPUT1 => Operators::IN,
AvailableForTotalAmountManager::INPUT2 => Operators::EQUAL
);
$values = array(
AvailableForTotalAmountManager::INPUT1 => '400',
AvailableForTotalAmountManager::INPUT2 => 'EUR');
$rule1->setValidatorsFromForm($operators, $values);
// /**
// * Check if validity test on BackOffice inputs are working
// *
// * @covers Thelia\Coupon\Rule\AvailableForTotalAmount::checkBackOfficeInput
// * @expectedException \ErrorException
// *
// */
// public function testInValidBackOfficeInputValue()
// {
// $adapter = $this->generateValidCouponBaseAdapterMock();
//
// $validators = array(
// AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator(
// Operators::SUPERIOR,
// 421
// )
// );
//
// $rule = new AvailableForTotalAmount($adapter, $validators);
//
// $expected = false;
// $actual = $rule->checkBackOfficeInput();
// $this->assertEquals($expected, $actual);
// }
$isValid = $rule1->isMatching();
$expected = true;
$actual =$isValid;
$this->assertEquals($expected, $actual);
}
/**
* Check if validity test on BackOffice inputs are working
*
* @covers Thelia\Coupon\Rule\AvailableForTotalAmountManager::setValidators
* @expectedException \Thelia\Exception\InvalidRuleOperatorException
*
*/
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()));
$rule1 = new AvailableForTotalAmountManager($stubAdapter);
$operators = array(
AvailableForTotalAmountManager::INPUT1 => Operators::SUPERIOR,
AvailableForTotalAmountManager::INPUT2 => Operators::INFERIOR
);
$values = array(
AvailableForTotalAmountManager::INPUT1 => '400',
AvailableForTotalAmountManager::INPUT2 => 'EUR');
$rule1->setValidatorsFromForm($operators, $values);
$isValid = $rule1->isMatching();
$expected = true;
$actual =$isValid;
$this->assertEquals($expected, $actual);
}
/**
* Check if validity test on BackOffice inputs are working
*
* @covers Thelia\Coupon\Rule\AvailableForTotalAmountManager::setValidators
* @expectedException \Thelia\Exception\InvalidRuleValueException
*
*/
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()));
$rule1 = new AvailableForTotalAmountManager($stubAdapter);
$operators = array(
AvailableForTotalAmountManager::INPUT1 => Operators::SUPERIOR,
AvailableForTotalAmountManager::INPUT2 => Operators::EQUAL
);
$values = array(
AvailableForTotalAmountManager::INPUT1 => 'X',
AvailableForTotalAmountManager::INPUT2 => 'EUR');
$rule1->setValidatorsFromForm($operators, $values);
$isValid = $rule1->isMatching();
$expected = true;
$actual =$isValid;
$this->assertEquals($expected, $actual);
}
/**
* Check if validity test on BackOffice inputs are working
*
* @covers Thelia\Coupon\Rule\AvailableForTotalAmountManager::setValidators
* @expectedException \Thelia\Exception\InvalidRuleValueException
*
*/
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()));
$rule1 = new AvailableForTotalAmountManager($stubAdapter);
$operators = array(
AvailableForTotalAmountManager::INPUT1 => Operators::SUPERIOR,
AvailableForTotalAmountManager::INPUT2 => Operators::EQUAL
);
$values = array(
AvailableForTotalAmountManager::INPUT1 => 400,
AvailableForTotalAmountManager::INPUT2 => 'FLA');
$rule1->setValidatorsFromForm($operators, $values);
$isValid = $rule1->isMatching();
$expected = true;
$actual =$isValid;
$this->assertEquals($expected, $actual);
}
/**
* Check if test inferior operator is working

View File

@@ -33,13 +33,13 @@ use Thelia\Constraint\Rule\SerializableRule;
* Date: 8/19/13
* Time: 3:24 PM
*
* Unit Test AvailableForXArticles Class
* Unit Test AvailableForXArticlesManager Class
*
* @package Constraint
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
*/
class AvailableForXArticlesTest extends \PHPUnit_Framework_TestCase
class AvailableForXArticlesManagerTest extends \PHPUnit_Framework_TestCase
{
// /** @var CouponAdapterInterface $stubTheliaAdapter */
@@ -51,30 +51,8 @@ class AvailableForXArticlesTest extends \PHPUnit_Framework_TestCase
*/
protected function setUp()
{
// /** @var CouponAdapterInterface $stubTheliaAdapter */
// $this->stubTheliaAdapter = $this->generateValidCouponBaseAdapterMock();
}
// /**
// * Generate valid CouponBaseAdapter
// *
// * @param int $nbArticlesInCart Total articles in the current Cart
// *
// * @return CouponAdapterInterface
// */
// protected function generateValidCouponBaseAdapterMock($nbArticlesInCart = 4)
// {
// /** @var CouponAdapterInterface $stubTheliaAdapter */
// $stubTheliaAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter')
// ->disableOriginalConstructor()
// ->setMethods(array('getNbArticlesInCart'))
// ->getMock();
// $stubTheliaAdapter->expects($this->any())
// ->method('getNbArticlesInCart')
// ->will($this->returnValue($nbArticlesInCart));
//
// return $stubTheliaAdapter;
// }
}
// /**
// * Check if validity test on BackOffice inputs are working
@@ -100,31 +78,75 @@ class AvailableForXArticlesTest extends \PHPUnit_Framework_TestCase
// $this->assertEquals($expected, $actual);
// }
// /**
// * Check if validity test on BackOffice inputs are working
// *
// * @covers Thelia\Coupon\Rule\AvailableForXArticles::checkBackOfficeInput
// * @expectedException \Thelia\Exception\InvalidRuleValueException
// */
// public function testInValidBackOfficeInputFloat()
// {
// $adapter = $this->stubTheliaAdapter;
//
// $validators = array(
// AvailableForXArticles::PARAM1_QUANTITY => new RuleValidator(
// Operators::SUPERIOR,
// new QuantityParam(
// $adapter,
// 4.5
// )
// )
// );
// $rule = new AvailableForXArticles($adapter, $validators);
//
// $expected = false;
// $actual = $rule->checkBackOfficeInput();
// $this->assertEquals($expected, $actual);
// }
/**
* Check if validity test on BackOffice inputs are working
*
* @covers Thelia\Coupon\Rule\AvailableForXArticles::checkBackOfficeInput
* @expectedException \Thelia\Exception\InvalidRuleOperatorException
*/
public function testInValidBackOfficeInputOperator()
{
$stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter')
->disableOriginalConstructor()
->getMock();
$stubAdapter->expects($this->any())
->method('getNbArticlesInCart')
->will($this->returnValue(4));
$stubAdapter->expects($this->any())
->method('getConstraintValidator')
->will($this->returnValue(new ConstraintValidator()));
$rule1 = new AvailableForXArticlesManager($stubAdapter);
$operators = array(
AvailableForXArticlesManager::INPUT1 => Operators::IN
);
$values = array(
AvailableForXArticlesManager::INPUT1 => 5
);
$rule1->setValidatorsFromForm($operators, $values);
$isValid = $rule1->isMatching();
$expected = true;
$actual =$isValid;
$this->assertEquals($expected, $actual);
}
/**
* Check if validity test on BackOffice inputs are working
*
* @covers Thelia\Coupon\Rule\AvailableForXArticles::checkBackOfficeInput
* @expectedException \Thelia\Exception\InvalidRuleValueException
*/
public function testInValidBackOfficeInputValue()
{
$stubAdapter = $this->getMockBuilder('\Thelia\Coupon\CouponBaseAdapter')
->disableOriginalConstructor()
->getMock();
$stubAdapter->expects($this->any())
->method('getNbArticlesInCart')
->will($this->returnValue(4));
$stubAdapter->expects($this->any())
->method('getConstraintValidator')
->will($this->returnValue(new ConstraintValidator()));
$rule1 = new AvailableForXArticlesManager($stubAdapter);
$operators = array(
AvailableForXArticlesManager::INPUT1 => Operators::SUPERIOR
);
$values = array(
AvailableForXArticlesManager::INPUT1 => 'X'
);
$rule1->setValidatorsFromForm($operators, $values);
$isValid = $rule1->isMatching();
$expected = true;
$actual =$isValid;
$this->assertEquals($expected, $actual);
}
// /**
// * Check if validity test on BackOffice inputs are working

View File

@@ -96,6 +96,10 @@ class OperatorsTest extends \PHPUnit_Framework_TestCase
$actual = Operators::getI18n($stubTranslator, Operators::OUT);
$expected = 'not in';
$this->assertEquals($expected, $actual);
$actual = Operators::getI18n($stubTranslator, 'unexpected operator');
$expected = 'unexpected operator';
$this->assertEquals($expected, $actual);
}