| Conditions | 1 | 
| Paths | 1 | 
| Total Lines | 93 | 
| Code Lines | 65 | 
| 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 | ||
| 140 | public function testTreeSearch() | ||
| 141 |     { | ||
| 142 |         $field = new TreeDropdownField('TestTree', 'Test tree', Folder::class); | ||
| 143 | |||
| 144 | // case insensitive search against keyword 'sub' for folders | ||
| 145 |         $request = new HTTPRequest('GET', 'url', ['search'=>'sub']); | ||
| 146 | $request->setSession(new Session([])); | ||
| 147 | $response = $field->tree($request); | ||
| 148 | $tree = $response->getBody(); | ||
| 149 | |||
| 150 | $folder1 = $this->objFromFixture(Folder::class, 'folder1'); | ||
| 151 | $folder1Subfolder1 = $this->objFromFixture(Folder::class, 'folder1-subfolder1'); | ||
| 152 | |||
| 153 | $parser = new CSSContentParser($tree); | ||
| 154 | $cssPath = 'ul.tree li#selector-TestTree-' . $folder1->ID . ' li#selector-TestTree-' . $folder1Subfolder1->ID . ' a span.item'; | ||
| 155 | $firstResult = $parser->getBySelector($cssPath); | ||
| 156 | $this->assertEquals( | ||
| 157 | $folder1Subfolder1->Name, | ||
| 158 | (string)$firstResult[0], | ||
| 159 | $folder1Subfolder1->Name . ' is found, nested under ' . $folder1->Name | ||
| 160 | ); | ||
| 161 | |||
| 162 | $subfolder = $this->objFromFixture(Folder::class, 'subfolder'); | ||
| 163 | $cssPath = 'ul.tree li#selector-TestTree-' . $subfolder->ID . ' a span.item'; | ||
| 164 | $secondResult = $parser->getBySelector($cssPath); | ||
| 165 | $this->assertEquals( | ||
| 166 | $subfolder->Name, | ||
| 167 | (string)$secondResult[0], | ||
| 168 | $subfolder->Name . ' is found at root level' | ||
| 169 | ); | ||
| 170 | |||
| 171 | // other folders which don't contain the keyword 'sub' are not returned in search results | ||
| 172 | $folder2 = $this->objFromFixture(Folder::class, 'folder2'); | ||
| 173 | $cssPath = 'ul.tree li#selector-TestTree-' . $folder2->ID . ' a span.item'; | ||
| 174 | $noResult = $parser->getBySelector($cssPath); | ||
| 175 | $this->assertEmpty( | ||
| 176 | $noResult, | ||
| 177 | $folder2 . ' is not found' | ||
| 178 | ); | ||
| 179 | |||
| 180 |         $field = new TreeDropdownField('TestTree', 'Test tree', File::class); | ||
| 181 | |||
| 182 | // case insensitive search against keyword 'sub' for files | ||
| 183 |         $request = new HTTPRequest('GET', 'url', ['search'=>'sub']); | ||
| 184 | $request->setSession(new Session([])); | ||
| 185 | $response = $field->tree($request); | ||
| 186 | $tree = $response->getBody(); | ||
| 187 | |||
| 188 | $parser = new CSSContentParser($tree); | ||
| 189 | |||
| 190 | // Even if we used File as the source object, folders are still returned because Folder is a File | ||
| 191 | $cssPath = 'ul.tree li#selector-TestTree-' . $folder1->ID . ' li#selector-TestTree-' . $folder1Subfolder1->ID . ' a span.item'; | ||
| 192 | $firstResult = $parser->getBySelector($cssPath); | ||
| 193 | $this->assertEquals( | ||
| 194 | $folder1Subfolder1->Name, | ||
| 195 | (string)$firstResult[0], | ||
| 196 | $folder1Subfolder1->Name . ' is found, nested under ' . $folder1->Name | ||
| 197 | ); | ||
| 198 | |||
| 199 | // Looking for two files with 'sub' in their name, both under the same folder | ||
| 200 | $file1 = $this->objFromFixture(File::class, 'subfolderfile1'); | ||
| 201 | $file2 = $this->objFromFixture(File::class, 'subfolderfile2'); | ||
| 202 | $cssPath = 'ul.tree li#selector-TestTree-' . $subfolder->ID . ' li#selector-TestTree-' . $file1->ID . ' a'; | ||
| 203 | $firstResult = $parser->getBySelector($cssPath); | ||
| 204 | $this->assertNotEmpty( | ||
| 205 | $firstResult, | ||
| 206 | $file1->Name . ' with ID ' . $file1->ID . ' is in search results' | ||
| 207 | ); | ||
| 208 | $this->assertEquals( | ||
| 209 | $file1->Name, | ||
| 210 | (string)$firstResult[0], | ||
| 211 | $file1->Name . ' is found nested under ' . $subfolder->Name | ||
| 212 | ); | ||
| 213 | |||
| 214 | $cssPath = 'ul.tree li#selector-TestTree-' . $subfolder->ID . ' li#selector-TestTree-' . $file2->ID . ' a'; | ||
| 215 | $secondResult = $parser->getBySelector($cssPath); | ||
| 216 | $this->assertNotEmpty( | ||
| 217 | $secondResult, | ||
| 218 | $file2->Name . ' with ID ' . $file2->ID . ' is in search results' | ||
| 219 | ); | ||
| 220 | $this->assertEquals( | ||
| 221 | $file2->Name, | ||
| 222 | (string)$secondResult[0], | ||
| 223 | $file2->Name . ' is found nested under ' . $subfolder->Name | ||
| 224 | ); | ||
| 225 | |||
| 226 | // other files which don't include 'sub' are not returned in search results | ||
| 227 | $file3 = $this->objFromFixture(File::class, 'asdf'); | ||
| 228 | $cssPath = 'ul.tree li#selector-TestTree-' . $file3->ID; | ||
| 229 | $noResult = $parser->getBySelector($cssPath); | ||
| 230 | $this->assertEmpty( | ||
| 231 | $noResult, | ||
| 232 | $file3->Name . ' is not found' | ||
| 233 | ); | ||
| 270 |