Completed
Push — master ( db5e85...36b1aa )
by Artem
02:41
created

ReadableEnumValueExtensionTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 10
Bugs 4 Features 4
Metric Value
wmc 9
c 10
b 4
f 4
lcom 1
cbo 3
dl 0
loc 96
rs 10
1
<?php
2
/*
3
 * This file is part of the FreshDoctrineEnumBundle
4
 *
5
 * (c) Artem Genvald <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Fresh\DoctrineEnumBundle\Tests\Twig\Extension;
12
13
use Fresh\DoctrineEnumBundle\Tests\Fixtures\DBAL\Types\BasketballPositionType;
14
use Fresh\DoctrineEnumBundle\Tests\Fixtures\DBAL\Types\MapLocationType;
15
use Fresh\DoctrineEnumBundle\Twig\Extension\ReadableEnumValueExtension;
16
17
/**
18
 * ReadableEnumValueExtensionTest.
19
 *
20
 * @author Artem Genvald <[email protected]>
21
 */
22
class ReadableEnumValueExtensionTest extends \PHPUnit_Framework_TestCase
23
{
24
    /**
25
     * @var ReadableEnumValueExtension $readableEnumValueExtension ReadableEnumValueExtension
26
     */
27
    private $readableEnumValueExtension;
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function setUp()
33
    {
34
        $this->readableEnumValueExtension = new ReadableEnumValueExtension([
35
            'BasketballPositionType' => ['class' => 'Fresh\DoctrineEnumBundle\Tests\Fixtures\DBAL\Types\BasketballPositionType'],
36
            'MapLocationType'        => ['class' => 'Fresh\DoctrineEnumBundle\Tests\Fixtures\DBAL\Types\MapLocationType'],
37
        ]);
38
    }
39
40
    public function testGetName()
41
    {
42
        $this->assertEquals('Readable ENUM Value', $this->readableEnumValueExtension->getName());
43
    }
44
45
    public function testGetFilters()
46
    {
47
        $this->assertEquals(
48
            [
49
                new \Twig_SimpleFilter(
50
                    'readable_enum',
51
                    [$this->readableEnumValueExtension, 'getReadableEnumValue']
52
                ),
53
                new \Twig_SimpleFilter(
54
                    'readable',
55
                    [$this->readableEnumValueExtension, 'getReadableEnumValue'],
56
                    ['deprecated' => true, 'alternative' => 'readable_enum']
57
                ),
58
            ],
59
            $this->readableEnumValueExtension->getFilters()
60
        );
61
    }
62
63
    /**
64
     * @dataProvider dataProviderForGetReadableEnumValueTest
65
     */
66
    public function testGetReadableEnumValue($expectedReadableValue, $enumValue, $enumType)
67
    {
68
        $this->assertEquals(
69
            $expectedReadableValue,
70
            $this->readableEnumValueExtension->getReadableEnumValue($enumValue, $enumType)
71
        );
72
    }
73
74
    public function dataProviderForGetReadableEnumValueTest()
75
    {
76
        return [
77
            ['Point Guard', BasketballPositionType::POINT_GUARD, 'BasketballPositionType'],
78
            ['Point Guard', BasketballPositionType::POINT_GUARD, null],
79
            ['Center', BasketballPositionType::CENTER, 'BasketballPositionType'],
80
            ['Center', MapLocationType::CENTER, 'MapLocationType'],
81
        ];
82
    }
83
84
    /**
85
     * @expectedException \Fresh\DoctrineEnumBundle\Exception\EnumTypeIsNotRegisteredException
86
     */
87
    public function testEnumTypeIsNotRegisteredException()
88
    {
89
        $this->readableEnumValueExtension->getReadableEnumValue('Pitcher', 'BaseballPositionType');
90
    }
91
92
    /**
93
     * @expectedException \Fresh\DoctrineEnumBundle\Exception\ValueIsFoundInFewRegisteredEnumTypesException
94
     */
95
    public function testValueIsFoundInFewRegisteredEnumTypesException()
96
    {
97
        $this->readableEnumValueExtension->getReadableEnumValue(BasketballPositionType::CENTER);
98
    }
99
100
    /**
101
     * @expectedException \Fresh\DoctrineEnumBundle\Exception\ValueIsNotFoundInAnyRegisteredEnumTypeException
102
     */
103
    public function testValueIsNotFoundInAnyRegisteredEnumTypeException()
104
    {
105
        $this->readableEnumValueExtension->getReadableEnumValue('Pitcher');
106
    }
107
108
    /**
109
     * @expectedException \Fresh\DoctrineEnumBundle\Exception\NoRegisteredEnumTypesException
110
     */
111
    public function testNoRegisteredEnumTypesException()
112
    {
113
        // Create ReadableEnumValueExtension without any registered ENUM type
114
        $extension = new ReadableEnumValueExtension([]);
115
        $extension->getReadableEnumValue(BasketballPositionType::POINT_GUARD, 'BasketballPositionType');
116
    }
117
}
118