Пример #1
0
 /**
  * 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);
 }
Пример #2
0
 /**
  * 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);
 }
Пример #3
0
 /**
  * @covers Thelia\Coupon\CouponFactory::buildCouponFromModel
  */
 public function testBuildCouponFromModel()
 {
     $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();
     $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);
 }
Пример #4
0
 /**
  * @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);
 }