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\Twig\Extension\EnumConstantExtension; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* EnumConstantExtensionTest |
18
|
|
|
* |
19
|
|
|
* @author Artem Genvald <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class EnumConstantExtensionTest extends \PHPUnit_Framework_TestCase |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var EnumConstantExtension $enumConstantExtension EnumConstantExtension |
25
|
|
|
*/ |
26
|
|
|
private $enumConstantExtension; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
|
|
public function setUp() |
32
|
|
|
{ |
33
|
|
|
$this->enumConstantExtension = new EnumConstantExtension([ |
34
|
|
|
'BasketballPositionType' => [ |
35
|
|
|
'class' => 'Fresh\DoctrineEnumBundle\Tests\Fixtures\DBAL\Types\BasketballPositionType', |
36
|
|
|
], |
37
|
|
|
'MapLocationType' => [ |
38
|
|
|
'class' => 'Fresh\DoctrineEnumBundle\Tests\Fixtures\DBAL\Types\MapLocationType', |
39
|
|
|
], |
40
|
|
|
]); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Test method `getName` |
45
|
|
|
*/ |
46
|
|
|
public function testGetName() |
47
|
|
|
{ |
48
|
|
|
$this->assertEquals('ENUM Constant', $this->enumConstantExtension->getName()); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Test method `getFilters` |
53
|
|
|
*/ |
54
|
|
|
public function testGetFilters() |
55
|
|
|
{ |
56
|
|
|
$this->assertEquals( |
57
|
|
|
[new \Twig_SimpleFilter('enum_constant', [$this->enumConstantExtension, 'getEnumConstant'])], |
58
|
|
|
$this->enumConstantExtension->getFilters() |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Test that method `getEnumConstant` returns expected value of ENUM constant |
64
|
|
|
* |
65
|
|
|
* @param string $expectedValueOfConstant Expected readable value |
66
|
|
|
* @param string $enumConstant ENUM constant |
67
|
|
|
* @param string $enumType ENUM type |
68
|
|
|
* |
69
|
|
|
* @dataProvider dataProviderForGetReadableEnumValueTest |
70
|
|
|
*/ |
71
|
|
|
public function testGetEnumConstant($expectedValueOfConstant, $enumConstant, $enumType) |
72
|
|
|
{ |
73
|
|
|
$this->assertEquals( |
74
|
|
|
$expectedValueOfConstant, |
75
|
|
|
$this->enumConstantExtension->getEnumConstant($enumConstant, $enumType) |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Data provider for method `testGetEnumValue` |
81
|
|
|
* |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
|
|
public function dataProviderForGetReadableEnumValueTest() |
85
|
|
|
{ |
86
|
|
|
return [ |
87
|
|
|
['PG', 'POINT_GUARD', 'BasketballPositionType'], |
88
|
|
|
['PG', 'POINT_GUARD', null], |
89
|
|
|
['C', 'CENTER', 'BasketballPositionType'], |
90
|
|
|
['C', 'CENTER', 'MapLocationType'], |
91
|
|
|
]; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Test that using ENUM constant extension for ENUM type that is not registered throws exception |
96
|
|
|
* |
97
|
|
|
* @expectedException \Fresh\DoctrineEnumBundle\Exception\EnumTypeIsNotRegisteredException |
98
|
|
|
*/ |
99
|
|
|
public function testEnumTypeIsNotRegisteredException() |
100
|
|
|
{ |
101
|
|
|
$this->enumConstantExtension->getEnumConstant('Pitcher', 'BaseballPositionType'); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Test that using ENUM constant that is found in few registered ENUM types throws exception |
106
|
|
|
* |
107
|
|
|
* @expectedException \Fresh\DoctrineEnumBundle\Exception\ConstantIsFoundInFewRegisteredEnumTypesException |
108
|
|
|
*/ |
109
|
|
|
public function testConstantIsFoundInFewRegisteredEnumTypesException() |
110
|
|
|
{ |
111
|
|
|
$this->enumConstantExtension->getEnumConstant('CENTER'); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Test that using ENUM constant that is not found in any registered ENUM type throws exception |
116
|
|
|
* |
117
|
|
|
* @expectedException \Fresh\DoctrineEnumBundle\Exception\ConstantIsNotFoundInAnyRegisteredEnumTypeException |
118
|
|
|
*/ |
119
|
|
|
public function testConstantIsNotFoundInAnyRegisteredEnumTypeException() |
120
|
|
|
{ |
121
|
|
|
$this->enumConstantExtension->getEnumConstant('Pitcher'); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Test that using ENUM constant extension without any registered ENUM type throws exception |
126
|
|
|
* |
127
|
|
|
* @expectedException \Fresh\DoctrineEnumBundle\Exception\NoRegisteredEnumTypesException |
128
|
|
|
*/ |
129
|
|
|
public function testNoRegisteredEnumTypesException() |
130
|
|
|
{ |
131
|
|
|
// Create EnumConstantExtension without any registered ENUM type |
132
|
|
|
$extension = new EnumConstantExtension([]); |
133
|
|
|
$extension->getEnumConstant(BasketballPositionType::POINT_GUARD, 'BasketballPositionType'); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|