8000 Add test case to show how behaves TransformationFailedExceptions with… · ogizanagi/symfony@4f76c21 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4f76c21

Browse files
committed
Add test case to show how behaves TransformationFailedExceptions with value objects & the Form ValidatorExtension.
1 parent 9da6746 commit 4f76c21

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

src/Symfony/Component/Form/Tests/Extension/Core/Type/FormTypeTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
namespace Symfony\Component\Form\Tests\Extension\Core\Type;
1313

14+
use Symfony\Component\Form\Extension\Core\Type\TextType;
15+
use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
16+
use Symfony\Component\Form\Forms;
1417
use Symfony\Component\Form\Tests\Fixtures\MoneyType;
1518
use Symfony\Component\Form\Tests\Fixtures\MoneyTypeConverter;
1619
use Symfony\Component\PropertyAccess\PropertyPath;
@@ -23,6 +26,7 @@
2326
use Symfony\Component\Form\FormError;
2427
use Symfony\Component\Form\Tests\Fixtures\Money;
2528
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
29+
use Symfony\Component\Validator\Validation;
2630

2731
class FormTest_AuthorWithoutRefSetter
2832
{
@@ -729,6 +733,37 @@ public function testSimpleObjectMapperOption($initialData, $submittedData, $expe
729733
$this->assertEquals($expectedData, $form->getData());
730734
}
731735

736+
public function testSimpleObjectMapperOptionTransformationFailedExceptionIsConvertedAsFormError()
737+
{
738+
$money = new Money(20.5, 'EUR');
739+
740+
$factory = Forms::createFormFactoryBuilder()
741+
->addExtensions(array(new ValidatorExtension(Validation::createValidator())))
742+
->getFormFactory()
743+
;
744+
745+
$form = $factory->create(MoneyType::class, $money);
746+
$form->submit(array('amount' => 'invalid_amount', 'currency' => 'USD'));
747+
748+
$this->assertFalse($form->isValid());
749+
$this->assertNull($form->getData());
750+
$this->assertCount(1, $form->get('amount')->getErrors());
751+
$error = $form->get('amount')->getErrors()[0];
752+
$this->assertSame('This value is not valid.', $error->getMessage());
753+
754+
$form = $factory->create(MoneyType::class, $money, array(
755+
'invalid_message' => "Money is not valid."
756+
))->add('amount', TextType::class); // replace the NumberType by a simple TextType
757+
$form->submit(array('amount' => 'invalid_amount', 'currency' => 'USD'));
758+
759+
$this->assertFalse($form->isValid());
760+
$this->assertNull($form->getData());
761+
$this->assertCount(1, $form->getErrors());
762+
$this->assertSame('Money "amount" should be numeric.', $form->getTransformationFailure()->getMessage());
763+
$error = $form->getErrors()[0];
764+
$this->assertSame('Money is not valid.', $error->getMessage());
765+
}
766+
732767
protected function getTestedType()
733768
{
734769
return 'Symfony\Component\Form\Extension\Core\Type\FormType';

src/Symfony/Component/Form/Tests/Fixtures/Money.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ class Money
2121

2222
public function __construct($amount, $currency)
2323
{
24-
$this->amount = $amount;
24+
if (!is_numeric($amount)) {
25+
throw new \InvalidArgumentException('Money "amount" should be numeric.');
26+
}
27+
28+
$this->amount = (float) $amount;
2529
$this->currency = $currency;
2630
}
2731

src/Symfony/Component/Form/Tests/Fixtures/MoneyTypeConverter.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Form\Tests\Fixtures;
1313

14+
use Symfony\Component\Form\Exception\TransformationFailedException;
1415
use Symfony\Component\Form\Extension\Core\DataMapper\FormDataToObjectConverterInterface;
1516

1617
class MoneyTypeConverter implements FormDataToObjectConverterInterface
@@ -27,6 +28,10 @@ public function convertFormDataToObject(array $data, $originalData)
2728
return;
2829
}
2930

30-
return new Money($data['amount'], $data['currency']);
31+
try {
32+
return new Money($data['amount'], $data['currency']);
33+
} catch (\InvalidArgumentException $e) {
34+
throw new TransformationFailedException($e->getMessage(), 0, $e);
35+
}
3136
}
3237
}

0 commit comments

Comments
 (0)
0