Added per-user coupon maximum usage count
This commit is contained in:
@@ -16,9 +16,11 @@ 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
|
||||
@@ -72,6 +74,13 @@ class CouponFactoryTest extends \PHPUnit_Framework_TestCase
|
||||
->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();
|
||||
@@ -221,6 +230,75 @@ Sed facilisis pellentesque nisl, eu tincidunt erat scelerisque a. Nullam malesua
|
||||
$this->assertEquals($expected, $actual);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Thelia\Coupon\CouponFactory::buildCouponFromCode
|
||||
* @expectedException \Thelia\Exception\CouponNoUsageLeftException
|
||||
*/
|
||||
public function testBuildCouponFromCodeUsageLimitCoupon()
|
||||
{
|
||||
$stubFacade = $this->generateFacadeStub();
|
||||
|
||||
$stubContainer = $this->getMock('\Symfony\Component\DependencyInjection\Container');
|
||||
|
||||
$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
|
||||
*/
|
||||
@@ -312,6 +390,8 @@ Sed facilisis pellentesque nisl, eu tincidunt erat scelerisque a. Nullam malesua
|
||||
->method('has')
|
||||
->will($this->returnValue(true));
|
||||
|
||||
$dummy = new Translator($stubContainer);
|
||||
|
||||
$factory = new CouponFactory($stubContainer);
|
||||
$factory->buildCouponFromCode('XMAS');
|
||||
|
||||
|
||||
@@ -402,7 +402,8 @@ Sed facilisis pellentesque nisl, eu tincidunt erat scelerisque a. Nullam malesua
|
||||
$stubFacade, 'XMAS', '', '', '', array('amount' => 21.00),
|
||||
true, true, true, true, 254, $date->setTimestamp(strtotime("today + 3 months")),
|
||||
new ObjectCollection(),
|
||||
new ObjectCollection()
|
||||
new ObjectCollection(),
|
||||
false
|
||||
);
|
||||
|
||||
$condition1 = new MatchForTotalAmount($stubFacade);
|
||||
@@ -446,13 +447,13 @@ Sed facilisis pellentesque nisl, eu tincidunt erat scelerisque a. Nullam malesua
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$stubModel->expects($this->any())
|
||||
->method('getMaxUsage')
|
||||
->method('getUsagesLeft')
|
||||
->will($this->returnValue(21));
|
||||
$stubModel->expects($this->any())
|
||||
->method('setMaxUsage')
|
||||
->will($this->returnValue(true));
|
||||
|
||||
$actual = $couponManager->decrementQuantity($stubModel);
|
||||
$actual = $couponManager->decrementQuantity($stubModel, null);
|
||||
$expected = 20;
|
||||
|
||||
$this->assertEquals($expected, $actual);
|
||||
@@ -472,7 +473,8 @@ Sed facilisis pellentesque nisl, eu tincidunt erat scelerisque a. Nullam malesua
|
||||
$stubFacade, 'XMAS', '', '', '', array('amount' => 21.00),
|
||||
true, true, true, true, 254, $date->setTimestamp(strtotime("today + 3 months")),
|
||||
new ObjectCollection(),
|
||||
new ObjectCollection()
|
||||
new ObjectCollection(),
|
||||
false
|
||||
);
|
||||
|
||||
$condition1 = new MatchForTotalAmount($stubFacade);
|
||||
@@ -522,8 +524,8 @@ Sed facilisis pellentesque nisl, eu tincidunt erat scelerisque a. Nullam malesua
|
||||
->method('setMaxUsage')
|
||||
->will($this->returnValue(true));
|
||||
|
||||
$actual = $couponManager->decrementQuantity($stubModel);
|
||||
$expected = -1;
|
||||
$actual = $couponManager->decrementQuantity($stubModel, null);
|
||||
$expected = false;
|
||||
|
||||
$this->assertEquals($expected, $actual);
|
||||
}
|
||||
|
||||
@@ -119,7 +119,8 @@ class RemoveXAmountTest extends \PHPUnit_Framework_TestCase
|
||||
$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()
|
||||
new ObjectCollection(),
|
||||
false
|
||||
);
|
||||
|
||||
$condition1 = new MatchForTotalAmount($stubFacade);
|
||||
|
||||
@@ -109,7 +109,8 @@ class RemoveXPercentTest extends \PHPUnit_Framework_TestCase
|
||||
$description, array('amount' => 0.00, 'percentage' => 10.00), true, true, true, true,
|
||||
254, $date->setTimestamp(strtotime("today + 3 months")),
|
||||
new ObjectCollection(),
|
||||
new ObjectCollection()
|
||||
new ObjectCollection(),
|
||||
false
|
||||
);
|
||||
|
||||
$condition1 = new MatchForTotalAmount($stubFacade);
|
||||
|
||||
Reference in New Issue
Block a user