Skip to content

Commit c8f94d4

Browse files
committed
Add a test to check that the translation domain is passed on
1 parent e152d96 commit c8f94d4

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/Symfony/Component/Validator/Test/ConstraintValidatorTestCase.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use PHPUnit\Framework\Constraint\IsNull;
1818
use PHPUnit\Framework\Constraint\LogicalOr;
1919
use PHPUnit\Framework\ExpectationFailedException;
20+
use PHPUnit\Framework\MockObject\MockObject;
2021
use PHPUnit\Framework\TestCase;
2122
use Symfony\Component\Validator\Constraint;
2223
use Symfony\Component\Validator\Constraints\GroupSequence;
@@ -61,6 +62,8 @@ abstract class ConstraintValidatorTestCase extends TestCase
6162
protected Constraint $constraint;
6263
protected ?string $defaultTimezone = null;
6364

65+
private TranslatorInterface&MockObject $translator;
66+
6467
private string $defaultLocale;
6568
private array $expectedViolations;
6669
private int $call;
@@ -122,14 +125,14 @@ protected function restoreDefaultTimezone()
122125

123126
protected function createContext()
124127
{
125-
$translator = $this->createMock(TranslatorInterface::class);
126-
$translator->expects($this->any())->method('trans')->willReturnArgument(0);
128+
$this->translator = $this->createMock(TranslatorInterface::class);
129+
$this->translator->expects($this->any())->method('trans')->willReturnArgument(0);
127130
$validator = $this->createMock(ValidatorInterface::class);
128131
$validator->expects($this->any())
129132
->method('validate')
130133
->willReturnCallback(fn () => $this->expectedViolations[$this->call++] ?? new ConstraintViolationList());
131134

132-
$context = new ExecutionContext($validator, $this->root, $translator);
135+
$context = new ExecutionContext($validator, $this->root, $this->translator);
133136
$context->setGroup($this->group);
134137
$context->setNode($this->value, $this->object, $this->metadata, $this->propertyPath);
135138
$context->setConstraint($this->constraint);
@@ -277,6 +280,14 @@ protected function expectViolationsAt(int $i, mixed $value, Constraint $constrai
277280
return $context->getViolations();
278281
}
279282

283+
protected function expectTranslationDomain(string $translationDomain)
284+
{
285+
$this->translator
286+
->expects($this->atLeastOnce())
287+
->method('trans')
288+
->with($this->anything(), $this->anything(), $translationDomain, $this->anything());
289+
}
290+
280291
protected function assertNoViolation()
281292
{
282293
$this->assertSame(0, $violationsCount = \count($this->context->getViolations()), \sprintf('0 violation expected. Got %u.', $violationsCount));
@@ -301,6 +312,7 @@ final class ConstraintViolationAssertion
301312
private ?int $plural = null;
302313
private ?string $code = null;
303314
private mixed $cause = null;
315+
private ?string $translationDomain = null;
304316

305317
/**
306318
* @param ConstraintViolationAssertion[] $assertions

src/Symfony/Component/Validator/Tests/Constraints/NotBlankValidatorTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ public function testNullIsInvalid()
5555
->assertRaised();
5656
}
5757

58+
public function testTranslationDomain()
59+
{
60+
$constraint = new NotBlank(translationDomain: 'my_domain');
61+
62+
$this->expectTranslationDomain('my_domain');
63+
64+
$this->validator->validate(null, $constraint);
65+
}
66+
5867
public function testBlankIsInvalid()
5968
{
6069
$constraint = new NotBlank(message: 'myMessage');

0 commit comments

Comments
 (0)