|
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' => ['class' => 'Fresh\DoctrineEnumBundle\Tests\Fixtures\DBAL\Types\BasketballPositionType'], |
|
35
|
|
|
'MapLocationType' => ['class' => 'Fresh\DoctrineEnumBundle\Tests\Fixtures\DBAL\Types\MapLocationType'], |
|
36
|
|
|
]); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function testGetName() |
|
40
|
|
|
{ |
|
41
|
|
|
$this->assertEquals('ENUM Constant', $this->enumConstantExtension->getName()); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function testGetFilters() |
|
45
|
|
|
{ |
|
46
|
|
|
$this->assertEquals( |
|
47
|
|
|
[new \Twig_SimpleFilter('enum_constant', [$this->enumConstantExtension, 'getEnumConstant'])], |
|
48
|
|
|
$this->enumConstantExtension->getFilters() |
|
49
|
|
|
); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @dataProvider dataProviderForGetReadableEnumValueTest |
|
54
|
|
|
*/ |
|
55
|
|
|
public function testGetEnumConstant($expectedValueOfConstant, $enumConstant, $enumType) |
|
56
|
|
|
{ |
|
57
|
|
|
$this->assertEquals( |
|
58
|
|
|
$expectedValueOfConstant, |
|
59
|
|
|
$this->enumConstantExtension->getEnumConstant($enumConstant, $enumType) |
|
60
|
|
|
); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function dataProviderForGetReadableEnumValueTest() |
|
64
|
|
|
{ |
|
65
|
|
|
return [ |
|
66
|
|
|
['PG', 'POINT_GUARD', 'BasketballPositionType'], |
|
67
|
|
|
['PG', 'POINT_GUARD', null], |
|
68
|
|
|
['C', 'CENTER', 'BasketballPositionType'], |
|
69
|
|
|
['C', 'CENTER', 'MapLocationType'], |
|
70
|
|
|
]; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @expectedException \Fresh\DoctrineEnumBundle\Exception\EnumTypeIsNotRegisteredException |
|
75
|
|
|
*/ |
|
76
|
|
|
public function testEnumTypeIsNotRegisteredException() |
|
77
|
|
|
{ |
|
78
|
|
|
$this->enumConstantExtension->getEnumConstant('Pitcher', 'BaseballPositionType'); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @expectedException \Fresh\DoctrineEnumBundle\Exception\ConstantIsFoundInFewRegisteredEnumTypesException |
|
83
|
|
|
*/ |
|
84
|
|
|
public function testConstantIsFoundInFewRegisteredEnumTypesException() |
|
85
|
|
|
{ |
|
86
|
|
|
$this->enumConstantExtension->getEnumConstant('CENTER'); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @expectedException \Fresh\DoctrineEnumBundle\Exception\ConstantIsNotFoundInAnyRegisteredEnumTypeException |
|
91
|
|
|
*/ |
|
92
|
|
|
public function testConstantIsNotFoundInAnyRegisteredEnumTypeException() |
|
93
|
|
|
{ |
|
94
|
|
|
$this->enumConstantExtension->getEnumConstant('Pitcher'); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @expectedException \Fresh\DoctrineEnumBundle\Exception\NoRegisteredEnumTypesException |
|
99
|
|
|
*/ |
|
100
|
|
|
public function testNoRegisteredEnumTypesException() |
|
101
|
|
|
{ |
|
102
|
|
|
// Create EnumConstantExtension without any registered ENUM type |
|
103
|
|
|
$extension = new EnumConstantExtension([]); |
|
104
|
|
|
$extension->getEnumConstant(BasketballPositionType::POINT_GUARD, 'BasketballPositionType'); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|