From f471fcda00b461bafaec6c111359dcb7de026a1b Mon Sep 17 00:00:00 2001 From: christopherheroo Date: Wed, 15 Feb 2023 02:56:25 +0100 Subject: [PATCH] fix reset bonus points by zero value and reset button + tests --- features/shop/resetting_bonus_points.feature | 2 +- spec/Processor/ResetOrderBonusPointsProcessorSpec.php | 3 +++ spec/Purifier/OrderBonusPointsPurifierSpec.php | 9 ++++++--- src/Processor/ResetOrderBonusPointsProcessor.php | 3 +++ src/Purifier/OrderBonusPointsPurifier.php | 10 ++++++++-- src/Resources/config/services/purifiers.xml | 1 + 6 files changed, 22 insertions(+), 6 deletions(-) diff --git a/features/shop/resetting_bonus_points.feature b/features/shop/resetting_bonus_points.feature index 8749031..f251152 100644 --- a/features/shop/resetting_bonus_points.feature +++ b/features/shop/resetting_bonus_points.feature @@ -29,7 +29,7 @@ Feature: Collecting bonus points @ui @javascript Scenario: Resetting points by applying zero bonus points - When I want to use "0.54" bonus points + When I want to use "2" bonus points And I want to resign of using bonus points and I apply zero bonus points And I specified the billing address And I proceed with "DHL" shipping method and "Offline" payment diff --git a/spec/Processor/ResetOrderBonusPointsProcessorSpec.php b/spec/Processor/ResetOrderBonusPointsProcessorSpec.php index 8b2a25f..6403f2d 100644 --- a/spec/Processor/ResetOrderBonusPointsProcessorSpec.php +++ b/spec/Processor/ResetOrderBonusPointsProcessorSpec.php @@ -10,6 +10,7 @@ namespace spec\BitBag\SyliusBonusPointsPlugin\Processor; +use BitBag\SyliusBonusPointsPlugin\Entity\AdjustmentInterface; use BitBag\SyliusBonusPointsPlugin\Entity\BonusPointsInterface; use BitBag\SyliusBonusPointsPlugin\Processor\ResetOrderBonusPointsProcessor; use BitBag\SyliusBonusPointsPlugin\Processor\ResetOrderBonusPointsProcessorInterface; @@ -45,6 +46,7 @@ function it_processes( OrderBonusPointsPurifierInterface $orderBonusPointsPurifier ): void { $bonusPointsCollection = new ArrayCollection([$bonusPoints->getWrappedObject()]); + $order->removeAdjustmentsRecursively(AdjustmentInterface::ORDER_BONUS_POINTS_ADJUSTMENT)->shouldBeCalled();; $bonusPointsRepository->findBy(['order' => $order, 'isUsed' => true])->willReturn($bonusPointsCollection); $bonusPointsRepository->findBy(['order' => $order, 'isUsed' => true])->shouldBeCalled(); @@ -59,6 +61,7 @@ function it_does_not_process_if_it_does_not_find_bonus_points( BonusPointsInterface $bonusPoints, OrderBonusPointsPurifierInterface $orderBonusPointsPurifier ): void { + $order->removeAdjustmentsRecursively(AdjustmentInterface::ORDER_BONUS_POINTS_ADJUSTMENT)->shouldBeCalled();; $bonusPointsRepository->findBy(['order' => $order, 'isUsed' => true])->willReturn([]); $bonusPointsRepository->findBy(['order' => $order, 'isUsed' => true])->shouldBeCalled(); diff --git a/spec/Purifier/OrderBonusPointsPurifierSpec.php b/spec/Purifier/OrderBonusPointsPurifierSpec.php index 105fb5e..cf5445e 100644 --- a/spec/Purifier/OrderBonusPointsPurifierSpec.php +++ b/spec/Purifier/OrderBonusPointsPurifierSpec.php @@ -21,14 +21,16 @@ use PhpSpec\ObjectBehavior; use Sylius\Component\Core\Model\CustomerInterface; use Sylius\Component\Core\Model\OrderInterface; +use Sylius\Component\Resource\Repository\RepositoryInterface; final class OrderBonusPointsPurifierSpec extends ObjectBehavior { function let( CustomerBonusPointsContextInterface $customerBonusPointsContext, - ObjectManager $manager + ObjectManager $manager, + RepositoryInterface $repository ): void { - $this->beConstructedWith($customerBonusPointsContext, $manager); + $this->beConstructedWith($customerBonusPointsContext, $manager, $repository); } function it_is_initializable(): void @@ -44,6 +46,7 @@ function it_implements_interface(): void function it_purifies_bonus_points_form_order( CustomerBonusPointsContextInterface $customerBonusPointsContext, ObjectManager $manager, + RepositoryInterface $repository, CustomerInterface $customer, CustomerBonusPointsInterface $customerBonusPoints, OrderInterface $order, @@ -62,7 +65,7 @@ function it_purifies_bonus_points_form_order( $customerBonusPoints->removeBonusPointsUsed($bonusPoints)->shouldBeCalled(); $manager->persist($customerBonusPoints)->shouldBeCalled(); $manager->persist($order)->shouldBeCalled(); - $manager->remove($bonusPoints)->shouldBeCalled(); + $repository->remove($bonusPoints)->shouldBeCalled(); $this->purify($bonusPoints); } diff --git a/src/Processor/ResetOrderBonusPointsProcessor.php b/src/Processor/ResetOrderBonusPointsProcessor.php index 88d3ab9..61aa9bb 100644 --- a/src/Processor/ResetOrderBonusPointsProcessor.php +++ b/src/Processor/ResetOrderBonusPointsProcessor.php @@ -10,6 +10,7 @@ namespace BitBag\SyliusBonusPointsPlugin\Processor; +use BitBag\SyliusBonusPointsPlugin\Entity\AdjustmentInterface; use BitBag\SyliusBonusPointsPlugin\Entity\BonusPointsInterface; use BitBag\SyliusBonusPointsPlugin\Purifier\OrderBonusPointsPurifierInterface; use Sylius\Component\Order\Model\OrderInterface; @@ -36,6 +37,8 @@ public function process(OrderInterface $order): void /** @var BonusPointsInterface[] $bonusPoints */ $bonusPoints = $this->bonusPointsRepository->findBy(['order' => $order, 'isUsed' => true]); + $order->removeAdjustmentsRecursively(AdjustmentInterface::ORDER_BONUS_POINTS_ADJUSTMENT); + foreach ($bonusPoints as $bonusPoint) { $this->orderBonusPointsPurifier->purify($bonusPoint); } diff --git a/src/Purifier/OrderBonusPointsPurifier.php b/src/Purifier/OrderBonusPointsPurifier.php index 774edd4..270c2ce 100644 --- a/src/Purifier/OrderBonusPointsPurifier.php +++ b/src/Purifier/OrderBonusPointsPurifier.php @@ -16,6 +16,7 @@ use BitBag\SyliusBonusPointsPlugin\Entity\CustomerBonusPointsInterface; use Doctrine\Persistence\ObjectManager; use Sylius\Component\Core\Model\OrderInterface; +use Sylius\Component\Resource\Repository\RepositoryInterface; final class OrderBonusPointsPurifier implements OrderBonusPointsPurifierInterface { @@ -25,12 +26,17 @@ final class OrderBonusPointsPurifier implements OrderBonusPointsPurifierInterfac /** @var ObjectManager */ private $persistenceManager; + /** @var RepositoryInterface */ + private $bonusPointsRepository; + public function __construct( CustomerBonusPointsContextInterface $customerBonusPointsContext, - ObjectManager $persistenceManager + ObjectManager $persistenceManager, + RepositoryInterface $bonusPointsRepository ) { $this->customerBonusPointsContext = $customerBonusPointsContext; $this->persistenceManager = $persistenceManager; + $this->bonusPointsRepository = $bonusPointsRepository; } public function purify( @@ -53,6 +59,6 @@ public function purify( $this->persistenceManager->persist($customerBonusPoints); $this->persistenceManager->persist($order); - $this->persistenceManager->remove($bonusPoints); + $this->bonusPointsRepository->remove($bonusPoints); } } diff --git a/src/Resources/config/services/purifiers.xml b/src/Resources/config/services/purifiers.xml index cc64871..d355851 100644 --- a/src/Resources/config/services/purifiers.xml +++ b/src/Resources/config/services/purifiers.xml @@ -7,6 +7,7 @@ +