ConstantListTranslatedEnumTest::getEnum()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 2
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(),
0 ignored issues
show
Bug introduced by
The method reveal does only exist in Prophecy\Prophecy\ObjectProphecy, but not in Symfony\Contracts\Translation\TranslatorInterface.

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:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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');
0 ignored issues
show
Bug introduced by
The method trans does only exist in Symfony\Contracts\Translation\TranslatorInterface, but not in Prophecy\Prophecy\ObjectProphecy.

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:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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