Completed
Push — master ( c7757e...39cb21 )
by Luís
16s
created

DBAL/Functional/Schema/MySqlSchemaManagerTest.php (1 issue)

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
28
        $this->_sm->createTable($tableOld);
29
        $tableFetched = $this->_sm->listTableDetails("switch_primary_key_columns");
30
        $tableNew = clone $tableFetched;
31
        $tableNew->setPrimaryKey(array('bar_id', 'foo_id'));
32
33
        $comparator = new Comparator;
34
        $this->_sm->alterTable($comparator->diffTable($tableFetched, $tableNew));
35
36
        $table      = $this->_sm->listTableDetails('switch_primary_key_columns');
37
        $primaryKey = $table->getPrimaryKeyColumns();
38
39
        self::assertCount(2, $primaryKey);
40
        self::assertContains('bar_id', $primaryKey);
41
        self::assertContains('foo_id', $primaryKey);
42
    }
43
44
    public function testDiffTableBug()
45
    {
46
        $schema = new Schema();
47
        $table = $schema->createTable('diffbug_routing_translations');
48
        $table->addColumn('id', 'integer');
49
        $table->addColumn('route', 'string');
50
        $table->addColumn('locale', 'string');
51
        $table->addColumn('attribute', 'string');
52
        $table->addColumn('localized_value', 'string');
53
        $table->addColumn('original_value', 'string');
54
        $table->setPrimaryKey(array('id'));
55
        $table->addUniqueIndex(array('route', 'locale', 'attribute'));
56
        $table->addIndex(array('localized_value')); // this is much more selective than the unique index
57
58
        $this->_sm->createTable($table);
59
        $tableFetched = $this->_sm->listTableDetails("diffbug_routing_translations");
60
61
        $comparator = new Comparator;
62
        $diff = $comparator->diffTable($tableFetched, $table);
63
64
        self::assertFalse($diff, "no changes expected.");
1 ignored issue
show
It seems like $diff can also be of type Doctrine\DBAL\Schema\TableDiff; however, parameter $condition of PHPUnit\Framework\Assert::assertFalse() does only seem to accept boolean, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

64
        self::assertFalse(/** @scrutinizer ignore-type */ $diff, "no changes expected.");
Loading history...
65
    }
66
67 View Code Duplication
    public function testFulltextIndex()
68
    {
69
        $table = new Table('fulltext_index');
70
        $table->addColumn('text', 'text');
71
        $table->addIndex(array('text'), 'f_index');
72
        $table->addOption('engine', 'MyISAM');
73
74
        $index = $table->getIndex('f_index');
75
        $index->addFlag('fulltext');
76
77
        $this->_sm->dropAndCreateTable($table);
78
79
        $indexes = $this->_sm->listTableIndexes('fulltext_index');
80
        self::assertArrayHasKey('f_index', $indexes);
81
        self::assertTrue($indexes['f_index']->hasFlag('fulltext'));
82
    }
83
84 View Code Duplication
    public function testSpatialIndex()
85
    {
86
        $table = new Table('spatial_index');
87
        $table->addColumn('point', 'point');
88
        $table->addIndex(array('point'), 's_index');
89
        $table->addOption('engine', 'MyISAM');
90
91
        $index = $table->getIndex('s_index');
92
        $index->addFlag('spatial');
93
94
        $this->_sm->dropAndCreateTable($table);
95
96
        $indexes = $this->_sm->listTableIndexes('spatial_index');
97
        self::assertArrayHasKey('s_index', $indexes);
98
        self::assertTrue($indexes['s_index']->hasFlag('spatial'));
99
    }
100
101
    /**
102
     * @group DBAL-400
103
     */
104
    public function testAlterTableAddPrimaryKey()
105
    {
106
        $table = new Table('alter_table_add_pk');
107
        $table->addColumn('id', 'integer');
108
        $table->addColumn('foo', 'integer');
109
        $table->addIndex(array('id'), 'idx_id');
110
111
        $this->_sm->createTable($table);
112
113
        $comparator = new Comparator();
114
        $diffTable  = clone $table;
115
116
        $diffTable->dropIndex('idx_id');
117
        $diffTable->setPrimaryKey(array('id'));
118
119
        $this->_sm->alterTable($comparator->diffTable($table, $diffTable));
120
121
        $table = $this->_sm->listTableDetails("alter_table_add_pk");
122
123
        self::assertFalse($table->hasIndex('idx_id'));
124
        self::assertTrue($table->hasPrimaryKey());
125
    }
126
127
    /**
128
     * @group DBAL-464
129
     */
130
    public function testDropPrimaryKeyWithAutoincrementColumn()
