|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Yokai\EnumBundle\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
|
6
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
|
7
|
|
|
use Yokai\EnumBundle\ConfigurableTranslatedEnum; |
|
8
|
|
|
use Yokai\EnumBundle\Exception\InvalidTranslatePatternException; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @author Yann Eugoné <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
class ConfigurableTranslatedEnumTest extends TestCase |
|
14
|
|
|
{ |
|
15
|
|
|
public function testConstructedWithInvalidPattern(): void |
|
16
|
|
|
{ |
|
17
|
|
|
$this->expectException(InvalidTranslatePatternException::class); |
|
18
|
|
|
$translator = $this->prophesize(TranslatorInterface::class); |
|
19
|
|
|
new ConfigurableTranslatedEnum($translator->reveal(), 'invalid.pattern', 'invalid', ['foo', 'bar']); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function testTranslatedChoices(): void |
|
23
|
|
|
{ |
|
24
|
|
|
/** @var ObjectProphecy|TranslatorInterface $translator */ |
|
25
|
|
|
$translator = $this->prophesize(TranslatorInterface::class); |
|
26
|
|
|
$translator->trans('choice.something.foo', [], 'messages', null)->shouldBeCalled()->willReturn('FOO translated'); |
|
|
|
|
|
|
27
|
|
|
$translator->trans('choice.something.bar', [], 'messages', null)->shouldBeCalled()->willReturn('BAR translated'); |
|
28
|
|
|
$type = new ConfigurableTranslatedEnum($translator->reveal(), 'choice.something.%s', 'something', ['foo', 'bar']); |
|
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
$expectedChoices = [ |
|
31
|
|
|
'foo' => 'FOO translated', |
|
32
|
|
|
'bar' => 'BAR translated', |
|
33
|
|
|
]; |
|
34
|
|
|
$this->assertEquals($expectedChoices, $type->getChoices()); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function testTranslatedWithDomainChoices(): void |
|
38
|
|
|
{ |
|
39
|
|
|
/** @var ObjectProphecy|TranslatorInterface $translator */ |
|
40
|
|
|
$translator = $this->prophesize(TranslatorInterface::class); |
|
41
|
|
|
$translator->trans('choice.something.foo', [], 'messages', null)->shouldNotBeCalled(); |
|
|
|
|
|
|
42
|
|
|
$translator->trans('choice.something.bar', [], 'messages', null)->shouldNotBeCalled(); |
|
43
|
|
|
$translator->trans('something.foo', [], 'choices', null)->shouldBeCalled()->willReturn('FOO translated'); |
|
44
|
|
|
$translator->trans('something.bar', [], 'choices', null)->shouldBeCalled()->willReturn('BAR translated'); |
|
45
|
|
|
$type = new ConfigurableTranslatedEnum($translator->reveal(), 'something.%s', 'something', ['foo', 'bar']); |
|
|
|
|
|
|
46
|
|
|
$type->setTransDomain('choices'); |
|
47
|
|
|
|
|
48
|
|
|
$expectedChoices = [ |
|
49
|
|
|
'foo' => 'FOO translated', |
|
50
|
|
|
'bar' => 'BAR translated', |
|
51
|
|
|
]; |
|
52
|
|
|
$this->assertEquals($expectedChoices, $type->getChoices()); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: