-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #11863 [API]Payment Method integrity check (arti0090)
This PR was merged into the 1.9-dev branch. Discussion ---------- | Q | A | --------------- | ----- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | License | MIT Commits ------- 7bd768e Payment Method integrity check 8265dbd added spec and changed validator for to be used on command 298d4ba fixed psalm issues 58038a7 fixed specs after changing class used in validator 2b4dff3 fix styling issues 3f7b8ae Change names of validators and move validators to one file
- Loading branch information
Showing
7 changed files
with
275 additions
and
3 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
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
33 changes: 33 additions & 0 deletions
33
src/Sylius/Bundle/ApiBundle/Validator/Constraints/OrderPaymentMethodEligibility.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,33 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Bundle\ApiBundle\Validator\Constraints; | ||
|
||
use Symfony\Component\Validator\Constraint; | ||
|
||
/** @experimental */ | ||
final class OrderPaymentMethodEligibility extends Constraint | ||
{ | ||
/** @var string */ | ||
public $message = 'sylius.order.payment_method_eligibility'; | ||
|
||
public function validatedBy(): string | ||
{ | ||
return 'sylius_api_order_payment_method_eligibility'; | ||
} | ||
|
||
public function getTargets(): string | ||
{ | ||
return self::CLASS_CONSTRAINT; | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/Sylius/Bundle/ApiBundle/Validator/Constraints/OrderPaymentMethodEligibilityValidator.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,55 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Bundle\ApiBundle\Validator\Constraints; | ||
|
||
use Sylius\Bundle\ApiBundle\Command\OrderTokenValueAwareInterface; | ||
use Sylius\Component\Core\Model\OrderInterface; | ||
use Sylius\Component\Core\Model\PaymentInterface; | ||
use Sylius\Component\Core\Repository\OrderRepositoryInterface; | ||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\ConstraintValidator; | ||
use Webmozart\Assert\Assert; | ||
|
||
/** @experimental */ | ||
final class OrderPaymentMethodEligibilityValidator extends ConstraintValidator | ||
{ | ||
/** @var OrderRepositoryInterface */ | ||
private $orderRepository; | ||
|
||
public function __construct(OrderRepositoryInterface $orderRepository) | ||
{ | ||
$this->orderRepository = $orderRepository; | ||
} | ||
|
||
public function validate($command, Constraint $constraint): void | ||
{ | ||
Assert::isInstanceOf($command, OrderTokenValueAwareInterface::class); | ||
|
||
/** @var OrderPaymentMethodEligibility $constraint */ | ||
Assert::isInstanceOf($constraint, OrderPaymentMethodEligibility::class); | ||
|
||
/** @var OrderInterface $order */ | ||
Assert::notNull($order = $this->orderRepository->findOneBy(['tokenValue' => $command->getOrderTokenValue()])); | ||
|
||
/** @var PaymentInterface $payment */ | ||
foreach($order->getPayments() as $payment) { | ||
if (!$payment->getMethod()->isEnabled()) { | ||
$this->context->addViolation( | ||
$constraint->message, | ||
['%paymentMethodName%' => $payment->getMethod()->getName()] | ||
); | ||
} | ||
} | ||
} | ||
} |
153 changes: 153 additions & 0 deletions
153
...undle/ApiBundle/spec/Validator/Constraints/OrderPaymentMethodEligibilityValidatorSpec.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,153 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace spec\Sylius\Bundle\ApiBundle\Validator\Constraints; | ||
|
||
use Doctrine\Common\Collections\ArrayCollection; | ||
use PhpSpec\ObjectBehavior; | ||
use Sylius\Bundle\ApiBundle\Command\Checkout\CompleteOrder; | ||
use Sylius\Bundle\ApiBundle\Command\OrderTokenValueAwareInterface; | ||
use Sylius\Bundle\ApiBundle\Validator\Constraints\OrderPaymentMethodEligibility; | ||
use Sylius\Component\Core\Model\OrderInterface; | ||
use Sylius\Component\Core\Model\PaymentInterface; | ||
use Sylius\Component\Core\Model\PaymentMethodInterface; | ||
use Sylius\Component\Core\Repository\OrderRepositoryInterface; | ||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\ConstraintValidatorInterface; | ||
use Symfony\Component\Validator\Context\ExecutionContextInterface; | ||
|
||
final class OrderPaymentMethodEligibilityValidatorSpec extends ObjectBehavior | ||
{ | ||
function let(OrderRepositoryInterface $orderRepository): void | ||
{ | ||
$this->beConstructedWith($orderRepository); | ||
} | ||
|
||
function it_is_a_constraint_validator(): void | ||
{ | ||
$this->shouldImplement(ConstraintValidatorInterface::class); | ||
} | ||
|
||
function it_throws_an_exception_if_constraint_does_not_extend_order_token_value_aware_interface(): void | ||
{ | ||
$this | ||
->shouldThrow(\InvalidArgumentException::class) | ||
->during('validate', ['', new class() extends Constraint {}]) | ||
; | ||
} | ||
|
||
function it_throws_an_exception_if_constraint_does_not_type_of_order_shipping_method_eligibility(): void { | ||
$constraint = new class() extends Constraint implements OrderTokenValueAwareInterface { | ||
private $orderTokenValue; | ||
|
||
function getOrderTokenValue(): ?string | ||
{ | ||
return 'abc'; | ||
} | ||
|
||
function setOrderTokenValue(?string $orderTokenValue): void | ||
{ | ||
$this->orderTokenValue = $orderTokenValue; | ||
} | ||
}; | ||
|
||
$this | ||
->shouldThrow(\InvalidArgumentException::class) | ||
->during('validate', ['', $constraint]) | ||
; | ||
} | ||
|
||
function it_throws_an_exception_if_order_is_null(OrderRepositoryInterface $orderRepository): void | ||
{ | ||
$constraint = new OrderPaymentMethodEligibility(); | ||
|
||
$value = new CompleteOrder(); | ||
$value->setOrderTokenValue('token'); | ||
|
||
$orderRepository->findOneBy(['tokenValue' => 'token'])->willReturn(null); | ||
|
||
$this | ||
->shouldThrow(\InvalidArgumentException::class) | ||
->during('validate', [$value, $constraint]) | ||
; | ||
} | ||
|
||
function it_adds_violation_if_payment_is_not_available_anymore( | ||
OrderRepositoryInterface $orderRepository, | ||
OrderInterface $order, | ||
PaymentInterface $payment, | ||
PaymentMethodInterface $paymentMethod, | ||
ExecutionContextInterface $executionContext | ||
): void { | ||
$this->initialize($executionContext); | ||
|
||
$constraint = new OrderPaymentMethodEligibility(); | ||
|
||
$value = new CompleteOrder(); | ||
$value->setOrderTokenValue('token'); | ||
|
||
$orderRepository->findOneBy(['tokenValue' => 'token'])->willReturn($order); | ||
|
||
$order->getPayments()->willReturn(new ArrayCollection([$payment->getWrappedObject()])); | ||
|
||
$payment->getMethod()->willReturn($paymentMethod); | ||
|
||
$paymentMethod->getName()->willReturn('bank transfer'); | ||
|
||
$paymentMethod->isEnabled()->willReturn(false); | ||
|
||
$executionContext | ||
->addViolation( | ||
"sylius.order.payment_method_eligibility", | ||
["%paymentMethodName%" => 'bank transfer'] | ||
) | ||
->shouldBeCalled() | ||
; | ||
|
||
$this->validate($value, $constraint); | ||
} | ||
|
||
function it_does_not_add_violation_if_payment_is_available( | ||
OrderRepositoryInterface $orderRepository, | ||
OrderInterface $order, | ||
PaymentInterface $payment, | ||
PaymentMethodInterface $paymentMethod, | ||
ExecutionContextInterface $executionContext | ||
): void { | ||
$this->initialize($executionContext); | ||
|
||
$constraint = new OrderPaymentMethodEligibility(); | ||
|
||
$value = new CompleteOrder(); | ||
$value->setOrderTokenValue('token'); | ||
|
||
$orderRepository->findOneBy(['tokenValue' => 'token'])->willReturn($order); | ||
|
||
$order->getPayments()->willReturn(new ArrayCollection([$payment->getWrappedObject()])); | ||
|
||
$payment->getMethod()->willReturn($paymentMethod); | ||
|
||
$paymentMethod->getName()->willReturn('bank transfer'); | ||
$paymentMethod->isEnabled()->willReturn(true); | ||
|
||
$executionContext | ||
->addViolation( | ||
"sylius.order.payment_method_eligibility", | ||
["%paymentMethodName%" => 'bank transfer'] | ||
) | ||
->shouldNotBeCalled() | ||
; | ||
|
||
$this->validate($value, $constraint); | ||
} | ||
} |