diff --git a/config/services/validator.php b/config/services/validator.php index baebd1cd..b0028ecb 100644 --- a/config/services/validator.php +++ b/config/services/validator.php @@ -6,6 +6,7 @@ use CommerceWeavers\SyliusTpayPlugin\Validator\Constraint\EncodedGooglePayTokenValidator; use CommerceWeavers\SyliusTpayPlugin\Validator\Constraint\ForAuthorizedUserOnlyValidator; +use CommerceWeavers\SyliusTpayPlugin\Validator\Constraint\ValidTpayChannelValidator; return static function(ContainerConfigurator $container): void { $services = $container->services(); @@ -20,4 +21,11 @@ ]) ->tag('validator.constraint_validator') ; + + $services->set('commerce_weavers_sylius_tpay.validator.constraint.valid_tpay_channel', ValidTpayChannelValidator::class) + ->args([ + service('commerce_weavers_sylius_tpay.tpay.provider.validated_tpay_api_bank_list'), + ]) + ->tag('validator.constraint_validator') + ; }; diff --git a/phpstan.neon b/phpstan.neon index 6c51955a..124f7098 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -37,3 +37,4 @@ parameters: - '#Comparison operation "<=" between int<1, max> and \(array\|float\|int\) results in an error\.#' - '#Parameter \#1 \$request \(CommerceWeavers\\SyliusTpayPlugin\\Payum\\Request\\Api\\InitializeApplePayPayment\) of method CommerceWeavers\\SyliusTpayPlugin\\Payum\\Action\\Api\\InitializeApplePayPaymentAction::doExecute\(\) should be contravariant with parameter \$request \(Payum\\Core\\Request\\Generic\) of method CommerceWeavers\\SyliusTpayPlugin\\Payum\\Action\\Api\\BasePaymentAwareAction::doExecute\(\)#' - '#Parameter \#1 \$request \(CommerceWeavers\\SyliusTpayPlugin\\Payum\\Request\\Api\\Notify\) of method CommerceWeavers\\SyliusTpayPlugin\\Payum\\Action\\Api\\NotifyAction::doExecute\(\) should be contravariant with parameter \$request \(Payum\\Core\\Request\\Generic\) of method CommerceWeavers\\SyliusTpayPlugin\\Payum\\Action\\Api\\BasePaymentAwareAction::doExecute\(\)#' + - '#Parameter \#2 \$callback of function array_filter expects \(callable\(array\|bool\|object\|string\): bool\)\|null, Closure\(array\): bool given#' diff --git a/src/ContextProvider/BankListContextProvider.php b/src/ContextProvider/BankListContextProvider.php index c0af5cef..7bea178c 100644 --- a/src/ContextProvider/BankListContextProvider.php +++ b/src/ContextProvider/BankListContextProvider.php @@ -12,14 +12,12 @@ final class BankListContextProvider implements ContextProviderInterface { public function __construct( - private readonly ValidTpayChannelListProviderInterface $validatedTpayApiBankListProvider, + private readonly ValidTpayChannelListProviderInterface $validTpayChannelListProvider, ) { } public function provide(array $templateContext, TemplateBlock $templateBlock): array { - // TODO this is runned few many times when on checkout/complete/choice-item - // propably there it should not be runned totaly but on summary step only if (isset($templateContext['order'])) { /** @var OrderInterface $order */ $order = $templateContext['order']; @@ -29,7 +27,7 @@ public function provide(array $templateContext, TemplateBlock $templateBlock): a } } - $templateContext['banks'] = $this->validatedTpayApiBankListProvider->provide(); + $templateContext['banks'] = $this->validTpayChannelListProvider->provide(); return $templateContext; } diff --git a/src/Form/Type/TpayPaymentDetailsType.php b/src/Form/Type/TpayPaymentDetailsType.php index 43b765dd..1d3ba242 100644 --- a/src/Form/Type/TpayPaymentDetailsType.php +++ b/src/Form/Type/TpayPaymentDetailsType.php @@ -5,7 +5,7 @@ namespace CommerceWeavers\SyliusTpayPlugin\Form\Type; use CommerceWeavers\SyliusTpayPlugin\Validator\Constraint\EncodedGooglePayToken; -use CommerceWeavers\SyliusTpayPlugin\Validator\Constraint\ValidTpayApiChannel; +use CommerceWeavers\SyliusTpayPlugin\Validator\Constraint\ValidTpayChannel; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\HiddenType; use Symfony\Component\Form\Extension\Core\Type\TelType; @@ -71,13 +71,12 @@ public function buildForm(FormBuilderInterface $builder, array $options): void ) ->add( 'tpay_channel_id', - HiddenType::class, [ 'property_path' => '[tpay_channel_id]', 'validation_groups' => ['sylius_checkout_complete'], 'constraints' => [ - new ValidTpayApiChannel(groups: ['sylius_checkout_complete']), + new ValidTpayChannel(groups: ['sylius_checkout_complete']), ], ], ) diff --git a/src/Model/GatewayConfigInterface.php b/src/Model/GatewayConfigInterface.php new file mode 100644 index 00000000..a23b7680 --- /dev/null +++ b/src/Model/GatewayConfigInterface.php @@ -0,0 +1,12 @@ + $paymentMethodsToRemoveByGroupId[] = PayGroup::APPLE_PAY, PaymentType::GOOGLE_PAY => $paymentMethodsToRemoveByGroupId[] = PayGroup::GOOGLE_PAY, PaymentType::BLIK => $paymentMethodsToRemoveByGroupId[] = PayGroup::BLIK, + PaymentType::CARD => $paymentMethodsToRemoveByGroupId[] = PayGroup::CARD, default => null, }; } if (!$hasPblPaymentAvailable) { throw new UnableToGetBankListException( - 'Bank list cannot be retrieved if there is no payment method with PayByLink type configured' + 'Bank list cannot be retrieved if there is no payment method with PayByLink type configured', ); } - foreach ($availableChannels as $availableChannel) { - $channelId = (int) $availableChannel['id']; - if (in_array($channelId, $paymentMethodsToRemoveByGroupId, true)) { - unset($availableChannels[$channelId]); - } - } + return array_filter($availableChannels, static function (array $channel) use ($paymentMethodsToRemoveByGroupId): bool { + $groupId = (int) $channel['groups'][0]['id']; - return $availableChannels; + return !in_array($groupId, $paymentMethodsToRemoveByGroupId, true); + }); } } diff --git a/src/Validator/Constraint/ValidTpayApiChannel.php b/src/Validator/Constraint/ValidTpayChannel.php similarity index 89% rename from src/Validator/Constraint/ValidTpayApiChannel.php rename to src/Validator/Constraint/ValidTpayChannel.php index 1cf8514d..98aea6e3 100644 --- a/src/Validator/Constraint/ValidTpayApiChannel.php +++ b/src/Validator/Constraint/ValidTpayChannel.php @@ -6,7 +6,7 @@ use Symfony\Component\Validator\Constraint; -final class ValidTpayApiChannel extends Constraint +final class ValidTpayChannel extends Constraint { public string $message = 'commerce_weavers_sylius_tpay.shop.pay.tpay_channel.not_valid'; diff --git a/src/Validator/Constraint/ValidTpayApiChannelValidator.php b/src/Validator/Constraint/ValidTpayChannelValidator.php similarity index 69% rename from src/Validator/Constraint/ValidTpayApiChannelValidator.php rename to src/Validator/Constraint/ValidTpayChannelValidator.php index dea25f6e..1a262e06 100644 --- a/src/Validator/Constraint/ValidTpayApiChannelValidator.php +++ b/src/Validator/Constraint/ValidTpayChannelValidator.php @@ -7,16 +7,25 @@ use CommerceWeavers\SyliusTpayPlugin\Tpay\Provider\ValidTpayChannelListProviderInterface; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; +use Webmozart\Assert\Assert; -final class ValidTpayApiChannelValidator extends ConstraintValidator +final class ValidTpayChannelValidator extends ConstraintValidator { public function __construct( private readonly ValidTpayChannelListProviderInterface $validatedTpayApiBankListProvider, ) { } - public function validate($value, Constraint $constraint): void + public function validate(mixed $value, Constraint $constraint): void { + if (null === $value) { + return; + } + + Assert::string($value); + + Assert::isInstanceOf($constraint, ValidTpayChannel::class); + $validChannels = $this->validatedTpayApiBankListProvider->provide(); if (array_key_exists($value, $validChannels)) { diff --git a/tests/Unit/ContextProvider/BankListContextProviderTest.php b/tests/Unit/ContextProvider/BankListContextProviderTest.php index df18e6a8..7758785c 100644 --- a/tests/Unit/ContextProvider/BankListContextProviderTest.php +++ b/tests/Unit/ContextProvider/BankListContextProviderTest.php @@ -5,7 +5,7 @@ namespace Tests\CommerceWeavers\SyliusTpayPlugin\Unit\ContextProvider; use CommerceWeavers\SyliusTpayPlugin\ContextProvider\BankListContextProvider; -use CommerceWeavers\SyliusTpayPlugin\Tpay\Provider\AvailableTpayChannelListProviderInterface; +use CommerceWeavers\SyliusTpayPlugin\Tpay\Provider\ValidTpayChannelListProviderInterface; use PHPUnit\Framework\TestCase; use Prophecy\PhpUnit\ProphecyTrait; use Prophecy\Prophecy\ObjectProphecy; @@ -17,10 +17,10 @@ class BankListContextProviderTest extends TestCase { use ProphecyTrait; - private AvailableTpayChannelListProviderInterface|ObjectProphecy $bankListProvider; + private ValidTpayChannelListProviderInterface|ObjectProphecy $validTpayChannelListProvider; protected function setUp(): void { - $this->bankListProvider = $this->prophesize(AvailableTpayChannelListProviderInterface::class); + $this->validTpayChannelListProvider = $this->prophesize(ValidTpayChannelListProviderInterface::class); } public function test_it_does_not_support_some_template_event_and_pay_by_link_template_name(): void @@ -54,8 +54,8 @@ public function test_it_provides_banks_for_context_if_there_is_no_order_in_templ { $templateBlock = new TemplateBlock('pay_by_link', 'cw.tpay.shop.select_payment.choice_item_form', null, null, null, null); - $this->bankListProvider->provide()->shouldBeCalled(); - $this->bankListProvider->provide()->willReturn(['1' => 'some bank']); + $this->validTpayChannelListProvider->provide()->shouldBeCalled(); + $this->validTpayChannelListProvider->provide()->willReturn(['1' => 'some bank']); $context = $this->createTestObject()->provide( ['i_am_not_an_order' => 'some_context'], @@ -72,8 +72,8 @@ public function test_it_provides_nothing_new_for_context_if_order_has_no_payment $order = $this->prophesize(OrderInterface::class); $order->getLastPayment()->willReturn(null); - $this->bankListProvider->provide()->shouldNotBeCalled(); - $this->bankListProvider->provide()->willReturn(['1' => 'some bank']); + $this->validTpayChannelListProvider->provide()->shouldNotBeCalled(); + $this->validTpayChannelListProvider->provide()->willReturn(['1' => 'some bank']); $context = $this->createTestObject()->provide( ['order' => $order->reveal()], @@ -90,8 +90,8 @@ public function test_it_provides_bank_list_as_context_if_order_has_payment(): vo $payment = $this->prophesize(PaymentInterface::class); $order->getLastPayment()->willReturn($payment); - $this->bankListProvider->provide()->shouldBeCalled(); - $this->bankListProvider->provide()->willReturn(['1' => 'some bank']); + $this->validTpayChannelListProvider->provide()->shouldBeCalled(); + $this->validTpayChannelListProvider->provide()->willReturn(['1' => 'some bank']); $context = $this->createTestObject()->provide( ['order' => $order->reveal()], @@ -104,6 +104,6 @@ public function test_it_provides_bank_list_as_context_if_order_has_payment(): vo private function createTestObject(): BankListContextProvider { - return new BankListContextProvider($this->bankListProvider->reveal()); + return new BankListContextProvider($this->validTpayChannelListProvider->reveal()); } } diff --git a/tests/Unit/Tpay/Provider/ValidTpayChannelListProviderTest.php b/tests/Unit/Tpay/Provider/ValidTpayChannelListProviderTest.php new file mode 100644 index 00000000..97bab5f9 --- /dev/null +++ b/tests/Unit/Tpay/Provider/ValidTpayChannelListProviderTest.php @@ -0,0 +1,216 @@ + [ + 'id' => '1', + 'name' => 'some bank', + 'available' => true, + 'groups' => [ + ['id' => '1'], + ], + ], + '2' => [ + 'id' => '2', + 'name' => 'card payment', + 'available' => true, + 'groups' => [ + ['id' => '103'], + ], + ], + '3' => [ + 'id' => '3', + 'name' => 'BLIK', + 'available' => true, + 'groups' => [ + ['id' => '150'], + ], + ], + '4' => [ + 'id' => '4', + 'name' => 'Apple Pay', + 'available' => true, + 'groups' => [ + ['id' => '170'], + ], + ], + '5' => [ + 'id' => '5', + 'name' => 'Google Pay', + 'available' => true, + 'groups' => [ + ['id' => '166'], + ], + ], + '6' => [ + 'id' => '6', + 'name' => 'Visa mobile', + 'available' => true, + 'groups' => [ + ['id' => '171'], + ], + ], + ]; + + private AvailableTpayChannelListProviderInterface|ObjectProphecy $availableTpayApiBankListProvider; + + private RepositoryInterface|ObjectProphecy $gatewayMethodRepository; + + private CypherInterface|ObjectProphecy $cypher; + + protected function setUp(): void + { + $this->availableTpayApiBankListProvider = $this->prophesize(AvailableTpayChannelListProviderInterface::class); + $this->gatewayMethodRepository = $this->prophesize(RepositoryInterface::class); + $this->cypher = $this->prophesize(CypherInterface::class); + } + + public function test_it_throws_exception_if_there_is_no_tpay_based_payment(): void + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('There is no gateway config of Tpay type available'); + + $this->availableTpayApiBankListProvider->provide()->willReturn(self::BANK_LIST); + + $this->gatewayMethodRepository + ->findBy(['gatewayName' => 'tpay']) + ->willReturn([]) + ; + + $this->createTestSubject()->provide(); + } + + public function test_it_throws_exception_if_there_is_no_gateway_config_that_is_pbl_type(): void + { + $this->expectException(UnableToGetBankListException::class); + $this->expectExceptionMessage('Bank list cannot be retrieved if there is no payment method with PayByLink type configured'); + + $this->availableTpayApiBankListProvider->provide()->willReturn(self::BANK_LIST); + + $notPblGatewayConfig = $this->prophesize(GatewayConfigInterface::class); + $notPblGatewayConfigConfig = [ + 'type' => 'visa_mobile', + ]; + $notPblGatewayConfig->decrypt($this->cypher)->shouldBeCalled(); + $notPblGatewayConfig->getConfig()->willReturn($notPblGatewayConfigConfig); + + $this->gatewayMethodRepository + ->findBy(['gatewayName' => 'tpay']) + ->willReturn([$notPblGatewayConfig->reveal()]) + ; + + $this->createTestSubject()->provide(); + } + + public function test_it_returns_all_available_payments_if_only_tpay_payment_method_is_pbl(): void + { + $tpayPblGatewayConfig = $this->prophesize(GatewayConfigInterface::class); + $tpayPblGatewayConfigConfig = [ + 'type' => 'pay_by_link', + ]; + $tpayPblGatewayConfig->decrypt($this->cypher)->shouldNotBeCalled(); + $tpayPblGatewayConfig->getConfig()->willReturn($tpayPblGatewayConfigConfig); + + $this->availableTpayApiBankListProvider->provide()->willReturn(self::BANK_LIST); + + $this->gatewayMethodRepository + ->findBy(['gatewayName' => 'tpay']) + ->willReturn([$tpayPblGatewayConfig->reveal()]) + ; + + $result = $this->createTestSubject()->provide(); + + $this->assertSame(self::BANK_LIST, $result); + } + + public function test_it_returns_valid_payments_according_to_available_tpay_payment_methods(): void + { + $tpayPblGatewayConfig = $this->prophesize(GatewayConfigInterface::class); + $tpayPblGatewayConfigConfig = [ + 'type' => 'pay_by_link', + ]; + $tpayPblGatewayConfig->decrypt($this->cypher)->shouldBeCalled(); + $tpayPblGatewayConfig->getConfig()->willReturn($tpayPblGatewayConfigConfig); + + $anotherTpayPblGatewayConfig = $this->prophesize(GatewayConfigInterface::class); + $anotherTpayPblGatewayConfigConfig = [ + 'type' => 'visa_mobile', + ]; + $anotherTpayPblGatewayConfig->decrypt($this->cypher)->shouldBeCalled(); + $anotherTpayPblGatewayConfig->getConfig()->willReturn($anotherTpayPblGatewayConfigConfig); + + $this->availableTpayApiBankListProvider->provide()->willReturn(self::BANK_LIST); + + $this->gatewayMethodRepository + ->findBy(['gatewayName' => 'tpay']) + ->willReturn([ + $tpayPblGatewayConfig->reveal(), + $anotherTpayPblGatewayConfig->reveal(), + ]) + ; + + $result = $this->createTestSubject()->provide(); + + $expected = self::BANK_LIST; + unset($expected['6']); + + $this->assertSame($expected, $result); + } + + public function test_it_returns_valid_payments_even_if_gateway_config_lacks_type(): void + { + $tpayPblGatewayConfig = $this->prophesize(GatewayConfigInterface::class); + $tpayPblGatewayConfigConfig = [ + 'type' => 'pay_by_link', + ]; + $tpayPblGatewayConfig->decrypt($this->cypher)->shouldBeCalled(); + $tpayPblGatewayConfig->getConfig()->willReturn($tpayPblGatewayConfigConfig); + + $anotherTpayPblGatewayConfig = $this->prophesize(GatewayConfigInterface::class); + $anotherTpayPblGatewayConfigConfig = [ + 'i_have_no_type' => 'i_should_still_work', + ]; + $anotherTpayPblGatewayConfig->decrypt($this->cypher)->shouldBeCalled(); + $anotherTpayPblGatewayConfig->getConfig()->willReturn($anotherTpayPblGatewayConfigConfig); + + $this->availableTpayApiBankListProvider->provide()->willReturn(self::BANK_LIST); + + $this->gatewayMethodRepository + ->findBy(['gatewayName' => 'tpay']) + ->willReturn([ + $tpayPblGatewayConfig->reveal(), + $anotherTpayPblGatewayConfig->reveal(), + ]) + ; + + $result = $this->createTestSubject()->provide(); + + $this->assertSame(self::BANK_LIST, $result); + } + + private function createTestSubject(): ValidTpayChannelListProvider + { + return new ValidTpayChannelListProvider( + $this->availableTpayApiBankListProvider->reveal(), + $this->gatewayMethodRepository->reveal(), + $this->cypher->reveal(), + ); + } +} diff --git a/tests/Unit/Tpay/Provider/ValidatedTpayApiBankListProviderTest.php b/tests/Unit/Tpay/Provider/ValidatedTpayApiBankListProviderTest.php deleted file mode 100644 index bd858c0b..00000000 --- a/tests/Unit/Tpay/Provider/ValidatedTpayApiBankListProviderTest.php +++ /dev/null @@ -1,144 +0,0 @@ - [ - 'id' => '1', - 'name' => 'some bank', - 'available' => true, - ], - '103' => [ - 'id' => '103', - 'name' => 'card payment', - 'available' => true, - ], - '150' => [ - 'id' => '150', - 'name' => 'BLIK', - 'available' => true, - ], - '170' => [ - 'id' => '170', - 'name' => 'Apple Pay', - 'available' => true, - ], - '166' => [ - 'id' => '166', - 'name' => 'Google Pay', - 'available' => true, - ], - '171' => [ - 'id' => '171', - 'name' => 'Visa mobile', - 'available' => true, - ], - ]; - - private AvailableTpayChannelListProviderInterface|ObjectProphecy $availableTpayApiBankListProvider; - - private RepositoryInterface|ObjectProphecy $paymentMethodRepository; - - protected function setUp(): void - { - $this->availableTpayApiBankListProvider = $this->prophesize(AvailableTpayChannelListProviderInterface::class); - $this->paymentMethodRepository = $this->prophesize(RepositoryInterface::class); - } - - public function test_it_throws_exception_if_there_is_no_tpay_based_payment(): void - { - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('There is no payment of Tpay type available'); - - $this->availableTpayApiBankListProvider->provide()->willReturn(self::BANK_LIST); - - $this->paymentMethodRepository - ->findBy(['gatewayConfig.gateway_name' => 'tpay']) - ->willReturn(new ArrayCollection([])) - ; - - $this->createTestSubject()->provide(); - } - - public function test_it_returns_all_available_payments_if_only_tpay_payment_method_is_pbl(): void - { - $tpayPblPaymentMethod = $this->prophesize(PaymentMethodInterface::class); - $tpayPblGatewayConfig = $this->prophesize(GatewayConfigInterface::class); - $tpayPblGatewayConfigConfig = [ - 'type' => 'pay_by_link', - ]; - $tpayPblGatewayConfig->getConfig()->willReturn($tpayPblGatewayConfigConfig); - $tpayPblPaymentMethod->getGatewayConfig()->willReturn($tpayPblGatewayConfig->reveal()); - - $this->availableTpayApiBankListProvider->provide()->willReturn(self::BANK_LIST); - - $this->paymentMethodRepository - ->findBy(['gatewayConfig.gateway_name' => 'tpay']) - ->willReturn(new ArrayCollection([$tpayPblPaymentMethod->reveal()])) - ; - - $result = $this->createTestSubject()->provide(); - - $this->assertSame(self::BANK_LIST, $result); - } - - public function test_it_returns_valid_payments_according_to_available_tpay_payment_methods(): void - { - $tpayPblPaymentMethod = $this->prophesize(PaymentMethodInterface::class); - $tpayPblGatewayConfig = $this->prophesize(GatewayConfigInterface::class); - $tpayPblGatewayConfigConfig = [ - 'type' => 'pay_by_link', - ]; - $tpayPblGatewayConfig->getConfig()->willReturn($tpayPblGatewayConfigConfig); - $tpayPblPaymentMethod->getGatewayConfig()->willReturn($tpayPblGatewayConfig->reveal()); - - $anotherTpayPblPaymentMethod = $this->prophesize(PaymentMethodInterface::class); - $anotherTpayPblGatewayConfig = $this->prophesize(GatewayConfigInterface::class); - $anotherTpayPblGatewayConfigConfig = [ - 'type' => 'visa_mobile', - ]; - $anotherTpayPblGatewayConfig->getConfig()->willReturn($anotherTpayPblGatewayConfigConfig); - $anotherTpayPblPaymentMethod->getGatewayConfig()->willReturn($anotherTpayPblGatewayConfig->reveal()); - - $this->availableTpayApiBankListProvider->provide()->willReturn(self::BANK_LIST); - - $this->paymentMethodRepository - ->findBy(['gatewayConfig.gateway_name' => 'tpay']) - ->willReturn(new ArrayCollection([ - $tpayPblPaymentMethod->reveal(), - $anotherTpayPblPaymentMethod->reveal(), - ])) - ; - - $result = $this->createTestSubject()->provide(); - - $expected = self::BANK_LIST; - unset($expected['171']); - - $this->assertSame($expected, $result); - } - - private function createTestSubject(): ValidTpayChannelListProvider - { - return new ValidTpayChannelListProvider( - $this->availableTpayApiBankListProvider->reveal(), - $this->paymentMethodRepository->reveal(), - ); - } -} diff --git a/tests/Unit/Validator/Constraint/ValidTpayChannelValidatorTest.php b/tests/Unit/Validator/Constraint/ValidTpayChannelValidatorTest.php new file mode 100644 index 00000000..0801493d --- /dev/null +++ b/tests/Unit/Validator/Constraint/ValidTpayChannelValidatorTest.php @@ -0,0 +1,91 @@ + [ + 'id' => '1', + 'name' => 'some bank', + 'available' => true, + 'groups' => [ + ['id' => '1'], + ], + ], + '2' => [ + 'id' => '2', + 'name' => 'card payment', + 'available' => true, + 'groups' => [ + ['id' => '103'], + ], + ], + ]; + + private ValidTpayChannelListProviderInterface|ObjectProphecy $validTpayChannelListProvider; + + protected function setUp(): void + { + $this->validTpayChannelListProvider = $this->prophesize(ValidTpayChannelListProviderInterface::class); + + parent::setUp(); + } + + public function test_it_throws_an_exception_if_a_value_is_not_a_string(): void + { + $this->expectException(InvalidArgumentException::class); + + $this->validator->validate(new \stdClass(), new ValidTpayChannel()); + } + + public function test_it_throws_an_exception_if_a_value_has_an_invalid_type(): void + { + $this->expectException(InvalidArgumentException::class); + + $this->validator->validate(1, new ValidTpayChannel()); + } + + public function test_it_throws_an_exception_if_a_constraint_has_an_invalid_type(): void + { + $this->expectException(InvalidArgumentException::class); + + $this->validator->validate( + '11', + $this->prophesize(Constraint::class)->reveal(), + ); + } + + public function test_it_builds_violation_if_tpay_channel_id_is_not_valid(): void + { + $this->validTpayChannelListProvider->provide()->willReturn(self::EXAMPLE_PROVIDE); + + $this->validator->validate( + '111', + new ValidTpayChannel(), + ); + + $this->buildViolation('commerce_weavers_sylius_tpay.shop.pay.tpay_channel.not_valid') + ->setCode('632f97f3-c302-409b-a321-ec078194302d') + ->assertRaised() + ; + } + + protected function createValidator(): ValidTpayChannelValidator + { + return new ValidTpayChannelValidator($this->validTpayChannelListProvider->reveal()); + } +} diff --git a/translations/validators.en.yaml b/translations/validators.en.yaml index d17c14ea..527396bc 100644 --- a/translations/validators.en.yaml +++ b/translations/validators.en.yaml @@ -16,6 +16,8 @@ commerce_weavers_sylius_tpay: not_json_encoded: 'The Google Pay token must be a JSON object encoded with Base64.' encoded_card_data: required: 'The card data is required.' + tpay_channel: + not_valid: 'Selected payment channel is not valid anymore.' tpay_channel_id: length: 'Bank channel id cannot be empty.' required: 'Bank channel id is required.' diff --git a/translations/validators.pl.yaml b/translations/validators.pl.yaml index 71b1775d..3e182089 100644 --- a/translations/validators.pl.yaml +++ b/translations/validators.pl.yaml @@ -16,6 +16,8 @@ commerce_weavers_sylius_tpay: not_json_encoded: 'Token Google Pay musi być obiektem JSON zakodowanym przez Base64.' encoded_card_data: required: 'Dane karty są wymagane.' + tpay_channel: + not_valid: 'Wybrany kanał płatności nie jest już dostępny.' tpay_channel_id: length: 'Id kanału banku nie może być puste.' required: 'Id kanału banku jest wymagane.'