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.
Refactor checking for payments edition
- Loading branch information
Showing
7 changed files
with
188 additions
and
29 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
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 OrderPaymentEditionChecker 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusOrderEditPlugin\Checker; | ||
|
||
use Sylius\Component\Order\Model\OrderInterface; | ||
|
||
interface OrderPaymentEditionCheckerInterface | ||
{ | ||
public function shouldOrderPaymentBeEdited(OrderInterface $order): bool; | ||
} |
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
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,71 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusOrderEditPlugin\Tests\Unit\Checker; | ||
|
||
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 Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
|
||
final class OrderPaymentEditionCheckerTest 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 OrderPaymentEditionChecker($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 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 | ||
{ | ||
$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 = $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 OrderPaymentEditionChecker($requestStack->reveal()); | ||
|
||
$order = $this->prophesize(OrderInterface::class); | ||
|
||
self::assertTrue($checker->shouldOrderPaymentBeEdited($order->reveal())); | ||
} | ||
} |
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