ConfigurableTranslatedEnumTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 3
dl 0
loc 42
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testConstructedWithInvalidPattern() 0 6 1
A testTranslatedChoices() 0 14 1
A testTranslatedWithDomainChoices() 0 17 1
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');
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...
27
        $translator->trans('choice.something.bar', [], 'messages', null)->shouldBeCalled()->willReturn('BAR translated');
28
        $type = new ConfigurableTranslatedEnum($translator->reveal(), 'choice.something.%s', 'something', ['foo', 'bar']);
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...
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();
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...
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']);
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...
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