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" option is not supported, setting it is deprecated and will cause an error in 3.0 |
62
|
|
|
*/ |
63
|
|
|
public function testSettingUnknownOptionIsStillSupported() : void |
64
|
|
|
{ |
65
|
|
|
new Column('foo', $this->createMock(Type::class), ['unknown_option' => 'bar']); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return Column |
70
|
|
|
*/ |
71
|
|
|
public function createColumn() |
72
|
|
|
{ |
73
|
|
|
$options = array( |
74
|
|
|
'length' => 200, |
75
|
|
|
'precision' => 5, |
76
|
|
|
'scale' => 2, |
77
|
|
|
'unsigned' => true, |
78
|
|
|
'notnull' => false, |
79
|
|
|
'fixed' => true, |
80
|
|
|
'default' => 'baz', |
81
|
|
|
'platformOptions' => array('foo' => 'bar'), |
82
|
|
|
'customSchemaOptions' => array('bar' => 'baz'), |
83
|
|
|
); |
84
|
|
|
|
85
|
|
|
$string = Type::getType('string'); |
86
|
|
|
return new Column("foo", $string, $options); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @group DBAL-64 |
91
|
|
|
* @group DBAL-830 |
92
|
|
|
*/ |
93
|
|
|
public function testQuotedColumnName() |
94
|
|
|
{ |
95
|
|
|
$string = Type::getType('string'); |
96
|
|
|
$column = new Column("`bar`", $string, array()); |
97
|
|
|
|
98
|
|
|
$mysqlPlatform = new \Doctrine\DBAL\Platforms\MySqlPlatform(); |
99
|
|
|
$sqlitePlatform = new \Doctrine\DBAL\Platforms\SqlitePlatform(); |
100
|
|
|
|
101
|
|
|
self::assertEquals('bar', $column->getName()); |
102
|
|
|
self::assertEquals('`bar`', $column->getQuotedName($mysqlPlatform)); |
103
|
|
|
self::assertEquals('"bar"', $column->getQuotedName($sqlitePlatform)); |
104
|
|
|
|
105
|
|
|
$column = new Column("[bar]", $string); |
106
|
|
|
|
107
|
|
|
$sqlServerPlatform = new \Doctrine\DBAL\Platforms\SQLServerPlatform(); |
108
|
|
|
|
109
|
|
|
self::assertEquals('bar', $column->getName()); |
110
|
|
|
self::assertEquals('[bar]', $column->getQuotedName($sqlServerPlatform)); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @dataProvider getIsQuoted |
115
|
|
|
* @group DBAL-830 |
116
|
|
|
*/ |
117
|
|
|
public function testIsQuoted($columnName, $isQuoted) |
118
|
|
|
{ |
119
|
|
|
$type = Type::getType('string'); |
120
|
|
|
$column = new Column($columnName, $type); |
121
|
|
|
|
122
|
|
|
self::assertSame($isQuoted, $column->isQuoted()); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function getIsQuoted() |
126
|
|
|
{ |
127
|
|
|
return array( |
128
|
|
|
array('bar', false), |
129
|
|
|
array('`bar`', true), |
130
|
|
|
array('"bar"', true), |
131
|
|
|
array('[bar]', true), |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @group DBAL-42 |
137
|
|
|
*/ |
138
|
|
|
public function testColumnComment() |
139
|
|
|
{ |
140
|
|
|
$column = new Column("bar", Type::getType('string')); |
141
|
|
|
self::assertNull($column->getComment()); |
142
|
|
|
|
143
|
|
|
$column->setComment("foo"); |
144
|
|
|
self::assertEquals("foo", $column->getComment()); |
145
|
|
|
|
146
|
|
|
$columnArray = $column->toArray(); |
147
|
|
|
self::assertArrayHasKey('comment', $columnArray); |
148
|
|
|
self::assertEquals('foo', $columnArray['comment']); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|