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.
Merge pull request #39 from Setono/do-not-touched-payment-on-edited-o…
…rders Do not touch payments on edited orders
- Loading branch information
Showing
9 changed files
with
275 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,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusOrderEditPlugin\Checker; | ||
|
||
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; | ||
} | ||
|
||
return 'setono_sylius_order_edit_admin_update' !== $request->attributes->get('_route'); | ||
} | ||
} |
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
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
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,52 @@ | ||
<?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\Order; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
|
||
final class OrderPaymentEditionCheckerTest extends TestCase | ||
{ | ||
use ProphecyTrait; | ||
|
||
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(); | ||
|
||
self::assertFalse($checker->shouldOrderPaymentBeEdited($order)); | ||
} | ||
|
||
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(); | ||
|
||
self::assertTrue($checker->shouldOrderPaymentBeEdited($order)); | ||
} | ||
|
||
public function testItSaysOrderPaymentCanBeEditedIfThereIsNoCurrentRequest(): void | ||
{ | ||
$requestStack = $this->prophesize(RequestStack::class); | ||
$requestStack->getCurrentRequest()->willReturn(null); | ||
|
||
$checker = new OrderPaymentEditionChecker($requestStack->reveal()); | ||
$order = new Order(); | ||
|
||
self::assertTrue($checker->shouldOrderPaymentBeEdited($order)); | ||
} | ||
} |
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())); | ||
} | ||
} |
Oops, something went wrong.