Written coupons unit tests

This commit is contained in:
Franck Allimant
2014-06-17 19:58:56 +02:00
parent 6f12882850
commit cbf2eae842
16 changed files with 4583 additions and 3 deletions

View File

@@ -55,6 +55,8 @@ class StartDate extends ConditionAbstract
*/
public function setValidatorsFromForm(array $operators, array $values)
{
$this->checkComparisonOperatorValue($operators, self::START_DATE);
if (! isset($values[self::START_DATE])) {
$values[self::START_DATE] = time();
}

View File

@@ -0,0 +1,365 @@
<?php
/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* web : http://www.thelia.net */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
/*************************************************************************************/
namespace Thelia\Condition\Implementation;
use Thelia\Condition\ConditionEvaluator;
use Thelia\Condition\Operators;
use Thelia\Condition\SerializableCondition;
use Thelia\Coupon\FacadeInterface;
use Thelia\Model\Category;
use Thelia\Model\Product;
/**
* @package Coupon
* @author Franck Allimant <franck@cqfdev.fr>
*/
class CartContainsCategoriesTest extends \PHPUnit_Framework_TestCase
{
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
}
/**
* Generate adapter stub
*
* @param int $cartTotalPrice Cart total price
* @param string $checkoutCurrency Checkout currency
* @param string $i18nOutput Output from each translation
*
* @return \PHPUnit_Framework_MockObject_MockObject
*/
public function generateFacadeStub($cartTotalPrice = 400, $checkoutCurrency = 'EUR', $i18nOutput = '')
{
$stubFacade = $this->getMockBuilder('\Thelia\Coupon\BaseFacade')
->disableOriginalConstructor()
->getMock();
$stubFacade->expects($this->any())
->method('getCartTotalPrice')
->will($this->returnValue($cartTotalPrice));
$stubFacade->expects($this->any())
->method('getCheckoutCurrency')
->will($this->returnValue($checkoutCurrency));
$stubFacade->expects($this->any())
->method('getConditionEvaluator')
->will($this->returnValue(new ConditionEvaluator()));
$stubTranslator = $this->getMockBuilder('\Thelia\Core\Translation\Translator')
->disableOriginalConstructor()
->getMock();
$stubTranslator->expects($this->any())
->method('trans')
->will($this->returnValue($i18nOutput));
$stubFacade->expects($this->any())
->method('getTranslator')
->will($this->returnValue($stubTranslator));
$category1 = new Category();
$category1->setId(10);
$category2 = new Category();
$category2->setId(20);
$category3 = new Category();
$category3->setId(30);
$product1 = new Product();
$product1->addCategory($category1)->addCategory($category2);
$product2 = new Product();
$product2->addCategory($category3);
$cartItem1Stub = $this->getMockBuilder('\Thelia\Model\CartItem')
->disableOriginalConstructor()
->getMock();
$cartItem1Stub
->expects($this->any())
->method('getProduct')
->will($this->returnValue($product1))
;
$cartItem1Stub
->expects($this->any())
->method('getQuantity')
->will($this->returnValue(1))
;
$cartItem2Stub = $this->getMockBuilder('\Thelia\Model\CartItem')
->disableOriginalConstructor()
->getMock();
$cartItem2Stub
->expects($this->any())
->method('getProduct')
->will($this->returnValue($product2));
$cartItem2Stub
->expects($this->any())
->method('getQuantity')
->will($this->returnValue(2))
;
$cartStub = $this->getMockBuilder('\Thelia\Model\Cart')
->disableOriginalConstructor()
->getMock();
$cartStub
->expects($this->any())
->method('getCartItems')
->will($this->returnValue([$cartItem1Stub, $cartItem2Stub]));
$stubFacade->expects($this->any())
->method('getCart')
->will($this->returnValue($cartStub));
return $stubFacade;
}
/**
* Check if validity test on BackOffice inputs are working
*
* @covers Thelia\Condition\Implementation\CartContainsCategories::setValidators
* @expectedException \Thelia\Exception\InvalidConditionOperatorException
*/
public function testInValidBackOfficeInputOperator()
{
/** @var FacadeInterface $stubFacade */
$stubFacade = $this->generateFacadeStub();
$condition1 = new CartContainsCategories($stubFacade);
$operators = array(
CartContainsCategories::CATEGORIES_LIST => Operators::INFERIOR_OR_EQUAL
);
$values = array(
CartContainsCategories::CATEGORIES_LIST => array()
);
$condition1->setValidatorsFromForm($operators, $values);
$isValid = $condition1->isMatching();
$expected = true;
$actual =$isValid;
$this->assertEquals($expected, $actual);
}
/**
* Check if validity test on BackOffice inputs are working
*
* @covers Thelia\Condition\Implementation\CartContainsCategories::setValidators
* @expectedException \Thelia\Exception\InvalidConditionValueException
*/
public function testInValidBackOfficeInputValue()
{
/** @var FacadeInterface $stubFacade */
$stubFacade = $this->generateFacadeStub();
$condition1 = new CartContainsCategories($stubFacade);
$operators = array(
CartContainsCategories::CATEGORIES_LIST => Operators::IN
);
$values = array(
CartContainsCategories::CATEGORIES_LIST => array()
);
$condition1->setValidatorsFromForm($operators, $values);
}
/**
* Check if test inferior operator is working
*
* @covers Thelia\Condition\Implementation\CartContainsCategories::isMatching
*
*/
public function testMatchingRule()
{
/** @var FacadeInterface $stubFacade */
$stubFacade = $this->generateFacadeStub();
$condition1 = new CartContainsCategories($stubFacade);
$operators = array(
CartContainsCategories::CATEGORIES_LIST => Operators::IN
);
$values = array(
CartContainsCategories::CATEGORIES_LIST => array(10, 20)
);
$condition1->setValidatorsFromForm($operators, $values);
$isValid = $condition1->isMatching();
$expected = true;
$actual =$isValid;
$this->assertEquals($expected, $actual);
}
/**
* Check if test inferior operator is working
*
* @covers Thelia\Condition\Implementation\CartContainsCategories::isMatching
*
*/
public function testNotMatching()
{
/** @var FacadeInterface $stubFacade */
$stubFacade = $this->generateFacadeStub();
$condition1 = new CartContainsCategories($stubFacade);
$operators = array(
CartContainsCategories::CATEGORIES_LIST => Operators::IN
);
$values = array(
CartContainsCategories::CATEGORIES_LIST => array(50, 60)
);
$condition1->setValidatorsFromForm($operators, $values);
$isValid = $condition1->isMatching();
$expected = false;
$actual =$isValid;
$this->assertEquals($expected, $actual);
}
public function testGetSerializableRule()
{
/** @var FacadeInterface $stubFacade */
$stubFacade = $this->generateFacadeStub();
$condition1 = new CartContainsCategories($stubFacade);
$operators = array(
CartContainsCategories::CATEGORIES_LIST => Operators::IN
);
$values = array(
CartContainsCategories::CATEGORIES_LIST => array(50, 60)
);
$condition1->setValidatorsFromForm($operators, $values);
$serializableRule = $condition1->getSerializableCondition();
$expected = new SerializableCondition();
$expected->conditionServiceId = $condition1->getServiceId();
$expected->operators = $operators;
$expected->values = $values;
$actual = $serializableRule;
$this->assertEquals($expected, $actual);
}
/**
* Check getName i18n
*
* @covers Thelia\Condition\Implementation\CartContainsCategories::getName
*
*/
public function testGetName()
{
$stubFacade = $this->generateFacadeStub(399, 'EUR', 'Number of articles in cart');
/** @var FacadeInterface $stubFacade */
$condition1 = new CartContainsCategories($stubFacade);
$actual = $condition1->getName();
$expected = 'Number of articles in cart';
$this->assertEquals($expected, $actual);
}
/**
* Check tooltip i18n
*
* @covers Thelia\Condition\Implementation\CartContainsCategories::getToolTip
*
*/
public function testGetToolTip()
{
$stubFacade = $this->generateFacadeStub(399, 'EUR', 'Sample coupon condition');
/** @var FacadeInterface $stubFacade */
$condition1 = new CartContainsCategories($stubFacade);
$actual = $condition1->getToolTip();
$expected = 'Sample coupon condition';
$this->assertEquals($expected, $actual);
}
/**
* Check validator
*
* @covers Thelia\Condition\Implementation\CartContainsCategories::generateInputs
*
*/
public function testGetValidator()
{
$stubFacade = $this->generateFacadeStub(399, 'EUR', 'Price');
/** @var FacadeInterface $stubFacade */
$condition1 = new CartContainsCategories($stubFacade);
$operators = array(
CartContainsCategories::CATEGORIES_LIST => Operators::IN
);
$values = array(
CartContainsCategories::CATEGORIES_LIST => array(50, 60)
);
$condition1->setValidatorsFromForm($operators, $values);
$actual = $condition1->getValidators();
$validators = array(
'inputs' => array(
CartContainsCategories::CATEGORIES_LIST => array(
'availableOperators' => array(
'in' => 'Price',
'out' => 'Price',
),
'value' => '',
'selectedOperator' => 'in'
)
),
'setOperators' => array(
'categories' => 'in'
),
'setValues' => array(
'categories' => array(50, 60)
)
);
$expected = $validators;
$this->assertEquals($expected, $actual);
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown()
{
}
}

View File

@@ -0,0 +1,365 @@
<?php
/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* web : http://www.thelia.net */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
/*************************************************************************************/
namespace Thelia\Condition\Implementation;
use Thelia\Condition\ConditionEvaluator;
use Thelia\Condition\Operators;
use Thelia\Condition\SerializableCondition;
use Thelia\Coupon\FacadeInterface;
use Thelia\Model\Category;
use Thelia\Model\Product;
/**
* @package Coupon
* @author Franck Allimant <franck@cqfdev.fr>
*/
class CartContainsProductsTest extends \PHPUnit_Framework_TestCase
{
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
}
/**
* Generate adapter stub
*
* @param int $cartTotalPrice Cart total price
* @param string $checkoutCurrency Checkout currency
* @param string $i18nOutput Output from each translation
*
* @return \PHPUnit_Framework_MockObject_MockObject
*/
public function generateFacadeStub($cartTotalPrice = 400, $checkoutCurrency = 'EUR', $i18nOutput = '')
{
$stubFacade = $this->getMockBuilder('\Thelia\Coupon\BaseFacade')
->disableOriginalConstructor()
->getMock();
$stubFacade->expects($this->any())
->method('getCartTotalPrice')
->will($this->returnValue($cartTotalPrice));
$stubFacade->expects($this->any())
->method('getCheckoutCurrency')
->will($this->returnValue($checkoutCurrency));
$stubFacade->expects($this->any())
->method('getConditionEvaluator')
->will($this->returnValue(new ConditionEvaluator()));
$stubTranslator = $this->getMockBuilder('\Thelia\Core\Translation\Translator')
->disableOriginalConstructor()
->getMock();
$stubTranslator->expects($this->any())
->method('trans')
->will($this->returnValue($i18nOutput));
$stubFacade->expects($this->any())
->method('getTranslator')
->will($this->returnValue($stubTranslator));
$category1 = new Category();
$category1->setId(10);
$category2 = new Category();
$category2->setId(20);
$category3 = new Category();
$category3->setId(30);
$product1 = new Product();
$product1->setId(10)->addCategory($category1)->addCategory($category2);
$product2 = new Product();
$product2->setId(20)->addCategory($category3);
$cartItem1Stub = $this->getMockBuilder('\Thelia\Model\CartItem')
->disableOriginalConstructor()
->getMock();
$cartItem1Stub
->expects($this->any())
->method('getProduct')
->will($this->returnValue($product1))
;
$cartItem1Stub
->expects($this->any())
->method('getQuantity')
->will($this->returnValue(1))
;
$cartItem2Stub = $this->getMockBuilder('\Thelia\Model\CartItem')
->disableOriginalConstructor()
->getMock();
$cartItem2Stub
->expects($this->any())
->method('getProduct')
->will($this->returnValue($product2));
$cartItem2Stub
->expects($this->any())
->method('getQuantity')
->will($this->returnValue(2))
;
$cartStub = $this->getMockBuilder('\Thelia\Model\Cart')
->disableOriginalConstructor()
->getMock();
$cartStub
->expects($this->any())
->method('getCartItems')
->will($this->returnValue([$cartItem1Stub, $cartItem2Stub]));
$stubFacade->expects($this->any())
->method('getCart')
->will($this->returnValue($cartStub));
return $stubFacade;
}
/**
* Check if validity test on BackOffice inputs are working
*
* @covers Thelia\Condition\Implementation\CartContainsProducts::setValidators
* @expectedException \Thelia\Exception\InvalidConditionOperatorException
*/
public function testInValidBackOfficeInputOperator()
{
/** @var FacadeInterface $stubFacade */
$stubFacade = $this->generateFacadeStub();
$condition1 = new CartContainsProducts($stubFacade);
$operators = array(
CartContainsProducts::PRODUCTS_LIST => Operators::INFERIOR_OR_EQUAL
);
$values = array(
CartContainsProducts::PRODUCTS_LIST => array()
);
$condition1->setValidatorsFromForm($operators, $values);
$isValid = $condition1->isMatching();
$expected = true;
$actual =$isValid;
$this->assertEquals($expected, $actual);
}
/**
* Check if validity test on BackOffice inputs are working
*
* @covers Thelia\Condition\Implementation\CartContainsProducts::setValidators
* @expectedException \Thelia\Exception\InvalidConditionValueException
*/
public function testInValidBackOfficeInputValue()
{
/** @var FacadeInterface $stubFacade */
$stubFacade = $this->generateFacadeStub();
$condition1 = new CartContainsProducts($stubFacade);
$operators = array(
CartContainsProducts::PRODUCTS_LIST => Operators::IN
);
$values = array(
CartContainsProducts::PRODUCTS_LIST => array()
);
$condition1->setValidatorsFromForm($operators, $values);
}
/**
* Check if test inferior operator is working
*
* @covers Thelia\Condition\Implementation\CartContainsProducts::isMatching
*
*/
public function testMatchingRule()
{
/** @var FacadeInterface $stubFacade */
$stubFacade = $this->generateFacadeStub();
$condition1 = new CartContainsProducts($stubFacade);
$operators = array(
CartContainsProducts::PRODUCTS_LIST => Operators::IN
);
$values = array(
CartContainsProducts::PRODUCTS_LIST => array(10, 20)
);
$condition1->setValidatorsFromForm($operators, $values);
$isValid = $condition1->isMatching();
$expected = true;
$actual =$isValid;
$this->assertEquals($expected, $actual);
}
/**
* Check if test inferior operator is working
*
* @covers Thelia\Condition\Implementation\CartContainsProducts::isMatching
*
*/
public function testNotMatching()
{
/** @var FacadeInterface $stubFacade */
$stubFacade = $this->generateFacadeStub();
$condition1 = new CartContainsProducts($stubFacade);
$operators = array(
CartContainsProducts::PRODUCTS_LIST => Operators::IN
);
$values = array(
CartContainsProducts::PRODUCTS_LIST => array(50, 60)
);
$condition1->setValidatorsFromForm($operators, $values);
$isValid = $condition1->isMatching();
$expected = false;
$actual =$isValid;
$this->assertEquals($expected, $actual);
}
public function testGetSerializableRule()
{
/** @var FacadeInterface $stubFacade */
$stubFacade = $this->generateFacadeStub();
$condition1 = new CartContainsProducts($stubFacade);
$operators = array(
CartContainsProducts::PRODUCTS_LIST => Operators::IN
);
$values = array(
CartContainsProducts::PRODUCTS_LIST => array(50, 60)
);
$condition1->setValidatorsFromForm($operators, $values);
$serializableRule = $condition1->getSerializableCondition();
$expected = new SerializableCondition();
$expected->conditionServiceId = $condition1->getServiceId();
$expected->operators = $operators;
$expected->values = $values;
$actual = $serializableRule;
$this->assertEquals($expected, $actual);
}
/**
* Check getName i18n
*
* @covers Thelia\Condition\Implementation\CartContainsProducts::getName
*
*/
public function testGetName()
{
$stubFacade = $this->generateFacadeStub(399, 'EUR', 'Number of articles in cart');
/** @var FacadeInterface $stubFacade */
$condition1 = new CartContainsProducts($stubFacade);
$actual = $condition1->getName();
$expected = 'Number of articles in cart';
$this->assertEquals($expected, $actual);
}
/**
* Check tooltip i18n
*
* @covers Thelia\Condition\Implementation\CartContainsProducts::getToolTip
*
*/
public function testGetToolTip()
{
$stubFacade = $this->generateFacadeStub(399, 'EUR', 'Sample coupon condition');
/** @var FacadeInterface $stubFacade */
$condition1 = new CartContainsProducts($stubFacade);
$actual = $condition1->getToolTip();
$expected = 'Sample coupon condition';
$this->assertEquals($expected, $actual);
}
/**
* Check validator
*
* @covers Thelia\Condition\Implementation\CartContainsProducts::generateInputs
*
*/
public function testGetValidator()
{
$stubFacade = $this->generateFacadeStub(399, 'EUR', 'Price');
/** @var FacadeInterface $stubFacade */
$condition1 = new CartContainsProducts($stubFacade);
$operators = array(
CartContainsProducts::PRODUCTS_LIST => Operators::IN
);
$values = array(
CartContainsProducts::PRODUCTS_LIST => array(50, 60)
);
$condition1->setValidatorsFromForm($operators, $values);
$actual = $condition1->getValidators();
$validators = array(
'inputs' => array(
CartContainsProducts::PRODUCTS_LIST => array(
'availableOperators' => array(
'in' => 'Price',
'out' => 'Price',
),
'value' => '',
'selectedOperator' => 'in'
)
),
'setOperators' => array(
'products' => 'in'
),
'setValues' => array(
'products' => array(50, 60)
)
);
$expected = $validators;
$this->assertEquals($expected, $actual);
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown()
{
}
}

View File

@@ -0,0 +1,304 @@
<?php
/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* web : http://www.thelia.net */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
/*************************************************************************************/
namespace Thelia\Condition\Implementation;
use Thelia\Condition\ConditionEvaluator;
use Thelia\Condition\Operators;
use Thelia\Condition\SerializableCondition;
use Thelia\Coupon\FacadeInterface;
use Thelia\Model\Customer;
/**
* @package Coupon
* @author Franck Allimant <franck@cqfdev.fr>
*/
class ForSomeCustomersTest extends \PHPUnit_Framework_TestCase
{
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
}
/**
* Generate adapter stub
*
* @param int $cartTotalPrice Cart total price
* @param string $checkoutCurrency Checkout currency
* @param string $i18nOutput Output from each translation
*
* @return \PHPUnit_Framework_MockObject_MockObject
*/
public function generateFacadeStub($cartTotalPrice = 400, $checkoutCurrency = 'EUR', $i18nOutput = '')
{
$stubFacade = $this->getMockBuilder('\Thelia\Coupon\BaseFacade')
->disableOriginalConstructor()
->getMock();
$customer = new Customer();
$customer->setId(10);
$stubFacade->expects($this->any())
->method('getCustomer')
->will($this->returnValue($customer));
$stubFacade->expects($this->any())
->method('getConditionEvaluator')
->will($this->returnValue(new ConditionEvaluator()));
$stubTranslator = $this->getMockBuilder('\Thelia\Core\Translation\Translator')
->disableOriginalConstructor()
->getMock();
$stubTranslator->expects($this->any())
->method('trans')
->will($this->returnValue($i18nOutput));
$stubFacade->expects($this->any())
->method('getTranslator')
->will($this->returnValue($stubTranslator));
return $stubFacade;
}
/**
* Check if validity test on BackOffice inputs are working
*
* @covers Thelia\Condition\Implementation\ForSomeCustomers::setValidators
* @expectedException \Thelia\Exception\InvalidConditionOperatorException
*/
public function testInValidBackOfficeInputOperator()
{
/** @var FacadeInterface $stubFacade */
$stubFacade = $this->generateFacadeStub();
$condition1 = new ForSomeCustomers($stubFacade);
$operators = array(
ForSomeCustomers::CUSTOMERS_LIST => Operators::INFERIOR_OR_EQUAL
);
$values = array(
ForSomeCustomers::CUSTOMERS_LIST => array()
);
$condition1->setValidatorsFromForm($operators, $values);
$isValid = $condition1->isMatching();
$expected = true;
$actual =$isValid;
$this->assertEquals($expected, $actual);
}
/**
* Check if validity test on BackOffice inputs are working
*
* @covers Thelia\Condition\Implementation\ForSomeCustomers::setValidators
* @expectedException \Thelia\Exception\InvalidConditionValueException
*/
public function testInValidBackOfficeInputValue()
{
/** @var FacadeInterface $stubFacade */
$stubFacade = $this->generateFacadeStub();
$condition1 = new ForSomeCustomers($stubFacade);
$operators = array(
ForSomeCustomers::CUSTOMERS_LIST => Operators::IN
);
$values = array(
ForSomeCustomers::CUSTOMERS_LIST => array()
);
$condition1->setValidatorsFromForm($operators, $values);
}
/**
* Check if test inferior operator is working
*
* @covers Thelia\Condition\Implementation\ForSomeCustomers::isMatching
*
*/
public function testMatchingRule()
{
/** @var FacadeInterface $stubFacade */
$stubFacade = $this->generateFacadeStub();
$condition1 = new ForSomeCustomers($stubFacade);
$operators = array(
ForSomeCustomers::CUSTOMERS_LIST => Operators::IN
);
$values = array(
ForSomeCustomers::CUSTOMERS_LIST => array(10, 20)
);
$condition1->setValidatorsFromForm($operators, $values);
$isValid = $condition1->isMatching();
$expected = true;
$actual =$isValid;
$this->assertEquals($expected, $actual);
}
/**
* Check if test inferior operator is working
*
* @covers Thelia\Condition\Implementation\ForSomeCustomers::isMatching
*
*/
public function testNotMatching()
{
/** @var FacadeInterface $stubFacade */
$stubFacade = $this->generateFacadeStub();
$condition1 = new ForSomeCustomers($stubFacade);
$operators = array(
ForSomeCustomers::CUSTOMERS_LIST => Operators::IN
);
$values = array(
ForSomeCustomers::CUSTOMERS_LIST => array(50, 60)
);
$condition1->setValidatorsFromForm($operators, $values);
$isValid = $condition1->isMatching();
$expected = false;
$actual =$isValid;
$this->assertEquals($expected, $actual);
}
public function testGetSerializableRule()
{
/** @var FacadeInterface $stubFacade */
$stubFacade = $this->generateFacadeStub();
$condition1 = new ForSomeCustomers($stubFacade);
$operators = array(
ForSomeCustomers::CUSTOMERS_LIST => Operators::IN
);
$values = array(
ForSomeCustomers::CUSTOMERS_LIST => array(50, 60)
);
$condition1->setValidatorsFromForm($operators, $values);
$serializableRule = $condition1->getSerializableCondition();
$expected = new SerializableCondition();
$expected->conditionServiceId = $condition1->getServiceId();
$expected->operators = $operators;
$expected->values = $values;
$actual = $serializableRule;
$this->assertEquals($expected, $actual);
}
/**
* Check getName i18n
*
* @covers Thelia\Condition\Implementation\ForSomeCustomers::getName
*
*/
public function testGetName()
{
$stubFacade = $this->generateFacadeStub(399, 'EUR', 'Number of articles in cart');
/** @var FacadeInterface $stubFacade */
$condition1 = new ForSomeCustomers($stubFacade);
$actual = $condition1->getName();
$expected = 'Number of articles in cart';
$this->assertEquals($expected, $actual);
}
/**
* Check tooltip i18n
*
* @covers Thelia\Condition\Implementation\ForSomeCustomers::getToolTip
*
*/
public function testGetToolTip()
{
$stubFacade = $this->generateFacadeStub(399, 'EUR', 'Sample coupon condition');
/** @var FacadeInterface $stubFacade */
$condition1 = new ForSomeCustomers($stubFacade);
$actual = $condition1->getToolTip();
$expected = 'Sample coupon condition';
$this->assertEquals($expected, $actual);
}
/**
* Check validator
*
* @covers Thelia\Condition\Implementation\ForSomeCustomers::generateInputs
*
*/
public function testGetValidator()
{
$stubFacade = $this->generateFacadeStub(399, 'EUR', 'Price');
/** @var FacadeInterface $stubFacade */
$condition1 = new ForSomeCustomers($stubFacade);
$operators = array(
ForSomeCustomers::CUSTOMERS_LIST => Operators::IN
);
$values = array(
ForSomeCustomers::CUSTOMERS_LIST => array(50, 60)
);
$condition1->setValidatorsFromForm($operators, $values);
$actual = $condition1->getValidators();
$validators = array(
'inputs' => array(
ForSomeCustomers::CUSTOMERS_LIST => array(
'availableOperators' => array(
'in' => 'Price',
'out' => 'Price',
),
'value' => '',
'selectedOperator' => 'in'
)
),
'setOperators' => array(
'customers' => 'in'
),
'setValues' => array(
'customers' => array(50, 60)
)
);
$expected = $validators;
$this->assertEquals($expected, $actual);
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown()
{
}
}

View File

@@ -0,0 +1,312 @@
<?php
/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* web : http://www.thelia.net */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
/*************************************************************************************/
namespace Thelia\Condition\Implementation;
use Thelia\Condition\ConditionEvaluator;
use Thelia\Condition\Operators;
use Thelia\Condition\SerializableCondition;
use Thelia\Coupon\FacadeInterface;
use Thelia\Model\Address;
/**
* @package Coupon
* @author Franck Allimant <franck@cqfdev.fr>
*/
class MatchBillingCountriesTest extends \PHPUnit_Framework_TestCase
{
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
}
/**
* Generate adapter stub
*
* @param int $cartTotalPrice Cart total price
* @param string $checkoutCurrency Checkout currency
* @param string $i18nOutput Output from each translation
*
* @return \PHPUnit_Framework_MockObject_MockObject
*/
public function generateFacadeStub($cartTotalPrice = 400, $checkoutCurrency = 'EUR', $i18nOutput = '')
{
$stubFacade = $this->getMockBuilder('\Thelia\Coupon\BaseFacade')
->disableOriginalConstructor()
->getMock();
$address = new Address();
$address->setCountryId(10);
$stubCustomer = $this->getMockBuilder('\Thelia\Model\Customer')
->disableOriginalConstructor()
->getMock();
$stubCustomer->expects($this->any())
->method('getDefaultAddress')
->will($this->returnValue($address));
$stubFacade->expects($this->any())
->method('getCustomer')
->will($this->returnValue($stubCustomer));
$stubFacade->expects($this->any())
->method('getConditionEvaluator')
->will($this->returnValue(new ConditionEvaluator()));
$stubTranslator = $this->getMockBuilder('\Thelia\Core\Translation\Translator')
->disableOriginalConstructor()
->getMock();
$stubTranslator->expects($this->any())
->method('trans')
->will($this->returnValue($i18nOutput));
$stubFacade->expects($this->any())
->method('getTranslator')
->will($this->returnValue($stubTranslator));
return $stubFacade;
}
/**
* Check if validity test on BackOffice inputs are working
*
* @covers Thelia\Condition\Implementation\MatchBillingCountries::setValidators
* @expectedException \Thelia\Exception\InvalidConditionOperatorException
*/
public function testInValidBackOfficeInputOperator()
{
/** @var FacadeInterface $stubFacade */
$stubFacade = $this->generateFacadeStub();
$condition1 = new MatchBillingCountries($stubFacade);
$operators = array(
MatchBillingCountries::COUNTRIES_LIST => Operators::INFERIOR_OR_EQUAL
);
$values = array(
MatchBillingCountries::COUNTRIES_LIST => array()
);
$condition1->setValidatorsFromForm($operators, $values);
$isValid = $condition1->isMatching();
$expected = true;
$actual =$isValid;
$this->assertEquals($expected, $actual);
}
/**
* Check if validity test on BackOffice inputs are working
*
* @covers Thelia\Condition\Implementation\MatchBillingCountries::setValidators
* @expectedException \Thelia\Exception\InvalidConditionValueException
*/
public function testInValidBackOfficeInputValue()
{
/** @var FacadeInterface $stubFacade */
$stubFacade = $this->generateFacadeStub();
$condition1 = new MatchBillingCountries($stubFacade);
$operators = array(
MatchBillingCountries::COUNTRIES_LIST => Operators::IN
);
$values = array(
MatchBillingCountries::COUNTRIES_LIST => array()
);
$condition1->setValidatorsFromForm($operators, $values);
}
/**
* Check if test inferior operator is working
*
* @covers Thelia\Condition\Implementation\MatchBillingCountries::isMatching
*
*/
public function testMatchingRule()
{
/** @var FacadeInterface $stubFacade */
$stubFacade = $this->generateFacadeStub();
$condition1 = new MatchBillingCountries($stubFacade);
$operators = array(
MatchBillingCountries::COUNTRIES_LIST => Operators::IN
);
$values = array(
MatchBillingCountries::COUNTRIES_LIST => array(10, 20)
);
$condition1->setValidatorsFromForm($operators, $values);
$isValid = $condition1->isMatching();
$expected = true;
$actual =$isValid;
$this->assertEquals($expected, $actual);
}
/**
* Check if test inferior operator is working
*
* @covers Thelia\Condition\Implementation\MatchBillingCountries::isMatching
*
*/
public function testNotMatching()
{
/** @var FacadeInterface $stubFacade */
$stubFacade = $this->generateFacadeStub();
$condition1 = new MatchBillingCountries($stubFacade);
$operators = array(
MatchBillingCountries::COUNTRIES_LIST => Operators::IN
);
$values = array(
MatchBillingCountries::COUNTRIES_LIST => array(50, 60)
);
$condition1->setValidatorsFromForm($operators, $values);
$isValid = $condition1->isMatching();
$expected = false;
$actual =$isValid;
$this->assertEquals($expected, $actual);
}
public function testGetSerializableRule()
{
/** @var FacadeInterface $stubFacade */
$stubFacade = $this->generateFacadeStub();
$condition1 = new MatchBillingCountries($stubFacade);
$operators = array(
MatchBillingCountries::COUNTRIES_LIST => Operators::IN
);
$values = array(
MatchBillingCountries::COUNTRIES_LIST => array(50, 60)
);
$condition1->setValidatorsFromForm($operators, $values);
$serializableRule = $condition1->getSerializableCondition();
$expected = new SerializableCondition();
$expected->conditionServiceId = $condition1->getServiceId();
$expected->operators = $operators;
$expected->values = $values;
$actual = $serializableRule;
$this->assertEquals($expected, $actual);
}
/**
* Check getName i18n
*
* @covers Thelia\Condition\Implementation\MatchBillingCountries::getName
*
*/
public function testGetName()
{
$stubFacade = $this->generateFacadeStub(399, 'EUR', 'Number of articles in cart');
/** @var FacadeInterface $stubFacade */
$condition1 = new MatchBillingCountries($stubFacade);
$actual = $condition1->getName();
$expected = 'Number of articles in cart';
$this->assertEquals($expected, $actual);
}
/**
* Check tooltip i18n
*
* @covers Thelia\Condition\Implementation\MatchBillingCountries::getToolTip
*
*/
public function testGetToolTip()
{
$stubFacade = $this->generateFacadeStub(399, 'EUR', 'Sample coupon condition');
/** @var FacadeInterface $stubFacade */
$condition1 = new MatchBillingCountries($stubFacade);
$actual = $condition1->getToolTip();
$expected = 'Sample coupon condition';
$this->assertEquals($expected, $actual);
}
/**
* Check validator
*
* @covers Thelia\Condition\Implementation\MatchBillingCountries::generateInputs
*
*/
public function testGetValidator()
{
$stubFacade = $this->generateFacadeStub(399, 'EUR', 'Price');
/** @var FacadeInterface $stubFacade */
$condition1 = new MatchBillingCountries($stubFacade);
$operators = array(
MatchBillingCountries::COUNTRIES_LIST => Operators::IN
);
$values = array(
MatchBillingCountries::COUNTRIES_LIST => array(50, 60)
);
$condition1->setValidatorsFromForm($operators, $values);
$actual = $condition1->getValidators();
$validators = array(
'inputs' => array(
MatchBillingCountries::COUNTRIES_LIST => array(
'availableOperators' => array(
'in' => 'Price',
'out' => 'Price',
),
'value' => '',
'selectedOperator' => 'in'
)
),
'setOperators' => array(
'countries' => 'in'
),
'setValues' => array(
'countries' => array(50, 60)
)
);
$expected = $validators;
$this->assertEquals($expected, $actual);
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown()
{
}
}

View File

@@ -0,0 +1,304 @@
<?php
/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* web : http://www.thelia.net */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
/*************************************************************************************/
namespace Thelia\Condition\Implementation;
use Thelia\Condition\ConditionEvaluator;
use Thelia\Condition\Operators;
use Thelia\Condition\SerializableCondition;
use Thelia\Coupon\FacadeInterface;
use Thelia\Model\Address;
/**
* @package Coupon
* @author Franck Allimant <franck@cqfdev.fr>
*/
class MatchDeliveryCountriesTest extends \PHPUnit_Framework_TestCase
{
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
}
/**
* Generate adapter stub
*
* @param int $cartTotalPrice Cart total price
* @param string $checkoutCurrency Checkout currency
* @param string $i18nOutput Output from each translation
*
* @return \PHPUnit_Framework_MockObject_MockObject
*/
public function generateFacadeStub($cartTotalPrice = 400, $checkoutCurrency = 'EUR', $i18nOutput = '')
{
$stubFacade = $this->getMockBuilder('\Thelia\Coupon\BaseFacade')
->disableOriginalConstructor()
->getMock();
$address = new Address();
$address->setCountryId(10);
$stubFacade->expects($this->any())
->method('getDeliveryAddress')
->will($this->returnValue($address));
$stubFacade->expects($this->any())
->method('getConditionEvaluator')
->will($this->returnValue(new ConditionEvaluator()));
$stubTranslator = $this->getMockBuilder('\Thelia\Core\Translation\Translator')
->disableOriginalConstructor()
->getMock();
$stubTranslator->expects($this->any())
->method('trans')
->will($this->returnValue($i18nOutput));
$stubFacade->expects($this->any())
->method('getTranslator')
->will($this->returnValue($stubTranslator));
return $stubFacade;
}
/**
* Check if validity test on BackOffice inputs are working
*
* @covers Thelia\Condition\Implementation\MatchDeliveryCountries::setValidators
* @expectedException \Thelia\Exception\InvalidConditionOperatorException
*/
public function testInValidBackOfficeInputOperator()
{
/** @var FacadeInterface $stubFacade */
$stubFacade = $this->generateFacadeStub();
$condition1 = new MatchDeliveryCountries($stubFacade);
$operators = array(
MatchDeliveryCountries::COUNTRIES_LIST => Operators::INFERIOR_OR_EQUAL
);
$values = array(
MatchDeliveryCountries::COUNTRIES_LIST => array()
);
$condition1->setValidatorsFromForm($operators, $values);
$isValid = $condition1->isMatching();
$expected = true;
$actual =$isValid;
$this->assertEquals($expected, $actual);
}
/**
* Check if validity test on BackOffice inputs are working
*
* @covers Thelia\Condition\Implementation\MatchDeliveryCountries::setValidators
* @expectedException \Thelia\Exception\InvalidConditionValueException
*/
public function testInValidBackOfficeInputValue()
{
/** @var FacadeInterface $stubFacade */
$stubFacade = $this->generateFacadeStub();
$condition1 = new MatchDeliveryCountries($stubFacade);
$operators = array(
MatchDeliveryCountries::COUNTRIES_LIST => Operators::IN
);
$values = array(
MatchDeliveryCountries::COUNTRIES_LIST => array()
);
$condition1->setValidatorsFromForm($operators, $values);
}
/**
* Check if test inferior operator is working
*
* @covers Thelia\Condition\Implementation\MatchDeliveryCountries::isMatching
*
*/
public function testMatchingRule()
{
/** @var FacadeInterface $stubFacade */
$stubFacade = $this->generateFacadeStub();
$condition1 = new MatchDeliveryCountries($stubFacade);
$operators = array(
MatchDeliveryCountries::COUNTRIES_LIST => Operators::IN
);
$values = array(
MatchDeliveryCountries::COUNTRIES_LIST => array(10, 20)
);
$condition1->setValidatorsFromForm($operators, $values);
$isValid = $condition1->isMatching();
$expected = true;
$actual =$isValid;
$this->assertEquals($expected, $actual);
}
/**
* Check if test inferior operator is working
*
* @covers Thelia\Condition\Implementation\MatchDeliveryCountries::isMatching
*
*/
public function testNotMatching()
{
/** @var FacadeInterface $stubFacade */
$stubFacade = $this->generateFacadeStub();
$condition1 = new MatchDeliveryCountries($stubFacade);
$operators = array(
MatchDeliveryCountries::COUNTRIES_LIST => Operators::IN
);
$values = array(
MatchDeliveryCountries::COUNTRIES_LIST => array(50, 60)
);
$condition1->setValidatorsFromForm($operators, $values);
$isValid = $condition1->isMatching();
$expected = false;
$actual =$isValid;
$this->assertEquals($expected, $actual);
}
public function testGetSerializableRule()
{
/** @var FacadeInterface $stubFacade */
$stubFacade = $this->generateFacadeStub();
$condition1 = new MatchDeliveryCountries($stubFacade);
$operators = array(
MatchDeliveryCountries::COUNTRIES_LIST => Operators::IN
);
$values = array(
MatchDeliveryCountries::COUNTRIES_LIST => array(50, 60)
);
$condition1->setValidatorsFromForm($operators, $values);
$serializableRule = $condition1->getSerializableCondition();
$expected = new SerializableCondition();
$expected->conditionServiceId = $condition1->getServiceId();
$expected->operators = $operators;
$expected->values = $values;
$actual = $serializableRule;
$this->assertEquals($expected, $actual);
}
/**
* Check getName i18n
*
* @covers Thelia\Condition\Implementation\MatchDeliveryCountries::getName
*
*/
public function testGetName()
{
$stubFacade = $this->generateFacadeStub(399, 'EUR', 'Number of articles in cart');
/** @var FacadeInterface $stubFacade */
$condition1 = new MatchDeliveryCountries($stubFacade);
$actual = $condition1->getName();
$expected = 'Number of articles in cart';
$this->assertEquals($expected, $actual);
}
/**
* Check tooltip i18n
*
* @covers Thelia\Condition\Implementation\MatchDeliveryCountries::getToolTip
*
*/
public function testGetToolTip()
{
$stubFacade = $this->generateFacadeStub(399, 'EUR', 'Sample coupon condition');
/** @var FacadeInterface $stubFacade */
$condition1 = new MatchDeliveryCountries($stubFacade);
$actual = $condition1->getToolTip();
$expected = 'Sample coupon condition';
$this->assertEquals($expected, $actual);
}
/**
* Check validator
*
* @covers Thelia\Condition\Implementation\MatchDeliveryCountries::generateInputs
*
*/
public function testGetValidator()
{
$stubFacade = $this->generateFacadeStub(399, 'EUR', 'Price');
/** @var FacadeInterface $stubFacade */
$condition1 = new MatchDeliveryCountries($stubFacade);
$operators = array(
MatchDeliveryCountries::COUNTRIES_LIST => Operators::IN
);
$values = array(
MatchDeliveryCountries::COUNTRIES_LIST => array(50, 60)
);
$condition1->setValidatorsFromForm($operators, $values);
$actual = $condition1->getValidators();
$validators = array(
'inputs' => array(
MatchDeliveryCountries::COUNTRIES_LIST => array(
'availableOperators' => array(
'in' => 'Price',
'out' => 'Price',
),
'value' => '',
'selectedOperator' => 'in'
)
),
'setOperators' => array(
'countries' => 'in'
),
'setValues' => array(
'countries' => array(50, 60)
)
);
$expected = $validators;
$this->assertEquals($expected, $actual);
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown()
{
}
}

View File

@@ -13,7 +13,6 @@
namespace Thelia\Condition\Implementation;
use Thelia\Condition\ConditionEvaluator;
use Thelia\Coupon\FacadeInterface;
use Thelia\Model\Currency;

View File

@@ -12,13 +12,12 @@
namespace Thelia\Tests\Condition\Implementation;
use Thelia\Condition\ConditionCollection;
use Thelia\Condition\ConditionEvaluator;
use Thelia\Condition\ConditionFactory;
use Thelia\Condition\Implementation\MatchForTotalAmount;
use Thelia\Condition\Operators;
use Thelia\Condition\ConditionCollection;
use Thelia\Coupon\FacadeInterface;
use Thelia\Model\Currency;
use Thelia\Model\CurrencyQuery;

View File

@@ -0,0 +1,329 @@
<?php
/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* web : http://www.thelia.net */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
/*************************************************************************************/
namespace Thelia\Condition\Implementation;
use Thelia\Condition\ConditionEvaluator;
use Thelia\Condition\Operators;
use Thelia\Condition\SerializableCondition;
use Thelia\Coupon\FacadeInterface;
use Thelia\Model\Address;
use Thelia\Model\Lang;
/**
* @package Coupon
* @author Franck Allimant <franck@cqfdev.fr>
*/
class StartDateTest extends \PHPUnit_Framework_TestCase
{
var $startDate;
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
$this->startDate = time() - 2000;
}
/**
* Generate adapter stub
*
* @param int $cartTotalPrice Cart total price
* @param string $checkoutCurrency Checkout currency
* @param string $i18nOutput Output from each translation
*
* @return \PHPUnit_Framework_MockObject_MockObject
*/
public function generateFacadeStub($cartTotalPrice = 400, $checkoutCurrency = 'EUR', $i18nOutput = '')
{
$stubFacade = $this->getMockBuilder('\Thelia\Coupon\BaseFacade')
->disableOriginalConstructor()
->getMock();
$address = new Address();
$address->setCountryId(10);
$stubFacade->expects($this->any())
->method('getDeliveryAddress')
->will($this->returnValue($address));
$stubFacade->expects($this->any())
->method('getConditionEvaluator')
->will($this->returnValue(new ConditionEvaluator()));
$stubTranslator = $this->getMockBuilder('\Thelia\Core\Translation\Translator')
->disableOriginalConstructor()
->getMock();
$stubTranslator->expects($this->any())
->method('trans')
->will($this->returnValue($i18nOutput));
$stubFacade->expects($this->any())
->method('getTranslator')
->will($this->returnValue($stubTranslator));
$lang = new Lang();
$lang->setDateFormat("d/m/Y");
$stubSession = $this->getMockBuilder('\Thelia\Core\HttpFoundation\Session\Session')
->disableOriginalConstructor()
->getMock();
$stubSession->expects($this->any())
->method('getLang')
->will($this->returnValue($lang));
$stubRequest = $this->getMockBuilder('\Thelia\Core\HttpFoundation\Request')
->disableOriginalConstructor()
->getMock();
$stubRequest->expects($this->any())
->method('getSession')
->will($this->returnValue($stubSession));
$stubFacade->expects($this->any())
->method('getRequest')
->will($this->returnValue($stubRequest));
return $stubFacade;
}
/**
* Check if validity test on BackOffice inputs are working
*
* @covers Thelia\Condition\Implementation\StartDate::setValidators
* @expectedException \Thelia\Exception\InvalidConditionOperatorException
*/
public function testInValidBackOfficeInputOperator()
{
/** @var FacadeInterface $stubFacade */
$stubFacade = $this->generateFacadeStub();
$condition1 = new StartDate($stubFacade);
$operators = array(
StartDate::START_DATE => 'petite licorne'
);
$values = array(
StartDate::START_DATE => $this->startDate
);
$condition1->setValidatorsFromForm($operators, $values);
$isValid = $condition1->isMatching();
$expected = true;
$actual =$isValid;
$this->assertEquals($expected, $actual);
}
/**
* Check if validity test on BackOffice inputs are working
*
* @covers Thelia\Condition\Implementation\StartDate::setValidators
* @expectedException \Thelia\Exception\InvalidConditionValueException
*/
public function testInValidBackOfficeInputValue()
{
/** @var FacadeInterface $stubFacade */
$stubFacade = $this->generateFacadeStub();
$condition1 = new StartDate($stubFacade);
$operators = array(
StartDate::START_DATE => Operators::SUPERIOR_OR_EQUAL
);
$values = array(
StartDate::START_DATE => 'petit poney'
);
$condition1->setValidatorsFromForm($operators, $values);
}
/**
* Check if test inferior operator is working
*
* @covers Thelia\Condition\Implementation\StartDate::isMatching
*
*/
public function testMatchingRule()
{
/** @var FacadeInterface $stubFacade */
$stubFacade = $this->generateFacadeStub();
$condition1 = new StartDate($stubFacade);
$operators = array(
StartDate::START_DATE => Operators::SUPERIOR_OR_EQUAL
);
$values = array(
StartDate::START_DATE => $this->startDate
);
$condition1->setValidatorsFromForm($operators, $values);
$isValid = $condition1->isMatching();
$expected = true;
$actual =$isValid;
$this->assertEquals($expected, $actual);
}
/**
* Check if test inferior operator is working
*
* @covers Thelia\Condition\Implementation\StartDate::isMatching
*
*/
public function testNotMatching()
{
/** @var FacadeInterface $stubFacade */
$stubFacade = $this->generateFacadeStub();
$condition1 = new StartDate($stubFacade);
$operators = array(
StartDate::START_DATE => Operators::SUPERIOR_OR_EQUAL
);
$values = array(
StartDate::START_DATE => time() + 2000
);
$condition1->setValidatorsFromForm($operators, $values);
$isValid = $condition1->isMatching();
$expected = false;
$actual =$isValid;
$this->assertEquals($expected, $actual);
}
public function testGetSerializableRule()
{
/** @var FacadeInterface $stubFacade */
$stubFacade = $this->generateFacadeStub();
$condition1 = new StartDate($stubFacade);
$operators = array(
StartDate::START_DATE => Operators::SUPERIOR_OR_EQUAL
);
$values = array(
StartDate::START_DATE => $this->startDate
);
$condition1->setValidatorsFromForm($operators, $values);
$serializableRule = $condition1->getSerializableCondition();
$expected = new SerializableCondition();
$expected->conditionServiceId = $condition1->getServiceId();
$expected->operators = $operators;
$expected->values = $values;
$actual = $serializableRule;
$this->assertEquals($expected, $actual);
}
/**
* Check getName i18n
*
* @covers Thelia\Condition\Implementation\StartDate::getName
*
*/
public function testGetName()
{
$stubFacade = $this->generateFacadeStub(399, 'EUR', 'Number of articles in cart');
/** @var FacadeInterface $stubFacade */
$condition1 = new StartDate($stubFacade);
$actual = $condition1->getName();
$expected = 'Number of articles in cart';
$this->assertEquals($expected, $actual);
}
/**
* Check tooltip i18n
*
* @covers Thelia\Condition\Implementation\StartDate::getToolTip
*
*/
public function testGetToolTip()
{
$stubFacade = $this->generateFacadeStub(399, 'EUR', 'Sample coupon condition');
/** @var FacadeInterface $stubFacade */
$condition1 = new StartDate($stubFacade);
$actual = $condition1->getToolTip();
$expected = 'Sample coupon condition';
$this->assertEquals($expected, $actual);
}
/**
* Check validator
*
* @covers Thelia\Condition\Implementation\StartDate::generateInputs
*
*/
public function testGetValidator()
{
$stubFacade = $this->generateFacadeStub(399, 'EUR', 'Price');
/** @var FacadeInterface $stubFacade */
$condition1 = new StartDate($stubFacade);
$operators = array(
StartDate::START_DATE => Operators::SUPERIOR_OR_EQUAL
);
$values = array(
StartDate::START_DATE => $this->startDate
);
$condition1->setValidatorsFromForm($operators, $values);
$actual = $condition1->getValidators();
$validators = array(
'inputs' => array(
StartDate::START_DATE => array(
'availableOperators' => array(
'>=' => 'Price',
),
'value' => '',
'selectedOperator' => '>='
)
),
'setOperators' => array(
'start_date' => '>='
),
'setValues' => array(
'start_date' => $this->startDate
)
);
$expected = $validators;
$this->assertEquals($expected, $actual);
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown()
{
}
}

View File

@@ -0,0 +1,412 @@
<?php
/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* web : http://www.thelia.net */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
/*************************************************************************************/
namespace Thelia\Coupon\Type;
use Propel\Runtime\Collection\ObjectCollection;
use Thelia\Condition\ConditionCollection;
use Thelia\Condition\ConditionEvaluator;
use Thelia\Condition\Implementation\MatchForTotalAmount;
use Thelia\Condition\Operators;
use Thelia\Coupon\FacadeInterface;
use Thelia\Model\CartItem;
use Thelia\Model\CurrencyQuery;
use Thelia\Model\Product;
use Thelia\Model\ProductQuery;
/**
* @package Coupon
* @author Franck Allimant <franck@cqfdev.fr>
*/
class FreeProductTest extends \PHPUnit_Framework_TestCase
{
/** @var Product $freeProduct */
var $freeProduct;
var $originalPrice;
var $originalPromo;
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
$currency = CurrencyQuery::create()->filterByCode('EUR')->findOne();
// Find a product
$this->freeProduct = ProductQuery::create()->findOne();
$this->originalPrice = $this->freeProduct->getDefaultSaleElements()->getPricesByCurrency($currency)->getPrice();
$this->originalPromo = $this->freeProduct->getDefaultSaleElements()->getPromo();
$this->freeProduct->getDefaultSaleElements()->setPromo(false)->save();
}
/**
* Generate adapter stub
*
* @param int $cartTotalPrice Cart total price
* @param string $checkoutCurrency Checkout currency
* @param string $i18nOutput Output from each translation
*
* @return \PHPUnit_Framework_MockObject_MockObject
*/
public function generateFacadeStub($cartTotalPrice = 400, $checkoutCurrency = 'EUR', $i18nOutput = '')
{
$stubFacade = $this->getMockBuilder('\Thelia\Coupon\BaseFacade')
->disableOriginalConstructor()
->getMock();
$currencies = CurrencyQuery::create();
$currencies = $currencies->find();
$stubFacade->expects($this->any())
->method('getAvailableCurrencies')
->will($this->returnValue($currencies));
$stubFacade->expects($this->any())
->method('getCartTotalPrice')
->will($this->returnValue($cartTotalPrice));
$stubFacade->expects($this->any())
->method('getCheckoutCurrency')
->will($this->returnValue($checkoutCurrency));
$stubFacade->expects($this->any())
->method('getConditionEvaluator')
->will($this->returnValue(new ConditionEvaluator()));
$stubTranslator = $this->getMockBuilder('\Thelia\Core\Translation\Translator')
->disableOriginalConstructor()
->getMock();
$stubTranslator->expects($this->any())
->method('trans')
->will($this->returnValue($i18nOutput));
$stubFacade->expects($this->any())
->method('getTranslator')
->will($this->returnValue($stubTranslator));
$stubDispatcher = $this->getMockBuilder('\Symfony\Component\EventDispatcher\EventDispatcher')
->disableOriginalConstructor()
->getMock();
$stubDispatcher->expects($this->any())
->method('dispatch')
->will($this->returnCallback(function($dummy, $cartEvent) {
$ci = new CartItem();
$ci->setId(3)->setPrice(123)->setPromo(0);
$cartEvent->setCartItem($ci);
}));
$stubFacade->expects($this->any())
->method('getDispatcher')
->will($this->returnValue($stubDispatcher));
$stubSession = $this->getMockBuilder('\Thelia\Core\HttpFoundation\Session\Session')
->disableOriginalConstructor()
->getMock();
$stubSession->expects($this->any())
->method('get')
->will($this->onConsecutiveCalls(-1, 3));
$stubRequest = $this->getMockBuilder('\Thelia\Core\HttpFoundation\Request')
->disableOriginalConstructor()
->getMock();
$stubRequest->expects($this->any())
->method('getSession')
->will($this->returnValue($stubSession));
$stubFacade->expects($this->any())
->method('getRequest')
->will($this->returnValue($stubRequest));
return $stubFacade;
}
public function generateMatchingCart(\PHPUnit_Framework_MockObject_MockObject $stubFacade, $count) {
$product1 = new Product();
$product1->setId(10);
$product2 = new Product();
$product2->setId(20);
$cartItem1Stub = $this->getMockBuilder('\Thelia\Model\CartItem')
->disableOriginalConstructor()
->getMock();
$cartItem1Stub
->expects($this->any())
->method('getProduct')
->will($this->returnValue($product1))
;
$cartItem1Stub
->expects($this->any())
->method('getQuantity')
->will($this->returnValue(1))
;
$cartItem1Stub
->expects($this->any())
->method('getPrice')
->will($this->returnValue(100))
;
$cartItem2Stub = $this->getMockBuilder('\Thelia\Model\CartItem')
->disableOriginalConstructor()
->getMock();
$cartItem2Stub
->expects($this->any())
->method('getProduct')
->will($this->returnValue($product2));
$cartItem2Stub
->expects($this->any())
->method('getQuantity')
->will($this->returnValue(2))
;
$cartItem2Stub
->expects($this->any())
->method('getPrice')
->will($this->returnValue(150))
;
$cartStub = $this->getMockBuilder('\Thelia\Model\Cart')
->disableOriginalConstructor()
->getMock();
if ($count == 1)
$ret = [$cartItem1Stub];
else
$ret = [$cartItem1Stub, $cartItem2Stub];
$cartStub
->expects($this->any())
->method('getCartItems')
->will($this->returnValue($ret));
$stubFacade->expects($this->any())
->method('getCart')
->will($this->returnValue($cartStub));
}
public function generateNoMatchingCart(\PHPUnit_Framework_MockObject_MockObject $stubFacade) {
$product2 = new Product();
$product2->setId(30);
$cartItem2Stub = $this->getMockBuilder('\Thelia\Model\CartItem')
->disableOriginalConstructor()
->getMock();
$cartItem2Stub->expects($this->any())
->method('getProduct')
->will($this->returnValue($product2))
;
$cartItem2Stub->expects($this->any())
->method('getQuantity')
->will($this->returnValue(2))
;
$cartItem2Stub
->expects($this->any())
->method('getPrice')
->will($this->returnValue(11000))
;
$cartStub = $this->getMockBuilder('\Thelia\Model\Cart')
->disableOriginalConstructor()
->getMock();
$cartStub
->expects($this->any())
->method('getCartItems')
->will($this->returnValue([$cartItem2Stub]));
$stubFacade->expects($this->any())
->method('getCart')
->will($this->returnValue($cartStub));
}
public function testSet()
{
$stubFacade = $this->generateFacadeStub();
$coupon = new FreeProduct($stubFacade);
$date = new \DateTime();
$coupon->set(
$stubFacade,
'TEST', 'TEST Coupon', 'This is a test coupon title', 'This is a test coupon description',
array('percentage' => 10.00, 'products' => [10, 20], 'offered_product_id' => $this->freeProduct->getId(), 'offered_category_id' => 1),
true, true, true, true,
254,
$date->setTimestamp(strtotime("today + 3 months")),
new ObjectCollection(),
new ObjectCollection(),
false
);
$condition1 = new MatchForTotalAmount($stubFacade);
$operators = array(
MatchForTotalAmount::CART_TOTAL => Operators::SUPERIOR,
MatchForTotalAmount::CART_CURRENCY => Operators::EQUAL
);
$values = array(
MatchForTotalAmount::CART_TOTAL => 40.00,
MatchForTotalAmount::CART_CURRENCY => 'EUR'
);
$condition1->setValidatorsFromForm($operators, $values);
$condition2 = new MatchForTotalAmount($stubFacade);
$operators = array(
MatchForTotalAmount::CART_TOTAL => Operators::INFERIOR,
MatchForTotalAmount::CART_CURRENCY => Operators::EQUAL
);
$values = array(
MatchForTotalAmount::CART_TOTAL => 400.00,
MatchForTotalAmount::CART_CURRENCY => 'EUR'
);
$condition2->setValidatorsFromForm($operators, $values);
$conditions = new ConditionCollection();
$conditions[] = $condition1;
$conditions[] = $condition2;
$coupon->setConditions($conditions);
$this->assertEquals('TEST', $coupon->getCode());
$this->assertEquals('TEST Coupon', $coupon->getTitle());
$this->assertEquals('This is a test coupon title', $coupon->getShortDescription());
$this->assertEquals('This is a test coupon description', $coupon->getDescription());
$this->assertEquals(true, $coupon->isCumulative());
$this->assertEquals(true, $coupon->isRemovingPostage());
$this->assertEquals(true, $coupon->isAvailableOnSpecialOffers());
$this->assertEquals(true, $coupon->isEnabled());
$this->assertEquals(254, $coupon->getMaxUsage());
$this->assertEquals($date, $coupon->getExpirationDate());
}
public function testMatchOne()
{
$stubFacade = $this->generateFacadeStub();
$coupon = new FreeProduct($stubFacade);
$date = new \DateTime();
$coupon->set(
$stubFacade,
'TEST', 'TEST Coupon', 'This is a test coupon title', 'This is a test coupon description',
array('percentage' => 10.00, 'products' => [10, 20], 'offered_product_id' => $this->freeProduct->getId(), 'offered_category_id' => 1),
true, true, true, true,
254,
$date->setTimestamp(strtotime("today + 3 months")),
new ObjectCollection(),
new ObjectCollection(),
false
);
$this->generateMatchingCart($stubFacade, 1);
$this->assertEquals(123.00, $coupon->exec());
}
public function testMatchSeveral()
{
$stubFacade = $this->generateFacadeStub();
$coupon = new FreeProduct($stubFacade);
$date = new \DateTime();
$coupon->set(
$stubFacade,
'TEST', 'TEST Coupon', 'This is a test coupon title', 'This is a test coupon description',
array('percentage' => 10.00, 'products' => [10, 20], 'offered_product_id' => $this->freeProduct->getId(), 'offered_category_id' => 1),
true, true, true, true,
254,
$date->setTimestamp(strtotime("today + 3 months")),
new ObjectCollection(),
new ObjectCollection(),
false
);
$this->generateMatchingCart($stubFacade, 2);
$this->assertEquals(123.00, $coupon->exec());
}
public function testNoMatch()
{
$stubFacade = $this->generateFacadeStub();
$coupon = new FreeProduct($stubFacade);
$date = new \DateTime();
$coupon->set(
$stubFacade,
'TEST', 'TEST Coupon', 'This is a test coupon title', 'This is a test coupon description',
array('percentage' => 10.00, 'products' => [10, 20], 'offered_product_id' => $this->freeProduct->getId(), 'offered_category_id' => 1),
true, true, true, true,
254,
$date->setTimestamp(strtotime("today + 3 months")),
new ObjectCollection(),
new ObjectCollection(),
false
);
$this->generateNoMatchingCart($stubFacade);
$this->assertEquals(0.00, $coupon->exec());
}
public function testGetName()
{
$stubFacade = $this->generateFacadeStub(399, 'EUR', 'Coupon test name');
/** @var FacadeInterface $stubFacade */
$coupon = new FreeProduct($stubFacade);
$actual = $coupon->getName();
$expected = 'Coupon test name';
$this->assertEquals($expected, $actual);
}
public function testGetToolTip()
{
$tooltip = 'Coupon test tooltip';
$stubFacade = $this->generateFacadeStub(399, 'EUR', $tooltip);
/** @var FacadeInterface $stubFacade */
$coupon = new FreeProduct($stubFacade);
$actual = $coupon->getToolTip();
$expected = $tooltip;
$this->assertEquals($expected, $actual);
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown()
{
$this->freeProduct->getDefaultSaleElements()->setPromo($this->originalPromo)->save();
}
}

View File

@@ -0,0 +1,398 @@
<?php
/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* web : http://www.thelia.net */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
/*************************************************************************************/
namespace Thelia\Coupon\Type;
use Propel\Runtime\Collection\ObjectCollection;
use Thelia\Condition\ConditionCollection;
use Thelia\Condition\ConditionEvaluator;
use Thelia\Condition\Implementation\MatchForTotalAmount;
use Thelia\Condition\Operators;
use Thelia\Coupon\FacadeInterface;
use Thelia\Model\CurrencyQuery;
/**
* @package Coupon
* @author Franck Allimant <franck@cqfdev.fr>
*/
class RemoveAmountOnAttributeValuesTest extends \PHPUnit_Framework_TestCase
{
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
}
/**
* Generate adapter stub
*
* @param int $cartTotalPrice Cart total price
* @param string $checkoutCurrency Checkout currency
* @param string $i18nOutput Output from each translation
*
* @return \PHPUnit_Framework_MockObject_MockObject
*/
public function generateFacadeStub($cartTotalPrice = 400, $checkoutCurrency = 'EUR', $i18nOutput = '')
{
$stubFacade = $this->getMockBuilder('\Thelia\Coupon\BaseFacade')
->disableOriginalConstructor()
->getMock();
$currencies = CurrencyQuery::create();
$currencies = $currencies->find();
$stubFacade->expects($this->any())
->method('getAvailableCurrencies')
->will($this->returnValue($currencies));
$stubFacade->expects($this->any())
->method('getCartTotalPrice')
->will($this->returnValue($cartTotalPrice));
$stubFacade->expects($this->any())
->method('getCheckoutCurrency')
->will($this->returnValue($checkoutCurrency));
$stubFacade->expects($this->any())
->method('getConditionEvaluator')
->will($this->returnValue(new ConditionEvaluator()));
$stubTranslator = $this->getMockBuilder('\Thelia\Core\Translation\Translator')
->disableOriginalConstructor()
->getMock();
$stubTranslator->expects($this->any())
->method('trans')
->will($this->returnValue($i18nOutput));
$stubFacade->expects($this->any())
->method('getTranslator')
->will($this->returnValue($stubTranslator));
return $stubFacade;
}
public function generateMatchingCart(\PHPUnit_Framework_MockObject_MockObject $stubFacade, $count) {
$attrCombination1 = $this->getMockBuilder('\Thelia\Model\AttributeCombination')
->disableOriginalConstructor()
->getMock()
;
$attrCombination1
->expects($this->any())
->method('getAttributeAvId')
->will($this->returnValue(10))
;
$attrCombination2 = $this->getMockBuilder('\Thelia\Model\AttributeCombination')
->disableOriginalConstructor()
->getMock()
;
$attrCombination2
->expects($this->any())
->method('getAttributeAvId')
->will($this->returnValue(20))
;
$pse1 = $this->getMockBuilder('\Thelia\Model\ProductSaleElements')
->disableOriginalConstructor()
->getMock()
;
$pse1
->expects($this->any())
->method('getAttributeCombinations')
->will($this->returnValue([$attrCombination1]))
;
$pse2 = $this->getMockBuilder('\Thelia\Model\ProductSaleElements')
->disableOriginalConstructor()
->getMock();
;
$pse2
->expects($this->any())
->method('getAttributeCombinations')
->will($this->returnValue([$attrCombination1, $attrCombination2]))
;
$cartItem1Stub = $this->getMockBuilder('\Thelia\Model\CartItem')
->disableOriginalConstructor()
->getMock()
;
$cartItem1Stub
->expects($this->any())
->method('getProductSaleElements')
->will($this->returnValue($pse1))
;
$cartItem1Stub
->expects($this->any())
->method('getQuantity')
->will($this->returnValue(1))
;
$cartItem2Stub = $this->getMockBuilder('\Thelia\Model\CartItem')
->disableOriginalConstructor()
->getMock()
;
$cartItem2Stub
->expects($this->any())
->method('getProductSaleElements')
->will($this->returnValue($pse2))
;
$cartItem2Stub
->expects($this->any())
->method('getQuantity')
->will($this->returnValue(2))
;
$cartStub = $this->getMockBuilder('\Thelia\Model\Cart')
->disableOriginalConstructor()
->getMock();
if ($count == 1)
$ret = [$cartItem1Stub];
else
$ret = [$cartItem1Stub, $cartItem2Stub];
$cartStub
->expects($this->any())
->method('getCartItems')
->will($this->returnValue($ret));
$stubFacade->expects($this->any())
->method('getCart')
->will($this->returnValue($cartStub));
}
public function generateNoMatchingCart(\PHPUnit_Framework_MockObject_MockObject $stubFacade) {
$attrCombination1 = $this->getMockBuilder('\Thelia\Model\AttributeCombination')
->disableOriginalConstructor()
->getMock()
;
$attrCombination1
->expects($this->any())
->method('getAttributeAvId')
->will($this->returnValue(30))
;
$pse1 = $this->getMockBuilder('\Thelia\Model\ProductSaleElements')
->disableOriginalConstructor()
->getMock()
;
$pse1
->expects($this->any())
->method('getAttributeCombinations')
->will($this->returnValue([$attrCombination1]))
;
$cartItem1Stub = $this->getMockBuilder('\Thelia\Model\CartItem')
->disableOriginalConstructor()
->getMock()
;
$cartItem1Stub
->expects($this->any())
->method('getProductSaleElements')
->will($this->returnValue($pse1))
;
$cartItem1Stub
->expects($this->any())
->method('getQuantity')
->will($this->returnValue(1))
;
$cartStub = $this->getMockBuilder('\Thelia\Model\Cart')
->disableOriginalConstructor()
->getMock();
$cartStub
->expects($this->any())
->method('getCartItems')
->will($this->returnValue([$cartItem1Stub]));
$stubFacade->expects($this->any())
->method('getCart')
->will($this->returnValue($cartStub));
}
public function testSet()
{
$stubFacade = $this->generateFacadeStub();
$coupon = new RemoveAmountOnAttributeValues($stubFacade);
$date = new \DateTime();
$coupon->set(
$stubFacade,
'TEST', 'TEST Coupon', 'This is a test coupon title', 'This is a test coupon description',
array('amount' => 10.00, 'attribute_avs' => [10, 20]),
true, true, true, true,
254,
$date->setTimestamp(strtotime("today + 3 months")),
new ObjectCollection(),
new ObjectCollection(),
false
);
$condition1 = new MatchForTotalAmount($stubFacade);
$operators = array(
MatchForTotalAmount::CART_TOTAL => Operators::SUPERIOR,
MatchForTotalAmount::CART_CURRENCY => Operators::EQUAL
);
$values = array(
MatchForTotalAmount::CART_TOTAL => 40.00,
MatchForTotalAmount::CART_CURRENCY => 'EUR'
);
$condition1->setValidatorsFromForm($operators, $values);
$condition2 = new MatchForTotalAmount($stubFacade);
$operators = array(
MatchForTotalAmount::CART_TOTAL => Operators::INFERIOR,
MatchForTotalAmount::CART_CURRENCY => Operators::EQUAL
);
$values = array(
MatchForTotalAmount::CART_TOTAL => 400.00,
MatchForTotalAmount::CART_CURRENCY => 'EUR'
);
$condition2->setValidatorsFromForm($operators, $values);
$conditions = new ConditionCollection();
$conditions[] = $condition1;
$conditions[] = $condition2;
$coupon->setConditions($conditions);
$this->assertEquals('TEST', $coupon->getCode());
$this->assertEquals('TEST Coupon', $coupon->getTitle());
$this->assertEquals('This is a test coupon title', $coupon->getShortDescription());
$this->assertEquals('This is a test coupon description', $coupon->getDescription());
$this->assertEquals(true, $coupon->isCumulative());
$this->assertEquals(true, $coupon->isRemovingPostage());
$this->assertEquals(true, $coupon->isAvailableOnSpecialOffers());
$this->assertEquals(true, $coupon->isEnabled());
$this->assertEquals(254, $coupon->getMaxUsage());
$this->assertEquals($date, $coupon->getExpirationDate());
}
public function testMatchOne()
{
$stubFacade = $this->generateFacadeStub();
$coupon = new RemoveAmountOnAttributeValues($stubFacade);
$date = new \DateTime();
$coupon->set(
$stubFacade,
'TEST', 'TEST Coupon', 'This is a test coupon title', 'This is a test coupon description',
array('amount' => 10.00, 'attribute_avs' => [10, 20]),
true, true, true, true,
254,
$date->setTimestamp(strtotime("today + 3 months")),
new ObjectCollection(),
new ObjectCollection(),
false
);
$this->generateMatchingCart($stubFacade, 1);
$this->assertEquals(10.00, $coupon->exec());
}
public function testMatchSeveral()
{
$stubFacade = $this->generateFacadeStub();
$coupon = new RemoveAmountOnAttributeValues($stubFacade);
$date = new \DateTime();
$coupon->set(
$stubFacade,
'TEST', 'TEST Coupon', 'This is a test coupon title', 'This is a test coupon description',
array('amount' => 10.00, 'attribute_avs' => [10, 20]),
true, true, true, true,
254,
$date->setTimestamp(strtotime("today + 3 months")),
new ObjectCollection(),
new ObjectCollection(),
false
);
$this->generateMatchingCart($stubFacade, 2);
$this->assertEquals(30.00, $coupon->exec());
}
public function testNoMatch()
{
$stubFacade = $this->generateFacadeStub();
$coupon = new RemoveAmountOnAttributeValues($stubFacade);
$date = new \DateTime();
$coupon->set(
$stubFacade,
'TEST', 'TEST Coupon', 'This is a test coupon title', 'This is a test coupon description',
array('amount' => 10.00, 'attribute_avs' => [10, 20]),
true, true, true, true,
254,
$date->setTimestamp(strtotime("today + 3 months")),
new ObjectCollection(),
new ObjectCollection(),
false
);
$this->generateNoMatchingCart($stubFacade);
$this->assertEquals(0.00, $coupon->exec());
}
public function testGetName()
{
$stubFacade = $this->generateFacadeStub(399, 'EUR', 'Coupon test name');
/** @var FacadeInterface $stubFacade */
$coupon = new RemoveAmountOnAttributeValues($stubFacade);
$actual = $coupon->getName();
$expected = 'Coupon test name';
$this->assertEquals($expected, $actual);
}
public function testGetToolTip()
{
$tooltip = 'Coupon test tooltip';
$stubFacade = $this->generateFacadeStub(399, 'EUR', $tooltip);
/** @var FacadeInterface $stubFacade */
$coupon = new RemoveAmountOnAttributeValues($stubFacade);
$actual = $coupon->getToolTip();
$expected = $tooltip;
$this->assertEquals($expected, $actual);
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown()
{
}
}

View File

@@ -0,0 +1,330 @@
<?php
/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* web : http://www.thelia.net */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
/*************************************************************************************/
namespace Thelia\Coupon\Type;
use Propel\Runtime\Collection\ObjectCollection;
use Thelia\Condition\ConditionCollection;
use Thelia\Condition\ConditionEvaluator;
use Thelia\Condition\Implementation\MatchForTotalAmount;
use Thelia\Condition\Operators;
use Thelia\Coupon\FacadeInterface;
use Thelia\Model\Category;
use Thelia\Model\CurrencyQuery;
use Thelia\Model\Product;
/**
* @package Coupon
* @author Franck Allimant <franck@cqfdev.fr>
*/
class RemoveAmountOnCategoriesTest extends \PHPUnit_Framework_TestCase
{
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
}
/**
* Generate adapter stub
*
* @param int $cartTotalPrice Cart total price
* @param string $checkoutCurrency Checkout currency
* @param string $i18nOutput Output from each translation
*
* @return \PHPUnit_Framework_MockObject_MockObject
*/
public function generateFacadeStub($cartTotalPrice = 400, $checkoutCurrency = 'EUR', $i18nOutput = '')
{
$stubFacade = $this->getMockBuilder('\Thelia\Coupon\BaseFacade')
->disableOriginalConstructor()
->getMock();
$currencies = CurrencyQuery::create();
$currencies = $currencies->find();
$stubFacade->expects($this->any())
->method('getAvailableCurrencies')
->will($this->returnValue($currencies));
$stubFacade->expects($this->any())
->method('getCartTotalPrice')
->will($this->returnValue($cartTotalPrice));
$stubFacade->expects($this->any())
->method('getCheckoutCurrency')
->will($this->returnValue($checkoutCurrency));
$stubFacade->expects($this->any())
->method('getConditionEvaluator')
->will($this->returnValue(new ConditionEvaluator()));
$stubTranslator = $this->getMockBuilder('\Thelia\Core\Translation\Translator')
->disableOriginalConstructor()
->getMock();
$stubTranslator->expects($this->any())
->method('trans')
->will($this->returnValue($i18nOutput));
$stubFacade->expects($this->any())
->method('getTranslator')
->will($this->returnValue($stubTranslator));
return $stubFacade;
}
public function generateMatchingCart(\PHPUnit_Framework_MockObject_MockObject $stubFacade) {
$category1 = new Category();
$category1->setId(10);
$category2 = new Category();
$category2->setId(20);
$category3 = new Category();
$category3->setId(30);
$product1 = new Product();
$product1->addCategory($category1)->addCategory($category2);
$product2 = new Product();
$product2->addCategory($category3);
$cartItem1Stub = $this->getMockBuilder('\Thelia\Model\CartItem')
->disableOriginalConstructor()
->getMock();
$cartItem1Stub
->expects($this->any())
->method('getProduct')
->will($this->returnValue($product1))
;
$cartItem1Stub
->expects($this->any())
->method('getQuantity')
->will($this->returnValue(1))
;
$cartItem2Stub = $this->getMockBuilder('\Thelia\Model\CartItem')
->disableOriginalConstructor()
->getMock();
$cartItem2Stub
->expects($this->any())
->method('getProduct')
->will($this->returnValue($product2));
$cartItem2Stub
->expects($this->any())
->method('getQuantity')
->will($this->returnValue(2))
;
$cartStub = $this->getMockBuilder('\Thelia\Model\Cart')
->disableOriginalConstructor()
->getMock();
$cartStub
->expects($this->any())
->method('getCartItems')
->will($this->returnValue([$cartItem1Stub, $cartItem2Stub]));
$stubFacade->expects($this->any())
->method('getCart')
->will($this->returnValue($cartStub));
}
public function generateNoMatchingCart(\PHPUnit_Framework_MockObject_MockObject $stubFacade) {
$category3 = new Category();
$category3->setId(30);
$product2 = new Product();
$product2->addCategory($category3);
$cartItem2Stub = $this->getMockBuilder('\Thelia\Model\CartItem')
->disableOriginalConstructor()
->getMock();
$cartItem2Stub->expects($this->any())
->method('getProduct')
->will($this->returnValue($product2));
$cartItem2Stub->expects($this->any())
->method('getQuantity')
->will($this->returnValue(2));
$cartStub = $this->getMockBuilder('\Thelia\Model\Cart')
->disableOriginalConstructor()
->getMock();
$cartStub
->expects($this->any())
->method('getCartItems')
->will($this->returnValue([$cartItem2Stub]));
$stubFacade->expects($this->any())
->method('getCart')
->will($this->returnValue($cartStub));
}
public function testSet()
{
$stubFacade = $this->generateFacadeStub();
$coupon = new RemoveAmountOnCategories($stubFacade);
$date = new \DateTime();
$coupon->set(
$stubFacade,
'TEST', 'TEST Coupon', 'This is a test coupon title', 'This is a test coupon description',
array('amount' => 10.00, 'categories' => [10, 20]),
true, true, true, true,
254,
$date->setTimestamp(strtotime("today + 3 months")),
new ObjectCollection(),
new ObjectCollection(),
false
);
$condition1 = new MatchForTotalAmount($stubFacade);
$operators = array(
MatchForTotalAmount::CART_TOTAL => Operators::SUPERIOR,
MatchForTotalAmount::CART_CURRENCY => Operators::EQUAL
);
$values = array(
MatchForTotalAmount::CART_TOTAL => 40.00,
MatchForTotalAmount::CART_CURRENCY => 'EUR'
);
$condition1->setValidatorsFromForm($operators, $values);
$condition2 = new MatchForTotalAmount($stubFacade);
$operators = array(
MatchForTotalAmount::CART_TOTAL => Operators::INFERIOR,
MatchForTotalAmount::CART_CURRENCY => Operators::EQUAL
);
$values = array(
MatchForTotalAmount::CART_TOTAL => 400.00,
MatchForTotalAmount::CART_CURRENCY => 'EUR'
);
$condition2->setValidatorsFromForm($operators, $values);
$conditions = new ConditionCollection();
$conditions[] = $condition1;
$conditions[] = $condition2;
$coupon->setConditions($conditions);
$this->assertEquals('TEST', $coupon->getCode());
$this->assertEquals('TEST Coupon', $coupon->getTitle());
$this->assertEquals('This is a test coupon title', $coupon->getShortDescription());
$this->assertEquals('This is a test coupon description', $coupon->getDescription());
$this->assertEquals(true, $coupon->isCumulative());
$this->assertEquals(true, $coupon->isRemovingPostage());
$this->assertEquals(true, $coupon->isAvailableOnSpecialOffers());
$this->assertEquals(true, $coupon->isEnabled());
$this->assertEquals(254, $coupon->getMaxUsage());
$this->assertEquals($date, $coupon->getExpirationDate());
$this->generateMatchingCart($stubFacade);
$this->assertEquals(10.00, $coupon->exec());
}
public function testMatch()
{
$stubFacade = $this->generateFacadeStub();
$coupon = new RemoveAmountOnCategories($stubFacade);
$date = new \DateTime();
$coupon->set(
$stubFacade,
'TEST', 'TEST Coupon', 'This is a test coupon title', 'This is a test coupon description',
array('amount' => 10.00, 'categories' => [10, 20]),
true, true, true, true,
254,
$date->setTimestamp(strtotime("today + 3 months")),
new ObjectCollection(),
new ObjectCollection(),
false
);
$this->generateMatchingCart($stubFacade);
$this->assertEquals(10.00, $coupon->exec());
}
public function testNoMatch()
{
$stubFacade = $this->generateFacadeStub();
$coupon = new RemoveAmountOnCategories($stubFacade);
$date = new \DateTime();
$coupon->set(
$stubFacade,
'TEST', 'TEST Coupon', 'This is a test coupon title', 'This is a test coupon description',
array('amount' => 10.00, 'categories' => [10, 20]),
true, true, true, true,
254,
$date->setTimestamp(strtotime("today + 3 months")),
new ObjectCollection(),
new ObjectCollection(),
false
);
$this->generateNoMatchingCart($stubFacade);
$this->assertEquals(0.00, $coupon->exec());
}
public function testGetName()
{
$stubFacade = $this->generateFacadeStub(399, 'EUR', 'Coupon test name');
/** @var FacadeInterface $stubFacade */
$coupon = new RemoveAmountOnCategories($stubFacade);
$actual = $coupon->getName();
$expected = 'Coupon test name';
$this->assertEquals($expected, $actual);
}
public function testGetToolTip()
{
$tooltip = 'Coupon test tooltip';
$stubFacade = $this->generateFacadeStub(399, 'EUR', $tooltip);
/** @var FacadeInterface $stubFacade */
$coupon = new RemoveAmountOnCategories($stubFacade);
$actual = $coupon->getToolTip();
$expected = $tooltip;
$this->assertEquals($expected, $actual);
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown()
{
}
}

View File

@@ -0,0 +1,343 @@
<?php
/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* web : http://www.thelia.net */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
/*************************************************************************************/
namespace Thelia\Coupon\Type;
use Propel\Runtime\Collection\ObjectCollection;
use Thelia\Condition\ConditionCollection;
use Thelia\Condition\ConditionEvaluator;
use Thelia\Condition\Implementation\MatchForTotalAmount;
use Thelia\Condition\Operators;
use Thelia\Coupon\FacadeInterface;
use Thelia\Model\CurrencyQuery;
use Thelia\Model\Product;
/**
* @package Coupon
* @author Franck Allimant <franck@cqfdev.fr>
*/
class RemoveAmountOnProductsTest extends \PHPUnit_Framework_TestCase
{
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
}
/**
* Generate adapter stub
*
* @param int $cartTotalPrice Cart total price
* @param string $checkoutCurrency Checkout currency
* @param string $i18nOutput Output from each translation
*
* @return \PHPUnit_Framework_MockObject_MockObject
*/
public function generateFacadeStub($cartTotalPrice = 400, $checkoutCurrency = 'EUR', $i18nOutput = '')
{
$stubFacade = $this->getMockBuilder('\Thelia\Coupon\BaseFacade')
->disableOriginalConstructor()
->getMock();
$currencies = CurrencyQuery::create();
$currencies = $currencies->find();
$stubFacade->expects($this->any())
->method('getAvailableCurrencies')
->will($this->returnValue($currencies));
$stubFacade->expects($this->any())
->method('getCartTotalPrice')
->will($this->returnValue($cartTotalPrice));
$stubFacade->expects($this->any())
->method('getCheckoutCurrency')
->will($this->returnValue($checkoutCurrency));
$stubFacade->expects($this->any())
->method('getConditionEvaluator')
->will($this->returnValue(new ConditionEvaluator()));
$stubTranslator = $this->getMockBuilder('\Thelia\Core\Translation\Translator')
->disableOriginalConstructor()
->getMock();
$stubTranslator->expects($this->any())
->method('trans')
->will($this->returnValue($i18nOutput));
$stubFacade->expects($this->any())
->method('getTranslator')
->will($this->returnValue($stubTranslator));
return $stubFacade;
}
public function generateMatchingCart(\PHPUnit_Framework_MockObject_MockObject $stubFacade, $count) {
$product1 = new Product();
$product1->setId(10);
$product2 = new Product();
$product2->setId(20);
$cartItem1Stub = $this->getMockBuilder('\Thelia\Model\CartItem')
->disableOriginalConstructor()
->getMock();
$cartItem1Stub
->expects($this->any())
->method('getProduct')
->will($this->returnValue($product1))
;
$cartItem1Stub
->expects($this->any())
->method('getQuantity')
->will($this->returnValue(1))
;
$cartItem2Stub = $this->getMockBuilder('\Thelia\Model\CartItem')
->disableOriginalConstructor()
->getMock();
$cartItem2Stub
->expects($this->any())
->method('getProduct')
->will($this->returnValue($product2));
$cartItem2Stub
->expects($this->any())
->method('getQuantity')
->will($this->returnValue(2))
;
$cartStub = $this->getMockBuilder('\Thelia\Model\Cart')
->disableOriginalConstructor()
->getMock();
if ($count == 1)
$ret = [$cartItem1Stub];
else
$ret = [$cartItem1Stub, $cartItem2Stub];
$cartStub
->expects($this->any())
->method('getCartItems')
->will($this->returnValue($ret));
$stubFacade->expects($this->any())
->method('getCart')
->will($this->returnValue($cartStub));
}
public function generateNoMatchingCart(\PHPUnit_Framework_MockObject_MockObject $stubFacade) {
$product2 = new Product();
$product2->setId(30);
$cartItem2Stub = $this->getMockBuilder('\Thelia\Model\CartItem')
->disableOriginalConstructor()
->getMock();
$cartItem2Stub->expects($this->any())
->method('getProduct')
->will($this->returnValue($product2));
$cartItem2Stub->expects($this->any())
->method('getQuantity')
->will($this->returnValue(2));
$cartStub = $this->getMockBuilder('\Thelia\Model\Cart')
->disableOriginalConstructor()
->getMock();
$cartStub
->expects($this->any())
->method('getCartItems')
->will($this->returnValue([$cartItem2Stub]));
$stubFacade->expects($this->any())
->method('getCart')
->will($this->returnValue($cartStub));
}
public function testSet()
{
$stubFacade = $this->generateFacadeStub();
$coupon = new RemoveAmountOnProducts($stubFacade);
$date = new \DateTime();
$coupon->set(
$stubFacade,
'TEST', 'TEST Coupon', 'This is a test coupon title', 'This is a test coupon description',
array('amount' => 10.00, 'products' => [10, 20]),
true, true, true, true,
254,
$date->setTimestamp(strtotime("today + 3 months")),
new ObjectCollection(),
new ObjectCollection(),
false
);
$condition1 = new MatchForTotalAmount($stubFacade);
$operators = array(
MatchForTotalAmount::CART_TOTAL => Operators::SUPERIOR,
MatchForTotalAmount::CART_CURRENCY => Operators::EQUAL
);
$values = array(
MatchForTotalAmount::CART_TOTAL => 40.00,
MatchForTotalAmount::CART_CURRENCY => 'EUR'
);
$condition1->setValidatorsFromForm($operators, $values);
$condition2 = new MatchForTotalAmount($stubFacade);
$operators = array(
MatchForTotalAmount::CART_TOTAL => Operators::INFERIOR,
MatchForTotalAmount::CART_CURRENCY => Operators::EQUAL
);
$values = array(
MatchForTotalAmount::CART_TOTAL => 400.00,
MatchForTotalAmount::CART_CURRENCY => 'EUR'
);
$condition2->setValidatorsFromForm($operators, $values);
$conditions = new ConditionCollection();
$conditions[] = $condition1;
$conditions[] = $condition2;
$coupon->setConditions($conditions);
$this->assertEquals('TEST', $coupon->getCode());
$this->assertEquals('TEST Coupon', $coupon->getTitle());
$this->assertEquals('This is a test coupon title', $coupon->getShortDescription());
$this->assertEquals('This is a test coupon description', $coupon->getDescription());
$this->assertEquals(true, $coupon->isCumulative());
$this->assertEquals(true, $coupon->isRemovingPostage());
$this->assertEquals(true, $coupon->isAvailableOnSpecialOffers());
$this->assertEquals(true, $coupon->isEnabled());
$this->assertEquals(254, $coupon->getMaxUsage());
$this->assertEquals($date, $coupon->getExpirationDate());
}
public function testMatchOne()
{
$stubFacade = $this->generateFacadeStub();
$coupon = new RemoveAmountOnProducts($stubFacade);
$date = new \DateTime();
$coupon->set(
$stubFacade,
'TEST', 'TEST Coupon', 'This is a test coupon title', 'This is a test coupon description',
array('amount' => 10.00, 'products' => [10, 20]),
true, true, true, true,
254,
$date->setTimestamp(strtotime("today + 3 months")),
new ObjectCollection(),
new ObjectCollection(),
false
);
$this->generateMatchingCart($stubFacade, 1);
$this->assertEquals(10.00, $coupon->exec());
}
public function testMatchSeveral()
{
$stubFacade = $this->generateFacadeStub();
$coupon = new RemoveAmountOnProducts($stubFacade);
$date = new \DateTime();
$coupon->set(
$stubFacade,
'TEST', 'TEST Coupon', 'This is a test coupon title', 'This is a test coupon description',
array('amount' => 10.00, 'products' => [10, 20]),
true, true, true, true,
254,
$date->setTimestamp(strtotime("today + 3 months")),
new ObjectCollection(),
new ObjectCollection(),
false
);
$this->generateMatchingCart($stubFacade, 2);
$this->assertEquals(30.00, $coupon->exec());
}
public function testNoMatch()
{
$stubFacade = $this->generateFacadeStub();
$coupon = new RemoveAmountOnProducts($stubFacade);
$date = new \DateTime();
$coupon->set(
$stubFacade,
'TEST', 'TEST Coupon', 'This is a test coupon title', 'This is a test coupon description',
array('amount' => 10.00, 'products' => [10, 20]),
true, true, true, true,
254,
$date->setTimestamp(strtotime("today + 3 months")),
new ObjectCollection(),
new ObjectCollection(),
false
);
$this->generateNoMatchingCart($stubFacade);
$this->assertEquals(0.00, $coupon->exec());
}
public function testGetName()
{
$stubFacade = $this->generateFacadeStub(399, 'EUR', 'Coupon test name');
/** @var FacadeInterface $stubFacade */
$coupon = new RemoveAmountOnProducts($stubFacade);
$actual = $coupon->getName();
$expected = 'Coupon test name';
$this->assertEquals($expected, $actual);
}
public function testGetToolTip()
{
$tooltip = 'Coupon test tooltip';
$stubFacade = $this->generateFacadeStub(399, 'EUR', $tooltip);
/** @var FacadeInterface $stubFacade */
$coupon = new RemoveAmountOnProducts($stubFacade);
$actual = $coupon->getToolTip();
$expected = $tooltip;
$this->assertEquals($expected, $actual);
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown()
{
}
}

View File

@@ -0,0 +1,412 @@
<?php
/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* web : http://www.thelia.net */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
/*************************************************************************************/
namespace Thelia\Coupon\Type;
use Propel\Runtime\Collection\ObjectCollection;
use Thelia\Condition\ConditionCollection;
use Thelia\Condition\ConditionEvaluator;
use Thelia\Condition\Implementation\MatchForTotalAmount;
use Thelia\Condition\Operators;
use Thelia\Coupon\FacadeInterface;
use Thelia\Model\CurrencyQuery;
/**
* @package Coupon
* @author Franck Allimant <franck@cqfdev.fr>
*/
class RemovePercentageOnAttributeValuesTest extends \PHPUnit_Framework_TestCase
{
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
}
/**
* Generate adapter stub
*
* @param int $cartTotalPrice Cart total price
* @param string $checkoutCurrency Checkout currency
* @param string $i18nOutput Output from each translation
*
* @return \PHPUnit_Framework_MockObject_MockObject
*/
public function generateFacadeStub($cartTotalPrice = 400, $checkoutCurrency = 'EUR', $i18nOutput = '')
{
$stubFacade = $this->getMockBuilder('\Thelia\Coupon\BaseFacade')
->disableOriginalConstructor()
->getMock();
$currencies = CurrencyQuery::create();
$currencies = $currencies->find();
$stubFacade->expects($this->any())
->method('getAvailableCurrencies')
->will($this->returnValue($currencies));
$stubFacade->expects($this->any())
->method('getCartTotalPrice')
->will($this->returnValue($cartTotalPrice));
$stubFacade->expects($this->any())
->method('getCheckoutCurrency')
->will($this->returnValue($checkoutCurrency));
$stubFacade->expects($this->any())
->method('getConditionEvaluator')
->will($this->returnValue(new ConditionEvaluator()));
$stubTranslator = $this->getMockBuilder('\Thelia\Core\Translation\Translator')
->disableOriginalConstructor()
->getMock();
$stubTranslator->expects($this->any())
->method('trans')
->will($this->returnValue($i18nOutput));
$stubFacade->expects($this->any())
->method('getTranslator')
->will($this->returnValue($stubTranslator));
return $stubFacade;
}
public function generateMatchingCart(\PHPUnit_Framework_MockObject_MockObject $stubFacade, $count) {
$attrCombination1 = $this->getMockBuilder('\Thelia\Model\AttributeCombination')
->disableOriginalConstructor()
->getMock()
;
$attrCombination1
->expects($this->any())
->method('getAttributeAvId')
->will($this->returnValue(10))
;
$attrCombination2 = $this->getMockBuilder('\Thelia\Model\AttributeCombination')
->disableOriginalConstructor()
->getMock()
;
$attrCombination2
->expects($this->any())
->method('getAttributeAvId')
->will($this->returnValue(20))
;
$pse1 = $this->getMockBuilder('\Thelia\Model\ProductSaleElements')
->disableOriginalConstructor()
->getMock()
;
$pse1
->expects($this->any())
->method('getAttributeCombinations')
->will($this->returnValue([$attrCombination1]))
;
$pse2 = $this->getMockBuilder('\Thelia\Model\ProductSaleElements')
->disableOriginalConstructor()
->getMock();
;
$pse2
->expects($this->any())
->method('getAttributeCombinations')
->will($this->returnValue([$attrCombination1, $attrCombination2]))
;
$cartItem1Stub = $this->getMockBuilder('\Thelia\Model\CartItem')
->disableOriginalConstructor()
->getMock()
;
$cartItem1Stub
->expects($this->any())
->method('getProductSaleElements')
->will($this->returnValue($pse1))
;
$cartItem1Stub
->expects($this->any())
->method('getQuantity')
->will($this->returnValue(1))
;
$cartItem1Stub
->expects($this->any())
->method('getPrice')
->will($this->returnValue(100))
;
$cartItem2Stub = $this->getMockBuilder('\Thelia\Model\CartItem')
->disableOriginalConstructor()
->getMock()
;
$cartItem2Stub
->expects($this->any())
->method('getProductSaleElements')
->will($this->returnValue($pse2))
;
$cartItem2Stub
->expects($this->any())
->method('getQuantity')
->will($this->returnValue(2))
;
$cartItem2Stub
->expects($this->any())
->method('getPrice')
->will($this->returnValue(150))
;
$cartStub = $this->getMockBuilder('\Thelia\Model\Cart')
->disableOriginalConstructor()
->getMock();
if ($count == 1)
$ret = [$cartItem1Stub];
else
$ret = [$cartItem1Stub, $cartItem2Stub];
$cartStub
->expects($this->any())
->method('getCartItems')
->will($this->returnValue($ret));
$stubFacade->expects($this->any())
->method('getCart')
->will($this->returnValue($cartStub));
}
public function generateNoMatchingCart(\PHPUnit_Framework_MockObject_MockObject $stubFacade) {
$attrCombination1 = $this->getMockBuilder('\Thelia\Model\AttributeCombination')
->disableOriginalConstructor()
->getMock()
;
$attrCombination1
->expects($this->any())
->method('getAttributeAvId')
->will($this->returnValue(30))
;
$pse1 = $this->getMockBuilder('\Thelia\Model\ProductSaleElements')
->disableOriginalConstructor()
->getMock()
;
$pse1
->expects($this->any())
->method('getAttributeCombinations')
->will($this->returnValue([$attrCombination1]))
;
$cartItem1Stub = $this->getMockBuilder('\Thelia\Model\CartItem')
->disableOriginalConstructor()
->getMock()
;
$cartItem1Stub
->expects($this->any())
->method('getProductSaleElements')
->will($this->returnValue($pse1))
;
$cartItem1Stub
->expects($this->any())
->method('getQuantity')
->will($this->returnValue(1))
;
$cartItem1Stub
->expects($this->any())
->method('getPrice')
->will($this->returnValue(100))
;
$cartStub = $this->getMockBuilder('\Thelia\Model\Cart')
->disableOriginalConstructor()
->getMock();
$cartStub
->expects($this->any())
->method('getCartItems')
->will($this->returnValue([$cartItem1Stub]));
$stubFacade->expects($this->any())
->method('getCart')
->will($this->returnValue($cartStub));
}
public function testSet()
{
$stubFacade = $this->generateFacadeStub();
$coupon = new RemovePercentageOnAttributeValues($stubFacade);
$date = new \DateTime();
$coupon->set(
$stubFacade,
'TEST', 'TEST Coupon', 'This is a test coupon title', 'This is a test coupon description',
array('percentage' => 10.00, 'attribute_avs' => [10, 20]),
true, true, true, true,
254,
$date->setTimestamp(strtotime("today + 3 months")),
new ObjectCollection(),
new ObjectCollection(),
false
);
$condition1 = new MatchForTotalAmount($stubFacade);
$operators = array(
MatchForTotalAmount::CART_TOTAL => Operators::SUPERIOR,
MatchForTotalAmount::CART_CURRENCY => Operators::EQUAL
);
$values = array(
MatchForTotalAmount::CART_TOTAL => 40.00,
MatchForTotalAmount::CART_CURRENCY => 'EUR'
);
$condition1->setValidatorsFromForm($operators, $values);
$condition2 = new MatchForTotalAmount($stubFacade);
$operators = array(
MatchForTotalAmount::CART_TOTAL => Operators::INFERIOR,
MatchForTotalAmount::CART_CURRENCY => Operators::EQUAL
);
$values = array(
MatchForTotalAmount::CART_TOTAL => 400.00,
MatchForTotalAmount::CART_CURRENCY => 'EUR'
);
$condition2->setValidatorsFromForm($operators, $values);
$conditions = new ConditionCollection();
$conditions[] = $condition1;
$conditions[] = $condition2;
$coupon->setConditions($conditions);
$this->assertEquals('TEST', $coupon->getCode());
$this->assertEquals('TEST Coupon', $coupon->getTitle());
$this->assertEquals('This is a test coupon title', $coupon->getShortDescription());
$this->assertEquals('This is a test coupon description', $coupon->getDescription());
$this->assertEquals(true, $coupon->isCumulative());
$this->assertEquals(true, $coupon->isRemovingPostage());
$this->assertEquals(true, $coupon->isAvailableOnSpecialOffers());
$this->assertEquals(true, $coupon->isEnabled());
$this->assertEquals(254, $coupon->getMaxUsage());
$this->assertEquals($date, $coupon->getExpirationDate());
}
public function testMatchOne()
{
$stubFacade = $this->generateFacadeStub();
$coupon = new RemovePercentageOnAttributeValues($stubFacade);
$date = new \DateTime();
$coupon->set(
$stubFacade,
'TEST', 'TEST Coupon', 'This is a test coupon title', 'This is a test coupon description',
array('percentage' => 10.00, 'attribute_avs' => [10, 20]),
true, true, true, true,
254,
$date->setTimestamp(strtotime("today + 3 months")),
new ObjectCollection(),
new ObjectCollection(),
false
);
$this->generateMatchingCart($stubFacade, 1);
$this->assertEquals(10.00, $coupon->exec());
}
public function testMatchSeveral()
{
$stubFacade = $this->generateFacadeStub();
$coupon = new RemovePercentageOnAttributeValues($stubFacade);
$date = new \DateTime();
$coupon->set(
$stubFacade,
'TEST', 'TEST Coupon', 'This is a test coupon title', 'This is a test coupon description',
array('percentage' => 10.00, 'attribute_avs' => [10, 20]),
true, true, true, true,
254,
$date->setTimestamp(strtotime("today + 3 months")),
new ObjectCollection(),
new ObjectCollection(),
false
);
$this->generateMatchingCart($stubFacade, 2);
$this->assertEquals(40.00, $coupon->exec());
}
public function testNoMatch()
{
$stubFacade = $this->generateFacadeStub();
$coupon = new RemovePercentageOnAttributeValues($stubFacade);
$date = new \DateTime();
$coupon->set(
$stubFacade,
'TEST', 'TEST Coupon', 'This is a test coupon title', 'This is a test coupon description',
array('percentage' => 10.00, 'attribute_avs' => [10, 20]),
true, true, true, true,
254,
$date->setTimestamp(strtotime("today + 3 months")),
new ObjectCollection(),
new ObjectCollection(),
false
);
$this->generateNoMatchingCart($stubFacade);
$this->assertEquals(0.00, $coupon->exec());
}
public function testGetName()
{
$stubFacade = $this->generateFacadeStub(399, 'EUR', 'Coupon test name');
/** @var FacadeInterface $stubFacade */
$coupon = new RemovePercentageOnAttributeValues($stubFacade);
$actual = $coupon->getName();
$expected = 'Coupon test name';
$this->assertEquals($expected, $actual);
}
public function testGetToolTip()
{
$tooltip = 'Coupon test tooltip';
$stubFacade = $this->generateFacadeStub(399, 'EUR', $tooltip);
/** @var FacadeInterface $stubFacade */
$coupon = new RemovePercentageOnAttributeValues($stubFacade);
$actual = $coupon->getToolTip();
$expected = $tooltip;
$this->assertEquals($expected, $actual);
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown()
{
}
}

View File

@@ -0,0 +1,346 @@
<?php
/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* web : http://www.thelia.net */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
/*************************************************************************************/
namespace Thelia\Coupon\Type;
use Propel\Runtime\Collection\ObjectCollection;
use Thelia\Condition\ConditionCollection;
use Thelia\Condition\ConditionEvaluator;
use Thelia\Condition\Implementation\MatchForTotalAmount;
use Thelia\Condition\Operators;
use Thelia\Coupon\FacadeInterface;
use Thelia\Model\Category;
use Thelia\Model\CurrencyQuery;
use Thelia\Model\Product;
/**
* @package Coupon
* @author Franck Allimant <franck@cqfdev.fr>
*/
class RemovePercentageOnCategoriesTest extends \PHPUnit_Framework_TestCase
{
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
}
/**
* Generate adapter stub
*
* @param int $cartTotalPrice Cart total price
* @param string $checkoutCurrency Checkout currency
* @param string $i18nOutput Output from each translation
*
* @return \PHPUnit_Framework_MockObject_MockObject
*/
public function generateFacadeStub($cartTotalPrice = 400, $checkoutCurrency = 'EUR', $i18nOutput = '')
{
$stubFacade = $this->getMockBuilder('\Thelia\Coupon\BaseFacade')
->disableOriginalConstructor()
->getMock();
$currencies = CurrencyQuery::create();
$currencies = $currencies->find();
$stubFacade->expects($this->any())
->method('getAvailableCurrencies')
->will($this->returnValue($currencies));
$stubFacade->expects($this->any())
->method('getCartTotalPrice')
->will($this->returnValue($cartTotalPrice));
$stubFacade->expects($this->any())
->method('getCheckoutCurrency')
->will($this->returnValue($checkoutCurrency));
$stubFacade->expects($this->any())
->method('getConditionEvaluator')
->will($this->returnValue(new ConditionEvaluator()));
$stubTranslator = $this->getMockBuilder('\Thelia\Core\Translation\Translator')
->disableOriginalConstructor()
->getMock();
$stubTranslator->expects($this->any())
->method('trans')
->will($this->returnValue($i18nOutput));
$stubFacade->expects($this->any())
->method('getTranslator')
->will($this->returnValue($stubTranslator));
return $stubFacade;
}
public function generateMatchingCart(\PHPUnit_Framework_MockObject_MockObject $stubFacade) {
$category1 = new Category();
$category1->setId(10);
$category2 = new Category();
$category2->setId(20);
$category3 = new Category();
$category3->setId(30);
$product1 = new Product();
$product1->addCategory($category1)->addCategory($category2);
$product2 = new Product();
$product2->addCategory($category3);
$cartItem1Stub = $this->getMockBuilder('\Thelia\Model\CartItem')
->disableOriginalConstructor()
->getMock();
$cartItem1Stub
->expects($this->any())
->method('getProduct')
->will($this->returnValue($product1))
;
$cartItem1Stub
->expects($this->any())
->method('getQuantity')
->will($this->returnValue(1))
;
$cartItem1Stub
->expects($this->any())
->method('getPrice')
->will($this->returnValue(100))
;
$cartItem2Stub = $this->getMockBuilder('\Thelia\Model\CartItem')
->disableOriginalConstructor()
->getMock();
$cartItem2Stub
->expects($this->any())
->method('getProduct')
->will($this->returnValue($product2))
;
$cartItem2Stub
->expects($this->any())
->method('getQuantity')
->will($this->returnValue(2))
;
$cartItem2Stub
->expects($this->any())
->method('getPrice')
->will($this->returnValue(200))
;
$cartStub = $this->getMockBuilder('\Thelia\Model\Cart')
->disableOriginalConstructor()
->getMock();
$cartStub
->expects($this->any())
->method('getCartItems')
->will($this->returnValue([$cartItem1Stub, $cartItem2Stub]));
$stubFacade->expects($this->any())
->method('getCart')
->will($this->returnValue($cartStub));
}
public function generateNoMatchingCart(\PHPUnit_Framework_MockObject_MockObject $stubFacade) {
$category3 = new Category();
$category3->setId(30);
$product2 = new Product();
$product2->addCategory($category3);
$cartItem2Stub = $this->getMockBuilder('\Thelia\Model\CartItem')
->disableOriginalConstructor()
->getMock();
$cartItem2Stub->expects($this->any())
->method('getProduct')
->will($this->returnValue($product2));
$cartItem2Stub->expects($this->any())
->method('getQuantity')
->will($this->returnValue(2));
$cartItem2Stub
->expects($this->any())
->method('getPrice')
->will($this->returnValue(200000))
;
$cartStub = $this->getMockBuilder('\Thelia\Model\Cart')
->disableOriginalConstructor()
->getMock();
$cartStub
->expects($this->any())
->method('getCartItems')
->will($this->returnValue([$cartItem2Stub]));
$stubFacade->expects($this->any())
->method('getCart')
->will($this->returnValue($cartStub));
}
public function testSet()
{
$stubFacade = $this->generateFacadeStub();
$coupon = new RemovePercentageOnCategories($stubFacade);
$date = new \DateTime();
$coupon->set(
$stubFacade,
'TEST', 'TEST Coupon', 'This is a test coupon title', 'This is a test coupon description',
array('percentage' => 10, 'categories' => [10, 20]),
true, true, true, true,
254,
$date->setTimestamp(strtotime("today + 3 months")),
new ObjectCollection(),
new ObjectCollection(),
false
);
$condition1 = new MatchForTotalAmount($stubFacade);
$operators = array(
MatchForTotalAmount::CART_TOTAL => Operators::SUPERIOR,
MatchForTotalAmount::CART_CURRENCY => Operators::EQUAL
);
$values = array(
MatchForTotalAmount::CART_TOTAL => 40.00,
MatchForTotalAmount::CART_CURRENCY => 'EUR'
);
$condition1->setValidatorsFromForm($operators, $values);
$condition2 = new MatchForTotalAmount($stubFacade);
$operators = array(
MatchForTotalAmount::CART_TOTAL => Operators::INFERIOR,
MatchForTotalAmount::CART_CURRENCY => Operators::EQUAL
);
$values = array(
MatchForTotalAmount::CART_TOTAL => 400.00,
MatchForTotalAmount::CART_CURRENCY => 'EUR'
);
$condition2->setValidatorsFromForm($operators, $values);
$conditions = new ConditionCollection();
$conditions[] = $condition1;
$conditions[] = $condition2;
$coupon->setConditions($conditions);
$this->assertEquals('TEST', $coupon->getCode());
$this->assertEquals('TEST Coupon', $coupon->getTitle());
$this->assertEquals('This is a test coupon title', $coupon->getShortDescription());
$this->assertEquals('This is a test coupon description', $coupon->getDescription());
$this->assertEquals(true, $coupon->isCumulative());
$this->assertEquals(true, $coupon->isRemovingPostage());
$this->assertEquals(true, $coupon->isAvailableOnSpecialOffers());
$this->assertEquals(true, $coupon->isEnabled());
$this->assertEquals(254, $coupon->getMaxUsage());
$this->assertEquals($date, $coupon->getExpirationDate());
}
public function testMatch()
{
$stubFacade = $this->generateFacadeStub();
$coupon = new RemovePercentageOnCategories($stubFacade);
$date = new \DateTime();
$coupon->set(
$stubFacade,
'TEST', 'TEST Coupon', 'This is a test coupon title', 'This is a test coupon description',
array('percentage' => 10, 'categories' => [10, 20]),
true, true, true, true,
254,
$date->setTimestamp(strtotime("today + 3 months")),
new ObjectCollection(),
new ObjectCollection(),
false
);
$this->generateMatchingCart($stubFacade);
$this->assertEquals(10.00, $coupon->exec());
}
public function testNoMatch()
{
$stubFacade = $this->generateFacadeStub();
$coupon = new RemovePercentageOnCategories($stubFacade);
$date = new \DateTime();
$coupon->set(
$stubFacade,
'TEST', 'TEST Coupon', 'This is a test coupon title', 'This is a test coupon description',
array('percentage' => 10, 'categories' => [10, 20]),
true, true, true, true,
254,
$date->setTimestamp(strtotime("today + 3 months")),
new ObjectCollection(),
new ObjectCollection(),
false
);
$this->generateNoMatchingCart($stubFacade);
$this->assertEquals(0.00, $coupon->exec());
}
/**
* @covers Thelia\Coupon\Type\RemoveXAmount::getName
*/
public function testGetName()
{
$stubFacade = $this->generateFacadeStub(399, 'EUR', 'Coupon test name');
/** @var FacadeInterface $stubFacade */
$coupon = new RemovePercentageOnCategories($stubFacade);
$actual = $coupon->getName();
$expected = 'Coupon test name';
$this->assertEquals($expected, $actual);
}
/**
* @covers Thelia\Coupon\Type\RemoveXAmount::getToolTip
*/
public function testGetToolTip()
{
$tooltip = 'Coupon test tooltip';
$stubFacade = $this->generateFacadeStub(399, 'EUR', $tooltip);
/** @var FacadeInterface $stubFacade */
$coupon = new RemovePercentageOnCategories($stubFacade);
$actual = $coupon->getToolTip();
$expected = $tooltip;
$this->assertEquals($expected, $actual);
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown()
{
}
}

View File

@@ -0,0 +1,360 @@
<?php
/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* web : http://www.thelia.net */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
/*************************************************************************************/
namespace Thelia\Coupon\Type;
use Propel\Runtime\Collection\ObjectCollection;
use Thelia\Condition\ConditionCollection;
use Thelia\Condition\ConditionEvaluator;
use Thelia\Condition\Implementation\MatchForTotalAmount;
use Thelia\Condition\Operators;
use Thelia\Coupon\FacadeInterface;
use Thelia\Model\CurrencyQuery;
use Thelia\Model\Product;
/**
* @package Coupon
* @author Franck Allimant <franck@cqfdev.fr>
*/
class RemovePercentageOnProductsTest extends \PHPUnit_Framework_TestCase
{
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
}
/**
* Generate adapter stub
*
* @param int $cartTotalPrice Cart total price
* @param string $checkoutCurrency Checkout currency
* @param string $i18nOutput Output from each translation
*
* @return \PHPUnit_Framework_MockObject_MockObject
*/
public function generateFacadeStub($cartTotalPrice = 400, $checkoutCurrency = 'EUR', $i18nOutput = '')
{
$stubFacade = $this->getMockBuilder('\Thelia\Coupon\BaseFacade')
->disableOriginalConstructor()
->getMock();
$currencies = CurrencyQuery::create();
$currencies = $currencies->find();
$stubFacade->expects($this->any())
->method('getAvailableCurrencies')
->will($this->returnValue($currencies));
$stubFacade->expects($this->any())
->method('getCartTotalPrice')
->will($this->returnValue($cartTotalPrice));
$stubFacade->expects($this->any())
->method('getCheckoutCurrency')
->will($this->returnValue($checkoutCurrency));
$stubFacade->expects($this->any())
->method('getConditionEvaluator')
->will($this->returnValue(new ConditionEvaluator()));
$stubTranslator = $this->getMockBuilder('\Thelia\Core\Translation\Translator')
->disableOriginalConstructor()
->getMock();
$stubTranslator->expects($this->any())
->method('trans')
->will($this->returnValue($i18nOutput));
$stubFacade->expects($this->any())
->method('getTranslator')
->will($this->returnValue($stubTranslator));
return $stubFacade;
}
public function generateMatchingCart(\PHPUnit_Framework_MockObject_MockObject $stubFacade, $count) {
$product1 = new Product();
$product1->setId(10);
$product2 = new Product();
$product2->setId(20);
$cartItem1Stub = $this->getMockBuilder('\Thelia\Model\CartItem')
->disableOriginalConstructor()
->getMock();
$cartItem1Stub
->expects($this->any())
->method('getProduct')
->will($this->returnValue($product1))
;
$cartItem1Stub
->expects($this->any())
->method('getQuantity')
->will($this->returnValue(1))
;
$cartItem1Stub
->expects($this->any())
->method('getPrice')
->will($this->returnValue(100))
;
$cartItem2Stub = $this->getMockBuilder('\Thelia\Model\CartItem')
->disableOriginalConstructor()
->getMock();
$cartItem2Stub
->expects($this->any())
->method('getProduct')
->will($this->returnValue($product2));
$cartItem2Stub
->expects($this->any())
->method('getQuantity')
->will($this->returnValue(2))
;
$cartItem2Stub
->expects($this->any())
->method('getPrice')
->will($this->returnValue(150))
;
$cartStub = $this->getMockBuilder('\Thelia\Model\Cart')
->disableOriginalConstructor()
->getMock();
if ($count == 1)
$ret = [$cartItem1Stub];
else
$ret = [$cartItem1Stub, $cartItem2Stub];
$cartStub
->expects($this->any())
->method('getCartItems')
->will($this->returnValue($ret));
$stubFacade->expects($this->any())
->method('getCart')
->will($this->returnValue($cartStub));
}
public function generateNoMatchingCart(\PHPUnit_Framework_MockObject_MockObject $stubFacade) {
$product2 = new Product();
$product2->setId(30);
$cartItem2Stub = $this->getMockBuilder('\Thelia\Model\CartItem')
->disableOriginalConstructor()
->getMock();
$cartItem2Stub->expects($this->any())
->method('getProduct')
->will($this->returnValue($product2))
;
$cartItem2Stub->expects($this->any())
->method('getQuantity')
->will($this->returnValue(2))
;
$cartItem2Stub
->expects($this->any())
->method('getPrice')
->will($this->returnValue(11000))
;
$cartStub = $this->getMockBuilder('\Thelia\Model\Cart')
->disableOriginalConstructor()
->getMock();
$cartStub
->expects($this->any())
->method('getCartItems')
->will($this->returnValue([$cartItem2Stub]));
$stubFacade->expects($this->any())
->method('getCart')
->will($this->returnValue($cartStub));
}
public function testSet()
{
$stubFacade = $this->generateFacadeStub();
$coupon = new RemovePercentageOnProducts($stubFacade);
$date = new \DateTime();
$coupon->set(
$stubFacade,
'TEST', 'TEST Coupon', 'This is a test coupon title', 'This is a test coupon description',
array('percentage' => 10.00, 'products' => [10, 20]),
true, true, true, true,
254,
$date->setTimestamp(strtotime("today + 3 months")),
new ObjectCollection(),
new ObjectCollection(),
false
);
$condition1 = new MatchForTotalAmount($stubFacade);
$operators = array(
MatchForTotalAmount::CART_TOTAL => Operators::SUPERIOR,
MatchForTotalAmount::CART_CURRENCY => Operators::EQUAL
);
$values = array(
MatchForTotalAmount::CART_TOTAL => 40.00,
MatchForTotalAmount::CART_CURRENCY => 'EUR'
);
$condition1->setValidatorsFromForm($operators, $values);
$condition2 = new MatchForTotalAmount($stubFacade);
$operators = array(
MatchForTotalAmount::CART_TOTAL => Operators::INFERIOR,
MatchForTotalAmount::CART_CURRENCY => Operators::EQUAL
);
$values = array(
MatchForTotalAmount::CART_TOTAL => 400.00,
MatchForTotalAmount::CART_CURRENCY => 'EUR'
);
$condition2->setValidatorsFromForm($operators, $values);
$conditions = new ConditionCollection();
$conditions[] = $condition1;
$conditions[] = $condition2;
$coupon->setConditions($conditions);
$this->assertEquals('TEST', $coupon->getCode());
$this->assertEquals('TEST Coupon', $coupon->getTitle());
$this->assertEquals('This is a test coupon title', $coupon->getShortDescription());
$this->assertEquals('This is a test coupon description', $coupon->getDescription());
$this->assertEquals(true, $coupon->isCumulative());
$this->assertEquals(true, $coupon->isRemovingPostage());
$this->assertEquals(true, $coupon->isAvailableOnSpecialOffers());
$this->assertEquals(true, $coupon->isEnabled());
$this->assertEquals(254, $coupon->getMaxUsage());
$this->assertEquals($date, $coupon->getExpirationDate());
}
public function testMatchOne()
{
$stubFacade = $this->generateFacadeStub();
$coupon = new RemovePercentageOnProducts($stubFacade);
$date = new \DateTime();
$coupon->set(
$stubFacade,
'TEST', 'TEST Coupon', 'This is a test coupon title', 'This is a test coupon description',
array('percentage' => 10.00, 'products' => [10, 20]),
true, true, true, true,
254,
$date->setTimestamp(strtotime("today + 3 months")),
new ObjectCollection(),
new ObjectCollection(),
false
);
$this->generateMatchingCart($stubFacade, 1);
$this->assertEquals(10.00, $coupon->exec());
}
public function testMatchSeveral()
{
$stubFacade = $this->generateFacadeStub();
$coupon = new RemovePercentageOnProducts($stubFacade);
$date = new \DateTime();
$coupon->set(
$stubFacade,
'TEST', 'TEST Coupon', 'This is a test coupon title', 'This is a test coupon description',
array('percentage' => 10.00, 'products' => [10, 20]),
true, true, true, true,
254,
$date->setTimestamp(strtotime("today + 3 months")),
new ObjectCollection(),
new ObjectCollection(),
false
);
$this->generateMatchingCart($stubFacade, 2);
$this->assertEquals(40.00, $coupon->exec());
}
public function testNoMatch()
{
$stubFacade = $this->generateFacadeStub();
$coupon = new RemovePercentageOnProducts($stubFacade);
$date = new \DateTime();
$coupon->set(
$stubFacade,
'TEST', 'TEST Coupon', 'This is a test coupon title', 'This is a test coupon description',
array('percentage' => 10.00, 'products' => [10, 20]),
true, true, true, true,
254,
$date->setTimestamp(strtotime("today + 3 months")),
new ObjectCollection(),
new ObjectCollection(),
false
);
$this->generateNoMatchingCart($stubFacade);
$this->assertEquals(0.00, $coupon->exec());
}
public function testGetName()
{
$stubFacade = $this->generateFacadeStub(399, 'EUR', 'Coupon test name');
/** @var FacadeInterface $stubFacade */
$coupon = new RemovePercentageOnProducts($stubFacade);
$actual = $coupon->getName();
$expected = 'Coupon test name';
$this->assertEquals($expected, $actual);
}
public function testGetToolTip()
{
$tooltip = 'Coupon test tooltip';
$stubFacade = $this->generateFacadeStub(399, 'EUR', $tooltip);
/** @var FacadeInterface $stubFacade */
$coupon = new RemovePercentageOnProducts($stubFacade);
$actual = $coupon->getToolTip();
$expected = $tooltip;
$this->assertEquals($expected, $actual);
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown()
{
}
}