|
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\ConstantExtractor; |
|
8
|
|
|
use Yokai\EnumBundle\ConstantListTranslatedEnum; |
|
9
|
|
|
use Yokai\EnumBundle\Tests\Fixtures\Vehicle; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @author Yann Eugoné <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
|
class ConstantListTranslatedEnumTest extends TestCase |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var TranslatorInterface|ObjectProphecy |
|
18
|
|
|
*/ |
|
19
|
|
|
private $translator; |
|
20
|
|
|
|
|
21
|
|
|
protected function setUp(): void |
|
22
|
|
|
{ |
|
23
|
|
|
$this->translator = $this->prophesize(TranslatorInterface::class); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function getEnum(string $pattern, string $name): ConstantListTranslatedEnum |
|
27
|
|
|
{ |
|
28
|
|
|
return new ConstantListTranslatedEnum( |
|
29
|
|
|
new ConstantExtractor(), |
|
30
|
|
|
$pattern, |
|
31
|
|
|
$this->translator->reveal(), |
|
|
|
|
|
|
32
|
|
|
$name.'.%s', |
|
33
|
|
|
$name |
|
34
|
|
|
); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function testVehicleEnums(): void |
|
38
|
|
|
{ |
|
39
|
|
|
$type = $this->getEnum(Vehicle::class.'::TYPE_*', 'vehicle.type'); |
|
40
|
|
|
$this->translator->trans('vehicle.type.bike', [], 'messages')->shouldBeCalledTimes(1)->willReturn('Moto'); |
|
|
|
|
|
|
41
|
|
|
$this->translator->trans('vehicle.type.car', [], 'messages')->shouldBeCalledTimes(1)->willReturn('Voiture'); |
|
42
|
|
|
$this->translator->trans('vehicle.type.bus', [], 'messages')->shouldBeCalledTimes(1)->willReturn('Bus'); |
|
43
|
|
|
self::assertSame('vehicle.type', $type->getName()); |
|
44
|
|
|
self::assertSame( |
|
45
|
|
|
['bike' => 'Moto', 'car' => 'Voiture', 'bus' => 'Bus'], |
|
46
|
|
|
$type->getChoices() |
|
47
|
|
|
); |
|
48
|
|
|
|
|
49
|
|
|
$engine = $this->getEnum(Vehicle::class.'::ENGINE_*', 'vehicle.engine'); |
|
50
|
|
|
$this->translator->trans('vehicle.engine.electic', [], 'messages')->shouldBeCalledTimes(1)->willReturn('Electrique'); |
|
51
|
|
|
$this->translator->trans('vehicle.engine.combustion', [], 'messages')->shouldBeCalledTimes(1)->willReturn('Combustion'); |
|
52
|
|
|
self::assertSame('vehicle.engine', $engine->getName()); |
|
53
|
|
|
self::assertSame( |
|
54
|
|
|
['electic' => 'Electrique', 'combustion' => 'Combustion'], |
|
55
|
|
|
$engine->getChoices() |
|
56
|
|
|
); |
|
57
|
|
|
|
|
58
|
|
|
$brand = $this->getEnum(Vehicle::class.'::BRAND_*', 'vehicle.brand'); |
|
59
|
|
|
$this->translator->trans('vehicle.brand.renault', [], 'messages')->shouldBeCalledTimes(1)->willReturn('Renault'); |
|
60
|
|
|
$this->translator->trans('vehicle.brand.volkswagen', [], 'messages')->shouldBeCalledTimes(1)->willReturn('Volkswagen'); |
|
61
|
|
|
$this->translator->trans('vehicle.brand.toyota', [], 'messages')->shouldBeCalledTimes(1)->willReturn('Toyota'); |
|
62
|
|
|
self::assertSame('vehicle.brand', $brand->getName()); |
|
63
|
|
|
self::assertSame( |
|
64
|
|
|
['renault' => 'Renault', 'volkswagen' => 'Volkswagen', 'toyota' => 'Toyota'], |
|
65
|
|
|
$brand->getChoices() |
|
66
|
|
|
); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
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: