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 |
||
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() |
||
227 | |||
228 | public function testGenerateRegexExpression() |
||
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() |
||
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() |
||
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() |
||
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() |
||
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() |
||
277 | |||
278 | /** |
||
279 | * @expectedException \Doctrine\DBAL\DBALException |
||
280 | */ |
||
281 | public function testTruncateTableSQL() |
||
285 | |||
286 | /** |
||
287 | * @expectedException \Doctrine\DBAL\DBALException |
||
288 | */ |
||
289 | public function testReadLockSQL() |
||
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() |
||
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() |
||
388 | |||
389 | /** |
||
390 | * @param Column $column |
||
391 | */ |
||
392 | private function getSQLDeclaration($column) |
||
397 | |||
398 | public function testGenerateObjectSQLDeclaration() |
||
425 | |||
426 | public function testGenerateArraySQLDeclaration() |
||
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 |