From f40476e3ea827f7b2d9d10947254933ea482497a Mon Sep 17 00:00:00 2001 From: gmorel Date: Sat, 23 Nov 2013 22:15:45 +0100 Subject: [PATCH] Coupon not found by the factory now just return false --- core/lib/Thelia/Action/Coupon.php | 59 ++++--- core/lib/Thelia/Coupon/CouponFactory.php | 5 +- .../Thelia/Tests/Coupon/CouponFactoryTest.php | 165 ++++++++++++++++++ 3 files changed, 196 insertions(+), 33 deletions(-) diff --git a/core/lib/Thelia/Action/Coupon.php b/core/lib/Thelia/Action/Coupon.php index 4ac8481f7..a4188c2fc 100755 --- a/core/lib/Thelia/Action/Coupon.php +++ b/core/lib/Thelia/Action/Coupon.php @@ -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); } diff --git a/core/lib/Thelia/Coupon/CouponFactory.php b/core/lib/Thelia/Coupon/CouponFactory.php index e528a1d49..fbbfa6a8a 100644 --- a/core/lib/Thelia/Coupon/CouponFactory.php +++ b/core/lib/Thelia/Coupon/CouponFactory.php @@ -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()) { diff --git a/core/lib/Thelia/Tests/Coupon/CouponFactoryTest.php b/core/lib/Thelia/Tests/Coupon/CouponFactoryTest.php index 218b70611..0baa4753a 100644 --- a/core/lib/Thelia/Tests/Coupon/CouponFactoryTest.php +++ b/core/lib/Thelia/Tests/Coupon/CouponFactoryTest.php @@ -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'); + + } }