-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract the RemoveUnnecessaryPaymentDetailsFieldsListener from the Tp…
…ayPaymentDetailsType
- Loading branch information
1 parent
fdc1446
commit 8b26e3a
Showing
5 changed files
with
102 additions
and
20 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
25 changes: 25 additions & 0 deletions
25
src/Form/EventListener/RemoveUnnecessaryPaymentDetailsFieldsListener.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,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'); | ||
} | ||
} | ||
} |
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
54 changes: 54 additions & 0 deletions
54
tests/Unit/Form/EventListener/RemoveUnnecessaryPaymentDetailsFieldsListenerTest.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,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(); | ||
} | ||
} |