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