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