| Conditions | 1 |
| Paths | 1 |
| Total Lines | 59 |
| Code Lines | 35 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 40 | public function testObsoleteValues() |
||
| 41 | { |
||
| 42 | $obj = new FieldType\DBEnumTestObject(); |
||
| 43 | $colourField = $obj->obj('Colour'); |
||
| 44 | $colourField->setTable('FieldType_DBEnumTestObject'); |
||
| 45 | |||
| 46 | // Test values prior to any database content |
||
| 47 | $this->assertEquals( |
||
| 48 | ['Red', 'Blue', 'Green'], |
||
| 49 | $colourField->getEnumObsolete() |
||
|
|
|||
| 50 | ); |
||
| 51 | |||
| 52 | // Test values with a record |
||
| 53 | $obj->Colour = 'Red'; |
||
| 54 | $obj->write(); |
||
| 55 | DBEnum::flushCache(); |
||
| 56 | |||
| 57 | $this->assertEquals( |
||
| 58 | ['Red', 'Blue', 'Green'], |
||
| 59 | $colourField->getEnumObsolete() |
||
| 60 | ); |
||
| 61 | |||
| 62 | // If the value is removed from the enum, obsolete content is still retained |
||
| 63 | $colourField->setEnum(['Blue', 'Green', 'Purple']); |
||
| 64 | DBEnum::flushCache(); |
||
| 65 | |||
| 66 | $this->assertEquals( |
||
| 67 | ['Blue', 'Green', 'Purple', 'Red'], // Red on the end now, because it's obsolete |
||
| 68 | $colourField->getEnumObsolete() |
||
| 69 | ); |
||
| 70 | |||
| 71 | // Check that old and new data is preserved after a schema update |
||
| 72 | DB::get_schema()->schemaUpdate(function () use ($colourField) { |
||
| 73 | $colourField->requireField(); |
||
| 74 | }); |
||
| 75 | |||
| 76 | $obj2 = new FieldType\DBEnumTestObject(); |
||
| 77 | $obj2->Colour = 'Purple'; |
||
| 78 | $obj2->write(); |
||
| 79 | |||
| 80 | $this->assertEquals( |
||
| 81 | ['Purple', 'Red'], |
||
| 82 | FieldType\DBEnumTestObject::get()->sort('Colour')->column('Colour') |
||
| 83 | ); |
||
| 84 | |||
| 85 | // Ensure that enum columns are retained |
||
| 86 | $colourField->setEnum(['Blue', 'Green']); |
||
| 87 | $this->assertEquals( |
||
| 88 | ['Blue', 'Green', 'Purple', 'Red'], |
||
| 89 | $colourField->getEnumObsolete() |
||
| 90 | ); |
||
| 91 | |||
| 92 | // If obsolete records are deleted, the extra values go away |
||
| 93 | $obj->delete(); |
||
| 94 | $obj2->delete(); |
||
| 95 | DBEnum::flushCache(); |
||
| 96 | $this->assertEquals( |
||
| 97 | ['Blue', 'Green'], |
||
| 98 | $colourField->getEnumObsolete() |
||
| 99 | ); |
||
| 102 |