Skip to content

Commit

Permalink
Extract the CardTypeDataTransformer from the TpayCardType
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubtobiasz committed Sep 13, 2024
1 parent be90545 commit fdc1446
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 20 deletions.
10 changes: 9 additions & 1 deletion config/services/form.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use CommerceWeavers\SyliusTpayPlugin\Form\DataTransformer\CardTypeDataTransformer;
use CommerceWeavers\SyliusTpayPlugin\Form\EventListener\PreventSavingEmptyClientSecretListener;
use CommerceWeavers\SyliusTpayPlugin\Form\Extension\CompleteTypeExtension;
use CommerceWeavers\SyliusTpayPlugin\Form\Type\TpayCardType;
Expand All @@ -14,6 +15,8 @@
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')
;
Expand All @@ -29,7 +32,12 @@
->tag('form.type')
;

$services->set(TpayCardType::class)->tag('form.type');
$services->set(TpayCardType::class)
->args([
service('commerce_weavers_tpay.form.data_transformer.card_type'),
])
->tag('form.type')
;

$services->set(TpayPaymentDetailsType::class)->tag('form.type');

Expand Down
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ parameters:
- '/Parameter \#1 \$request \([^)]+\) of method [^:]+::execute\(\) should be contravariant with parameter \$request \(mixed\) of method Payum\\Core\\Action\\ActionInterface::execute\(\)/'
- '/Parameter \$event of method CommerceWeavers\\SyliusTpayPlugin\\Refunding\\Workflow\\Listener\\DispatchRefundListener::__invoke\(\) has invalid type Symfony\\Component\\Workflow\\Event\\TransitionEvent\./'
- '/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#'
27 changes: 27 additions & 0 deletions src/Form/DataTransformer/CardTypeDataTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace CommerceWeavers\SyliusTpayPlugin\Form\DataTransformer;

use Symfony\Component\Form\DataTransformerInterface;

final class CardTypeDataTransformer implements DataTransformerInterface
{
public function transform($value): ?array
{
return null;
}

/**
* @param mixed|array{card: string} $value
*/
public function reverseTransform(mixed $value): string
{
if (!is_array($value)) {
return '';
}

return $value['card'];
}
}
25 changes: 6 additions & 19 deletions src/Form/Type/TpayCardType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@

final class TpayCardType extends AbstractType
{
public function __construct(
private DataTransformerInterface $cardTypeDataTransformer,
) {
}

/**
* @param array<string, mixed> $options
*/
Expand Down Expand Up @@ -76,24 +81,6 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
->add('card', HiddenType::class)
;

/** TODO: Extract into a separate class */
$builder->addModelTransformer(new class() implements DataTransformerInterface {
public function transform($value): ?array
{
return null;
}

/**
* @param mixed|array{card: string} $value
*/
public function reverseTransform(mixed $value): string
{
if (!is_array($value)) {
return '';
}

return $value['card'];
}
});
$builder->addModelTransformer($this->cardTypeDataTransformer);
}
}
37 changes: 37 additions & 0 deletions tests/Unit/Form/DataTransformer/CardTypeDataTransformerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

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

use CommerceWeavers\SyliusTpayPlugin\Form\DataTransformer\CardTypeDataTransformer;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\DataTransformerInterface;

final class CardTypeDataTransformerTest extends TestCase
{
public function test_it_returns_null_on_a_transform(): void
{
$dataTransformer = $this->createTestSubject();

$this->assertSame(null, $dataTransformer->transform(null));
$this->assertSame(null, $dataTransformer->transform(true));
$this->assertSame(null, $dataTransformer->transform('string'));
$this->assertSame(null, $dataTransformer->transform(1));
}

public function test_it_returns_an_empty_string_when_trying_to_reverse_transform_a_non_array(): void
{
$dataTransformer = $this->createTestSubject();

$this->assertSame('', $dataTransformer->reverseTransform(null));
$this->assertSame('', $dataTransformer->reverseTransform(true));
$this->assertSame('', $dataTransformer->reverseTransform('string'));
$this->assertSame('', $dataTransformer->reverseTransform(1));
}

private function createTestSubject(): DataTransformerInterface
{
return new CardTypeDataTransformer();
}
}

0 comments on commit fdc1446

Please sign in to comment.