-
-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
386 additions
and
32 deletions.
There are no files selected for viewing
94 changes: 94 additions & 0 deletions
94
spec/Validator/Cart/PaymentMethodAvailableValidatorSpec.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,94 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace spec\Sylius\ShopApiPlugin\Validator\Cart; | ||
|
||
use Doctrine\Common\Collections\ArrayCollection; | ||
use PhpSpec\ObjectBehavior; | ||
use Prophecy\Argument; | ||
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 Sylius\Component\Payment\Resolver\PaymentMethodsResolverInterface; | ||
use Sylius\ShopApiPlugin\Request\Checkout\ChoosePaymentMethodRequest; | ||
use Sylius\ShopApiPlugin\Validator\Constraints\PaymentMethodAvailable; | ||
use Symfony\Component\Validator\Context\ExecutionContextInterface; | ||
use Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface; | ||
|
||
final class PaymentMethodAvailableValidatorSpec extends ObjectBehavior | ||
{ | ||
function let( | ||
ChoosePaymentMethodRequest $request, | ||
OrderRepositoryInterface $orderRepository, | ||
PaymentMethodsResolverInterface $paymentMethodsResolver, | ||
ExecutionContextInterface $context | ||
): void { | ||
$this->beConstructedWith($orderRepository, $paymentMethodsResolver); | ||
$this->initialize($context); | ||
|
||
$request->getOrderToken()->willReturn('NON_EXISTING'); | ||
$request->getPaymentId()->willReturn(0); | ||
$request->getMethod()->willReturn('COD'); | ||
} | ||
|
||
function it_does_not_validate_carts_if_the_order_does_not_exist( | ||
OrderRepositoryInterface $orderRepository, | ||
PaymentMethodsResolverInterface $paymentMethodsResolver, | ||
ExecutionContextInterface $context, | ||
ChoosePaymentMethodRequest $request | ||
): void { | ||
$orderRepository->findOneBy(['tokenValue' => 'NON_EXISTING', 'state' => 'cart'])->willReturn(null); | ||
$paymentMethodsResolver->getSupportedMethods(Argument::any())->shouldNotBeCalled(); | ||
$context->buildViolation(Argument::any())->shouldNotBeCalled(); | ||
|
||
$this->validate($request, new PaymentMethodAvailable()); | ||
} | ||
|
||
function it_adds_a_violation_if_payment_method_is_not_available( | ||
OrderRepositoryInterface $orderRepository, | ||
OrderInterface $cart, | ||
PaymentInterface $payment, | ||
ConstraintViolationBuilderInterface $violationBuilder, | ||
PaymentMethodsResolverInterface $paymentMethodsResolver, | ||
PaymentMethodInterface $paymentMethod, | ||
ExecutionContextInterface $context, | ||
ChoosePaymentMethodRequest $request | ||
): void { | ||
$orderRepository->findOneBy(['tokenValue' => 'NON_EXISTING', 'state' => 'cart'])->willReturn($cart); | ||
$cart->getPayments()->willReturn(new ArrayCollection([$payment->getWrappedObject()])); | ||
|
||
$paymentMethodsResolver->getSupportedMethods($payment)->shouldBeCalled()->willReturn([$paymentMethod]); | ||
$paymentMethod->getCode()->willReturn('paypal'); | ||
|
||
$context->buildViolation('sylius.shop_api.checkout.payment_method_not_available')->willReturn( | ||
$violationBuilder | ||
) | ||
; | ||
$violationBuilder->atPath('method')->willReturn($violationBuilder); | ||
$violationBuilder->addViolation()->shouldBeCalled(); | ||
|
||
$this->validate($request, new PaymentMethodAvailable()); | ||
} | ||
|
||
function it_adds_no_violation_if_payment_method_is_available( | ||
OrderRepositoryInterface $orderRepository, | ||
OrderInterface $cart, | ||
PaymentInterface $payment, | ||
PaymentMethodsResolverInterface $paymentMethodsResolver, | ||
PaymentMethodInterface $paymentMethod, | ||
ExecutionContextInterface $context, | ||
ChoosePaymentMethodRequest $request | ||
): void { | ||
$orderRepository->findOneBy(['tokenValue' => 'NON_EXISTING', 'state' => 'cart'])->willReturn($cart); | ||
$cart->getPayments()->willReturn(new ArrayCollection([$payment->getWrappedObject()])); | ||
|
||
$paymentMethodsResolver->getSupportedMethods($payment)->shouldBeCalled()->willReturn([$paymentMethod]); | ||
$paymentMethod->getCode()->willReturn('COD'); | ||
|
||
$context->buildViolation(Argument::any())->shouldNotBeCalled(); | ||
|
||
$this->validate($request, new PaymentMethodAvailable()); | ||
} | ||
} |
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,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace spec\Sylius\ShopApiPlugin\Validator\Cart; | ||
|
||
use PhpSpec\ObjectBehavior; | ||
use Prophecy\Argument; | ||
use Sylius\Component\Core\Model\OrderInterface; | ||
use Sylius\Component\Core\Repository\PaymentMethodRepositoryInterface; | ||
use Sylius\ShopApiPlugin\Validator\Constraints\CartExists; | ||
use Sylius\ShopApiPlugin\Validator\Constraints\PaymentMethodExists; | ||
use Symfony\Component\Validator\Context\ExecutionContextInterface; | ||
|
||
final class PaymentMethodExistsValidatorSpec extends ObjectBehavior | ||
{ | ||
function let( | ||
ExecutionContextInterface $executionContext, | ||
PaymentMethodRepositoryInterface $paymentMethodRepository | ||
): void { | ||
$this->beConstructedWith($paymentMethodRepository); | ||
|
||
$this->initialize($executionContext); | ||
} | ||
|
||
function it_does_not_add_constraint_if_payment_method_exists( | ||
OrderInterface $order, | ||
PaymentMethodRepositoryInterface $paymentMethodRepository, | ||
ExecutionContextInterface $executionContext | ||
): void { | ||
$paymentMethodRepository->findOneBy(['code' => 'paypal'])->willReturn($order); | ||
|
||
$executionContext->addViolation(Argument::any(), Argument::any())->shouldNotBeCalled(); | ||
|
||
$this->validate('paypal', new PaymentMethodExists()); | ||
} | ||
|
||
function it_adds_constraint_if_payment_method_does_not_exists( | ||
PaymentMethodRepositoryInterface $paymentMethodRepository, | ||
ExecutionContextInterface $executionContext | ||
): void { | ||
$paymentMethodRepository->findOneBy(['code' => 'paypal'])->willReturn(null); | ||
|
||
$executionContext->addViolation('sylius.shop_api.checkout.payment_method_does_not_exist')->shouldBeCalled(); | ||
|
||
$this->validate('paypal', new PaymentMethodExists()); | ||
} | ||
|
||
function it_throws_an_exception_if_constraint_is_not_payment_exists(): void | ||
{ | ||
$this | ||
->shouldThrow(\InvalidArgumentException::class) | ||
->during('validate', ['paypal', new CartExists()]) | ||
; | ||
} | ||
} |
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
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,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\ShopApiPlugin\Validator\Cart; | ||
|
||
use Sylius\Component\Core\Repository\PaymentMethodRepositoryInterface; | ||
use Sylius\ShopApiPlugin\Validator\Constraints\PaymentMethodExists; | ||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\ConstraintValidator; | ||
use Webmozart\Assert\Assert; | ||
|
||
final class PaymentMethodExistsValidator extends ConstraintValidator | ||
{ | ||
/** @var PaymentMethodRepositoryInterface */ | ||
private $paymentMethodRepository; | ||
|
||
public function __construct(PaymentMethodRepositoryInterface $paymentMethodRepository) | ||
{ | ||
$this->paymentMethodRepository = $paymentMethodRepository; | ||
} | ||
|
||
/** {@inheritdoc} */ | ||
public function validate($value, Constraint $constraint): void | ||
{ | ||
Assert::isInstanceOf($constraint, PaymentMethodExists::class); | ||
|
||
if ($this->paymentMethodRepository->findOneBy(['code' => $value]) === null) { | ||
/** @var PaymentMethodExists $constraint */ | ||
$this->context->addViolation($constraint->message); | ||
} | ||
} | ||
} |
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,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\ShopApiPlugin\Validator\Constraints; | ||
|
||
use Sylius\ShopApiPlugin\Validator\Cart\PaymentMethodExistsValidator; | ||
use Symfony\Component\Validator\Constraint; | ||
|
||
final class PaymentMethodExists extends Constraint | ||
{ | ||
/** @var string */ | ||
public $message = 'sylius.shop_api.checkout.payment_method_does_not_exist'; | ||
|
||
public function validatedBy(): string | ||
{ | ||
return PaymentMethodExistsValidator::class; | ||
} | ||
} |
Oops, something went wrong.