Initial Commit

This commit is contained in:
2019-11-21 12:25:31 +01:00
commit f4aabcb9b1
13959 changed files with 787761 additions and 0 deletions

View File

@@ -0,0 +1,562 @@
<?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;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Thelia\Condition\ConditionCollection;
use Thelia\Condition\ConditionEvaluator;
use Thelia\Condition\ConditionFactory;
use Thelia\Condition\Implementation\MatchForTotalAmount;
use Thelia\Condition\Operators;
use Thelia\Core\Translation\Translator;
use Thelia\Coupon\Type\RemoveXAmount;
use Thelia\Model\Coupon;
use Thelia\Model\CurrencyQuery;
use Thelia\Model\Customer;
/**
* Unit Test CouponFactory Class
* Generated by PHPUnit_SkeletonGenerator 1.2.1 on 2013-11-17 at 18:59:24.
*
* @package Coupon
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
*/
class CouponFactoryTest 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()));
$customer = new Customer();
$customer->setId(1);
$stubFacade->expects($this->any())
->method('getCustomer')
->will($this->returnValue($customer));
$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;
}
/**
* Generate a valid Coupon model
*
* @param $facade
* @param ConditionFactory $conditionFactory
* @return Coupon
*/
public function generateCouponModel($facade, ConditionFactory $conditionFactory)
{
// Coupons
$coupon1 = new Coupon();
$coupon1->setCode('XMAS');
$coupon1->setType('thelia.coupon.type.remove_x_amount');
$coupon1->setTitle('Christmas coupon');
$coupon1->setShortDescription('Coupon for Christmas removing 10€ if your total checkout is more than 40€');
$coupon1->setDescription('Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras at luctus tellus. Integer turpis mauris, aliquet vitae risus tristique, pellentesque vestibulum urna. Vestibulum sodales laoreet lectus dictum suscipit. Praesent vulputate, sem id varius condimentum, quam magna tempor elit, quis venenatis ligula nulla eget libero. Cras egestas euismod tellus, id pharetra leo suscipit quis. Donec lacinia ac lacus et ultricies. Nunc in porttitor neque. Proin at quam congue, consectetur orci sed, congue nulla. Nulla eleifend nunc ligula, nec pharetra elit tempus quis. Vivamus vel mauris sed est dictum blandit. Maecenas blandit dapibus velit ut sollicitudin. In in euismod mauris, consequat viverra magna. Cras velit velit, sollicitudin commodo tortor gravida, tempus varius nulla.
Donec rhoncus leo mauris, id porttitor ante luctus tempus. Curabitur quis augue feugiat, ullamcorper mauris ac, interdum mi. Quisque aliquam lorem vitae felis lobortis, id interdum turpis mattis. Vestibulum diam massa, ornare congue blandit quis, facilisis at nisl. In tortor metus, venenatis non arcu nec, sollicitudin ornare nisl. Nunc erat risus, varius nec urna at, iaculis lacinia elit. Aenean ut felis tempus, tincidunt odio non, sagittis nisl. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Donec vitae hendrerit elit. Nunc sit amet gravida risus, euismod lobortis massa. Nam a erat mauris. Nam a malesuada lorem. Nulla id accumsan dolor, sed rhoncus tellus. Quisque dictum felis sed leo auctor, at volutpat lectus viverra. Morbi rutrum, est ac aliquam imperdiet, nibh sem sagittis justo, ac mattis magna lacus eu nulla.
Duis interdum lectus nulla, nec pellentesque sapien condimentum at. Suspendisse potenti. Sed eu purus tellus. Nunc quis rhoncus metus. Fusce vitae tellus enim. Interdum et malesuada fames ac ante ipsum primis in faucibus. Etiam tempor porttitor erat vitae iaculis. Sed est elit, consequat non ornare vitae, vehicula eget lectus. Etiam consequat sapien mauris, eget consectetur magna imperdiet eget. Nunc sollicitudin luctus velit, in commodo nulla adipiscing fermentum. Fusce nisi sapien, posuere vitae metus sit amet, facilisis sollicitudin dui. Fusce ultricies auctor enim sit amet iaculis. Morbi at vestibulum enim, eget adipiscing eros.
Praesent ligula lorem, faucibus ut metus quis, fermentum iaculis erat. Pellentesque elit erat, lacinia sed semper ac, sagittis vel elit. Nam eu convallis est. Curabitur rhoncus odio vitae consectetur pellentesque. Nam vitae arcu nec ante scelerisque dignissim vel nec neque. Suspendisse augue nulla, mollis eget dui et, tempor facilisis erat. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi ac diam ipsum. Donec convallis dui ultricies velit auctor, non lobortis nulla ultrices. Morbi vitae dignissim ante, sit amet lobortis tortor. Nunc dapibus condimentum augue, in molestie neque congue non.
Sed facilisis pellentesque nisl, eu tincidunt erat scelerisque a. Nullam malesuada tortor vel erat volutpat tincidunt. In vehicula diam est, a convallis eros scelerisque ut. Donec aliquet venenatis iaculis. Ut a arcu gravida, placerat dui eu, iaculis nisl. Quisque adipiscing orci sit amet dui dignissim lacinia. Sed vulputate lorem non dolor adipiscing ornare. Morbi ornare id nisl id aliquam. Ut fringilla elit ante, nec lacinia enim fermentum sit amet. Aenean rutrum lorem eu convallis pharetra. Cras malesuada varius metus, vitae gravida velit. Nam a varius ipsum, ac commodo dolor. Phasellus nec elementum elit. Etiam vel adipiscing leo.');
$coupon1->setAmount(10.00);
$coupon1->setIsUsed(true);
$coupon1->setIsEnabled(true);
$date = new \DateTime();
$coupon1->setExpirationDate($date->setTimestamp(strtotime("today + 3 months")));
$condition1 = new MatchForTotalAmount($facade);
$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($facade);
$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;
$serializedConditions = $conditionFactory->serializeConditionCollection($conditions);
$coupon1->setSerializedConditions($serializedConditions);
$coupon1->setMaxUsage(40);
$coupon1->setIsCumulative(true);
$coupon1->setIsRemovingPostage(false);
$coupon1->setIsAvailableOnSpecialOffers(true);
return $coupon1;
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown()
{
}
/**
* @covers Thelia\Coupon\CouponFactory::buildCouponFromCode
*/
public function testBuildCouponFromCode()
{
$this->markTestSkipped('It\'s necessary to mock the facade');
/** @var FacadeInterface|\PHPUnit_Framework_MockObject_MockObject $stubFacade */
$stubFacade = $this->generateFacadeStub();
$stubContainer = $this->getMockContainer();
$conditionFactory = new ConditionFactory($stubContainer);
$couponModel = $this->generateCouponModel($stubFacade, $conditionFactory);
$stubFacade->expects($this->any())
->method('findOneCouponByCode')
->will($this->returnValue($couponModel));
$couponManager = new RemoveXAmount($stubFacade);
$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;
$stubConditionFactory = $this->getMockBuilder('\Thelia\Condition\ConditionFactory')
->disableOriginalConstructor()
->getMock();
$stubConditionFactory->expects($this->any())
->method('unserializeConditionCollection')
->will($this->returnValue($conditions));
$stubContainer->expects($this->any())
->method('get')
->will($this->onConsecutiveCalls($stubFacade, $couponManager, $stubConditionFactory));
$stubContainer->expects($this->any())
->method('has')
->will($this->returnValue(true));
$factory = new CouponFactory($stubContainer);
$expected = $couponManager;
$actual = $factory->buildCouponFromCode('XMAS');
$this->assertEquals($expected, $actual);
}
/**
* @covers Thelia\Coupon\CouponFactory::buildCouponFromCode
* @expectedException \Thelia\Exception\CouponNoUsageLeftException
*/
public function testBuildCouponFromCodeUsageLimitCoupon()
{
$this->markTestSkipped('It\'s necessary to mock the facade');
/** @var FacadeInterface|\PHPUnit_Framework_MockObject_MockObject $stubFacade */
$stubFacade = $this->generateFacadeStub();
$stubContainer = $this->getMockContainer();
$conditionFactory = new ConditionFactory($stubContainer);
$couponModel = $this->generateCouponModel($stubFacade, $conditionFactory);
$date = new \DateTime();
$couponModel->setExpirationDate($date->setTimestamp(strtotime("today + 3 months")));
$couponModel->setMaxUsage(0);
$couponModel->setPerCustomerUsageCount(false);
$stubFacade->expects($this->any())
->method('findOneCouponByCode')
->will($this->returnValue($couponModel));
$couponManager = new RemoveXAmount($stubFacade);
$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;
$stubConditionFactory = $this->getMockBuilder('\Thelia\Condition\ConditionFactory')
->disableOriginalConstructor()
->getMock();
$stubConditionFactory->expects($this->any())
->method('unserializeConditionCollection')
->will($this->returnValue($conditions));
$stubContainer->expects($this->any())
->method('get')
->will($this->onConsecutiveCalls($stubFacade, $couponManager, $stubConditionFactory));
$stubContainer->expects($this->any())
->method('has')
->will($this->returnValue(true));
$dummy = new Translator($stubContainer);
$factory = new CouponFactory($stubContainer);
$factory->buildCouponFromCode('XMAS');
}
/**
* @covers Thelia\Coupon\CouponFactory::buildCouponFromCode
*/
public function testBuildCouponFromCodeUnknownCode()
{
/** @var FacadeInterface|\PHPUnit_Framework_MockObject_MockObject $stubFacade */
$stubFacade = $this->generateFacadeStub();
$stubContainer = $this->getMockContainer();
$stubFacade->expects($this->any())
->method('findOneCouponByCode')
->will($this->returnValue(null));
$couponManager = new RemoveXAmount($stubFacade);
$stubContainer->expects($this->any())
->method('get')
->will($this->onConsecutiveCalls($stubFacade, $couponManager));
$stubContainer->expects($this->any())
->method('has')
->will($this->returnValue(true));
$factory = new CouponFactory($stubContainer);
$actual = $factory->buildCouponFromCode('XMAS');
$expected = false;
$this->assertEquals($expected, $actual, 'CouponFactory->buildCouponFromCode should return false if the given code is unknown');
}
/**
* @covers Thelia\Coupon\CouponFactory::buildCouponFromCode
* @expectedException \Thelia\Exception\CouponExpiredException
*/
public function testBuildCouponFromCodeExpiredCoupon()
{
$this->markTestSkipped('It\'s necessary to mock the facade');
/** @var FacadeInterface|\PHPUnit_Framework_MockObject_MockObject $stubFacade */
$stubFacade = $this->generateFacadeStub();
$stubContainer = $this->getMockContainer();
$conditionFactory = new ConditionFactory($stubContainer);
$couponModel = $this->generateCouponModel($stubFacade, $conditionFactory);
$date = new \DateTime();
$couponModel->setExpirationDate($date->setTimestamp(strtotime("today - 3 months")));
$stubFacade->expects($this->any())
->method('findOneCouponByCode')
->will($this->returnValue($couponModel));
$couponManager = new RemoveXAmount($stubFacade);
$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;
$stubConditionFactory = $this->getMockBuilder('\Thelia\Condition\ConditionFactory')
->disableOriginalConstructor()
->getMock();
$stubConditionFactory->expects($this->any())
->method('unserializeConditionCollection')
->will($this->returnValue($conditions));
$stubContainer->expects($this->any())
->method('get')
->will($this->onConsecutiveCalls($stubFacade, $couponManager, $stubConditionFactory));
$stubContainer->expects($this->any())
->method('has')
->will($this->returnValue(true));
$dummy = new Translator($stubContainer);
$factory = new CouponFactory($stubContainer);
$factory->buildCouponFromCode('XMAS');
}
/**
* @covers Thelia\Coupon\CouponFactory::buildCouponFromCode
* @expectedException \Thelia\Exception\InvalidConditionException
*/
public function testBuildCouponFromCodeNoConditionCoupon()
{
$this->markTestSkipped('It\'s necessary to mock the facade');
/** @var FacadeInterface|\PHPUnit_Framework_MockObject_MockObject $stubFacade */
$stubFacade = $this->generateFacadeStub();
$stubContainer = $this->getMockContainer();
$conditionFactory = new ConditionFactory($stubContainer);
$couponModel = $this->generateCouponModel($stubFacade, $conditionFactory);
$stubFacade->expects($this->any())
->method('findOneCouponByCode')
->will($this->returnValue($couponModel));
$couponManager = new RemoveXAmount($stubFacade);
$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();
$stubConditionFactory = $this->getMockBuilder('\Thelia\Condition\ConditionFactory')
->disableOriginalConstructor()
->getMock();
$stubConditionFactory->expects($this->any())
->method('unserializeConditionCollection')
->will($this->returnValue($conditions));
$stubContainer->expects($this->any())
->method('get')
->will($this->onConsecutiveCalls($stubFacade, $couponManager, $stubConditionFactory));
$stubContainer->expects($this->any())
->method('has')
->will($this->returnValue(true));
$factory = new CouponFactory($stubContainer);
$factory->buildCouponFromCode('XMAS');
}
/**
* @covers Thelia\Coupon\CouponFactory::buildCouponFromModel
*/
public function testBuildCouponFromModel()
{
$this->markTestSkipped('It\'s necessary to mock the facade');
/** @var FacadeInterface|\PHPUnit_Framework_MockObject_MockObject $stubFacade */
$stubFacade = $this->generateFacadeStub();
$stubContainer = $this->getMockContainer();
$conditionFactory = new ConditionFactory($stubContainer);
$couponModel = $this->generateCouponModel($stubFacade, $conditionFactory);
$stubFacade->expects($this->any())
->method('findOneCouponByCode')
->will($this->returnValue($couponModel));
$couponManager = new RemoveXAmount($stubFacade);
$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;
$stubConditionFactory = $this->getMockBuilder('\Thelia\Condition\ConditionFactory')
->disableOriginalConstructor()
->getMock();
$stubConditionFactory->expects($this->any())
->method('unserializeConditionCollection')
->will($this->returnValue($conditions));
$stubContainer->expects($this->any())
->method('get')
->will($this->onConsecutiveCalls($stubFacade, $couponManager, $stubConditionFactory));
$stubContainer->expects($this->any())
->method('has')
->will($this->returnValue(true));
$factory = new CouponFactory($stubContainer);
$expected = $couponManager;
$actual = $factory->buildCouponFromModel($couponModel);
$this->assertEquals($expected, $actual);
}
/**
* @return \PHPUnit_Framework_MockObject_MockObject|ContainerInterface
*/
protected function getMockContainer()
{
$stubContainer = $this->getMock('\Symfony\Component\DependencyInjection\Container');
$stubContainer->expects($this->any())
->method('has')
->with('request_stack')
->will($this->returnValue(false));
$stubContainer->expects($this->any())
->method('has')
->with('request')
->will($this->returnValue(false));
return $stubContainer;
}
}

View File

@@ -0,0 +1,598 @@
<?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;
use Propel\Runtime\Collection\ObjectCollection;
use Thelia\Condition\ConditionCollection;
use Thelia\Condition\ConditionEvaluator;
use Thelia\Condition\ConditionFactory;
use Thelia\Condition\Implementation\MatchForTotalAmount;
use Thelia\Condition\Operators;
use Thelia\Coupon\Type\RemoveXAmount;
use Thelia\Model\Coupon;
use Thelia\Model\CurrencyQuery;
use Thelia\Model\Order;
/**
* Unit Test CouponManager Class
* Generated by PHPUnit_SkeletonGenerator 1.2.1 on 2013-11-17 at 18:59:24.
*
* @package Coupon
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
*/
class CouponManagerTest 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()
{
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown()
{
}
/**
* Generate a valid Coupon model
*/
public function generateCouponModel($facade, ConditionFactory $conditionFactory)
{
// Coupons
$coupon1 = new Coupon();
$coupon1->setCode('XMAS');
$coupon1->setType('thelia.coupon.type.remove_x_amount');
$coupon1->setTitle('Christmas coupon');
$coupon1->setShortDescription('Coupon for Christmas removing 10€ if your total checkout is more than 40€');
$coupon1->setDescription('Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras at luctus tellus. Integer turpis mauris, aliquet vitae risus tristique, pellentesque vestibulum urna. Vestibulum sodales laoreet lectus dictum suscipit. Praesent vulputate, sem id varius condimentum, quam magna tempor elit, quis venenatis ligula nulla eget libero. Cras egestas euismod tellus, id pharetra leo suscipit quis. Donec lacinia ac lacus et ultricies. Nunc in porttitor neque. Proin at quam congue, consectetur orci sed, congue nulla. Nulla eleifend nunc ligula, nec pharetra elit tempus quis. Vivamus vel mauris sed est dictum blandit. Maecenas blandit dapibus velit ut sollicitudin. In in euismod mauris, consequat viverra magna. Cras velit velit, sollicitudin commodo tortor gravida, tempus varius nulla.
Donec rhoncus leo mauris, id porttitor ante luctus tempus. Curabitur quis augue feugiat, ullamcorper mauris ac, interdum mi. Quisque aliquam lorem vitae felis lobortis, id interdum turpis mattis. Vestibulum diam massa, ornare congue blandit quis, facilisis at nisl. In tortor metus, venenatis non arcu nec, sollicitudin ornare nisl. Nunc erat risus, varius nec urna at, iaculis lacinia elit. Aenean ut felis tempus, tincidunt odio non, sagittis nisl. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Donec vitae hendrerit elit. Nunc sit amet gravida risus, euismod lobortis massa. Nam a erat mauris. Nam a malesuada lorem. Nulla id accumsan dolor, sed rhoncus tellus. Quisque dictum felis sed leo auctor, at volutpat lectus viverra. Morbi rutrum, est ac aliquam imperdiet, nibh sem sagittis justo, ac mattis magna lacus eu nulla.
Duis interdum lectus nulla, nec pellentesque sapien condimentum at. Suspendisse potenti. Sed eu purus tellus. Nunc quis rhoncus metus. Fusce vitae tellus enim. Interdum et malesuada fames ac ante ipsum primis in faucibus. Etiam tempor porttitor erat vitae iaculis. Sed est elit, consequat non ornare vitae, vehicula eget lectus. Etiam consequat sapien mauris, eget consectetur magna imperdiet eget. Nunc sollicitudin luctus velit, in commodo nulla adipiscing fermentum. Fusce nisi sapien, posuere vitae metus sit amet, facilisis sollicitudin dui. Fusce ultricies auctor enim sit amet iaculis. Morbi at vestibulum enim, eget adipiscing eros.
Praesent ligula lorem, faucibus ut metus quis, fermentum iaculis erat. Pellentesque elit erat, lacinia sed semper ac, sagittis vel elit. Nam eu convallis est. Curabitur rhoncus odio vitae consectetur pellentesque. Nam vitae arcu nec ante scelerisque dignissim vel nec neque. Suspendisse augue nulla, mollis eget dui et, tempor facilisis erat. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi ac diam ipsum. Donec convallis dui ultricies velit auctor, non lobortis nulla ultrices. Morbi vitae dignissim ante, sit amet lobortis tortor. Nunc dapibus condimentum augue, in molestie neque congue non.
Sed facilisis pellentesque nisl, eu tincidunt erat scelerisque a. Nullam malesuada tortor vel erat volutpat tincidunt. In vehicula diam est, a convallis eros scelerisque ut. Donec aliquet venenatis iaculis. Ut a arcu gravida, placerat dui eu, iaculis nisl. Quisque adipiscing orci sit amet dui dignissim lacinia. Sed vulputate lorem non dolor adipiscing ornare. Morbi ornare id nisl id aliquam. Ut fringilla elit ante, nec lacinia enim fermentum sit amet. Aenean rutrum lorem eu convallis pharetra. Cras malesuada varius metus, vitae gravida velit. Nam a varius ipsum, ac commodo dolor. Phasellus nec elementum elit. Etiam vel adipiscing leo.');
$coupon1->setAmount(10.00);
$coupon1->setIsUsed(true);
$coupon1->setIsEnabled(true);
$date = new \DateTime();
$coupon1->setExpirationDate($date->setTimestamp(strtotime("today + 3 months")));
$condition1 = new MatchForTotalAmount($facade);
$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($facade);
$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;
$serializedConditions = $conditionFactory->serializeConditionCollection($conditions);
$coupon1->setSerializedConditions($serializedConditions);
$coupon1->setMaxUsage(40);
$coupon1->setIsCumulative(true);
$coupon1->setIsRemovingPostage(true);
$coupon1->setIsAvailableOnSpecialOffers(true);
return $coupon1;
}
/**
* @covers Thelia\Coupon\CouponManager::getDiscount
* @covers Thelia\Coupon\CouponManager::isCouponRemovingPostage
* @covers Thelia\Coupon\CouponManager::sortCoupons
* @covers Thelia\Coupon\CouponManager::getEffect
*/
public function testGetDiscountCumulativeRemovingPostage()
{
$stubFacade = $this->generateFacadeStub();
$stubContainer = $this->getMock('\Symfony\Component\DependencyInjection\Container');
$conditionFactory = new ConditionFactory($stubContainer);
$conditions = new ConditionCollection();
$stubConditionFactory = $this->getMockBuilder('\Thelia\Condition\ConditionFactory')
->disableOriginalConstructor()
->getMock();
$stubConditionFactory->expects($this->any())
->method('unserializeConditionCollection')
->will($this->returnValue($conditions));
$couponManager = new RemoveXAmount($stubFacade);
$stubContainer->expects($this->any())
->method('get')
->will($this->onConsecutiveCalls($stubFacade, $couponManager, $stubConditionFactory, clone $couponManager, $stubConditionFactory, $stubFacade));
$stubContainer->expects($this->any())
->method('has')
->will($this->returnValue(true));
$couponFactory = new CouponFactory($stubContainer);
$model1 = $this->generateCouponModel($stubFacade, $conditionFactory);
$model1->setCode('XMAS')->setIsRemovingPostage(false)->setAmount(21.00);
$coupon1 = $couponFactory->buildCouponFromModel($model1);
$model2 = $this->generateCouponModel($stubFacade, $conditionFactory);
$model2->setCode('XMAS2')->setIsRemovingPostage(true)->setAmount(21.50);
$coupon2 = $couponFactory->buildCouponFromModel($model2);
$stubFacade->expects($this->any())
->method('getCurrentCoupons')
->will($this->returnValue(array($coupon1, $coupon2)));
$stubFacade->expects($this->any())
->method('getCheckoutPostagePrice')
->will($this->returnValue(8.30));
$stubFacade->expects($this->any())
->method('getCartTotalTaxPrice')
->will($this->returnValue(122.53));
$couponManager = new CouponManager($stubContainer);
$couponManager->addAvailableCoupon($coupon1);
$couponManager->addAvailableCoupon($coupon2);
$actual = $couponManager->getDiscount();
$expected = 21 + 21.50;
$order = new Order();
$order->setChoosenDeliveryAddress(1);
$order->setDeliveryModuleId(1);
$this->assertEquals($expected, $actual);
$this->assertTrue($couponManager->isCouponRemovingPostage($order));
}
/**
* @covers Thelia\Coupon\CouponManager::getDiscount
* @covers Thelia\Coupon\CouponManager::isCouponRemovingPostage
* @covers Thelia\Coupon\CouponManager::sortCoupons
* @covers Thelia\Coupon\CouponManager::getEffect
*/
public function testGetDiscountNonCumulativeNotRemovingPostage()
{
$stubFacade = $this->generateFacadeStub();
$stubContainer = $this->getMock('\Symfony\Component\DependencyInjection\Container');
$conditionFactory = new ConditionFactory($stubContainer);
$conditions = new ConditionCollection();
$stubConditionFactory = $this->getMockBuilder('\Thelia\Condition\ConditionFactory')
->disableOriginalConstructor()
->getMock();
$stubConditionFactory->expects($this->any())
->method('unserializeConditionCollection')
->will($this->returnValue($conditions));
$couponManager = new RemoveXAmount($stubFacade);
$stubContainer->expects($this->any())
->method('get')
->will($this->onConsecutiveCalls($stubFacade, $couponManager, $stubConditionFactory, clone $couponManager, $stubConditionFactory, $stubFacade));
$stubContainer->expects($this->any())
->method('has')
->will($this->returnValue(true));
$couponFactory = new CouponFactory($stubContainer);
$model1 = $this->generateCouponModel($stubFacade, $conditionFactory);
$model1->setAmount(21.00);
$coupon1 = $couponFactory->buildCouponFromModel($model1);
$model2 = $this->generateCouponModel($stubFacade, $conditionFactory);
$model2->setCode('XMAS2')->setIsRemovingPostage(false)->setAmount(21.50)->setIsCumulative(false);
$coupon2 = $couponFactory->buildCouponFromModel($model2);
$stubFacade->expects($this->any())
->method('getCurrentCoupons')
->will($this->returnValue(array($coupon1, $coupon2)));
$stubFacade->expects($this->any())
->method('getCheckoutPostagePrice')
->will($this->returnValue(8.30));
$stubFacade->expects($this->any())
->method('getCartTotalTaxPrice')
->will($this->returnValue(122.53));
$couponManager = new CouponManager($stubContainer);
$couponManager->addAvailableCoupon($coupon1);
$couponManager->addAvailableCoupon($coupon2);
$actual = $couponManager->getDiscount();
$expected = 21.50;
$this->assertEquals($expected, $actual);
}
/**
* @covers Thelia\Coupon\CouponManager::getDiscount
* @covers Thelia\Coupon\CouponManager::isCouponRemovingPostage
* @covers Thelia\Coupon\CouponManager::sortCoupons
* @covers Thelia\Coupon\CouponManager::getEffect
*/
public function testGetDiscountGreaterThanCartAmount()
{
$stubFacade = $this->generateFacadeStub(12.25);
$stubFacade->expects($this->any())
->method('getCheckoutPostagePrice')
->will($this->returnValue(8.30));
$stubContainer = $this->getMock('\Symfony\Component\DependencyInjection\Container');
$conditionFactory = new ConditionFactory($stubContainer);
$conditions = new ConditionCollection();
$stubConditionFactory = $this->getMockBuilder('\Thelia\Condition\ConditionFactory')
->disableOriginalConstructor()
->getMock();
$stubConditionFactory->expects($this->any())
->method('unserializeConditionCollection')
->will($this->returnValue($conditions));
$couponManager = new RemoveXAmount($stubFacade);
$stubContainer->expects($this->any())
->method('get')
->will($this->onConsecutiveCalls($stubFacade, $couponManager, $stubConditionFactory, clone $couponManager, $stubConditionFactory, $stubFacade));
$stubContainer->expects($this->any())
->method('has')
->will($this->returnValue(true));
$couponFactory = new CouponFactory($stubContainer);
$model1 = $this->generateCouponModel($stubFacade, $conditionFactory);
$model1->setAmount(21.00);
$coupon1 = $couponFactory->buildCouponFromModel($model1);
$model2 = $this->generateCouponModel($stubFacade, $conditionFactory);
$model2->setCode('XMAS2')->setIsRemovingPostage(false)->setAmount(21.50)->setIsCumulative(false);
$coupon2 = $couponFactory->buildCouponFromModel($model2);
$stubFacade->expects($this->any())
->method('getCurrentCoupons')
->will($this->returnValue(array($coupon1, $coupon2)));
$couponManager = new CouponManager($stubContainer);
$couponManager->addAvailableCoupon($coupon1);
$couponManager->addAvailableCoupon($coupon2);
$actual = $couponManager->getDiscount();
$expected = 12.25;
$this->assertEquals($expected, $actual);
}
/**
* @covers Thelia\Coupon\CouponManager::addAvailableCoupon
* @covers Thelia\Coupon\CouponManager::getAvailableCoupons
*/
public function testGetAvailableCoupons()
{
$stubFacade = $this->generateFacadeStub();
$stubContainer = $this->getMock('\Symfony\Component\DependencyInjection\Container');
$conditionFactory = new ConditionFactory($stubContainer);
$stubFacade->expects($this->any())
->method('getCurrentCoupons')
->will($this->returnValue(array()));
$conditions = new ConditionCollection();
$stubConditionFactory = $this->getMockBuilder('\Thelia\Condition\ConditionFactory')
->disableOriginalConstructor()
->getMock();
$stubConditionFactory->expects($this->any())
->method('unserializeConditionCollection')
->will($this->returnValue($conditions));
$couponManager = new RemoveXAmount($stubFacade);
$stubContainer->expects($this->any())
->method('get')
->will($this->onConsecutiveCalls($stubFacade, $couponManager, $stubConditionFactory, $stubFacade));
$stubContainer->expects($this->any())
->method('has')
->will($this->returnValue(true));
$couponFactory = new CouponFactory($stubContainer);
$model1 = $this->generateCouponModel($stubFacade, $conditionFactory);
$coupon1 = $couponFactory->buildCouponFromModel($model1);
$coupon2 = clone $coupon1;
$couponManager = new CouponManager($stubContainer);
$couponManager->addAvailableCoupon($coupon1);
$couponManager->addAvailableCoupon($coupon2);
$actual = $couponManager->getAvailableCoupons();
$expected = array($coupon1, $coupon2);
$this->assertEquals($expected, $actual);
}
/**
* @covers Thelia\Coupon\CouponManager::addAvailableCondition
* @covers Thelia\Coupon\CouponManager::getAvailableConditions
*/
public function testGetAvailableConditions()
{
$stubFacade = $this->generateFacadeStub();
$stubContainer = $this->getMock('\Symfony\Component\DependencyInjection\Container');
$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;
$stubFacade->expects($this->any())
->method('getCurrentCoupons')
->will($this->returnValue(array()));
$stubContainer->expects($this->any())
->method('get')
->will($this->onConsecutiveCalls($stubFacade));
$couponManager = new CouponManager($stubContainer);
$couponManager->addAvailableCondition($condition1);
$couponManager->addAvailableCondition($condition2);
$actual = $couponManager->getAvailableConditions();
$expected = array($condition1, $condition2);
$this->assertEquals($expected, $actual);
}
/**
* @covers Thelia\Coupon\CouponManager::decrementQuantity
*/
public function testDecrementeQuantity()
{
$stubFacade = $this->generateFacadeStub();
$stubContainer = $this->getMock('\Symfony\Component\DependencyInjection\Container');
$coupon = new RemoveXAmount($stubFacade);
$date = new \DateTime();
$coupon->set(
$stubFacade,
'XMAS',
'',
'',
'',
array('amount' => 21.00),
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);
$stubFacade->expects($this->any())
->method('getCurrentCoupons')
->will($this->returnValue(array($coupon)));
$stubContainer->expects($this->any())
->method('get')
->will($this->onConsecutiveCalls($stubFacade));
$couponManager = new CouponManager($stubContainer);
$stubModel = $this->getMockBuilder('\Thelia\Model\Coupon')
->disableOriginalConstructor()
->getMock();
$stubModel->expects($this->any())
->method('getUsagesLeft')
->will($this->returnValue(21));
$stubModel->expects($this->any())
->method('setMaxUsage')
->will($this->returnValue(true));
$actual = $couponManager->decrementQuantity($stubModel, null);
$expected = 20;
$this->assertEquals($expected, $actual);
}
/**
* @covers Thelia\Coupon\CouponManager::decrementQuantity
*/
public function testDecrementeQuantityIllimited()
{
$stubFacade = $this->generateFacadeStub();
$stubContainer = $this->getMock('\Symfony\Component\DependencyInjection\Container');
$coupon = new RemoveXAmount($stubFacade);
$date = new \DateTime();
$coupon->set(
$stubFacade,
'XMAS',
'',
'',
'',
array('amount' => 21.00),
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);
$stubFacade->expects($this->any())
->method('getCurrentCoupons')
->will($this->returnValue(array($coupon)));
$stubContainer->expects($this->any())
->method('get')
->will($this->onConsecutiveCalls($stubFacade));
$couponManager = new CouponManager($stubContainer);
$stubModel = $this->getMockBuilder('\Thelia\Model\Coupon')
->disableOriginalConstructor()
->getMock();
$stubModel->expects($this->any())
->method('getMaxUsage')
->will($this->returnValue(-1));
$stubModel->expects($this->any())
->method('setMaxUsage')
->will($this->returnValue(true));
$actual = $couponManager->decrementQuantity($stubModel, null);
$expected = false;
$this->assertEquals($expected, $actual);
}
/**
* 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('getCartTotalTaxPrice')
->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;
}
}

View File

@@ -0,0 +1,465 @@
<?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\ActiveQuery\Criteria;
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\CountryQuery;
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 */
public $freeProduct;
public $originalPrice;
public $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()
->joinProductSaleElements("pse_join")
->addJoinCondition("pse_join", "is_default = ?", 1, null, \PDO::PARAM_INT)
->findOne()
;
if (null === $this->freeProduct) {
$this->markTestSkipped("You can't run this test as there's no product with associated product_sale_elements");
}
$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)
->setProductId($this->freeProduct->getId())
;
$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));
$country = CountryQuery::create()
->findOneByByDefault(1);
$stubFacade->expects($this->any())
->method('getDeliveryCountry')
->will($this->returnValue($country));
return $stubFacade;
}
public function generateMatchingCart(\PHPUnit_Framework_MockObject_MockObject $stubFacade, $count)
{
$product1 = ProductQuery::create()->addAscendingOrderByColumn('RAND()')->findOne();
$product2 = ProductQuery::create()->filterById($product1->getId(), Criteria::NOT_IN)->addAscendingOrderByColumn('RAND()')->findOne();
$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));
return [$product1->getId(), $product2->getId()];
}
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
);
$products = $this->generateMatchingCart($stubFacade, 1);
$coupon->product_list = $products;
$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
);
$products = $this->generateMatchingCart($stubFacade, 2);
$coupon->product_list = $products;
$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()
{
if (null !== $this->freeProduct) {
$this->freeProduct->getDefaultSaleElements()->setPromo($this->originalPromo)->save();
}
}
}

