Skip to content

Commit

Permalink
test symfony translator contract
Browse files Browse the repository at this point in the history
  • Loading branch information
goetas committed Mar 14, 2020
1 parent 80c71ca commit 8f3f71b
Showing 1 changed file with 55 additions and 19 deletions.
74 changes: 55 additions & 19 deletions tests/Handler/FormErrorHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\Forms;
use Symfony\Component\Translation\Translator;
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Validation;
use Symfony\Contracts\Translation\TranslatorInterface;

class FormErrorHandlerTest extends TestCase
{
Expand Down Expand Up @@ -145,7 +147,11 @@ public function testSerializeChildElements()
public function testDefaultTranslationDomain()
{
/** @var Translator|\PHPUnit_Framework_MockObject_MockObject $translator */
$translator = $this->getMockBuilder('Symfony\Component\Translation\TranslatorInterface')->getMock();

$interface = interface_exists(TranslatorInterface::class)
? TranslatorInterface::class
: LegacyTranslatorInterface::class;
$translator = $this->getMockBuilder($interface)->getMock();

$handler = new FormErrorHandler($translator);

Expand All @@ -168,18 +174,31 @@ public function testDefaultTranslationDomain()
public function testDefaultTranslationDomainWithPluralTranslation()
{
/** @var Translator|\PHPUnit_Framework_MockObject_MockObject $translator */
$translator = $this->getMockBuilder('Symfony\Component\Translation\TranslatorInterface')->getMock();
$interface = interface_exists(TranslatorInterface::class)
? TranslatorInterface::class
: LegacyTranslatorInterface::class;
$translator = $this->getMockBuilder($interface)->getMock();

$handler = new FormErrorHandler($translator);

$translator->expects($this->once())
->method('transChoice')
->with(
$this->equalTo('error!'),
$this->equalTo(0),
$this->equalTo([]),
$this->equalTo('validators')
);
if (TranslatorInterface::class === $interface) {
$translator->expects($this->once())
->method('trans')
->with(
$this->equalTo('error!'),
$this->equalTo(['%count%' => 0]),
$this->equalTo('validators')
);
} else {
$translator->expects($this->once())
->method('transChoice')
->with(
$this->equalTo('error!'),
$this->equalTo(0),
$this->equalTo([]),
$this->equalTo('validators')
);
}

$formError = $this->getMockBuilder('Symfony\Component\Form\FormError')->disableOriginalConstructor()->getMock();
$formError->expects($this->once())->method('getMessageTemplate')->willReturn('error!');
Expand All @@ -192,7 +211,10 @@ public function testDefaultTranslationDomainWithPluralTranslation()
public function testCustomTranslationDomain()
{
/** @var Translator|\PHPUnit_Framework_MockObject_MockObject $translator */
$translator = $this->getMockBuilder('Symfony\Component\Translation\TranslatorInterface')->getMock();
$interface = interface_exists(TranslatorInterface::class)
? TranslatorInterface::class
: LegacyTranslatorInterface::class;
$translator = $this->getMockBuilder($interface)->getMock();

$handler = new FormErrorHandler($translator, 'custom_domain');

Expand All @@ -216,17 +238,31 @@ public function testCustomTranslationDomainWithPluralTranslation()
{
/** @var Translator|\PHPUnit_Framework_MockObject_MockObject $translator */
$translator = $this->getMockBuilder('Symfony\Component\Translation\TranslatorInterface')->getMock();
$interface = interface_exists(TranslatorInterface::class)
? TranslatorInterface::class
: LegacyTranslatorInterface::class;
$translator = $this->getMockBuilder($interface)->getMock();

$handler = new FormErrorHandler($translator, 'custom_domain');

$translator->expects($this->once())
->method('transChoice')
->with(
$this->equalTo('error!'),
$this->equalTo(0),
$this->equalTo([]),
$this->equalTo('custom_domain')
);
if (TranslatorInterface::class === $interface) {
$translator->expects($this->once())
->method('trans')
->with(
$this->equalTo('error!'),
$this->equalTo(['%count%' => 0]),
$this->equalTo('custom_domain')
);
} else {
$translator->expects($this->once())
->method('transChoice')
->with(
$this->equalTo('error!'),
$this->equalTo(0),
$this->equalTo([]),
$this->equalTo('custom_domain')
);
}

$formError = $this->getMockBuilder('Symfony\Component\Form\FormError')->disableOriginalConstructor()->getMock();
$formError->expects($this->once())->method('getMessageTemplate')->willReturn('error!');
Expand Down

0 comments on commit 8f3f71b

Please sign in to comment.