Skip to content

Commit

Permalink
fix reset bonus points by zero value and reset button + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherhero committed Feb 15, 2023
1 parent 23c569f commit f471fcd
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 6 deletions.
2 changes: 1 addition & 1 deletion features/shop/resetting_bonus_points.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions spec/Processor/ResetOrderBonusPointsProcessorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down
9 changes: 6 additions & 3 deletions spec/Purifier/OrderBonusPointsPurifierSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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);
}
Expand Down
3 changes: 3 additions & 0 deletions src/Processor/ResetOrderBonusPointsProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down
10 changes: 8 additions & 2 deletions src/Purifier/OrderBonusPointsPurifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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(
Expand All @@ -53,6 +59,6 @@ public function purify(

$this->persistenceManager->persist($customerBonusPoints);
$this->persistenceManager->persist($order);
$this->persistenceManager->remove($bonusPoints);
$this->bonusPointsRepository->remove($bonusPoints);
}
}
1 change: 1 addition & 0 deletions src/Resources/config/services/purifiers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<service id="bitbag_sylius_bonus_points.purifier.order_bonus_points" class="BitBag\SyliusBonusPointsPlugin\Purifier\OrderBonusPointsPurifier">
<argument type="service" id="bitbag_sylius_bonus_points.context.customer_bonus_points" />
<argument type="service" id="doctrine.orm.entity_manager" />
<argument type="service" id="bitbag_sylius_bonus_points.repository.bonus_points" />
</service>
</services>
</container>

0 comments on commit f471fcd

Please sign in to comment.