Completed
Pull Request — master (#2850)
by Adrien
04:18
created

testEscapedDefaultValueMustBePreserved()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 14
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 14
loc 14
rs 9.4285
cc 1
eloc 9
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\DBAL\Functional\Schema;
4
5
use Doctrine\DBAL\Platforms\MySqlPlatform;
6
use Doctrine\DBAL\Schema\Comparator;
7
use Doctrine\DBAL\Schema\Schema;
8
use Doctrine\DBAL\Schema\Table;
9
use Doctrine\DBAL\Types\Type;
10
11
class MySqlSchemaManagerTest extends SchemaManagerFunctionalTestCase
12
{
13
    protected function setUp()
14
    {
15
        parent::setUp();
16
17
        if (!Type::hasType('point')) {
18
            Type::addType('point', 'Doctrine\Tests\Types\MySqlPointType');
19
        }
20
    }
21
22
    public function testSwitchPrimaryKeyColumns()
23
    {
24
        $tableOld = new Table("switch_primary_key_columns");
25
        $tableOld->addColumn('foo_id', 'integer');
26
        $tableOld->addColumn('bar_id', 'integer');
27
        $tableNew = clone $tableOld;
0 ignored issues
show
Unused Code introduced by
$tableNew is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
28
29
        $this->_sm->createTable($tableOld);
30
        $tableFetched = $this->_sm->listTableDetails("switch_primary_key_columns");
31
        $tableNew = clone $tableFetched;
32
        $tableNew->setPrimaryKey(array('bar_id', 'foo_id'));
33
34
        $comparator = new Comparator;
35
        $this->_sm->alterTable($comparator->diffTable($tableFetched, $tableNew));
0 ignored issues
show
Security Bug introduced by
It seems like $comparator->diffTable($tableFetched, $tableNew) targeting Doctrine\DBAL\Schema\Comparator::diffTable() can also be of type false; however, Doctrine\DBAL\Schema\Abs...maManager::alterTable() does only seem to accept object<Doctrine\DBAL\Schema\TableDiff>, did you maybe forget to handle an error condition?
Loading history...
36
    }
37
38
    public function testDiffTableBug()
39
    {
40
        $schema = new Schema();
41
        $table = $schema->createTable('diffbug_routing_translations');
42
        $table->addColumn('id', 'integer');
43
        $table->addColumn('route', 'string');
44
        $table->addColumn('locale', 'string');
45
        $table->addColumn('attribute', 'string');
46
        $table->addColumn('localized_value', 'string');
47
        $table->addColumn('original_value', 'string');
48
        $table->setPrimaryKey(array('id'));
49
        $table->addUniqueIndex(array('route', 'locale', 'attribute'));
50
        $table->addIndex(array('localized_value')); // this is much more selective than the unique index
51
52
        $this->_sm->createTable($table);
53
        $tableFetched = $this->_sm->listTableDetails("diffbug_routing_translations");
54
55
        $comparator = new Comparator;
56
        $diff = $comparator->diffTable($tableFetched, $table);
57
58
        $this->assertFalse($diff, "no changes expected.");
59
    }
60
61 View Code Duplication
    public function testFulltextIndex()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
    {
63
        $table = new Table('fulltext_index');
64
        $table->addColumn('text', 'text');
65
        $table->addIndex(array('text'), 'f_index');
66
        $table->addOption('engine', 'MyISAM');
67
68
        $index = $table->getIndex('f_index');
69
        $index->addFlag('fulltext');
70
71
        $this->_sm->dropAndCreateTable($table);
72
73
        $indexes = $this->_sm->listTableIndexes('fulltext_index');
74
        $this->assertArrayHasKey('f_index', $indexes);
75
        $this->assertTrue($indexes['f_index']->hasFlag('fulltext'));
76
    }
77
78 View Code Duplication
    public function testSpatialIndex()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
    {
80
        $table = new Table('spatial_index');
81
        $table->addColumn('point', 'point');
82
        $table->addIndex(array('point'), 's_index');
83
        $table->addOption('engine', 'MyISAM');
84
85
        $index = $table->getIndex('s_index');
86
        $index->addFlag('spatial');
87
88
        $this->_sm->dropAndCreateTable($table);
89
90
        $indexes = $this->_sm->listTableIndexes('spatial_index');
91
        $this->assertArrayHasKey('s_index', $indexes);
92
        $this->assertTrue($indexes['s_index']->hasFlag('spatial'));
93
    }
94
95
    /**
96
     * @group DBAL-400
97
     */
98
    public function testAlterTableAddPrimaryKey()
99
    {
100
        $table = new Table('alter_table_add_pk');
101
        $table->addColumn('id', 'integer');
102
        $table->addColumn('foo', 'integer');
103
        $table->addIndex(array('id'), 'idx_id');
104
105
        $this->_sm->createTable($table);
106
107
        $comparator = new Comparator();
108
        $diffTable  = clone $table;
109
110
        $diffTable->dropIndex('idx_id');
111
        $diffTable->setPrimaryKey(array('id'));
112
113
        $this->_sm->alterTable($comparator->diffTable($table, $diffTable));
0 ignored issues
show
Security Bug introduced by
It seems like $comparator->diffTable($table, $diffTable) targeting Doctrine\DBAL\Schema\Comparator::diffTable() can also be of type false; however, Doctrine\DBAL\Schema\Abs...maManager::alterTable() does only seem to accept object<Doctrine\DBAL\Schema\TableDiff>, did you maybe forget to handle an error condition?
Loading history...
114
115
        $table = $this->_sm->listTableDetails("alter_table_add_pk");
116
117
        $this->assertFalse($table->hasIndex('idx_id'));
118
        $this->assertTrue($table->hasPrimaryKey());
119
    }
120
121
    /**
122
     * @group DBAL-464
123
     */
124
    public function testDropPrimaryKeyWithAutoincrementColumn()
125
    {
126
        $table = new Table("drop_primary_key");
127
        $table->addColumn('id', 'integer', array('primary' => true, 'autoincrement' => true));
128
        $table->addColumn('foo', 'integer', array('primary' => true));
129
        $table->setPrimaryKey(array('id', 'foo'));
130
131
        $this->_sm->dropAndCreateTable($table);
132
133
        $diffTable = clone $table;
134
135
        $diffTable->dropPrimaryKey();
136
137
        $comparator = new Comparator();
138
139
        $this->_sm->alterTable($comparator->diffTable($table, $diffTable));
0 ignored issues
show
Security Bug introduced by
It seems like $comparator->diffTable($table, $diffTable) targeting Doctrine\DBAL\Schema\Comparator::diffTable() can also be of type false; however, Doctrine\DBAL\Schema\Abs...maManager::alterTable() does only seem to accept object<Doctrine\DBAL\Schema\TableDiff>, did you maybe forget to handle an error condition?
Loading history...
140
141
        $table = $this->_sm->listTableDetails("drop_primary_key");
142
143
        $this->assertFalse($table->hasPrimaryKey());
144
        $this->assertFalse($table->getColumn('id')->getAutoincrement());
145
    }
146
147
    /**
148
     * @group DBAL-789
149
     */
150
    public function testDoesNotPropagateDefaultValuesForUnsupportedColumnTypes()
151
    {
152
        $table = new Table("text_blob_default_value");
153
        $table->addColumn('def_text', 'text', array('default' => 'def'));
154
        $table->addColumn('def_text_null', 'text', array('notnull' => false, 'default' => 'def'));
155
        $table->addColumn('def_blob', 'blob', array('default' => 'def'));
156
        $table->addColumn('def_blob_null', 'blob', array('notnull' => false, 'default' => 'def'));
157
158
        $this->_sm->dropAndCreateTable($table);
159
160
        $onlineTable = $this->_sm->listTableDetails("text_blob_default_value");
161
162
        $this->assertNull($onlineTable->getColumn('def_text')->getDefault());
163
        $this->assertNull($onlineTable->getColumn('def_text_null')->getDefault());
164
        $this->assertFalse($onlineTable->getColumn('def_text_null')->getNotnull());
165
        $this->assertNull($onlineTable->getColumn('def_blob')->getDefault());
166
        $this->assertNull($onlineTable->getColumn('def_blob_null')->getDefault());
167
        $this->assertFalse($onlineTable->getColumn('def_blob_null')->getNotnull());
168
169
        $comparator = new Comparator();
170
171
        $this->_sm->alterTable($comparator->diffTable($table, $onlineTable));
0 ignored issues
show
Security Bug introduced by
It seems like $comparator->diffTable($table, $onlineTable) targeting Doctrine\DBAL\Schema\Comparator::diffTable() can also be of type false; however, Doctrine\DBAL\Schema\Abs...maManager::alterTable() does only seem to accept object<Doctrine\DBAL\Schema\TableDiff>, did you maybe forget to handle an error condition?
Loading history...
172
173
        $onlineTable = $this->_sm->listTableDetails("text_blob_default_value");
174
175
        $this->assertNull($onlineTable->getColumn('def_text')->getDefault());
176
        $this->assertNull($onlineTable->getColumn('def_text_null')->getDefault());
177
        $this->assertFalse($onlineTable->getColumn('def_text_null')->getNotnull());
178
        $this->assertNull($onlineTable->getColumn('def_blob')->getDefault());
179
        $this->assertNull($onlineTable->getColumn('def_blob_null')->getDefault());
180
        $this->assertFalse($onlineTable->getColumn('def_blob_null')->getNotnull());
181
    }
182
183 View Code Duplication
    public function testEscapedDefaultValueMustBePreserved()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
184
    {
185
        $value = "a\\0a\\'a\"a\na\ra\ta\\Za\\\\a";
186
187
        $table = new Table('string_escaped_default_value');
188
        $table->addColumn('def_string', 'string', array('default' => $value));
189
        $this->_sm->dropAndCreateTable($table);
190
191
        $onlineTable = $this->_sm->listTableDetails('string_escaped_default_value');
192
        $this->assertSame($value, $onlineTable->getColumn('def_string')->getDefault());
193
194
        $comparator = new Comparator();
195
        $this->assertFalse($comparator->diffTable($table, $onlineTable));
196
    }
197
198 View Code Duplication
    public function testColumnCollation()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
199
    {
200
        $table = new Table('test_collation');
201
        $table->addOption('collate', $collation = 'latin1_swedish_ci');
202
        $table->addOption('charset', 'latin1');
203
        $table->addColumn('id', 'integer');
204
        $table->addColumn('text', 'text');
205
        $table->addColumn('foo', 'text')->setPlatformOption('collation', 'latin1_swedish_ci');
206
        $table->addColumn('bar', 'text')->setPlatformOption('collation', 'utf8_general_ci');
207
        $this->_sm->dropAndCreateTable($table);
208
209
        $columns = $this->_sm->listTableColumns('test_collation');
210
211
        $this->assertArrayNotHasKey('collation', $columns['id']->getPlatformOptions());
212
        $this->assertEquals('latin1_swedish_ci', $columns['text']->getPlatformOption('collation'));
213
        $this->assertEquals('latin1_swedish_ci', $columns['foo']->getPlatformOption('collation'));
214
        $this->assertEquals('utf8_general_ci', $columns['bar']->getPlatformOption('collation'));
215
    }
216
217
    /**
218
     * @group DBAL-843
219
     */
220
    public function testListLobTypeColumns()
221
    {
222
        $tableName = 'lob_type_columns';
223
        $table = new Table($tableName);
224
225
        $table->addColumn('col_tinytext', 'text', array('length' => MySqlPlatform::LENGTH_LIMIT_TINYTEXT));
226
        $table->addColumn('col_text', 'text', array('length' => MySqlPlatform::LENGTH_LIMIT_TEXT));
227
        $table->addColumn('col_mediumtext', 'text', array('length' => MySqlPlatform::LENGTH_LIMIT_MEDIUMTEXT));
228
        $table->addColumn('col_longtext', 'text');
229
230
        $table->addColumn('col_tinyblob', 'text', array('length' => MySqlPlatform::LENGTH_LIMIT_TINYBLOB));
231
        $table->addColumn('col_blob', 'blob', array('length' => MySqlPlatform::LENGTH_LIMIT_BLOB));
232
        $table->addColumn('col_mediumblob', 'blob', array('length' => MySqlPlatform::LENGTH_LIMIT_MEDIUMBLOB));
233
        $table->addColumn('col_longblob', 'blob');
234
235
        $this->_sm->dropAndCreateTable($table);
236
237
        $platform = $this->_sm->getDatabasePlatform();
238
        $offlineColumns = $table->getColumns();
239
        $onlineColumns = $this->_sm->listTableColumns($tableName);
240
241
        $this->assertSame(
242
            $platform->getClobTypeDeclarationSQL($offlineColumns['col_tinytext']->toArray()),
243
            $platform->getClobTypeDeclarationSQL($onlineColumns['col_tinytext']->toArray())
244
        );
245
        $this->assertSame(
246
            $platform->getClobTypeDeclarationSQL($offlineColumns['col_text']->toArray()),
247
            $platform->getClobTypeDeclarationSQL($onlineColumns['col_text']->toArray())
248
        );
249
        $this->assertSame(
250
            $platform->getClobTypeDeclarationSQL($offlineColumns['col_mediumtext']->toArray()),
251
            $platform->getClobTypeDeclarationSQL($onlineColumns['col_mediumtext']->toArray())
252
        );
253
        $this->assertSame(
254
            $platform->getClobTypeDeclarationSQL($offlineColumns['col_longtext']->toArray()),
255
            $platform->getClobTypeDeclarationSQL($onlineColumns['col_longtext']->toArray())
256
        );
257
258
        $this->assertSame(
259
            $platform->getBlobTypeDeclarationSQL($offlineColumns['col_tinyblob']->toArray()),
260
            $platform->getBlobTypeDeclarationSQL($onlineColumns['col_tinyblob']->toArray())
261
        );
262
        $this->assertSame(
263
            $platform->getBlobTypeDeclarationSQL($offlineColumns['col_blob']->toArray()),
264
            $platform->getBlobTypeDeclarationSQL($onlineColumns['col_blob']->toArray())
265
        );
266
        $this->assertSame(
267
            $platform->getBlobTypeDeclarationSQL($offlineColumns['col_mediumblob']->toArray()),
268
            $platform->getBlobTypeDeclarationSQL($onlineColumns['col_mediumblob']->toArray())
269
        );
270
        $this->assertSame(
271
            $platform->getBlobTypeDeclarationSQL($offlineColumns['col_longblob']->toArray()),
272
            $platform->getBlobTypeDeclarationSQL($onlineColumns['col_longblob']->toArray())
273
        );
274
    }
275
276
    /**
277
     * @group DBAL-423
278
     */
279
    public function testDiffListGuidTableColumn()
280
    {
281
        $offlineTable = new Table('list_guid_table_column');
282
        $offlineTable->addColumn('col_guid', 'guid');
283
284
        $this->_sm->dropAndCreateTable($offlineTable);
285
286
        $onlineTable = $this->_sm->listTableDetails('list_guid_table_column');
287
288
        $comparator = new Comparator();
289
290
        $this->assertFalse(
291
            $comparator->diffTable($offlineTable, $onlineTable),
292
            "No differences should be detected with the offline vs online schema."
293
        );
294
    }
295
296
    /**
297
     * @group DBAL-1082
298
     */
299 View Code Duplication
    public function testListDecimalTypeColumns()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
300
    {
301
        $tableName = 'test_list_decimal_columns';
302
        $table = new Table($tableName);
303
304
        $table->addColumn('col', 'decimal');
305
        $table->addColumn('col_unsigned', 'decimal', array('unsigned' => true));
306
307
        $this->_sm->dropAndCreateTable($table);
308
309
        $columns = $this->_sm->listTableColumns($tableName);
310
311
        $this->assertArrayHasKey('col', $columns);
312
        $this->assertArrayHasKey('col_unsigned', $columns);
313
        $this->assertFalse($columns['col']->getUnsigned());
314
        $this->assertTrue($columns['col_unsigned']->getUnsigned());
315
    }
316
317
    /**
318
     * @group DBAL-1082
319
     */
320 View Code Duplication
    public function testListFloatTypeColumns()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
321
    {
322
        $tableName = 'test_list_float_columns';
323
        $table = new Table($tableName);
324
325
        $table->addColumn('col', 'float');
326
        $table->addColumn('col_unsigned', 'float', array('unsigned' => true));
327
328
        $this->_sm->dropAndCreateTable($table);
329
330
        $columns = $this->_sm->listTableColumns($tableName);
331
332
        $this->assertArrayHasKey('col', $columns);
333
        $this->assertArrayHasKey('col_unsigned', $columns);
334
        $this->assertFalse($columns['col']->getUnsigned());
335
        $this->assertTrue($columns['col_unsigned']->getUnsigned());
336
    }
337
}
338