|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Enum; |
| 4 | + |
| 5 | +use Prophecy\Prophecy\ObjectProphecy; |
| 6 | +use Symfony\Component\Translation\TranslatorInterface; |
| 7 | +use Yokai\EnumBundle\Enum\ConfigurableTranslatedEnum; |
| 8 | + |
| 9 | +/** |
| 10 | + * @author Yann Eugoné <[email protected]> |
| 11 | + */ |
| 12 | +class ConfigurableTranslatedEnumTest extends \PHPUnit_Framework_TestCase |
| 13 | +{ |
| 14 | + public function testConstructedWithInvalidPattern() |
| 15 | + { |
| 16 | + $this->expectException('Yokai\EnumBundle\Exception\InvalidTranslatePatternException'); |
| 17 | + $translator = $this->prophesize('Symfony\Component\Translation\TranslatorInterface'); |
| 18 | + new ConfigurableTranslatedEnum($translator->reveal(), 'invalid.pattern', 'invalid', ['foo', 'bar']); |
| 19 | + } |
| 20 | + |
| 21 | + public function testTranslatedChoices() |
| 22 | + { |
| 23 | + /** @var ObjectProphecy|TranslatorInterface $translator */ |
| 24 | + $translator = $this->prophesize('Symfony\Component\Translation\TranslatorInterface'); |
| 25 | + $translator->trans('choice.something.foo', [], 'messages', null)->shouldBeCalled()->willReturn('FOO translated'); |
| 26 | + $translator->trans('choice.something.bar', [], 'messages', null)->shouldBeCalled()->willReturn('BAR translated'); |
| 27 | + $type = new ConfigurableTranslatedEnum($translator->reveal(), 'choice.something.%s', 'something', ['foo', 'bar']); |
| 28 | + |
| 29 | + $expectedChoices = [ |
| 30 | + 'foo' => 'FOO translated', |
| 31 | + 'bar' => 'BAR translated', |
| 32 | + ]; |
| 33 | + $this->assertEquals($expectedChoices, $type->getChoices()); |
| 34 | + } |
| 35 | + |
| 36 | + public function testTranslatedWithDomainChoices() |
| 37 | + { |
| 38 | + /** @var ObjectProphecy|TranslatorInterface $translator */ |
| 39 | + $translator = $this->prophesize('Symfony\Component\Translation\TranslatorInterface'); |
| 40 | + $translator->trans('choice.something.foo', [], 'messages', null)->shouldNotBeCalled(); |
| 41 | + $translator->trans('choice.something.bar', [], 'messages', null)->shouldNotBeCalled(); |
| 42 | + $translator->trans('something.foo', [], 'choices', null)->shouldBeCalled()->willReturn('FOO translated'); |
| 43 | + $translator->trans('something.bar', [], 'choices', null)->shouldBeCalled()->willReturn('BAR translated'); |
| 44 | + $type = new ConfigurableTranslatedEnum($translator->reveal(), 'something.%s', 'something', ['foo', 'bar']); |
| 45 | + $type->setTransDomain('choices'); |
| 46 | + |
| 47 | + $expectedChoices = [ |
| 48 | + 'foo' => 'FOO translated', |
| 49 | + 'bar' => 'BAR translated', |
| 50 | + ]; |
| 51 | + $this->assertEquals($expectedChoices, $type->getChoices()); |
| 52 | + } |
| 53 | +} |
0 commit comments