generated from Setono/SyliusPluginSkeleton
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Apply different checkers for order pre and post completion
- Loading branch information
Showing
5 changed files
with
123 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
tests/Unit/Checker/PostCheckoutOrderPaymentEditionCheckerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} | ||
} |