View File

@@ -0,0 +1,422 @@
<?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,347 @@
<?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,367 @@
<?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,446 @@
<?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\CountryQuery;
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();
$defaultCountry = CountryQuery::create()
->filterByByDefault(1)
->findOne();
$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('getDeliveryCountry')
->will($this->returnValue($defaultCountry));
$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('getRealTaxedPrice')
->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('getRealTaxedPrice')
->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('getRealTaxedPrice')
->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,373 @@
<?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\CountryQuery;
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();
$defaultCountry = CountryQuery::create()
->filterByByDefault(1)
->findOne();
$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('getDeliveryCountry')
->will($this->returnValue($defaultCountry));
$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('getRealTaxedPrice')
->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('getRealTaxedPrice')
->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('getRealTaxedPrice')
->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,394 @@
<?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\CountryQuery;
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();
$defaultCountry = CountryQuery::create()
->filterByByDefault(1)
->findOne();
$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('getDeliveryCountry')
->will($this->returnValue($defaultCountry));
$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('getRealTaxedPrice')
->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('getRealTaxedPrice')
->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('getRealTaxedPrice')
->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()
{
}
}

View File

@@ -0,0 +1,206 @@
<?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;
/**
* Unit Test RemoveXAmount Class
* Generated by PHPUnit_SkeletonGenerator 1.2.1 on 2013-11-17 at 18:59:24.
*
* @package Coupon
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
*/
class RemoveXAmountTest 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;
}
/**
* @covers Thelia\Coupon\Type\RemoveXAmount::set
* @covers Thelia\Coupon\Type\CouponAbstract::getCode
* @covers Thelia\Coupon\Type\RemoveXAmount::getTitle
* @covers Thelia\Coupon\Type\RemoveXAmount::getShortDescription
* @covers Thelia\Coupon\Type\RemoveXAmount::getDescription
* @covers Thelia\Coupon\Type\RemoveXAmount::isCumulative
* @covers Thelia\Coupon\Type\RemoveXAmount::isRemovingPostage
* @covers Thelia\Coupon\Type\RemoveXAmount::isAvailableOnSpecialOffers
* @covers Thelia\Coupon\Type\RemoveXAmount::isEnabled
* @covers Thelia\Coupon\Type\RemoveXAmount::getMaxUsage
* @covers Thelia\Coupon\Type\RemoveXAmount::getExpirationDate
* @covers Thelia\Coupon\Type\RemoveXAmount::setConditions
*/
public function testSet()
{
$stubFacade = $this->generateFacadeStub();
$coupon = new RemoveXAmount($stubFacade);
$date = new \DateTime();
$description = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras at luctus tellus. Integer turpis mauris, aliquet vitae risus tristique, pellentesque vestibulum urna. Vestibulum sodales laoreet lectus dictum suscipit. Praesent vulputate, sem id varius condimentum, quam magna tempor elit, quis venenatis ligula nulla eget libero. Cras egestas euismod tellus, id pharetra leo suscipit quis. Donec lacinia ac lacus et ultricies. Nunc in porttitor neque. Proin at quam congue, consectetur orci sed, congue nulla. Nulla eleifend nunc ligula, nec pharetra elit tempus quis. Vivamus vel mauris sed est dictum blandit. Maecenas blandit dapibus velit ut sollicitudin. In in euismod mauris, consequat viverra magna. Cras velit velit, sollicitudin commodo tortor gravida, tempus varius nulla.
Donec rhoncus leo mauris, id porttitor ante luctus tempus. Curabitur quis augue feugiat, ullamcorper mauris ac, interdum mi. Quisque aliquam lorem vitae felis lobortis, id interdum turpis mattis. Vestibulum diam massa, ornare congue blandit quis, facilisis at nisl. In tortor metus, venenatis non arcu nec, sollicitudin ornare nisl. Nunc erat risus, varius nec urna at, iaculis lacinia elit. Aenean ut felis tempus, tincidunt odio non, sagittis nisl. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Donec vitae hendrerit elit. Nunc sit amet gravida risus, euismod lobortis massa. Nam a erat mauris. Nam a malesuada lorem. Nulla id accumsan dolor, sed rhoncus tellus. Quisque dictum felis sed leo auctor, at volutpat lectus viverra. Morbi rutrum, est ac aliquam imperdiet, nibh sem sagittis justo, ac mattis magna lacus eu nulla.
Duis interdum lectus nulla, nec pellentesque sapien condimentum at. Suspendisse potenti. Sed eu purus tellus. Nunc quis rhoncus metus. Fusce vitae tellus enim. Interdum et malesuada fames ac ante ipsum primis in faucibus. Etiam tempor porttitor erat vitae iaculis. Sed est elit, consequat non ornare vitae, vehicula eget lectus. Etiam consequat sapien mauris, eget consectetur magna imperdiet eget. Nunc sollicitudin luctus velit, in commodo nulla adipiscing fermentum. Fusce nisi sapien, posuere vitae metus sit amet, facilisis sollicitudin dui. Fusce ultricies auctor enim sit amet iaculis. Morbi at vestibulum enim, eget adipiscing eros.
Praesent ligula lorem, faucibus ut metus quis, fermentum iaculis erat. Pellentesque elit erat, lacinia sed semper ac, sagittis vel elit. Nam eu convallis est. Curabitur rhoncus odio vitae consectetur pellentesque. Nam vitae arcu nec ante scelerisque dignissim vel nec neque. Suspendisse augue nulla, mollis eget dui et, tempor facilisis erat. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi ac diam ipsum. Donec convallis dui ultricies velit auctor, non lobortis nulla ultrices. Morbi vitae dignissim ante, sit amet lobortis tortor. Nunc dapibus condimentum augue, in molestie neque congue non.
Sed facilisis pellentesque nisl, eu tincidunt erat scelerisque a. Nullam malesuada tortor vel erat volutpat tincidunt. In vehicula diam est, a convallis eros scelerisque ut. Donec aliquet venenatis iaculis. Ut a arcu gravida, placerat dui eu, iaculis nisl. Quisque adipiscing orci sit amet dui dignissim lacinia. Sed vulputate lorem non dolor adipiscing ornare. Morbi ornare id nisl id aliquam. Ut fringilla elit ante, nec lacinia enim fermentum sit amet. Aenean rutrum lorem eu convallis pharetra. Cras malesuada varius metus, vitae gravida velit. Nam a varius ipsum, ac commodo dolor. Phasellus nec elementum elit. Etiam vel adipiscing leo.';
$coupon->set(
$stubFacade, 'XMAS', 'XMAS Coupon', 'Coupon for Springbreak removing 10€ if you have a cart between 40.00€ and 400.00€ (excluded)',
$description, array('amount' => 10.00), 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('XMAS', $coupon->getCode());
$this->assertEquals('XMAS Coupon', $coupon->getTitle());
$this->assertEquals('Coupon for Springbreak removing 10€ if you have a cart between 40.00€ and 400.00€ (excluded)', $coupon->getShortDescription());
$this->assertEquals($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->assertEquals(10.00, $coupon->exec());
}
/**
* @covers Thelia\Coupon\Type\RemoveXAmount::getName
*/
public function testGetName()
{
$stubFacade = $this->generateFacadeStub(399, 'EUR', 'Remove X amount to total cart');
/** @var FacadeInterface $stubFacade */
$coupon = new RemoveXAmount($stubFacade);
$actual = $coupon->getName();
$expected = 'Remove X amount to total cart';
$this->assertEquals($expected, $actual);
}
/**
* @covers Thelia\Coupon\Type\RemoveXAmount::getToolTip
*/
public function testGetToolTip()
{
$tooltip = 'This coupon will remove the entered amount to the customer total checkout. If the discount is superior to the total checkout price the customer will only pay the postage. Unless if the coupon is set to remove postage too.';
$stubFacade = $this->generateFacadeStub(399, 'EUR', $tooltip);
/** @var FacadeInterface $stubFacade */
$coupon = new RemoveXAmount($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,205 @@
<?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;
/**
* Unit Test RemoveXPercent Class
* Generated by PHPUnit_SkeletonGenerator 1.2.1 on 2013-11-17 at 18:59:24.
*
* @package Coupon
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
*/
class RemoveXPercentTest 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()->find();
$stubFacade->expects($this->any())
->method('getAvailableCurrencies')
->will($this->returnValue($currencies));
$stubFacade->expects($this->any())
->method('getCartTotalTaxPrice')
->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;
}
/**
* @covers Thelia\Coupon\Type\RemoveXPercent::set
* @covers Thelia\Coupon\Type\RemoveXPercent::exec
*/
public function testSet()
{
$stubFacade = $this->generateFacadeStub();
$coupon = new RemoveXPercent($stubFacade);
$date = new \DateTime();
$description = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras at luctus tellus. Integer turpis mauris, aliquet vitae risus tristique, pellentesque vestibulum urna. Vestibulum sodales laoreet lectus dictum suscipit. Praesent vulputate, sem id varius condimentum, quam magna tempor elit, quis venenatis ligula nulla eget libero. Cras egestas euismod tellus, id pharetra leo suscipit quis. Donec lacinia ac lacus et ultricies. Nunc in porttitor neque. Proin at quam congue, consectetur orci sed, congue nulla. Nulla eleifend nunc ligula, nec pharetra elit tempus quis. Vivamus vel mauris sed est dictum blandit. Maecenas blandit dapibus velit ut sollicitudin. In in euismod mauris, consequat viverra magna. Cras velit velit, sollicitudin commodo tortor gravida, tempus varius nulla.
Donec rhoncus leo mauris, id porttitor ante luctus tempus. Curabitur quis augue feugiat, ullamcorper mauris ac, interdum mi. Quisque aliquam lorem vitae felis lobortis, id interdum turpis mattis. Vestibulum diam massa, ornare congue blandit quis, facilisis at nisl. In tortor metus, venenatis non arcu nec, sollicitudin ornare nisl. Nunc erat risus, varius nec urna at, iaculis lacinia elit. Aenean ut felis tempus, tincidunt odio non, sagittis nisl. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Donec vitae hendrerit elit. Nunc sit amet gravida risus, euismod lobortis massa. Nam a erat mauris. Nam a malesuada lorem. Nulla id accumsan dolor, sed rhoncus tellus. Quisque dictum felis sed leo auctor, at volutpat lectus viverra. Morbi rutrum, est ac aliquam imperdiet, nibh sem sagittis justo, ac mattis magna lacus eu nulla.
Duis interdum lectus nulla, nec pellentesque sapien condimentum at. Suspendisse potenti. Sed eu purus tellus. Nunc quis rhoncus metus. Fusce vitae tellus enim. Interdum et malesuada fames ac ante ipsum primis in faucibus. Etiam tempor porttitor erat vitae iaculis. Sed est elit, consequat non ornare vitae, vehicula eget lectus. Etiam consequat sapien mauris, eget consectetur magna imperdiet eget. Nunc sollicitudin luctus velit, in commodo nulla adipiscing fermentum. Fusce nisi sapien, posuere vitae metus sit amet, facilisis sollicitudin dui. Fusce ultricies auctor enim sit amet iaculis. Morbi at vestibulum enim, eget adipiscing eros.
Praesent ligula lorem, faucibus ut metus quis, fermentum iaculis erat. Pellentesque elit erat, lacinia sed semper ac, sagittis vel elit. Nam eu convallis est. Curabitur rhoncus odio vitae consectetur pellentesque. Nam vitae arcu nec ante scelerisque dignissim vel nec neque. Suspendisse augue nulla, mollis eget dui et, tempor facilisis erat. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi ac diam ipsum. Donec convallis dui ultricies velit auctor, non lobortis nulla ultrices. Morbi vitae dignissim ante, sit amet lobortis tortor. Nunc dapibus condimentum augue, in molestie neque congue non.
Sed facilisis pellentesque nisl, eu tincidunt erat scelerisque a. Nullam malesuada tortor vel erat volutpat tincidunt. In vehicula diam est, a convallis eros scelerisque ut. Donec aliquet venenatis iaculis. Ut a arcu gravida, placerat dui eu, iaculis nisl. Quisque adipiscing orci sit amet dui dignissim lacinia. Sed vulputate lorem non dolor adipiscing ornare. Morbi ornare id nisl id aliquam. Ut fringilla elit ante, nec lacinia enim fermentum sit amet. Aenean rutrum lorem eu convallis pharetra. Cras malesuada varius metus, vitae gravida velit. Nam a varius ipsum, ac commodo dolor. Phasellus nec elementum elit. Etiam vel adipiscing leo.';
$coupon->set(
$stubFacade,
'XMAS',
'XMAS Coupon',
'Coupon for Springbreak removing 10% if you have a cart between 40.00€ and 400.00€ (excluded)',
$description,
array('amount' => 0.00, 'percentage' => 10.00),
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('XMAS', $coupon->getCode());
$this->assertEquals('XMAS Coupon', $coupon->getTitle());
$this->assertEquals('Coupon for Springbreak removing 10% if you have a cart between 40.00€ and 400.00€ (excluded)', $coupon->getShortDescription());
$this->assertEquals($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->assertEquals(40.00, $coupon->exec());
}
/**
* @covers Thelia\Coupon\Type\RemoveXPercent::getName
*/
public function testGetName()
{
$stubFacade = $this->generateFacadeStub(399, 'EUR', 'Remove X percent to total cart');
/** @var FacadeInterface $stubFacade */
$coupon = new RemoveXPercent($stubFacade);
$actual = $coupon->getName();
$expected = 'Remove X percent to total cart';
$this->assertEquals($expected, $actual);
}
/**
* @covers Thelia\Coupon\Type\RemoveXPercent::getToolTip
*/
public function testGetToolTip()
{
$tooltip = 'This coupon will remove the entered percentage to the customer total checkout. If the discount is superior to the total checkout price the customer will only pay the postage. Unless if the coupon is set to remove postage too.';
$stubFacade = $this->generateFacadeStub(399, 'EUR', $tooltip);
/** @var FacadeInterface $stubFacade */
$coupon = new RemoveXPercent($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()
{
}
}