Skip to content

Commit 8b1f111

Browse files
committed
Added some tests
1 parent 6ea2f41 commit 8b1f111

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

src/Symfony/Component/Form/Extension/Validator/ViolationMapper/ViolationMapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public function mapViolation(ConstraintViolation $violation, FormInterface $form
146146
if (false !== $label && null !== $this->translator) {
147147
$label = $this->translator->trans(
148148
$label,
149-
$scope->getConfig()->getOption('label_translation_parameters'),
149+
$scope->getConfig()->getOption('label_translation_parameters', []),
150150
$scope->getConfig()->getOption('translation_domain')
151151
);
152152
}

src/Symfony/Component/Form/Tests/Extension/Validator/ViolationMapper/ViolationMapperTest.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222
use Symfony\Component\Form\FormConfigBuilder;
2323
use Symfony\Component\Form\FormError;
2424
use Symfony\Component\Form\FormInterface;
25+
use Symfony\Component\Form\FormRenderer;
2526
use Symfony\Component\PropertyAccess\PropertyPath;
2627
use Symfony\Component\Validator\ConstraintViolation;
2728
use Symfony\Component\Validator\ConstraintViolationInterface;
29+
use Symfony\Contracts\Translation\TranslatorInterface;
2830

2931
/**
3032
* @author Bernhard Schussek <bschussek@gmail.com>
@@ -1565,4 +1567,74 @@ public function testBacktrackIfSeveralSubFormsWithSamePropertyPath()
15651567
$this->assertEquals([$this->getFormError($violation2, $grandChild2)], iterator_to_array($grandChild2->getErrors()), $grandChild2->getName().' should have an error, but has none');
15661568
$this->assertEquals([$this->getFormError($violation3, $grandChild3)], iterator_to_array($grandChild3->getErrors()), $grandChild3->getName().' should have an error, but has none');
15671569
}
1570+
1571+
public function testMessageWithLabel1()
1572+
{
1573+
$renderer = $this->getMockBuilder(FormRenderer::class)
1574+
->setMethods(null)
1575+
->disableOriginalConstructor()
1576+
->getMock()
1577+
;
1578+
$translator = $this->getMockBuilder(TranslatorInterface::class)->getMock();
1579+
$translator->expects($this->any())->method('trans')->willReturnMap([
1580+
['Name', [], null, null, 'Custom Name'],
1581+
]);
1582+
$this->mapper = new ViolationMapper($renderer, $translator);
1583+
1584+
$parent = $this->getForm('parent');
1585+
$child = $this->getForm('name', 'name');
1586+
$parent->add($child);
1587+
1588+
$parent->submit([]);
1589+
1590+
$violation = new ConstraintViolation('Message {{ label }}', null, [], null, 'data.name', null);
1591+
$this->mapper->mapViolation($violation, $parent);
1592+
1593+
$this->assertCount(1, $child->getErrors(), $child->getName().' should have an error, but has none');
1594+
1595+
$errors = iterator_to_array($child->getErrors());
1596+
if (isset($errors[0])) {
1597+
/** @var FormError $error */
1598+
$error = $errors[0];
1599+
$this->assertSame('Message Custom Name', $error->getMessage());
1600+
}
1601+
}
1602+
1603+
public function testMessageWithLabel2()
1604+
{
1605+
$translator = $this->getMockBuilder(TranslatorInterface::class)->getMock();
1606+
$translator->expects($this->any())->method('trans')->willReturnMap([
1607+
['options_label', [], null, null, 'Translated Label'],
1608+
]);
1609+
$this->mapper = new ViolationMapper(null, $translator);
1610+
1611+
$parent = $this->getForm('parent');
1612+
1613+
$config = new FormConfigBuilder('name', null, $this->dispatcher, [
1614+
'error_mapping' => [],
1615+
'label' => 'options_label',
1616+
]);
1617+
$config->setMapped(true);
1618+
$config->setInheritData(false);
1619+
$config->setPropertyPath('name');
1620+
$config->setCompound(true);
1621+
$config->setDataMapper(new PropertyPathMapper());
1622+
1623+
$child = new Form($config);
1624+
$parent->add($child);
1625+
1626+
$parent->submit([]);
1627+
1628+
$violation = new ConstraintViolation('Message {{ label }}', null, [], null, 'data.name', null);
1629+
$this->mapper->mapViolation($violation, $parent);
1630+
1631+
$this->assertCount(1, $child->getErrors(), $child->getName().' should have an error, but has none');
1632+
1633+
$errors = iterator_to_array($child->getErrors());
1634+
if (isset($errors[0])) {
1635+
/** @var FormError $error */
1636+
$error = $errors[0];
1637+
$this->assertSame('Message Translated Label', $error->getMessage());
1638+
}
1639+
}
15681640
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy