EnumExtensionTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 6
dl 0
loc 79
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A testEnumLabel() 0 29 1
A testEnumChoices() 0 16 1
A createEnvironment() 0 8 1
A createExtension() 0 4 1
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')
0 ignored issues
show
Bug introduced by
The method get does only exist in Yokai\EnumBundle\EnumRegistry, 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...
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')
0 ignored issues
show
Bug introduced by
The method get does only exist in Yokai\EnumBundle\EnumRegistry, 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...
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());
0 ignored issues
show
Bug introduced by
The method reveal does only exist in Prophecy\Prophecy\ObjectProphecy, but not in Yokai\EnumBundle\EnumRegistry.

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...
93
    }
94
}
95