Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 3 | class DMSDocumentSetTest extends SapphireTest |
||
|
|
|||
| 4 | { |
||
| 5 | protected static $fixture_file = 'dmstest.yml'; |
||
| 6 | |||
| 7 | /** |
||
| 8 | * Ensure that getDocuments is extensible |
||
| 9 | */ |
||
| 10 | View Code Duplication | public function testGetDocumentsIsExtensible() |
|
| 20 | |||
| 21 | /** |
||
| 22 | * Test that the GridField for documents isn't shown until you've saved the set |
||
| 23 | */ |
||
| 24 | public function testGridFieldShowsWhenSetIsSaved() |
||
| 42 | |||
| 43 | public function testRelations() |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Test that various components exist in the GridField config. See {@link DMSDocumentSet::getCMSFields} for context. |
||
| 60 | */ |
||
| 61 | public function testDocumentGridFieldConfig() |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Ensure that the display fields for the documents GridField can be returned |
||
| 88 | */ |
||
| 89 | public function testGetDocumentDisplayFields() |
||
| 90 | { |
||
| 91 | $document = $this->objFromFixture('DMSDocumentSet', 'ds1'); |
||
| 92 | $this->assertInternalType('array', $document->getDocumentDisplayFields()); |
||
| 93 | |||
| 94 | Config::inst()->update('DMSDocument', 'display_fields', array('apple' => 'Apple', 'orange' => 'Orange')); |
||
| 95 | $displayFields = $document->getDocumentDisplayFields(); |
||
| 96 | $this->assertContains('Apple', $displayFields); |
||
| 97 | $this->assertContains('Orange', $displayFields); |
||
| 98 | $this->assertArrayHasKey('ManuallyAdded', $displayFields); |
||
| 99 | $this->assertContains('Added', $displayFields); |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Tests to ensure that the callback for formatting ManuallyAdded will return a nice label for the user |
||
| 104 | */ |
||
| 105 | public function testNiceFormattingForManuallyAddedInGridField() |
||
| 106 | { |
||
| 107 | $fieldFormatting = $this->objFromFixture('DMSDocumentSet', 'ds1') |
||
| 108 | ->getCMSFields() |
||
| 109 | ->fieldByName('Root.Main.Documents') |
||
| 110 | ->getConfig() |
||
| 111 | ->getComponentByType('GridFieldDataColumns') |
||
| 112 | ->getFieldFormatting(); |
||
| 113 | |||
| 114 | $this->assertArrayHasKey('ManuallyAdded', $fieldFormatting); |
||
| 115 | $this->assertTrue(is_callable($fieldFormatting['ManuallyAdded'])); |
||
| 116 | |||
| 117 | $this->assertSame('Manually', $fieldFormatting['ManuallyAdded'](1)); |
||
| 118 | $this->assertSame('Query Builder', $fieldFormatting['ManuallyAdded'](0)); |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Test that query fields can be added to the gridfield |
||
| 123 | */ |
||
| 124 | public function testAddQueryFields() |
||
| 125 | { |
||
| 126 | /** @var DMSDocumentSet $set */ |
||
| 127 | $set = $this->objFromFixture('DMSDocumentSet', 'ds6'); |
||
| 128 | /** @var FieldList $fields */ |
||
| 129 | $fields = new FieldList(new TabSet('Root')); |
||
| 130 | /** @var FieldList $fields */ |
||
| 131 | $set->addQueryFields($fields); |
||
| 132 | $keyValuePairs = $fields->dataFieldByName('KeyValuePairs'); |
||
| 133 | $this->assertNotNull( |
||
| 134 | $keyValuePairs, |
||
| 135 | 'addQueryFields() includes KeyValuePairs composite field' |
||
| 136 | ); |
||
| 137 | $this->assertNotNull( |
||
| 138 | $keyValuePairs->fieldByName('KeyValuePairs[Title]'), |
||
| 139 | 'addQueryFields() includes KeyValuePairs composite field' |
||
| 140 | ); |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Ensure that the "direction" dropdown field has user friendly field labels |
||
| 145 | */ |
||
| 146 | public function testQueryBuilderDirectionFieldHasFriendlyLabels() |
||
| 147 | { |
||
| 148 | $fields = $this->objFromFixture('DMSDocumentSet', 'ds1')->getCMSFields(); |
||
| 149 | |||
| 150 | $dropdown = $fields->fieldByName('Root.QueryBuilder')->FieldList()->filterByCallback(function ($field) { |
||
| 151 | return $field instanceof FieldGroup; |
||
| 152 | })->first()->fieldByName('SortByDirection'); |
||
| 153 | |||
| 154 | $this->assertInstanceOf('DropdownField', $dropdown); |
||
| 155 | $source = $dropdown->getSource(); |
||
| 156 | $this->assertContains('Ascending', $source); |
||
| 157 | $this->assertContains('Descending', $source); |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Ensure that the configurable shortcode handler key is a hidden field in the CMS |
||
| 162 | */ |
||
| 163 | public function testShortcodeHandlerKeyFieldExists() |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Ensure that if the module is available, the orderable rows GridField component is added |
||
| 179 | */ |
||
| 180 | public function testDocumentsAreOrderable() |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Test that extra documents are added after write |
||
| 199 | */ |
||
| 200 | public function testSaveLinkedDocuments() |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Tests that an exception is thrown if no title entered for a DMSDocumentSet. |
||
| 214 | * @expectedException ValidationException |
||
| 215 | */ |
||
| 216 | public function testExceptionOnNoTitleGiven() |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Ensure that when editing in a page context that the "page" field is removed, or is labelled "Show on page" |
||
| 223 | * otherwise |
||
| 224 | */ |
||
| 225 | public function testPageFieldRemovedWhenEditingInPageContext() |
||
| 238 | } |
||
| 239 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.