Completed
Pull Request — master (#28)
by
unknown
19:12 queued 09:24
created

CratePlatformTest   B

Complexity

Total Complexity 52

Size/Duplication

Total Lines 421
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 5
Bugs 0 Features 2
Metric Value
wmc 52
lcom 0
cbo 7
dl 0
loc 421
rs 7.9487
c 5
b 0
f 2

52 Methods

Rating   Name   Duplication   Size   Complexity  
A testGenerateNowExpression() 0 4 1
A testGenerateRegexExpression() 0 4 1
A testGenerateDateDiffExpression() 0 4 1
A testCreateDatabases() 0 4 1
A testListDatabases() 0 4 1
A testDropDatabases() 0 4 1
A testGenerateBlobTypeGeneration() 0 4 1
A testTruncateTableSQL() 0 4 1
A testReadLockSQL() 0 4 1
A testSQLResultCasting() 0 4 1
A testConvertBooleans() 0 11 1
A testGenerateTableWithMultiColumnUniqueIndex() 0 12 1
A getSQLDeclaration() 0 5 1
B testGenerateObjectSQLDeclaration() 0 27 1
A testGenerateArraySQLDeclaration() 0 10 1
A createPlatform() 0 4 1
A getGenerateTableSql() 0 4 1
A getGenerateTableWithMultiColumnUniqueIndexSql() 0 7 1
A getGenerateIndexSql() 0 4 1
A getGenerateUniqueIndexSql() 0 4 1
A testGeneratesForeignKeyCreationSql() 0 9 1
A getGenerateForeignKeySql() 0 4 1
A getGenerateAlterTableSql() 0 6 1
A testAlterTableChangeQuotedColumn() 0 4 1
A getQuotedColumnInPrimaryKeySQL() 0 6 1
A getQuotedColumnInIndexSQL() 0 8 1
A getQuotedNameInIndexSQL() 0 6 1
A testQuotedColumnInForeignKeyPropagation() 0 4 1
A getQuotedColumnInForeignKeySQL() 0 1 1
A getQuotesReservedKeywordInUniqueConstraintDeclarationSQL() 0 4 1
A getQuotesReservedKeywordInIndexDeclarationSQL() 0 4 1
A testQuotesAlterTableRenameColumn() 0 4 1
A getQuotedAlterTableRenameColumnSQL() 0 1 1
A testQuotesAlterTableChangeColumnLength() 0 4 1
A getQuotedAlterTableChangeColumnLengthSQL() 0 1 1
A testQuotesAlterTableRenameIndexInSchema() 0 4 1
A getCommentOnColumnSQL() 0 8 1
A testGeneratesAlterTableRenameColumnSQL() 0 4 1
A getAlterTableRenameColumnSQL() 0 1 1
A testQuotesTableIdentifiersInAlterTableSQL() 0 4 1
A getQuotesTableIdentifiersInAlterTableSQL() 0 1 1
A testGeneratesAlterTableRenameIndexUsedByForeignKeySQL() 0 4 1
A getGeneratesAlterTableRenameIndexUsedByForeignKeySQL() 0 1 1
A testAlterStringToFixedString() 0 4 1
A getAlterStringToFixedStringSQL() 0 1 1
A testGenerateSubstrExpression() 0 5 1
A testGenerateTableSqlWithoutColumns() 0 6 1
A testGenerateTableSql() 0 16 1
A testGeneratesTableAlterationSql() 0 11 1
B testGetAlterTableSqlDispatchEvent() 0 24 1
A testPlatformSupport() 0 11 1
A getQuotesReservedKeywordInTruncateTableSQL() 0 4 1

How to fix   Complexity   

Complex Class

Complex classes like CratePlatformTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use CratePlatformTest, and based on these observations, apply Extract Interface, too.

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