131
    {
132
        $table = new Table("drop_primary_key");
133
        $table->addColumn('id', 'integer', array('primary' => true, 'autoincrement' => true));
134
        $table->addColumn('foo', 'integer', array('primary' => true));
135
        $table->setPrimaryKey(array('id', 'foo'));
136
137
        $this->_sm->dropAndCreateTable($table);
138
139
        $diffTable = clone $table;
140
141
        $diffTable->dropPrimaryKey();
142
143
        $comparator = new Comparator();
144
145
        $this->_sm->alterTable($comparator->diffTable($table, $diffTable));
146
147
        $table = $this->_sm->listTableDetails("drop_primary_key");
148
149
        self::assertFalse($table->hasPrimaryKey());
150
        self::assertFalse($table->getColumn('id')->getAutoincrement());
151
    }
152
153
    /**
154
     * @group DBAL-789
155
     */
156
    public function testDoesNotPropagateDefaultValuesForUnsupportedColumnTypes()
157
    {
158
        $table = new Table("text_blob_default_value");
159
        $table->addColumn('def_text', 'text', array('default' => 'def'));
160
        $table->addColumn('def_text_null', 'text', array('notnull' => false, 'default' => 'def'));
161
        $table->addColumn('def_blob', 'blob', array('default' => 'def'));
162
        $table->addColumn('def_blob_null', 'blob', array('notnull' => false, 'default' => 'def'));
163
164
        $this->_sm->dropAndCreateTable($table);
165
166
        $onlineTable = $this->_sm->listTableDetails("text_blob_default_value");
167
168
        self::assertNull($onlineTable->getColumn('def_text')->getDefault());
169
        self::assertNull($onlineTable->getColumn('def_text_null')->getDefault());
170
        self::assertFalse($onlineTable->getColumn('def_text_null')->getNotnull());
171
        self::assertNull($onlineTable->getColumn('def_blob')->getDefault());
172
        self::assertNull($onlineTable->getColumn('def_blob_null')->getDefault());
173
        self::assertFalse($onlineTable->getColumn('def_blob_null')->getNotnull());
174
175
        $comparator = new Comparator();
176
177
        $this->_sm->alterTable($comparator->diffTable($table, $onlineTable));
178
179
        $onlineTable = $this->_sm->listTableDetails("text_blob_default_value");
180
181
        self::assertNull($onlineTable->getColumn('def_text')->getDefault());
182
        self::assertNull($onlineTable->getColumn('def_text_null')->getDefault());
183
        self::assertFalse($onlineTable->getColumn('def_text_null')->getNotnull());
184
        self::assertNull($onlineTable->getColumn('def_blob')->getDefault());
185
        self::assertNull($onlineTable->getColumn('def_blob_null')->getDefault());
186
        self::assertFalse($onlineTable->getColumn('def_blob_null')->getNotnull());
187
    }
188
189 View Code Duplication
    public function testColumnCollation()
190
    {
191
        $table = new Table('test_collation');
192
        $table->addOption('collate', $collation = 'latin1_swedish_ci');
193
        $table->addOption('charset', 'latin1');
194
        $table->addColumn('id', 'integer');
195
        $table->addColumn('text', 'text');
196
        $table->addColumn('foo', 'text')->setPlatformOption('collation', 'latin1_swedish_ci');
197
        $table->addColumn('bar', 'text')->setPlatformOption('collation', 'utf8_general_ci');
198
        $this->_sm->dropAndCreateTable($table);
199
200
        $columns = $this->_sm->listTableColumns('test_collation');
201
202
        self::assertArrayNotHasKey('collation', $columns['id']->getPlatformOptions());
203
        self::assertEquals('latin1_swedish_ci', $columns['text']->getPlatformOption('collation'));
204
        self::assertEquals('latin1_swedish_ci', $columns['foo']->getPlatformOption('collation'));
205
        self::assertEquals('utf8_general_ci', $columns['bar']->getPlatformOption('collation'));
206
    }
207
208
    /**
209
     * @group DBAL-843
210
     */
211
    public function testListLobTypeColumns()
212
    {
213
        $tableName = 'lob_type_columns';
214
        $table = new Table($tableName);
215
216
        $table->addColumn('col_tinytext', 'text', array('length' => MySqlPlatform::LENGTH_LIMIT_TINYTEXT));
217
        $table->addColumn('col_text', 'text', array('length' => MySqlPlatform::LENGTH_LIMIT_TEXT));
218
        $table->addColumn('col_mediumtext', 'text', array('length' => MySqlPlatform::LENGTH_LIMIT_MEDIUMTEXT));
219
        $table->addColumn('col_longtext', 'text');
220
221
        $table->addColumn('col_tinyblob', 'text', array('length' => MySqlPlatform::LENGTH_LIMIT_TINYBLOB));
222
        $table->addColumn('col_blob', 'blob', array('length' => MySqlPlatform::LENGTH_LIMIT_BLOB));
223
        $table->addColumn('col_mediumblob', 'blob', array('length' => MySqlPlatform::LENGTH_LIMIT_MEDIUMBLOB));
224
        $table->addColumn('col_longblob', 'blob');
225
226
        $this->_sm->dropAndCreateTable($table);
227
228
        $platform = $this->_sm->getDatabasePlatform();
229
        $offlineColumns = $table->getColumns();
230
        $onlineColumns = $this->_sm->listTableColumns($tableName);
231
232
        self::assertSame(
233
            $platform->getClobTypeDeclarationSQL($offlineColumns['col_tinytext']->toArray()),
234
            $platform->getClobTypeDeclarationSQL($onlineColumns['col_tinytext']->toArray())
235
        );
236
        self::assertSame(
237
            $platform->getClobTypeDeclarationSQL($offlineColumns['col_text']->toArray()),
238
            $platform->getClobTypeDeclarationSQL($onlineColumns['col_text']->toArray())
239
        );
240
        self::assertSame(
241
            $platform->getClobTypeDeclarationSQL($offlineColumns['col_mediumtext']->toArray()),
242
            $platform->getClobTypeDeclarationSQL($onlineColumns['col_mediumtext']->toArray())
243
        );
244
        self::assertSame(
245
            $platform->getClobTypeDeclarationSQL($offlineColumns['col_longtext']->toArray()),
246
            $platform->getClobTypeDeclarationSQL($onlineColumns['col_longtext']->toArray())
247
        );
248
249
        self::assertSame(
250
            $platform->getBlobTypeDeclarationSQL($offlineColumns['col_tinyblob']->toArray()),
251
            $platform->getBlobTypeDeclarationSQL($onlineColumns['col_tinyblob']->toArray())
252
        );
253
        self::assertSame(
254
            $platform->getBlobTypeDeclarationSQL($offlineColumns['col_blob']->toArray()),
255
            $platform->getBlobTypeDeclarationSQL($onlineColumns['col_blob']->toArray())
256
        );
257
        self::assertSame(
258
            $platform->getBlobTypeDeclarationSQL($offlineColumns['col_mediumblob']->toArray()),
259
            $platform->getBlobTypeDeclarationSQL($onlineColumns['col_mediumblob']->toArray())
260
        );
261
        self::assertSame(
262
            $platform->getBlobTypeDeclarationSQL($offlineColumns['col_longblob']->toArray()),
263
            $platform->getBlobTypeDeclarationSQL($onlineColumns['col_longblob']->toArray())
264
        );
265
    }
266
267
    /**
268
     * @group DBAL-423
269
     */
270 View Code Duplication
    public function testDiffListGuidTableColumn()
271
    {
272
        $offlineTable = new Table('list_guid_table_column');
273
        $offlineTable->addColumn('col_guid', 'guid');
274
275
        $this->_sm->dropAndCreateTable($offlineTable);
276
277
        $onlineTable = $this->_sm->listTableDetails('list_guid_table_column');
278
279
        $comparator = new Comparator();
280
281
        self::assertFalse(
282
            $comparator->diffTable($offlineTable, $onlineTable),
283
            "No differences should be detected with the offline vs online schema."
284
        );
285
    }
286
287
    /**
288
     * @group DBAL-1082
289
     */
290 View Code Duplication
    public function testListDecimalTypeColumns()
291
    {
292
        $tableName = 'test_list_decimal_columns';
293
        $table = new Table($tableName);
294
295
        $table->addColumn('col', 'decimal');
296
        $table->addColumn('col_unsigned', 'decimal', array('unsigned' => true));
297
298
        $this->_sm->dropAndCreateTable($table);
299
300
        $columns = $this->_sm->listTableColumns($tableName);
301
302
        self::assertArrayHasKey('col', $columns);
303
        self::assertArrayHasKey('col_unsigned', $columns);
304
        self::assertFalse($columns['col']->getUnsigned());
305
        self::assertTrue($columns['col_unsigned']->getUnsigned());
306
    }
307
308
    /**
309
     * @group DBAL-1082
310
     */
311 View Code Duplication
    public function testListFloatTypeColumns()
312
    {
313
        $tableName = 'test_list_float_columns';
314
        $table = new Table($tableName);
315
316
        $table->addColumn('col', 'float');
317
        $table->addColumn('col_unsigned', 'float', array('unsigned' => true));
318
319
        $this->_sm->dropAndCreateTable($table);
320
321
        $columns = $this->_sm->listTableColumns($tableName);
322
323
        self::assertArrayHasKey('col', $columns);
324
        self::assertArrayHasKey('col_unsigned', $columns);
325
        self::assertFalse($columns['col']->getUnsigned());
326
        self::assertTrue($columns['col_unsigned']->getUnsigned());
327
    }
328
}
329