Passed
Pull Request — master (#3120)
by Sergei
12:25
created

SQLServerSchemaManagerTest::testColumnComments()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 160
Code Lines 121

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 160
rs 8.2857
c 0
b 0
f 0
cc 1
eloc 121
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Doctrine\Tests\DBAL\Functional\Schema;
4
5
use Doctrine\DBAL\Schema\Column;
6
use Doctrine\DBAL\Schema\ColumnDiff;
7
use Doctrine\DBAL\Schema\Table;
8
use Doctrine\DBAL\Schema\TableDiff;
9
use Doctrine\DBAL\Types\Type;
10
use function current;
11
12
class SQLServerSchemaManagerTest extends SchemaManagerFunctionalTestCase
13
{
14
    protected function getPlatformName()
15
    {
16
        return "mssql";
17
    }
18
19
    /**
20
     * @group DBAL-255
21
     */
22
    public function testDropColumnConstraints()
23
    {
24
        $table = new Table('sqlsrv_drop_column');
25
        $table->addColumn('id', 'integer');
26
        $table->addColumn('todrop', 'decimal', array('default' => 10.2));
27
28
        $this->_sm->createTable($table);
29
30
        $diff = new TableDiff('sqlsrv_drop_column', array(), array(), array(
31
            new Column('todrop', Type::getType('decimal'))
32
        ));
33
        $this->_sm->alterTable($diff);
34
35
        $columns = $this->_sm->listTableColumns('sqlsrv_drop_column');
36
        self::assertCount(1, $columns);
37
    }
38
39
    public function testColumnCollation()
40
    {
41
        $table = new \Doctrine\DBAL\Schema\Table($tableName = 'test_collation');
42
        $column = $table->addColumn($columnName = 'test', 'string');
43
44
        $this->_sm->dropAndCreateTable($table);
45
        $columns = $this->_sm->listTableColumns($tableName);
46
47
        self::assertTrue($columns[$columnName]->hasPlatformOption('collation')); // SQL Server should report a default collation on the column
48
49
        $column->setPlatformOption('collation', $collation = 'Icelandic_CS_AS');
50
51
        $this->_sm->dropAndCreateTable($table);
52
        $columns = $this->_sm->listTableColumns($tableName);
53
54
        self::assertEquals($collation, $columns[$columnName]->getPlatformOption('collation'));
55
    }
56
57
    public function testDefaultConstraints()
58
    {
59
        $platform = $this->_sm->getDatabasePlatform();
60
        $table = new Table('sqlsrv_default_constraints');
61
        $table->addColumn('no_default', 'string');
62
        $table->addColumn('df_integer', 'integer', array('default' => 666));
63
        $table->addColumn('df_string_1', 'string', array('default' => 'foobar'));
64
        $table->addColumn('df_string_2', 'string', array('default' => 'Doctrine rocks!!!'));
65
        $table->addColumn('df_string_3', 'string', array('default' => 'another default value'));
66
        $table->addColumn('df_string_4', 'string', array('default' => 'column to rename'));
67
        $table->addColumn('df_boolean', 'boolean', array('default' => true));
68
        $table->addColumn('df_current_date', 'date', array('default' => $platform->getCurrentDateSQL()));
69
        $table->addColumn('df_current_time', 'time', array('default' => $platform->getCurrentTimeSQL()));
70
71
        $this->_sm->createTable($table);
72
        $columns = $this->_sm->listTableColumns('sqlsrv_default_constraints');
73
74
        self::assertNull($columns['no_default']->getDefault());
75
        self::assertEquals(666, $columns['df_integer']->getDefault());
76
        self::assertEquals('foobar', $columns['df_string_1']->getDefault());
77
        self::assertEquals('Doctrine rocks!!!', $columns['df_string_2']->getDefault());
78
        self::assertEquals('another default value', $columns['df_string_3']->getDefault());
79
        self::assertEquals(1, $columns['df_boolean']->getDefault());
80
        self::assertSame($platform->getCurrentDateSQL(), $columns['df_current_date']->getDefault());
81
        self::assertSame($platform->getCurrentTimeSQL(), $columns['df_current_time']->getDefault());
82
83
        $diff = new TableDiff(
84
            'sqlsrv_default_constraints',
85
            array(
86
                new Column('df_current_timestamp', Type::getType('datetime'), array('default' => 'CURRENT_TIMESTAMP'))
87
            ),
88
            array(
89
                'df_integer' => new ColumnDiff(
90
                    'df_integer',
91
                    new Column('df_integer', Type::getType('integer'), array('default' => 0)),
92
                    array('default'),
93
                    new Column('df_integer', Type::getType('integer'), array('default' => 666))
94
                ),
95
                'df_string_2' => new ColumnDiff(
96
                    'df_string_2',
97
                    new Column('df_string_2', Type::getType('string')),
98
                    array('default'),
99
                    new Column('df_string_2', Type::getType('string'), array('default' => 'Doctrine rocks!!!'))
100
                ),
101
                'df_string_3' => new ColumnDiff(
102
                    'df_string_3',
103
                    new Column('df_string_3', Type::getType('string'), array('length' => 50, 'default' => 'another default value')),
104
                    array('length'),
105
                    new Column('df_string_3', Type::getType('string'), array('length' => 50, 'default' => 'another default value'))
106
                ),
107
                'df_boolean' => new ColumnDiff(
108
                    'df_boolean',
109
                    new Column('df_boolean', Type::getType('boolean'), array('default' => false)),
110
                    array('default'),
111
                    new Column('df_boolean', Type::getType('boolean'), array('default' => true))
112
                )
113
            ),
114
            array(
115
                'df_string_1' => new Column('df_string_1', Type::getType('string'))
116
            ),
117
            array(),
118
            array(),
119
            array(),
120
            $table
121
        );
122
        $diff->newName = 'sqlsrv_default_constraints';
123
        $diff->renamedColumns['df_string_4'] = new Column(
124
            'df_string_renamed',
125
            Type::getType('string'),
126
            array('default' => 'column to rename')
127
        );
128
129
        $this->_sm->alterTable($diff);
130
        $columns = $this->_sm->listTableColumns('sqlsrv_default_constraints');
131
132
        self::assertNull($columns['no_default']->getDefault());
133
        self::assertEquals('CURRENT_TIMESTAMP', $columns['df_current_timestamp']->getDefault());
134
        self::assertEquals(0, $columns['df_integer']->getDefault());
135
        self::assertNull($columns['df_string_2']->getDefault());
136
        self::assertEquals('another default value', $columns['df_string_3']->getDefault());
137
        self::assertEquals(0, $columns['df_boolean']->getDefault());
138
        self::assertEquals('column to rename', $columns['df_string_renamed']->getDefault());
139
140
        /**
141
         * Test that column default constraints can still be referenced after table rename
142
         */
143
        $diff = new TableDiff(
144
            'sqlsrv_default_constraints',
145
            array(),
146
            array(
147
                'df_current_timestamp' => new ColumnDiff(
148
                    'df_current_timestamp',
149
                    new Column('df_current_timestamp', Type::getType('datetime')),
150
                    array('default'),
151
                    new Column('df_current_timestamp', Type::getType('datetime'), array('default' => 'CURRENT_TIMESTAMP'))
152
                ),
153
                'df_integer' => new ColumnDiff(
154
                    'df_integer',
155
                    new Column('df_integer', Type::getType('integer'), array('default' => 666)),
156
                    array('default'),
157
                    new Column('df_integer', Type::getType('integer'), array('default' => 0))
158
                )
159
            ),
160
            array(),
161
            array(),
162
            array(),
163
            array(),
164
            $table
165
        );
166
167
        $this->_sm->alterTable($diff);
168
        $columns = $this->_sm->listTableColumns('sqlsrv_default_constraints');
169
170
        self::assertNull($columns['df_current_timestamp']->getDefault());
171
        self::assertEquals(666, $columns['df_integer']->getDefault());
172
    }
173
174
    /**
175
     * @group DBAL-543
176
     */
177
    public function testColumnComments()
178
    {
179
        $table = new Table('sqlsrv_column_comment');
180
        $table->addColumn('id', 'integer', array('autoincrement' => true));
181
        $table->addColumn('comment_null', 'integer', array('comment' => null));
182
        $table->addColumn('comment_false', 'integer', array('comment' => false));
183
        $table->addColumn('comment_empty_string', 'integer', array('comment' => ''));
184
        $table->addColumn('comment_integer_0', 'integer', array('comment' => 0));
185
        $table->addColumn('comment_float_0', 'integer', array('comment' => 0.0));
186
        $table->addColumn('comment_string_0', 'integer', array('comment' => '0'));
187
        $table->addColumn('comment', 'integer', array('comment' => 'Doctrine 0wnz you!'));
188
        $table->addColumn('`comment_quoted`', 'integer', array('comment' => 'Doctrine 0wnz comments for explicitly quoted columns!'));
189
        $table->addColumn('create', 'integer', array('comment' => 'Doctrine 0wnz comments for reserved keyword columns!'));
190
        $table->addColumn('commented_type', 'object');
191
        $table->addColumn('commented_type_with_comment', 'array', array('comment' => 'Doctrine array type.'));
192
        $table->setPrimaryKey(array('id'));
193
194
        $this->_sm->createTable($table);
195
196
        $columns = $this->_sm->listTableColumns("sqlsrv_column_comment");
197
        self::assertCount(12, $columns);
198
        self::assertNull($columns['id']->getComment());
199
        self::assertNull($columns['comment_null']->getComment());
200
        self::assertNull($columns['comment_false']->getComment());
201
        self::assertNull($columns['comment_empty_string']->getComment());
202
        self::assertEquals('0', $columns['comment_integer_0']->getComment());
203
        self::assertEquals('0', $columns['comment_float_0']->getComment());
204
        self::assertEquals('0', $columns['comment_string_0']->getComment());
205
        self::assertEquals('Doctrine 0wnz you!', $columns['comment']->getComment());
206
        self::assertEquals('Doctrine 0wnz comments for explicitly quoted columns!', $columns['comment_quoted']->getComment());
207
        self::assertEquals('Doctrine 0wnz comments for reserved keyword columns!', $columns['[create]']->getComment());
208
        self::assertNull($columns['commented_type']->getComment());
209
        self::assertEquals('Doctrine array type.', $columns['commented_type_with_comment']->getComment());
210
211
        $tableDiff = new TableDiff('sqlsrv_column_comment');
212
        $tableDiff->fromTable = $table;
213
        $tableDiff->addedColumns['added_comment_none'] = new Column('added_comment_none', Type::getType('integer'));
214
        $tableDiff->addedColumns['added_comment_null'] = new Column('added_comment_null', Type::getType('integer'), array('comment' => null));
215
        $tableDiff->addedColumns['added_comment_false'] = new Column('added_comment_false', Type::getType('integer'), array('comment' => false));
216
        $tableDiff->addedColumns['added_comment_empty_string'] = new Column('added_comment_empty_string', Type::getType('integer'), array('comment' => ''));
217
        $tableDiff->addedColumns['added_comment_integer_0'] = new Column('added_comment_integer_0', Type::getType('integer'), array('comment' => 0));
218
        $tableDiff->addedColumns['added_comment_float_0'] = new Column('added_comment_float_0', Type::getType('integer'), array('comment' => 0.0));
219
        $tableDiff->addedColumns['added_comment_string_0'] = new Column('added_comment_string_0', Type::getType('integer'), array('comment' => '0'));
220
        $tableDiff->addedColumns['added_comment'] = new Column('added_comment', Type::getType('integer'), array('comment' => 'Doctrine'));
221
        $tableDiff->addedColumns['`added_comment_quoted`'] = new Column('`added_comment_quoted`', Type::getType('integer'), array('comment' => 'rulez'));
222
        $tableDiff->addedColumns['select'] = new Column('select', Type::getType('integer'), array('comment' => '666'));
223
        $tableDiff->addedColumns['added_commented_type'] = new Column('added_commented_type', Type::getType('object'));
224
        $tableDiff->addedColumns['added_commented_type_with_comment'] = new Column('added_commented_type_with_comment', Type::getType('array'), array('comment' => '666'));
225
226
        $tableDiff->renamedColumns['comment_float_0'] = new Column('comment_double_0', Type::getType('decimal'), array('comment' => 'Double for real!'));
227
228
        // Add comment to non-commented column.
229
        $tableDiff->changedColumns['id'] = new ColumnDiff(
230
            'id',
231
            new Column('id', Type::getType('integer'), array('autoincrement' => true, 'comment' => 'primary')),
232
            array('comment'),
233
            new Column('id', Type::getType('integer'), array('autoincrement' => true))
234
        );
235
236
        // Remove comment from null-commented column.
237
        $tableDiff->changedColumns['comment_null'] = new ColumnDiff(
238
            'comment_null',
239
            new Column('comment_null', Type::getType('string')),
240
            array('type'),
241
            new Column('comment_null', Type::getType('integer'), array('comment' => null))
242
        );
243
244
        // Add comment to false-commented column.
245
        $tableDiff->changedColumns['comment_false'] = new ColumnDiff(
246
            'comment_false',
247
            new Column('comment_false', Type::getType('integer'), array('comment' => 'false')),
248
            array('comment'),
249
            new Column('comment_false', Type::getType('integer'), array('comment' => false))
250
        );
251
252
        // Change type to custom type from empty string commented column.
253
        $tableDiff->changedColumns['comment_empty_string'] = new ColumnDiff(
254
            'comment_empty_string',
255
            new Column('comment_empty_string', Type::getType('object')),
256
            array('type'),
257
            new Column('comment_empty_string', Type::getType('integer'), array('comment' => ''))
258
        );
259
260
        // Change comment to false-comment from zero-string commented column.
261
        $tableDiff->changedColumns['comment_string_0'] = new ColumnDiff(
262
            'comment_string_0',
263
            new Column('comment_string_0', Type::getType('integer'), array('comment' => false)),
264
            array('comment'),
265
            new Column('comment_string_0', Type::getType('integer'), array('comment' => '0'))
266
        );
267
268
        // Remove comment from regular commented column.
269
        $tableDiff->changedColumns['comment'] = new ColumnDiff(
270
            'comment',
271
            new Column('comment', Type::getType('integer')),
272
            array('comment'),
273
            new Column('comment', Type::getType('integer'), array('comment' => 'Doctrine 0wnz you!'))
274
        );
275
276
        // Change comment and change type to custom type from regular commented column.
277
        $tableDiff->changedColumns['`comment_quoted`'] = new ColumnDiff(
278
            '`comment_quoted`',
279
            new Column('`comment_quoted`', Type::getType('array'), array('comment' => 'Doctrine array.')),
280
            array('comment', 'type'),
281
            new Column('`comment_quoted`', Type::getType('integer'), array('comment' => 'Doctrine 0wnz you!'))
282
        );
283
284
        // Remove comment and change type to custom type from regular commented column.
285
        $tableDiff->changedColumns['create'] = new ColumnDiff(
286
            'create',
287
            new Column('create', Type::getType('object')),
288
            array('comment', 'type'),
289
            new Column('create', Type::getType('integer'), array('comment' => 'Doctrine 0wnz comments for reserved keyword columns!'))
290
        );
291
292
        // Add comment and change custom type to regular type from non-commented column.
293
        $tableDiff->changedColumns['commented_type'] = new ColumnDiff(
294
            'commented_type',
295
            new Column('commented_type', Type::getType('integer'), array('comment' => 'foo')),
296
            array('comment', 'type'),
297
            new Column('commented_type', Type::getType('object'))
298
        );
299
300
        // Remove comment from commented custom type column.
301
        $tableDiff->changedColumns['commented_type_with_comment'] = new ColumnDiff(
302
            'commented_type_with_comment',
303
            new Column('commented_type_with_comment', Type::getType('array')),
304
            array('comment'),
305
            new Column('commented_type_with_comment', Type::getType('array'), array('comment' => 'Doctrine array type.'))
306
        );
307
308
        $tableDiff->removedColumns['comment_integer_0'] = new Column('comment_integer_0', Type::getType('integer'), array('comment' => 0));
309
310
        $this->_sm->alterTable($tableDiff);
311
312
        $columns = $this->_sm->listTableColumns("sqlsrv_column_comment");
313
        self::assertCount(23, $columns);
314
        self::assertEquals('primary', $columns['id']->getComment());
315
        self::assertNull($columns['comment_null']->getComment());
316
        self::assertEquals('false', $columns['comment_false']->getComment());
317
        self::assertNull($columns['comment_empty_string']->getComment());
318
        self::assertEquals('0', $columns['comment_double_0']->getComment());
319
        self::assertNull($columns['comment_string_0']->getComment());
320
        self::assertNull($columns['comment']->getComment());
321
        self::assertEquals('Doctrine array.', $columns['comment_quoted']->getComment());
322
        self::assertNull($columns['[create]']->getComment());
323
        self::assertEquals('foo', $columns['commented_type']->getComment());
324
        self::assertNull($columns['commented_type_with_comment']->getComment());
325
        self::assertNull($columns['added_comment_none']->getComment());
326
        self::assertNull($columns['added_comment_null']->getComment());
327
        self::assertNull($columns['added_comment_false']->getComment());
328
        self::assertNull($columns['added_comment_empty_string']->getComment());
329
        self::assertEquals('0', $columns['added_comment_integer_0']->getComment());
330
        self::assertEquals('0', $columns['added_comment_float_0']->getComment());
331
        self::assertEquals('0', $columns['added_comment_string_0']->getComment());
332
        self::assertEquals('Doctrine', $columns['added_comment']->getComment());
333
        self::assertEquals('rulez', $columns['added_comment_quoted']->getComment());
334
        self::assertEquals('666', $columns['[select]']->getComment());
335
        self::assertNull($columns['added_commented_type']->getComment());
336
        self::assertEquals('666', $columns['added_commented_type_with_comment']->getComment());
337
    }
338
339
    public function testPkOrdering()
340
    {
341
        // SQL Server stores index column information in a system table with two
342
        // columns that almost always have the same value: index_column_id and key_ordinal.
343
        // The only situation when the two values doesn't match up is when a clustered index
344
        // is declared that references columns in a different order from which they are
345
        // declared in the table. In that case, key_ordinal != index_column_id.
346
        // key_ordinal holds the index ordering. index_column_id is just a unique identifier
347
        // for index columns within the given index.
348
        $table = new Table('sqlsrv_pk_ordering');
349
        $table->addColumn('colA', 'integer', array('notnull' => true));
350
        $table->addColumn('colB', 'integer', array('notnull' => true));
351
        $table->setPrimaryKey(array('colB', 'colA'));
352
        $this->_sm->createTable($table);
353
354
        $indexes = $this->_sm->listTableIndexes('sqlsrv_pk_ordering');
355
356
        self::assertCount(1, $indexes);
357
358
        $firstIndex = current($indexes);
359
        $columns = $firstIndex->getColumns();
360
        self::assertCount(2, $columns);
361
        self::assertEquals('colB', $columns[0]);
362
        self::assertEquals('colA', $columns[1]);
363
    }
364
}
365