Completed
Pull Request — master (#29)
by Yann
10:30
created

EnumExtensionTest::testEnumLabel()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 19
nc 1
nop 0
1
<?php
2
3
namespace Yokai\Enum\Tests\Bridge\Symfony\Twig\Extension;
4
5
use Prophecy\Prophecy\ObjectProphecy;
6
use Yokai\Enum\Bridge\Twig\Extension\EnumExtension;
7
use Yokai\Enum\EnumInterface;
8
use Yokai\Enum\EnumRegistry;
9
10
/**
11
 * @author Yann Eugoné <[email protected]>
12
 */
13
class EnumExtensionTest extends \PHPUnit_Framework_TestCase
14
{
15
    /**
16
     * @var EnumRegistry|ObjectProphecy
17
     */
18
    private $registry;
19
20
    protected function setUp()
21
    {
22
        $this->registry = $this->prophesize(EnumRegistry::class);
23
    }
24
25
    protected function tearDown()
26
    {
27
        unset(
28
            $this->registry
29
        );
30
    }
31
32
    public function testEnumLabel()
33
    {
34
        $enum = $this->prophesize(EnumInterface::class);
35
        $enum->getChoices()
36
            ->willReturn(['foo' => 'FOO', 'bar' => 'BAR']);
37
38
        $this->registry->get('test')
0 ignored issues
show
Bug introduced by
The method get does only exist in Yokai\Enum\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...
39
            ->willReturn($enum->reveal());
40
41
        $twig = $this->createEnvironment();
42
43
        $this->assertSame(
44
            'FOO',
45
            $twig->createTemplate("{{ 'foo'|enum_label('test') }}")->render([])
46
        );
47
        $this->assertSame(
48
            'BAR',
49
            $twig->createTemplate("{{ enum_label('bar', 'test') }}")->render([])
50
        );
51
52
        $this->assertSame(
53
            'not_exist',
54
            $twig->createTemplate("{{ 'not_exist'|enum_label('test') }}")->render([])
55
        );
56
        $this->assertSame(
57
            'not_exist',
58
            $twig->createTemplate("{{ enum_label('not_exist', 'test') }}")->render([])
59
        );
60
    }
61
62
    public function testEnumChoices()
63
    {
64
        $enum = $this->prophesize(EnumInterface::class);
65
        $enum->getChoices()
66
            ->willReturn(['foo' => 'FOO', 'bar' => 'BAR']);
67
68
        $this->registry->get('test')
0 ignored issues
show
Bug introduced by
The method get does only exist in Yokai\Enum\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...
69
            ->willReturn($enum->reveal());
70
71
        $twig = $this->createEnvironment();
72
73
        $this->assertSame(
74
            'foo,FOO|bar,BAR|',
75
            $twig->createTemplate("{% for value,label in enum_choices('test') %}{{ value }},{{ label }}|{% endfor %}")->render([])
76
        );
77
    }
78
79
    /**
80
     * @return \Twig_Environment
81
     */
82
    protected function createEnvironment()
83
    {
84
        $loader = new \Twig_Loader_Array([]);
85
        $twig = new \Twig_Environment($loader, ['debug' => true, 'cache' => false, 'autoescape' => false]);
86
        $twig->addExtension($this->createExtension());
87
88
        return $twig;
89
    }
90
91
    /**
92
     * @return EnumExtension
93
     */
94
    private function createExtension()
95
    {
96
        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\Enum\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...
97
    }
98
}
99