Skip to content

Commit

Permalink
Apply different checkers for order pre and post completion
Browse files Browse the repository at this point in the history
  • Loading branch information
Zales0123 committed Aug 20, 2024
1 parent e3835bc commit 8004c4f
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 36 deletions.
7 changes: 1 addition & 6 deletions src/Checker/OrderPaymentEditionChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Setono\SyliusOrderEditPlugin\Checker;

use Sylius\Component\Core\OrderPaymentStates;
use Sylius\Component\Order\Model\OrderInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Webmozart\Assert\Assert;
Expand All @@ -24,10 +23,6 @@ public function shouldOrderPaymentBeEdited(OrderInterface $order): bool
return true;
}

if ('setono_sylius_order_edit_admin_update' !== $request->attributes->get('_route')) {
return true;
}

return $order->getPaymentState() === OrderPaymentStates::STATE_AWAITING_PAYMENT;
return 'setono_sylius_order_edit_admin_update' !== $request->attributes->get('_route');
}
}
33 changes: 33 additions & 0 deletions src/Checker/PostCheckoutOrderPaymentEditionChecker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusOrderEditPlugin\Checker;

use Sylius\Component\Core\OrderPaymentStates;
use Sylius\Component\Order\Model\OrderInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Webmozart\Assert\Assert;

final class PostCheckoutOrderPaymentEditionChecker implements OrderPaymentEditionCheckerInterface
{
public function __construct(private readonly RequestStack $requestStack)
{
}

public function shouldOrderPaymentBeEdited(OrderInterface $order): bool
{
Assert::isInstanceOf($order, \Sylius\Component\Core\Model\OrderInterface::class);

$request = $this->requestStack->getCurrentRequest();
if (null === $request) {
return true;
}

if ('setono_sylius_order_edit_admin_update' !== $request->attributes->get('_route')) {
return true;
}

return $order->getPaymentState() === OrderPaymentStates::STATE_AWAITING_PAYMENT;
}
}
9 changes: 8 additions & 1 deletion src/Resources/config/services/order_processing.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
decorates="sylius.order_processing.order_payment_processor.after_checkout" decoration-priority="64"
>
<argument type="service" id="setono_sylius_order_edit.order_processing.order_payment_processor.after_checkout.inner"/>
<argument type="service" id="setono_sylius_order_edit.checker.order_payment_edition"/>
<argument type="service" id="setono_sylius_order_edit.checker.order_payment_edition.post_checkout"/>
</service>

<service
Expand All @@ -27,6 +27,13 @@
<argument type="service" id="request_stack" />
</service>

<service
id="setono_sylius_order_edit.checker.order_payment_edition.post_checkout"
class="Setono\SyliusOrderEditPlugin\Checker\PostCheckoutOrderPaymentEditionChecker"
>
<argument type="service" id="request_stack" />
</service>

<service
id="setono_sylius_order_edit.checker.post_update_changes_checker"
class="\Setono\SyliusOrderEditPlugin\Checker\PostUpdateChangesChecker"
Expand Down
39 changes: 10 additions & 29 deletions tests/Unit/Checker/OrderPaymentEditionCheckerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,65 +7,46 @@
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Setono\SyliusOrderEditPlugin\Checker\OrderPaymentEditionChecker;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\OrderPaymentStates;
use Sylius\Component\Core\Model\Order;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

