Skip to content

Commit

Permalink
Extract the RemoveUnnecessaryPaymentDetailsFieldsListener from the Tp…
Browse files Browse the repository at this point in the history
…ayPaymentDetailsType
  • Loading branch information
jakubtobiasz committed Sep 13, 2024
1 parent fdc1446 commit 8b26e3a
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 20 deletions.
18 changes: 13 additions & 5 deletions config/services/form.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use CommerceWeavers\SyliusTpayPlugin\Form\DataTransformer\CardTypeDataTransformer;
use CommerceWeavers\SyliusTpayPlugin\Form\EventListener\PreventSavingEmptyClientSecretListener;
use CommerceWeavers\SyliusTpayPlugin\Form\EventListener\RemoveUnnecessaryPaymentDetailsFieldsListener;
use CommerceWeavers\SyliusTpayPlugin\Form\Extension\CompleteTypeExtension;
use CommerceWeavers\SyliusTpayPlugin\Form\Type\TpayCardType;
use CommerceWeavers\SyliusTpayPlugin\Form\Type\TpayGatewayConfigurationType;
Expand All @@ -15,15 +16,13 @@
return function(ContainerConfigurator $container): void {
$services = $container->services();

$services->set('commerce_weavers_tpay.form.data_transformer.card_type', CardTypeDataTransformer::class);

$services->set(CompleteTypeExtension::class)
->tag('form.type_extension')
;

$services->set(TpayGatewayConfigurationType::class)
->args([
service(PreventSavingEmptyClientSecretListener::class),
service('commerce_weavers_tpay.form.event_listener.prevent_saving_empty_client_secret'),
])
->tag(
'sylius.gateway_configuration_type',
Expand All @@ -39,7 +38,16 @@
->tag('form.type')
;

$services->set(TpayPaymentDetailsType::class)->tag('form.type');
$services->set(TpayPaymentDetailsType::class)
->args([
service('commerce_weavers_tpay.form.event_listener.remove_unnecessary_payment_details_fields'),
])
->tag('form.type')
;

$services->set('commerce_weavers_tpay.form.data_transformer.card_type', CardTypeDataTransformer::class);

$services->set('commerce_weavers_tpay.form.event_listener.prevent_saving_empty_client_secret', PreventSavingEmptyClientSecretListener::class);

$services->set(PreventSavingEmptyClientSecretListener::class);
$services->set('commerce_weavers_tpay.form.event_listener.remove_unnecessary_payment_details_fields', RemoveUnnecessaryPaymentDetailsFieldsListener::class);
};
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ parameters:
- '/Call to method getSubject\(\) on an unknown class Symfony\\Component\\Workflow\\Event\\TransitionEvent\./'
- '#Class CommerceWeavers\\SyliusTpayPlugin\\Form\\DataTransformer\\CardTypeDataTransformer implements generic interface Symfony\\Component\\Form\\DataTransformerInterface but does not specify its types: TValue, TTransformedValue#'
- '#Method CommerceWeavers\\SyliusTpayPlugin\\Form\\Type\\TpayCardType::__construct\(\) has parameter \$cardTypeDataTransformer with generic interface Symfony\\Component\\Form\\DataTransformerInterface but does not specify its types: TValue, TTransformedValue#'
- '#Parameter \#2 \$listener of method .+::addEventListener\(\) expects callable\(\): mixed, array.+ given\.#'
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace CommerceWeavers\SyliusTpayPlugin\Form\EventListener;

use Symfony\Component\Form\FormEvent;

final class RemoveUnnecessaryPaymentDetailsFieldsListener
{
public function __invoke(FormEvent $event): void
{
/** @var array{card?: string, blik_token?: string} $data */
$data = $event->getData() ?? [];
$form = $event->getForm();

if (!isset($data['card'])) {
$form->remove('card');
}

if (!isset($data['blik_token'])) {
$form->remove('blik_token');
}
}
}
24 changes: 9 additions & 15 deletions src/Form/Type/TpayPaymentDetailsType.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;

final class TpayPaymentDetailsType extends AbstractType
{
public function __construct(
private object $removeUnnecessaryPaymentDetailsFieldsListener,
) {
}

/**
* @param array<string, mixed> $options
*/
Expand All @@ -35,19 +39,9 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
)
;

/** TODO: Extract into a separate class */
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
/** @var array{card?: string, blik_token?: string} $data */
$data = $event->getData() ?? [];
$form = $event->getForm();

if (!isset($data['card'])) {
$form->remove('card');
}

if (!isset($data['blik_token'])) {
$form->remove('blik_token');
}
});
$builder->addEventListener(
FormEvents::PRE_SUBMIT,
[$this->removeUnnecessaryPaymentDetailsFieldsListener, '__invoke'],
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace Tests\CommerceWeavers\SyliusTpayPlugin\Unit\Form\EventListener;

use CommerceWeavers\SyliusTpayPlugin\Form\EventListener\RemoveUnnecessaryPaymentDetailsFieldsListener;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormInterface;

final class RemoveUnnecessaryPaymentDetailsFieldsListenerTest extends TestCase
{
use ProphecyTrait;

public function test_it_removes_card_field_once_card_data_is_not_set(): void
{
$form = $this->prophesize(FormInterface::class);
$form->remove('card')->shouldBeCalled()->willReturn($form);
$form->remove('blik_token')->shouldNotBeCalled();

$event = new FormEvent($form->reveal(), ['blik_token' => '123456']);

$this->createTestSubject()->__invoke($event);
}

public function test_it_removes_blik_token_field_once_blik_token_is_not_set(): void
{
$form = $this->prophesize(FormInterface::class);
$form->remove('card')->shouldNotBeCalled();
$form->remove('blik_token')->shouldBeCalled()->willReturn($form);

$event = new FormEvent($form->reveal(), ['card' => 'h45h']);

$this->createTestSubject()->__invoke($event);
}

public function test_it_removes_both_card_and_blik_token_if_none_of_them_is_passed(): void
{
$form = $this->prophesize(FormInterface::class);
$form->remove('card')->shouldBeCalled()->willReturn($form);
$form->remove('blik_token')->shouldBeCalled()->willReturn($form);

$event = new FormEvent($form->reveal(), []);

$this->createTestSubject()->__invoke($event);
}

public function createTestSubject(): object
{
return new RemoveUnnecessaryPaymentDetailsFieldsListener();
}
}

0 comments on commit 8b26e3a

Please sign in to comment.