|
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\DBAL\Types; |
|
12
|
|
|
|
|
13
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
|
14
|
|
|
use Doctrine\DBAL\Platforms\MySqlPlatform; |
|
15
|
|
|
use Doctrine\DBAL\Platforms\PostgreSqlPlatform; |
|
16
|
|
|
use Doctrine\DBAL\Platforms\SqlitePlatform; |
|
17
|
|
|
use Doctrine\DBAL\Platforms\SQLServerPlatform; |
|
18
|
|
|
use Doctrine\DBAL\Types\Type; |
|
19
|
|
|
use Fresh\DoctrineEnumBundle\DBAL\Types\AbstractEnumType; |
|
20
|
|
|
use Fresh\DoctrineEnumBundle\Tests\Fixtures\DBAL\Types\BasketballPositionType; |
|
21
|
|
|
use Fresh\DoctrineEnumBundle\Util\LegacyFormHelper; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* AbstractEnumTypeTest. |
|
25
|
|
|
* |
|
26
|
|
|
* @author Artem Genvald <[email protected]> |
|
27
|
|
|
* @author Ben Davies <[email protected]> |
|
28
|
|
|
* |
|
29
|
|
|
* @coversDefaultClass \Fresh\DoctrineEnumBundle\DBAL\Types\AbstractEnumType |
|
30
|
|
|
*/ |
|
31
|
|
|
class AbstractEnumTypeTest extends \PHPUnit_Framework_TestCase |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @var AbstractEnumType $type Abstract EnumType |
|
35
|
|
|
*/ |
|
36
|
|
|
private $type; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* {@inheritdoc} |
|
40
|
|
|
*/ |
|
41
|
|
|
public static function setUpBeforeClass() |
|
42
|
|
|
{ |
|
43
|
|
|
Type::addType( |
|
44
|
|
|
'BasketballPositionType', |
|
45
|
|
|
'\Fresh\DoctrineEnumBundle\Tests\Fixtures\DBAL\Types\BasketballPositionType' |
|
46
|
|
|
); |
|
47
|
|
|
Type::addType('StubType', '\Fresh\DoctrineEnumBundle\Tests\Fixtures\DBAL\Types\StubType'); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* {@inheritdoc} |
|
52
|
|
|
*/ |
|
53
|
|
|
public function setUp() |
|
54
|
|
|
{ |
|
55
|
|
|
$this->type = Type::getType('BasketballPositionType'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @dataProvider platformProvider |
|
60
|
|
|
*/ |
|
61
|
|
|
public function testGetSqlDeclaration(array $fieldDeclaration, AbstractPlatform $platform, $expected) |
|
62
|
|
|
{ |
|
63
|
|
|
$this->assertEquals($expected, $this->type->getSqlDeclaration($fieldDeclaration, $platform)); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function platformProvider() |
|
67
|
|
|
{ |
|
68
|
|
|
return [ |
|
69
|
|
|
[ |
|
70
|
|
|
['name' => 'position'], |
|
71
|
|
|
new MySqlPlatform(), |
|
72
|
|
|
"ENUM('PG', 'SG', 'SF', 'PF', 'C')", |
|
73
|
|
|
], |
|
74
|
|
|
[ |
|
75
|
|
|
['name' => 'position'], |
|
76
|
|
|
new SqlitePlatform(), |
|
77
|
|
|
"TEXT CHECK(position IN ('PG', 'SG', 'SF', 'PF', 'C'))", |
|
78
|
|
|
], |
|
79
|
|
|
[ |
|
80
|
|
|
['name' => 'position'], |
|
81
|
|
|
new PostgreSqlPlatform(), |
|
82
|
|
|
"VARCHAR(255) CHECK(position IN ('PG', 'SG', 'SF', 'PF', 'C'))", |
|
83
|
|
|
], |
|
84
|
|
|
[ |
|
85
|
|
|
['name' => 'position'], |
|
86
|
|
|
new SQLServerPlatform(), |
|
87
|
|
|
"VARCHAR(255) CHECK(position IN ('PG', 'SG', 'SF', 'PF', 'C'))", |
|
88
|
|
|
], |
|
89
|
|
|
]; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function testGetName() |
|
93
|
|
|
{ |
|
94
|
|
|
$this->assertEquals('BasketballPositionType', $this->type->getName()); |
|
95
|
|
|
$this->assertEquals('StubType', Type::getType('StubType')->getName()); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function testRequiresSQLCommentHint() |
|
99
|
|
|
{ |
|
100
|
|
|
$this->assertTrue($this->type->requiresSQLCommentHint(new MySqlPlatform())); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function testConvertToDatabaseValue() |
|
104
|
|
|
{ |
|
105
|
|
|
$this->assertNull($this->type->convertToDatabaseValue(null, new MySqlPlatform())); |
|
106
|
|
|
$this->assertEquals('SF', $this->type->convertToDatabaseValue('SF', new MySqlPlatform())); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @expectedException \InvalidArgumentException |
|
111
|
|
|
*/ |
|
112
|
|
|
public function testInvalidArgumentExceptionInConvertToDatabaseValue() |
|
113
|
|
|
{ |
|
114
|
|
|
$this->type->convertToDatabaseValue('YO', new MySqlPlatform()); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function testGetReadableValue() |
|
118
|
|
|
{ |
|
119
|
|
|
$this->assertEquals('Small Forward', $this->type->getReadableValue(BasketballPositionType::SMALL_FORWARD)); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @expectedException \InvalidArgumentException |
|
124
|
|
|
*/ |
|
125
|
|
|
public function testInvalidArgumentExceptionInGetReadableValue() |
|
126
|
|
|
{ |
|
127
|
|
|
$this->type->getReadableValue('YO'); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function testGetChoices() |
|
131
|
|
|
{ |
|
132
|
|
|
if (LegacyFormHelper::isLegacy()) { |
|
133
|
|
|
$choices = [ |
|
134
|
|
|
'PG' => 'Point Guard', |
|
135
|
|
|
'SG' => 'Shooting Guard', |
|
136
|
|
|
'SF' => 'Small Forward', |
|
137
|
|
|
'PF' => 'Power Forward', |
|
138
|
|
|
'C' => 'Center', |
|
139
|
|
|
]; |
|
140
|
|
|
} else { |
|
141
|
|
|
$choices = [ |
|
142
|
|
|
'Point Guard' => 'PG', |
|
143
|
|
|
'Shooting Guard' => 'SG', |
|
144
|
|
|
'Small Forward' => 'SF', |
|
145
|
|
|
'Power Forward' => 'PF', |
|
146
|
|
|
'Center' => 'C', |
|
147
|
|
|
]; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
$this->assertEquals($choices, $this->type->getChoices()); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|