/** * Occurring when a Coupon condition is about to be consumed * * @param CouponConsumeEvent $event Event consuming Coupon */ public function consume(CouponConsumeEvent $event) { $totalDiscount = 0; $isValid = false; /** @var CouponInterface $coupon */ $coupon = $this->couponFactory->buildCouponFromCode($event->getCode()); if ($coupon) { $isValid = $coupon->isMatching(); if ($isValid) { $consumedCoupons = $this->request->getSession()->getConsumedCoupons(); if (!isset($consumedCoupons) || !$consumedCoupons) { $consumedCoupons = array(); } if (!isset($consumedCoupons[$event->getCode()])) { // Prevent accumulation of the same Coupon on a Checkout $consumedCoupons[$event->getCode()] = $event->getCode(); $this->request->getSession()->setConsumedCoupons($consumedCoupons); } $totalDiscount = $this->couponManager->getDiscount(); $this->request->getSession()->getCart()->setDiscount($totalDiscount)->save(); $this->request->getSession()->getOrder()->setDiscount($totalDiscount); } } $event->setIsValid($isValid); $event->setDiscount($totalDiscount); }
/** * Occurring when a Coupon condition is about to be consumed * * @param CouponConsumeEvent $event Event consuming Coupon * @param $eventName * @param EventDispatcherInterface $dispatcher */ public function consume(CouponConsumeEvent $event, $eventName, EventDispatcherInterface $dispatcher) { $totalDiscount = 0; $isValid = false; /** @var CouponInterface $coupon */ $coupon = $this->couponFactory->buildCouponFromCode($event->getCode()); if ($coupon) { $isValid = $coupon->isMatching(); if ($isValid) { $this->couponManager->pushCouponInSession($event->getCode()); $totalDiscount = $this->couponManager->getDiscount(); $this->getSession()->getSessionCart($dispatcher)->setDiscount($totalDiscount)->save(); $this->getSession()->getOrder()->setDiscount($totalDiscount); } } $event->setIsValid($isValid); $event->setDiscount($totalDiscount); }
/** * @covers Thelia\Coupon\CouponFactory::buildCouponFromCode * @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::CART_TOTAL => Operators::SUPERIOR, MatchForTotalAmount::CART_CURRENCY => Operators::EQUAL); $values = array(MatchForTotalAmount::CART_TOTAL => 40.0, 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.0, 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::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.0, 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.0, 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'); }