final class OrderPaymentEditionCheckerTest extends TestCase
{
use ProphecyTrait;

public function testItSaysOrderPaymentsShouldNotBeEditedIfTheRouteIsOrderEditAndThePaymentIsAlreadyProcessed(): void
public function testItSaysOrderPaymentsShouldNotBeEditedIfTheRouteIsOrderEdit(): void
{
$requestStack = $this->prophesize(RequestStack::class);
$request = new Request([], [], ['_route' => 'setono_sylius_order_edit_admin_update']);
$requestStack->getCurrentRequest()->willReturn($request);

$checker = new OrderPaymentEditionChecker($requestStack->reveal());
$order = new Order();

$order = $this->prophesize(OrderInterface::class);
$order->getPaymentState()->willReturn(OrderPaymentStates::STATE_AUTHORIZED);

self::assertFalse($checker->shouldOrderPaymentBeEdited($order->reveal()));
self::assertFalse($checker->shouldOrderPaymentBeEdited($order));
}

public function testItSaysOrderPaymentShouldBeEditedItTheRouteIsOrderEditButOrderIsAwaitingPayment(): void
{
$requestStack = $this->prophesize(RequestStack::class);
$request = new Request([], [], ['_route' => 'setono_sylius_order_edit_admin_update']);
$requestStack->getCurrentRequest()->willReturn($request);

$checker = new OrderPaymentEditionChecker($requestStack->reveal());

$order = $this->prophesize(OrderInterface::class);
$order->getPaymentState()->willReturn(OrderPaymentStates::STATE_AWAITING_PAYMENT);

self::assertTrue($checker->shouldOrderPaymentBeEdited($order->reveal()));
}

public function testItSaysOrderPaymentShouldBeEditedIfItsNotOrderEditRoute(): void
public function testItSaysOrderPaymentCanBeEditedIfItsNotOrderEditRoute(): void
{
$requestStack = $this->prophesize(RequestStack::class);
$request = new Request([], [], ['_route' => 'sylius_admin_order_any_other_route']);
$requestStack->getCurrentRequest()->willReturn($request);

$checker = new OrderPaymentEditionChecker($requestStack->reveal());
$order = new Order();

$order = $this->prophesize(OrderInterface::class);

self::assertTrue($checker->shouldOrderPaymentBeEdited($order->reveal()));
self::assertTrue($checker->shouldOrderPaymentBeEdited($order));
}

public function testItSaysOrderPaymentShouldBeEditedIfThereIsNoCurrentRequest(): void
public function testItSaysOrderPaymentCanBeEditedIfThereIsNoCurrentRequest(): void
{
$requestStack = $this->prophesize(RequestStack::class);
$requestStack->getCurrentRequest()->willReturn(null);

$checker = new OrderPaymentEditionChecker($requestStack->reveal());
$order = new Order();

$order = $this->prophesize(OrderInterface::class);

self::assertTrue($checker->shouldOrderPaymentBeEdited($order->reveal()));
self::assertTrue($checker->shouldOrderPaymentBeEdited($order));
}
}
71 changes: 71 additions & 0 deletions tests/Unit/Checker/PostCheckoutOrderPaymentEditionCheckerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusOrderEditPlugin\Tests\Unit\Checker;

use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Setono\SyliusOrderEditPlugin\Checker\PostCheckoutOrderPaymentEditionChecker;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\OrderPaymentStates;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

final class PostCheckoutOrderPaymentEditionCheckerTest extends TestCase
{
use ProphecyTrait;

public function testItSaysOrderPaymentsShouldNotBeEditedIfTheRouteIsOrderEditAndThePaymentIsAlreadyProcessed(): void
{
$requestStack = $this->prophesize(RequestStack::class);
$request = new Request([], [], ['_route' => 'setono_sylius_order_edit_admin_update']);
$requestStack->getCurrentRequest()->willReturn($request);

$checker = new PostCheckoutOrderPaymentEditionChecker($requestStack->reveal());

$order = $this->prophesize(OrderInterface::class);
$order->getPaymentState()->willReturn(OrderPaymentStates::STATE_AUTHORIZED);

self::assertFalse($checker->shouldOrderPaymentBeEdited($order->reveal()));
}

public function testItSaysOrderPaymentShouldBeEditedItTheRouteIsOrderEditButOrderIsAwaitingPayment(): void
{
$requestStack = $this->prophesize(RequestStack::class);
$request = new Request([], [], ['_route' => 'setono_sylius_order_edit_admin_update']);
$requestStack->getCurrentRequest()->willReturn($request);

$checker = new PostCheckoutOrderPaymentEditionChecker($requestStack->reveal());

$order = $this->prophesize(OrderInterface::class);
$order->getPaymentState()->willReturn(OrderPaymentStates::STATE_AWAITING_PAYMENT);

self::assertTrue($checker->shouldOrderPaymentBeEdited($order->reveal()));
}

public function testItSaysOrderPaymentShouldBeEditedIfItsNotOrderEditRoute(): void
{
$requestStack = $this->prophesize(RequestStack::class);
$request = new Request([], [], ['_route' => 'sylius_admin_order_any_other_route']);
$requestStack->getCurrentRequest()->willReturn($request);

$checker = new PostCheckoutOrderPaymentEditionChecker($requestStack->reveal());

$order = $this->prophesize(OrderInterface::class);

self::assertTrue($checker->shouldOrderPaymentBeEdited($order->reveal()));
}

public function testItSaysOrderPaymentShouldBeEditedIfThereIsNoCurrentRequest(): void
{
$requestStack = $this->prophesize(RequestStack::class);
$requestStack->getCurrentRequest()->willReturn(null);

$checker = new PostCheckoutOrderPaymentEditionChecker($requestStack->reveal());

$order = $this->prophesize(OrderInterface::class);

self::assertTrue($checker->shouldOrderPaymentBeEdited($order->reveal()));
}
}

0 comments on commit 8004c4f

Please sign in to comment.