|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Yokai\EnumBundle\Tests\Twig\Extension; |
|
4
|
|
|
|
|
5
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
|
6
|
|
|
use Twig\Environment; |
|
7
|
|
|
use Twig\Loader\ArrayLoader; |
|
8
|
|
|
use Yokai\EnumBundle\EnumInterface; |
|
9
|
|
|
use Yokai\EnumBundle\EnumRegistry; |
|
10
|
|
|
use Yokai\EnumBundle\Tests\TestCase; |
|
11
|
|
|
use Yokai\EnumBundle\Twig\Extension\EnumExtension; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @author Yann Eugoné <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
class EnumExtensionTest extends TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var EnumRegistry|ObjectProphecy |
|
20
|
|
|
*/ |
|
21
|
|
|
private $registry; |
|
22
|
|
|
|
|
23
|
|
|
protected function setUp(): void |
|
24
|
|
|
{ |
|
25
|
|
|
$this->registry = $this->prophesize(EnumRegistry::class); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function testEnumLabel(): void |
|
29
|
|
|
{ |
|
30
|
|
|
$enum = $this->prophesize(EnumInterface::class); |
|
31
|
|
|
$enum->getChoices() |
|
32
|
|
|
->willReturn(['foo' => 'FOO', 'bar' => 'BAR']); |
|
33
|
|
|
|
|
34
|
|
|
$this->registry->get('test') |
|
|
|
|
|
|
35
|
|
|
->willReturn($enum->reveal()); |
|
36
|
|
|
|
|
37
|
|
|
$twig = $this->createEnvironment(); |
|
38
|
|
|
|
|
39
|
|
|
$this->assertSame( |
|
40
|
|
|
'FOO', |
|
41
|
|
|
$twig->createTemplate("{{ 'foo'|enum_label('test') }}")->render([]) |
|
42
|
|
|
); |
|
43
|
|
|
$this->assertSame( |
|
44
|
|
|
'BAR', |
|
45
|
|
|
$twig->createTemplate("{{ enum_label('bar', 'test') }}")->render([]) |
|
46
|
|
|
); |
|
47
|
|
|
|
|
48
|
|
|
$this->assertSame( |
|
49
|
|
|
'not_exist', |
|
50
|
|
|
$twig->createTemplate("{{ 'not_exist'|enum_label('test') }}")->render([]) |
|
51
|
|
|
); |
|
52
|
|
|
$this->assertSame( |
|
53
|
|
|
'not_exist', |
|
54
|
|
|
$twig->createTemplate("{{ enum_label('not_exist', 'test') }}")->render([]) |
|
55
|
|
|
); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function testEnumChoices(): void |
|
59
|
|
|
{ |
|
60
|
|
|
$enum = $this->prophesize(EnumInterface::class); |
|
61
|
|
|
$enum->getChoices() |
|
62
|
|
|
->willReturn(['foo' => 'FOO', 'bar' => 'BAR']); |
|
63
|
|
|
|
|
64
|
|
|
$this->registry->get('test') |
|
|
|
|
|
|
65
|
|
|
->willReturn($enum->reveal()); |
|
66
|
|
|
|
|
67
|
|
|
$twig = $this->createEnvironment(); |
|
68
|
|
|
|
|
69
|
|
|
$this->assertSame( |
|
70
|
|
|
'foo,FOO|bar,BAR|', |
|
71
|
|
|
$twig->createTemplate("{% for value,label in enum_choices('test') %}{{ value }},{{ label }}|{% endfor %}")->render([]) |
|
72
|
|
|
); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return Environment |
|
77
|
|
|
*/ |
|
78
|
|
|
protected function createEnvironment(): Environment |
|
79
|
|
|
{ |
|
80
|
|
|
$loader = new ArrayLoader([]); |
|
81
|
|
|
$twig = new Environment($loader, ['debug' => true, 'cache' => false, 'autoescape' => false]); |
|
82
|
|
|
$twig->addExtension($this->createExtension()); |
|
83
|
|
|
|
|
84
|
|
|
return $twig; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @return EnumExtension |
|
89
|
|
|
*/ |
|
90
|
|
|
private function createExtension(): EnumExtension |
|
91
|
|
|
{ |
|
92
|
|
|
return new EnumExtension($this->registry->reveal()); |
|
|
|
|
|
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
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: