Completed
Pull Request — master (#32)
by
unknown
13:02 queued 07:18
created

testAlterTableChangeQuotedColumn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Licensed to CRATE Technology GmbH("Crate") under one or more contributor
4
 * license agreements.  See the NOTICE file distributed with this work for
5
 * additional information regarding copyright ownership.  Crate licenses
6
 * this file to you under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.  You may
8
 * obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
15
 * License for the specific language governing permissions and limitations
16
 * under the License.
17
 *
18
 * However, if you have executed another commercial license agreement
19
 * with Crate these terms will supersede the license and you may use the
20
 * software solely pursuant to the terms of the relevant commercial agreement.
21
 */
22
23
namespace Crate\Test\DBAL\Platforms;
24
25
use Crate\DBAL\Platforms\CratePlatform;
26
use Crate\DBAL\Types\ArrayType;
27
use Crate\DBAL\Types\MapType;
28
use Doctrine\Common\EventManager;
29
use Doctrine\DBAL\DBALException;
30
use Doctrine\DBAL\Events;
31
use Doctrine\DBAL\Schema\Column;
32
use Doctrine\DBAL\Schema\Table;
33
use Doctrine\DBAL\Schema\TableDiff;
34
use Doctrine\DBAL\Types\Type;
35
use Doctrine\Tests\DBAL\Platforms\AbstractPlatformTestCase;
36
37
class CratePlatformTest extends AbstractPlatformTestCase {
38
39
    public function createPlatform()
40
    {
41
        return new CratePlatform();
42
    }
43
44
    public function getGenerateTableSql()
45
    {
46
        return 'CREATE TABLE test (id INTEGER, test STRING, PRIMARY KEY(id))';
47
    }
48
49
    public function getGenerateTableWithMultiColumnUniqueIndexSql()
50
    {
51
        return array(
52
                'CREATE TABLE test (foo STRING, bar STRING, ' .
53
                'INDEX UNIQ_D87F7E0C8C73652176FF8CAA USING FULLTEXT (foo, bar))'
54
        );
55
    }
56
57
    public function getGenerateIndexSql()
58
    {
59
        $this->markTestSkipped('Platform does not support CREATE INDEX.');
60
    }
61
62
    public function getGenerateUniqueIndexSql()
63
    {
64
        $this->markTestSkipped('Platform does not support CREATE UNIQUE INDEX.');
65
    }
66
67
    public function testGeneratesForeignKeyCreationSql()
68
    {
69
        $fk = new \Doctrine\DBAL\Schema\ForeignKeyConstraint(array('fk_name_id'), 'other_table', array('id'), '');
70
    
71
        $this->assertEquals(
72
            $this->getGenerateForeignKeySql(),
73
            $this->_platform->getCreateForeignKeySQL($fk, 'test')
74
        );
75
    }
76
    
77
    public function getGenerateForeignKeySql()
78
    {
79
        $this->markTestSkipped('Platform does not support ADD FOREIGN KEY.');
80
    }
81
82
    public function getGenerateAlterTableSql()
83
    {
84
        return array(
85
            'ALTER TABLE mytable ADD quota INTEGER',
86
        );
87
    }
88
    
89
    public function testAlterTableChangeQuotedColumn()
90
    {
91
        $this->markTestSkipped('Platform does not support ALTER TABLE.');
92
    }
93
94
    protected function getQuotedColumnInPrimaryKeySQL()
95
    {
96
        return array(
97
            'CREATE TABLE "quoted" ("create" STRING, PRIMARY KEY("create"))',
98
        );
99
    }
100
101
    protected function getQuotedColumnInIndexSQL()
102
    {
103
        return array(
104
            'CREATE TABLE "quoted" ("create" STRING, ' .
105
            'INDEX IDX_22660D028FD6E0FB USING FULLTEXT ("create")' .
106
            ')'
107
        );
108
    }
109
110
    protected function getQuotedNameInIndexSQL()
111
    {
112
        return array(
113
            'CREATE TABLE test (column1 STRING, INDEX key USING FULLTEXT (column1))'
114
        );
115
    }
116
117
    /**
118
     * @group DBAL-374
119
     */
120
    public function testQuotedColumnInForeignKeyPropagation()
121
    {
122
        $this->markTestSkipped('Platform does not support ADD FOREIGN KEY.');
123
    }
124
    
125
    protected function getQuotedColumnInForeignKeySQL() {}
126
    
127
    protected function getQuotesReservedKeywordInUniqueConstraintDeclarationSQL()
128
    {
129
        return 'CONSTRAINT "select" UNIQUE (foo)';
130
    }
131
    
132
    protected function getQuotesReservedKeywordInIndexDeclarationSQL()
133
    {
134
        return 'INDEX "select" USING FULLTEXT (foo)';
135
    }
136
    
137
    /**
138
     * @group DBAL-835
139
     */
140
    public function testQuotesAlterTableRenameColumn()
141
    {
142
        $this->markTestSkipped('Platform does not support ALTER TABLE.');
143
    }
144
    
145
    protected function getQuotedAlterTableRenameColumnSQL() {}
146
    
147
    /**
148
     * @group DBAL-835
149
     */
150
    public function testQuotesAlterTableChangeColumnLength()
151
    {
152
        $this->markTestSkipped('Platform does not support ALTER TABLE.');
153
    }
154
    
155
    protected function getQuotedAlterTableChangeColumnLengthSQL() {}
156
    
157
    /**
158
     * @group DBAL-807
159
     */
160
    public function testQuotesAlterTableRenameIndexInSchema()
161
    {
162
        $this->markTestSkipped('Platform does not support ALTER TABLE.');
163
    }
164
    
165
    protected function getCommentOnColumnSQL()
166
    {
167
        return array(
168
            "COMMENT ON COLUMN foo.bar IS 'comment'",
169
            "COMMENT ON COLUMN \"Foo\".\"BAR\" IS 'comment'",
170
            "COMMENT ON COLUMN \"select\".\"from\" IS 'comment'",
171
        );
172
    }
173
174
    /**
175
     * @group DBAL-1010
176
     */
177
    public function testGeneratesAlterTableRenameColumnSQL()
178
    {
179
        $this->markTestSkipped('Platform does not support ALTER TABLE.');
180
    }
181
    
182
    public function getAlterTableRenameColumnSQL() {}
183
    
184
    /**
185
     * @group DBAL-1016
186
     */
187
    public function testQuotesTableIdentifiersInAlterTableSQL()
188
    {
189
        $this->markTestSkipped('Platform does not support ALTER TABLE.');
190
    }
191
    
192
    protected function getQuotesTableIdentifiersInAlterTableSQL() {}
193
    
194
    /**
195
     * @group DBAL-1062
196
     */
197
    public function testGeneratesAlterTableRenameIndexUsedByForeignKeySQL()
198
    {
199
        $this->markTestSkipped('Platform does not support ALTER TABLE.');
200
    }
201
    
202
    protected function getGeneratesAlterTableRenameIndexUsedByForeignKeySQL() {}
203
    
204
    /**
205
     * @group DBAL-1090
206
     */
207
    public function testAlterStringToFixedString()
208
    {
209
        $this->markTestSkipped('Platform does not support ALTER TABLE.');
210
    }
211
    
212
    protected function getAlterStringToFixedStringSQL() {}
213
    
214
    public function testGenerateSubstrExpression()
215
    {
216
        $this->assertEquals($this->_platform->getSubstringExpression('col', 0), "SUBSTR(col, 0)");
217
        $this->assertEquals($this->_platform->getSubstringExpression('col', 1, 2), "SUBSTR(col, 1, 2)");
218
    }
219
220
    /**
221
     * @expectedException \Doctrine\DBAL\DBALException
222
     * @expectedExceptionMessage Operation 'Crate\DBAL\Platforms\CratePlatform::getNowExpression' is not supported by platform.
223
     */
224
    public function testGenerateNowExpression()
225
    {
226
        $this->_platform->getNowExpression();
227
    }
228
229
    public function testGenerateRegexExpression()
230
    {
231
        $this->assertEquals($this->_platform->getRegexpExpression(), "LIKE");
232
    }
233
234
    /**
235
     * @expectedException \Doctrine\DBAL\DBALException
236
     * @expectedExceptionMessage Operation 'Crate\DBAL\Platforms\CratePlatform::getDateDiffExpression' is not supported by platform.
237
     */
238
    public function testGenerateDateDiffExpression()
239
    {
240
        $this->_platform->getDateDiffExpression('2014-10-10 10:10:10', '2014-10-20 20:20:20');
241
    }
242
243
    /**
244
     * @expectedException \Doctrine\DBAL\DBALException
245
     * @expectedExceptionMessage Operation 'Crate\DBAL\Platforms\CratePlatform::getCreateDatabaseSQL' is not supported by platform.
246
     */
247
    public function testCreateDatabases()
248
    {
249
        $this->_platform->getCreateDatabaseSQL('foo');
250
    }
251
252
    /**
253
     * @expectedException \Doctrine\DBAL\DBALException
254
     * @expectedExceptionMessage Operation 'Crate\DBAL\Platforms\CratePlatform::getListDatabasesSQL' is not supported by platform.
255
     */
256
    public function testListDatabases()
257
    {
258
        $this->_platform->getListDatabasesSQL();
259
    }
260
261
    /**
262
     * @expectedException \Doctrine\DBAL\DBALException
263
     * @expectedExceptionMessage Operation 'Crate\DBAL\Platforms\CratePlatform::getDropDatabaseSQL' is not supported by platform.
264
     */
265
    public function testDropDatabases()
266
    {
267
        $this->_platform->getDropDatabaseSQL('foo');
268
    }
269
270
    /**
271
     * @expectedException \Doctrine\DBAL\DBALException
272
     * @expectedExceptionMessage Operation 'Crate\DBAL\Platforms\CratePlatform::getBlobTypeDeclarationSQL' is not supported by platform.
273
     */
274
    public function testGenerateBlobTypeGeneration()
275
    {
276
        $this->_platform->getBlobTypeDeclarationSQL(array());
277
    }
278
279
    /**
280
     * @expectedException \Doctrine\DBAL\DBALException
281
     */
282
    public function testTruncateTableSQL()
283
    {
284
        $this->_platform->getTruncateTableSQL('foo');
285
    }
286
287
    /**
288
     * @expectedException \Doctrine\DBAL\DBALException
289
     */
290
    public function testReadLockSQL()
291
    {
292
        $this->_platform->getReadLockSQL();
293
    }
294
295
    public function testConvertBooleans()
296
    {
297
        $this->assertEquals($this->_platform->convertBooleans(false), 'false');
298
        $this->assertEquals($this->_platform->convertBooleans(true), 'true');
299
300
        $this->assertEquals($this->_platform->convertBooleans(0), 'false');
301
        $this->assertEquals($this->_platform->convertBooleans(1), 'true');
302
303
        $this->assertEquals($this->_platform->convertBooleans(array(true, 1, false, 0)),
304
            array('true', 'true', 'false', 'false'));
305
    }
306
307
    public function testSQLResultCasting()
308
    {
309
        $this->assertEquals($this->_platform->getSQLResultCasing("LoWeRcAsE"), 'lowercase');
310
    }
311
312
    /**
313
     * @expectedException \Doctrine\DBAL\DBALException
314
     * @expectedExceptionMessage No columns specified for table foo
315
     */
316
    public function testGenerateTableSqlWithoutColumns()
317
    {
318
        $table = new Table("foo");
319
        $this->assertEquals($this->_platform->getCreateTableSQL($table)[0],
320
            'CREATE TABLE foo');
321
    }
322
323
    public function testGenerateTableSql()
324
    {
325
        $table = new Table("foo");
326
        $table->addColumn('col_bool', 'boolean');
327
        $table->addColumn('col_int', 'integer');
328
        $table->addColumn('col_float', 'float');
329
        $table->addColumn('col_timestamp', 'timestamp');
330
        $table->addColumn('col_datetimetz', 'datetimetz');
331
        $table->addColumn('col_datetime', 'datetime');
332
        $table->addColumn('col_date', 'date');
333
        $table->addColumn('col_time', 'time');
334
        $table->addColumn('col_array', 'array');
335
        $table->addColumn('col_object', 'map');
336
        $this->assertEquals($this->_platform->getCreateTableSQL($table)[0],
337
            'CREATE TABLE foo (col_bool BOOLEAN, col_int INTEGER, col_float DOUBLE, col_timestamp TIMESTAMP, col_datetimetz TIMESTAMP, col_datetime TIMESTAMP, col_date TIMESTAMP, col_time TIMESTAMP, col_array ARRAY ( STRING ), col_object OBJECT ( dynamic ))');
338
    }
339
340
    public function testUnsupportedUniqueIndexConstraint()
341
    {
342
        $this->setExpectedException(DBALException::class, "Unique constraints are not supported. Use `primary key` instead");
343
344
        $table = new Table("foo");
345
        $table->addColumn("unique_string", "string");
346
        $table->addUniqueIndex(array("unique_string"));
347
        $this->_platform->getCreateTableSQL($table);
348
    }
349
350
    public function testUniqueConstraintInCustomSchemaOptions()
351
    {
352
        $this->setExpectedException(DBALException::class, "Unique constraints are not supported. Use `primary key` instead");
353
354
        $table = new Table("foo");
355
        $table->addColumn("unique_string", "string")->setCustomSchemaOption("unique", true);
356
        $this->_platform->getCreateTableSQL($table);
357
    }
358
359
    public function testGeneratesTableAlterationSql()
360
    {
361
        $expectedSql = $this->getGenerateAlterTableSql();
362
363
        $tableDiff = new TableDiff('mytable');
364
        $tableDiff->addedColumns['quota'] = new \Doctrine\DBAL\Schema\Column('quota', \Doctrine\DBAL\Types\Type::getType('integer'), array('notnull' => false));
365
366
        $sql = $this->_platform->getAlterTableSQL($tableDiff);
367
368
        $this->assertEquals($expectedSql, $sql);
369
    }
370
371
    public function testGetAlterTableSqlDispatchEvent()
372
    {
373
        $events = array(
374
            'onSchemaAlterTableAddColumn'
375
        );
376
377
        $listenerMock = $this->getMock('GetAlterTableSqlDispatchEvenListener', $events);
378
        $listenerMock
379
            ->expects($this->once())
380
            ->method('onSchemaAlterTableAddColumn');
381
382
        $eventManager = new EventManager();
383
        $events = array(
384
            Events::onSchemaAlterTableAddColumn,
385
        );
386
        $eventManager->addEventListener($events, $listenerMock);
387
388
        $this->_platform->setEventManager($eventManager);
389
390
        $tableDiff = new TableDiff('mytable');
391
        $tableDiff->addedColumns['added'] = new \Doctrine\DBAL\Schema\Column('added', \Doctrine\DBAL\Types\Type::getType('integer'), array());
392
393
        $this->_platform->getAlterTableSQL($tableDiff);
394
    }
395
396
    public function testGenerateTableWithMultiColumnUniqueIndex()
397
    {
398
        $this->markTestSkipped("Custom index creation currently not supported");
399
400
        $table = new Table('test');
401
        $table->addColumn('foo', 'string', array('notnull' => false, 'length' => 255));
402
        $table->addColumn('bar', 'string', array('notnull' => false, 'length' => 255));
403
        $table->addUniqueIndex(array("foo", "bar"));
404
405
        $sql = $this->_platform->getCreateTableSQL($table);
406
        $this->assertEquals($this->getGenerateTableWithMultiColumnUniqueIndexSql(), $sql);
407
    }
408
409
    /**
410
     * @param Column $column
411
     */
412
    private function getSQLDeclaration($column)
413
    {
414
        $p = $this->_platform;
415
        return $p->getColumnDeclarationSQL($column->getName(), $p->prepareColumnData($column));
416
    }
417
418
    public function testGenerateObjectSQLDeclaration()
419
    {
420
421
        $column = new Column('obj', Type::getType(MapType::NAME));
422
        $this->assertEquals($this->getSQLDeclaration($column), 'obj OBJECT ( dynamic )');
423
424
        $column = new Column('obj', Type::getType(MapType::NAME),
425
            array('platformOptions'=>array('type'=>MapType::STRICT)));
426
        $this->assertEquals($this->getSQLDeclaration($column), 'obj OBJECT ( strict )');
427
428
        $column = new Column('obj', Type::getType(MapType::NAME),
429
            array('platformOptions'=>array('type'=>MapType::IGNORED, 'fields'=>array())));
430
        $this->assertEquals($this->getSQLDeclaration($column), 'obj OBJECT ( ignored )');
431
432
        $column = new Column('obj', Type::getType(MapType::NAME),
433
            array('platformOptions'=>array(
434
                'type'=>MapType::STRICT,
435
                'fields'=>array(
436
                    new Column('num', Type::getType(Type::INTEGER)),
437
                    new Column('text', Type::getType(Type::STRING)),
438
                    new Column('arr', Type::getType(ArrayType::NAME)),
439
                    new Column('obj', Type::getType(MapType::NAME)),
440
                ),
441
            )));
442
        $this->assertEquals($this->getSQLDeclaration($column), 'obj OBJECT ( strict ) AS ( num INTEGER, text STRING, arr ARRAY ( STRING ), obj OBJECT ( dynamic ) )');
443
444
    }
445
446
    public function testGenerateArraySQLDeclaration()
447
    {
448
        $column = new Column('arr', Type::getType(ArrayType::NAME));
449
        $this->assertEquals($this->getSQLDeclaration($column), 'arr ARRAY ( STRING )');
450
451
        $column = new Column('arr', Type::getType(ArrayType::NAME),
452
            array('platformOptions'=> array('type'=>Type::INTEGER)));
453
        $this->assertEquals($this->getSQLDeclaration($column), 'arr ARRAY ( INTEGER )');
454
455
    }
456
457
    public function testPlatformSupport() {
458
        $this->assertFalse($this->_platform->supportsSequences());
459
        $this->assertFalse($this->_platform->supportsSchemas());
460
        $this->assertTrue($this->_platform->supportsIdentityColumns());
461
        $this->assertFalse($this->_platform->supportsIndexes());
462
        $this->assertFalse($this->_platform->supportsCommentOnStatement());
463
        $this->assertFalse($this->_platform->supportsForeignKeyConstraints());
464
        $this->assertFalse($this->_platform->supportsForeignKeyOnUpdate());
465
        $this->assertFalse($this->_platform->supportsViews());
466
        $this->assertFalse($this->_platform->prefersSequences());
467
    }
468
469
    /**
470
     * @return string
471
     */
472
    protected function getQuotesReservedKeywordInTruncateTableSQL()
473
    {
474
        $this->markTestSkipped('Platform does not support TRUNCATE TABLE.');
475
    }
476
}
477