1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\DBAL\Schema; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Exception\InvalidArgumentException; |
6
|
|
|
use Doctrine\DBAL\Schema\Column; |
7
|
|
|
use Doctrine\DBAL\Types\Type; |
8
|
|
|
|
9
|
|
|
class ColumnTest extends \PHPUnit\Framework\TestCase |
10
|
|
|
{ |
11
|
|
|
public function testGet() |
12
|
|
|
{ |
13
|
|
|
$column = $this->createColumn(); |
14
|
|
|
|
15
|
|
|
self::assertEquals("foo", $column->getName()); |
16
|
|
|
self::assertSame(Type::getType('string'), $column->getType()); |
17
|
|
|
|
18
|
|
|
self::assertEquals(200, $column->getLength()); |
19
|
|
|
self::assertEquals(5, $column->getPrecision()); |
20
|
|
|
self::assertEquals(2, $column->getScale()); |
21
|
|
|
self::assertTrue($column->getUnsigned()); |
22
|
|
|
self::assertFalse($column->getNotNull()); |
23
|
|
|
self::assertTrue($column->getFixed()); |
24
|
|
|
self::assertEquals("baz", $column->getDefault()); |
25
|
|
|
|
26
|
|
|
self::assertEquals(array('foo' => 'bar'), $column->getPlatformOptions()); |
27
|
|
|
self::assertTrue($column->hasPlatformOption('foo')); |
28
|
|
|
self::assertEquals('bar', $column->getPlatformOption('foo')); |
29
|
|
|
self::assertFalse($column->hasPlatformOption('bar')); |
30
|
|
|
|
31
|
|
|
self::assertEquals(array('bar' => 'baz'), $column->getCustomSchemaOptions()); |
32
|
|
|
self::assertTrue($column->hasCustomSchemaOption('bar')); |
33
|
|
|
self::assertEquals('baz', $column->getCustomSchemaOption('bar')); |
34
|
|
|
self::assertFalse($column->hasCustomSchemaOption('foo')); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testToArray() |
38
|
|
|
{ |
39
|
|
|
$expected = array( |
40
|
|
|
'name' => 'foo', |
41
|
|
|
'type' => Type::getType('string'), |
42
|
|
|
'default' => 'baz', |
43
|
|
|
'notnull' => false, |
44
|
|
|
'length' => 200, |
45
|
|
|
'precision' => 5, |
46
|
|
|
'scale' => 2, |
47
|
|
|
'fixed' => true, |
48
|
|
|
'unsigned' => true, |
49
|
|
|
'autoincrement' => false, |
50
|
|
|
'columnDefinition' => null, |
51
|
|
|
'comment' => null, |
52
|
|
|
'foo' => 'bar', |
53
|
|
|
'bar' => 'baz' |
54
|
|
|
); |
55
|
|
|
|
56
|
|
|
self::assertEquals($expected, $this->createColumn()->toArray()); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @group legacy |
61
|
|
|
* @expectedDeprecation The "unknown_option" column option is not supported, setting it is deprecated and will cause an error in Doctrine 3.0 |
62
|
|
|
*/ |
63
|
|
|
public function testSettingUnknownOptionIsStillSupported() : void |
64
|
|
|
{ |
65
|
|
|
new Column('foo', $this->createMock(Type::class), ['unknown_option' => 'bar']); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @group legacy |
70
|
|
|
* @expectedDeprecation The "unknown_option" column option is not supported, setting it is deprecated and will cause an error in Doctrine 3.0 |
71
|
|
|
*/ |
72
|
|
|
public function testOptionsShouldNotBeIgnored() : void |
73
|
|
|
{ |
74
|
|
|
$col1 = new Column('bar', Type::getType(Type::INTEGER), ['unknown_option' => 'bar', 'notnull' => true]); |
75
|
|
|
self::assertTrue($col1->getNotnull()); |
76
|
|
|
|
77
|
|
|
$col2 = new Column('bar', Type::getType(Type::INTEGER), ['unknown_option' => 'bar', 'notnull' => false]); |
78
|
|
|
self::assertFalse($col2->getNotnull()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return Column |
83
|
|
|
*/ |
84
|
|
|
public function createColumn() |
85
|
|
|
{ |
86
|
|
|
$options = array( |
87
|
|
|
'length' => 200, |
88
|
|
|
'precision' => 5, |
89
|
|
|
'scale' => 2, |
90
|
|
|
'unsigned' => true, |
91
|
|
|
'notnull' => false, |
92
|
|
|
'fixed' => true, |
93
|
|
|
'default' => 'baz', |
94
|
|
|
'platformOptions' => array('foo' => 'bar'), |
95
|
|
|
'customSchemaOptions' => array('bar' => 'baz'), |
96
|
|
|
); |
97
|
|
|
|
98
|
|
|
$string = Type::getType('string'); |
99
|
|
|
return new Column("foo", $string, $options); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @group DBAL-64 |
104
|
|
|
* @group DBAL-830 |
105
|
|
|
*/ |
106
|
|
|
public function testQuotedColumnName() |
107
|
|
|
{ |
108
|
|
|
$string = Type::getType('string'); |
109
|
|
|
$column = new Column("`bar`", $string, array()); |
110
|
|
|
|
111
|
|
|
$mysqlPlatform = new \Doctrine\DBAL\Platforms\MySqlPlatform(); |
112
|
|
|
$sqlitePlatform = new \Doctrine\DBAL\Platforms\SqlitePlatform(); |
113
|
|
|
|
114
|
|
|
self::assertEquals('bar', $column->getName()); |
115
|
|
|
self::assertEquals('`bar`', $column->getQuotedName($mysqlPlatform)); |
116
|
|
|
self::assertEquals('"bar"', $column->getQuotedName($sqlitePlatform)); |
117
|
|
|
|
118
|
|
|
$column = new Column("[bar]", $string); |
119
|
|
|
|
120
|
|
|
$sqlServerPlatform = new \Doctrine\DBAL\Platforms\SQLServerPlatform(); |
121
|
|
|
|
122
|
|
|
self::assertEquals('bar', $column->getName()); |
123
|
|
|
self::assertEquals('[bar]', $column->getQuotedName($sqlServerPlatform)); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @dataProvider getIsQuoted |
128
|
|
|
* @group DBAL-830 |
129
|
|
|
*/ |
130
|
|
|
public function testIsQuoted($columnName, $isQuoted) |
131
|
|
|
{ |
132
|
|
|
$type = Type::getType('string'); |
133
|
|
|
$column = new Column($columnName, $type); |
134
|
|
|
|
135
|
|
|
self::assertSame($isQuoted, $column->isQuoted()); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function getIsQuoted() |
139
|
|
|
{ |
140
|
|
|
return array( |
141
|
|
|
array('bar', false), |
142
|
|
|
array('`bar`', true), |
143
|
|
|
array('"bar"', true), |
144
|
|
|
array('[bar]', true), |
145
|
|
|
); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @group DBAL-42 |
150
|
|
|
*/ |
151
|
|
|
public function testColumnComment() |
152
|
|
|
{ |
153
|
|
|
$column = new Column("bar", Type::getType('string')); |
154
|
|
|
self::assertNull($column->getComment()); |
155
|
|
|
|
156
|
|
|
$column->setComment("foo"); |
157
|
|
|
self::assertEquals("foo", $column->getComment()); |
158
|
|
|
|
159
|
|
|
$columnArray = $column->toArray(); |
160
|
|
|
self::assertArrayHasKey('comment', $columnArray); |
161
|
|
|
self::assertEquals('foo', $columnArray['comment']); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|