Passed
Pull Request — main (#109)
by Andreas
07:41
created

CratePlatformTest   C

Complexity

Total Complexity 57

Size/Duplication

Total Lines 457
Duplicated Lines 0 %

Importance

Changes 13
Bugs 1 Features 2
Metric Value
wmc 57
eloc 167
c 13
b 1
f 2
dl 0
loc 457
rs 5.04

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.

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