Coupon not found by the factory now just return false
This commit is contained in:
@@ -94,6 +94,7 @@ class Coupon extends BaseAction implements EventSubscriberInterface
|
||||
public function consume(CouponConsumeEvent $event)
|
||||
{
|
||||
$totalDiscount = 0;
|
||||
$isValid = false;
|
||||
|
||||
/** @var CouponFactory $couponFactory */
|
||||
$couponFactory = $this->container->get('thelia.coupon.factory');
|
||||
@@ -104,40 +105,40 @@ class Coupon extends BaseAction implements EventSubscriberInterface
|
||||
/** @var CouponInterface $coupon */
|
||||
$coupon = $couponFactory->buildCouponManagerFromCode($event->getCode());
|
||||
|
||||
$isValid = $coupon->isMatching();
|
||||
if ($coupon) {
|
||||
$isValid = $coupon->isMatching();
|
||||
if ($isValid) {
|
||||
/** @var Request $request */
|
||||
$request = $this->container->get('request');
|
||||
$consumedCoupons = $request->getSession()->getConsumedCoupons();
|
||||
|
||||
if ($isValid) {
|
||||
/** @var Request $request */
|
||||
$request = $this->container->get('request');
|
||||
$consumedCoupons = $request->getSession()->getConsumedCoupons();
|
||||
if (!isset($consumedCoupons) || !$consumedCoupons) {
|
||||
$consumedCoupons = array();
|
||||
}
|
||||
|
||||
if (!isset($consumedCoupons) || !$consumedCoupons) {
|
||||
$consumedCoupons = array();
|
||||
// Prevent accumulation of the same Coupon on a Checkout
|
||||
$consumedCoupons[$event->getCode()] = $event->getCode();
|
||||
|
||||
$request->getSession()->setConsumedCoupons($consumedCoupons);
|
||||
|
||||
$totalDiscount = $couponManager->getDiscount();
|
||||
// @todo insert false product in cart with the name of the coupon and the discount as negative price
|
||||
|
||||
// Decrement coupon quantity
|
||||
// @todo move this part in after order event
|
||||
$couponQuery = CouponQuery::create();
|
||||
$couponModel = $couponQuery->findOneByCode($coupon->getCode());
|
||||
$couponManager->decrementeQuantity($couponModel);
|
||||
|
||||
$request
|
||||
->getSession()
|
||||
->getCart()
|
||||
->setDiscount($totalDiscount)
|
||||
->save();
|
||||
}
|
||||
|
||||
// Prevent accumulation of the same Coupon on a Checkout
|
||||
$consumedCoupons[$event->getCode()] = $event->getCode();
|
||||
|
||||
$request->getSession()->setConsumedCoupons($consumedCoupons);
|
||||
|
||||
$totalDiscount = $couponManager->getDiscount();
|
||||
// @todo insert false product in cart with the name of the coupon and the discount as negative price
|
||||
|
||||
// Decrement coupon quantity
|
||||
// @todo move this part in after order event
|
||||
$couponQuery = CouponQuery::create();
|
||||
$couponModel = $couponQuery->findOneByCode($coupon->getCode());
|
||||
$couponManager->decrementeQuantity($couponModel);
|
||||
|
||||
$request
|
||||
->getSession()
|
||||
->getCart()
|
||||
->setDiscount($totalDiscount)
|
||||
->save()
|
||||
;
|
||||
|
||||
}
|
||||
|
||||
|
||||
$event->setIsValid($isValid);
|
||||
$event->setDiscount($totalDiscount);
|
||||
}
|
||||
|
||||
@@ -68,7 +68,6 @@ class CouponFactory
|
||||
*
|
||||
* @throws \Thelia\Exception\CouponExpiredException
|
||||
* @throws \Thelia\Exception\InvalidConditionException
|
||||
* @throws \Symfony\Component\Translation\Exception\NotFoundResourceException
|
||||
* @return CouponInterface ready to be processed
|
||||
*/
|
||||
public function buildCouponManagerFromCode($couponCode)
|
||||
@@ -76,9 +75,7 @@ class CouponFactory
|
||||
/** @var Coupon $couponModel */
|
||||
$couponModel = $this->facade->findOneCouponByCode($couponCode);
|
||||
if ($couponModel === null) {
|
||||
throw new NotFoundResourceException(
|
||||
'Coupon ' . $couponCode . ' not found in Database'
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($couponModel->getExpirationDate() < new \DateTime()) {
|
||||
|
||||
@@ -238,4 +238,169 @@ Sed facilisis pellentesque nisl, eu tincidunt erat scelerisque a. Nullam malesua
|
||||
|
||||
$this->assertEquals($expected, $actual);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Thelia\Coupon\CouponFactory::buildCouponManagerFromCode
|
||||
*/
|
||||
public function testBuildCouponFromCodeUnknownCode()
|
||||
{
|
||||
$stubFacade = $this->generateFacadeStub();
|
||||
|
||||
$stubContainer = $this->getMock('\Symfony\Component\DependencyInjection\Container');
|
||||
|
||||
$conditionFactory = new ConditionFactory($stubContainer);
|
||||
$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->buildCouponManagerFromCode('XMAS');
|
||||
$expected = false;
|
||||
|
||||
$this->assertEquals($expected, $actual);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Thelia\Coupon\CouponFactory::buildCouponManagerFromCode
|
||||
* @expectedException \Thelia\Exception\CouponExpiredException
|
||||
*/
|
||||
public function testBuildCouponFromCodeExpiredCoupon()
|
||||
{
|
||||
$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")));
|
||||
$stubFacade->expects($this->any())
|
||||
->method('findOneCouponByCode')
|
||||
->will($this->returnValue($couponModel));
|
||||
|
||||
$couponManager = new RemoveXAmount($stubFacade);
|
||||
|
||||
|
||||
$condition1 = new MatchForTotalAmount($stubFacade);
|
||||
$operators = array(
|
||||
MatchForTotalAmount::INPUT1 => Operators::SUPERIOR,
|
||||
MatchForTotalAmount::INPUT2 => Operators::EQUAL
|
||||
);
|
||||
$values = array(
|
||||
MatchForTotalAmount::INPUT1 => 40.00,
|
||||
MatchForTotalAmount::INPUT2 => 'EUR'
|
||||
);
|
||||
$condition1->setValidatorsFromForm($operators, $values);
|
||||
|
||||
$condition2 = new MatchForTotalAmount($stubFacade);
|
||||
$operators = array(
|
||||
MatchForTotalAmount::INPUT1 => Operators::INFERIOR,
|
||||
MatchForTotalAmount::INPUT2 => Operators::EQUAL
|
||||
);
|
||||
$values = array(
|
||||
MatchForTotalAmount::INPUT1 => 400.00,
|
||||
MatchForTotalAmount::INPUT2 => 'EUR'
|
||||
);
|
||||
$condition2->setValidatorsFromForm($operators, $values);
|
||||
|
||||
$conditions = new ConditionCollection();
|
||||
$conditions->add($condition1);
|
||||
$conditions->add($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);
|
||||
$actual = $factory->buildCouponManagerFromCode('XMAS');
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Thelia\Coupon\CouponFactory::buildCouponManagerFromCode
|
||||
* @expectedException \Thelia\Exception\InvalidConditionException
|
||||
*/
|
||||
public function testBuildCouponFromCodeNoConditionCoupon()
|
||||
{
|
||||
$stubFacade = $this->generateFacadeStub();
|
||||
|
||||
$stubContainer = $this->getMock('\Symfony\Component\DependencyInjection\Container');
|
||||
|
||||
$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::INPUT1 => Operators::SUPERIOR,
|
||||
MatchForTotalAmount::INPUT2 => Operators::EQUAL
|
||||
);
|
||||
$values = array(
|
||||
MatchForTotalAmount::INPUT1 => 40.00,
|
||||
MatchForTotalAmount::INPUT2 => 'EUR'
|
||||
);
|
||||
$condition1->setValidatorsFromForm($operators, $values);
|
||||
|
||||
$condition2 = new MatchForTotalAmount($stubFacade);
|
||||
$operators = array(
|
||||
MatchForTotalAmount::INPUT1 => Operators::INFERIOR,
|
||||
MatchForTotalAmount::INPUT2 => Operators::EQUAL
|
||||
);
|
||||
$values = array(
|
||||
MatchForTotalAmount::INPUT1 => 400.00,
|
||||
MatchForTotalAmount::INPUT2 => '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);
|
||||
$expected = $couponManager;
|
||||
$actual = $factory->buildCouponManagerFromCode('XMAS');
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user