Complex classes like SugarApplicationTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SugarApplicationTest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 4 | class SugarApplicationTest extends PHPUnit_Framework_TestCase |
||
| 5 | { |
||
| 6 | |||
| 7 | |||
| 8 | public function testexecute(){ |
||
| 9 | |||
| 10 | $SugarApplication = new SugarApplication(); |
||
| 11 | |||
| 12 | //execute the method and test if it works and does not throws an exception other than headers output exception. |
||
| 13 | try { |
||
| 14 | $SugarApplication->execute(); |
||
| 15 | |||
| 16 | } catch (Exception $e) { |
||
| 17 | $this->assertStringStartsWith('Cannot modify header information',$e->getMessage()); |
||
| 18 | } |
||
| 19 | |||
| 20 | } |
||
| 21 | |||
| 22 | |||
| 23 | public function testloadUser(){ |
||
| 24 | |||
| 25 | //cannot test this method as it uses die which stops execution of php unit as well |
||
| 26 | /* |
||
| 27 | error_reporting(E_ERROR | E_PARSE); |
||
| 28 | |||
| 29 | $SugarApplication = new SugarApplication(); |
||
| 30 | $SugarApplication->controller = new SugarController(); |
||
| 31 | |||
| 32 | try { |
||
| 33 | $SugarApplication->loadUser(); |
||
| 34 | } |
||
| 35 | catch (Exception $e) { |
||
| 36 | $this->fail(); |
||
| 37 | } |
||
| 38 | |||
| 39 | $this->assertTrue(TRUE); |
||
| 40 | */ |
||
| 41 | $this->markTestIncomplete('Can Not be implemented'); |
||
| 42 | |||
| 43 | } |
||
| 44 | |||
| 45 | public function testACLFilter(){ |
||
| 46 | |||
| 47 | $SugarApplication = new SugarApplication(); |
||
| 48 | |||
| 49 | //execute the method and test if it works and does not throws an exception. |
||
| 50 | try { |
||
| 51 | $SugarApplication->ACLFilter(); |
||
| 52 | } |
||
| 53 | catch (Exception $e) { |
||
| 54 | $this->fail(); |
||
| 55 | } |
||
| 56 | |||
| 57 | $this->assertTrue(TRUE); |
||
| 58 | |||
| 59 | } |
||
| 60 | |||
| 61 | |||
| 62 | public function testsetupResourceManagement() { |
||
| 63 | |||
| 64 | |||
| 65 | $SugarApplication = new SugarApplication(); |
||
| 66 | |||
| 67 | //execute the method with invalid input and test if it works and does not throws an exception. |
||
| 68 | try { |
||
| 69 | $SugarApplication->setupResourceManagement(''); |
||
| 70 | } |
||
| 71 | catch (Exception $e) { |
||
| 72 | $this->fail(); |
||
| 73 | } |
||
| 74 | |||
| 75 | //execute the method with valid input and test if it works and does not throws an exception. |
||
| 76 | try { |
||
| 77 | $SugarApplication->setupResourceManagement('Users'); |
||
| 78 | } |
||
| 79 | catch (Exception $e) { |
||
| 80 | $this->fail(); |
||
| 81 | } |
||
| 82 | |||
| 83 | $this->assertTrue(TRUE); |
||
| 84 | |||
| 85 | } |
||
| 86 | |||
| 87 | public function testsetupPrint() { |
||
| 88 | |||
| 89 | $SugarApplication = new SugarApplication(); |
||
| 90 | |||
| 91 | //execute the method and test if it works and does not throws an exception. |
||
| 92 | try { |
||
| 93 | $SugarApplication->setupPrint(); |
||
| 94 | } |
||
| 95 | catch (Exception $e) { |
||
| 96 | $this->fail(); |
||
| 97 | } |
||
| 98 | |||
| 99 | $this->assertTrue(TRUE); |
||
| 100 | |||
| 101 | } |
||
| 102 | |||
| 103 | public function testpreProcess(){ |
||
| 104 | |||
| 105 | $SugarApplication = new SugarApplication(); |
||
| 106 | $SugarApplication->controller = new SugarController(); |
||
| 107 | |||
| 108 | //execute the method and test if it works and does not throws an exception. |
||
| 109 | try { |
||
| 110 | $SugarApplication->preProcess(); |
||
| 111 | } |
||
| 112 | catch (Exception $e) { |
||
| 113 | $this->fail(); |
||
| 114 | } |
||
| 115 | |||
| 116 | $this->assertTrue(TRUE); |
||
| 117 | |||
| 118 | } |
||
| 119 | |||
| 120 | |||
| 121 | public function testhandleOfflineClient(){ |
||
| 122 | $SugarApplication = new SugarApplication(); |
||
| 123 | |||
| 124 | //execute the method and test if it works and does not throws an exception. |
||
| 125 | try { |
||
| 126 | $SugarApplication->handleOfflineClient(); |
||
| 127 | } |
||
| 128 | catch (Exception $e) { |
||
| 129 | $this->fail(); |
||
| 130 | } |
||
| 131 | |||
| 132 | $this->assertTrue(TRUE); |
||
| 133 | } |
||
| 134 | |||
| 135 | |||
| 136 | public function testhandleAccessControl(){ |
||
| 137 | |||
| 138 | $SugarApplication = new SugarApplication(); |
||
| 139 | $SugarApplication->controller = new SugarController(); |
||
| 140 | |||
| 141 | $result = $SugarApplication->handleAccessControl(); |
||
| 142 | |||
| 143 | //check that it returns Null |
||
| 144 | $this->assertEquals(NULL,$result); |
||
| 145 | |||
| 146 | //check that controller->hasAccess is true i-e default setting. |
||
| 147 | $this->assertEquals(True,$SugarApplication->controller->hasAccess); |
||
| 148 | |||
| 149 | } |
||
| 150 | |||
| 151 | |||
| 152 | public function testpreLoadLanguages() |
||
| 153 | { |
||
| 154 | try { |
||
| 155 | SugarApplication::preLoadLanguages(); |
||
| 156 | |||
| 157 | //check that method call got the current_language global variable set. |
||
| 158 | $this->assertTrue(isset($GLOBALS['current_language'] )); |
||
| 159 | |||
| 160 | //check that method call got the app_strings global variable set. |
||
| 161 | $this->assertTrue(is_array($GLOBALS['app_strings']) && count($GLOBALS['app_strings']) > 0 ); |
||
| 162 | |||
| 163 | } |
||
| 164 | catch (Exception $e) { |
||
| 165 | $this->fail(); |
||
| 166 | } |
||
| 167 | |||
| 168 | } |
||
| 169 | |||
| 170 | public function testloadLanguages(){ |
||
| 171 | |||
| 172 | $SugarApplication = new SugarApplication(); |
||
| 173 | $SugarApplication->controller = new SugarController(); |
||
| 174 | |||
| 175 | try { |
||
| 176 | $SugarApplication->loadLanguages(); |
||
| 177 | |||
| 178 | //check that method call got the current_language global variable set. |
||
| 179 | $this->assertTrue(isset($GLOBALS['current_language'] )); |
||
| 180 | |||
| 181 | //check that method call got the app_strings global variable set. |
||
| 182 | $this->assertTrue(is_array($GLOBALS['app_strings']) && count($GLOBALS['app_strings']) > 0 ); |
||
| 183 | |||
| 184 | //check that method call got the app_list_strings global variable set. |
||
| 185 | $this->assertTrue(is_array($GLOBALS['app_list_strings']) && count($GLOBALS['app_list_strings']) > 0 ); |
||
| 186 | |||
| 187 | //check that method call got the mod_strings global variable set. |
||
| 188 | $this->assertTrue(is_array($GLOBALS['mod_strings']) && count($GLOBALS['mod_strings']) > 0 ); |
||
| 189 | |||
| 190 | } |
||
| 191 | catch (Exception $e) { |
||
| 192 | $this->fail(); |
||
| 193 | } |
||
| 194 | |||
| 195 | } |
||
| 196 | |||
| 197 | public function testcheckDatabaseVersion() |
||
| 198 | { |
||
| 199 | error_reporting(E_ERROR | E_PARSE); |
||
| 200 | |||
| 201 | $SugarApplication = new SugarApplication(); |
||
| 202 | |||
| 203 | //execute the method with false parameter and check for false returned as it cannot connect to DB. |
||
| 204 | //testing with true will allow it to use die() which stops phpunit execution as well. |
||
| 205 | $result = $SugarApplication->checkDatabaseVersion(false); |
||
| 206 | $this->assertFalse($result); |
||
| 207 | |||
| 208 | } |
||
| 209 | |||
| 210 | |||
| 211 | public function testloadDisplaySettings(){ |
||
| 212 | |||
| 213 | $SugarApplication = new SugarApplication(); |
||
| 214 | |||
| 215 | //execute the method and test if it works and does not throws an exception. |
||
| 216 | try { |
||
| 217 | $SugarApplication->loadDisplaySettings(); |
||
| 218 | } |
||
| 219 | catch (Exception $e) { |
||
| 220 | $this->fail(); |
||
| 221 | } |
||
| 222 | |||
| 223 | $this->assertTrue(TRUE); |
||
| 224 | |||
| 225 | } |
||
| 226 | |||
| 227 | public function testloadLicense(){ |
||
| 228 | |||
| 229 | $SugarApplication = new SugarApplication(); |
||
| 230 | |||
| 231 | //execute the method and test if it works and does not throws an exception. |
||
| 232 | try { |
||
| 233 | $SugarApplication->loadLicense(); |
||
| 234 | |||
| 235 | } |
||
| 236 | catch (Exception $e) { |
||
| 237 | $this->fail(); |
||
| 238 | } |
||
| 239 | |||
| 240 | $this->assertTrue(TRUE); |
||
| 241 | |||
| 242 | } |
||
| 243 | |||
| 244 | public function testloadGlobals(){ |
||
| 245 | |||
| 246 | $SugarApplication = new SugarApplication(); |
||
| 247 | $SugarApplication->controller = new SugarController(); |
||
| 248 | |||
| 249 | //execute the method and test if it works and does not throws an exception. |
||
| 250 | try { |
||
| 251 | $SugarApplication->loadGlobals(); |
||
| 252 | |||
| 253 | } |
||
| 254 | catch (Exception $e) { |
||
| 255 | $this->fail(); |
||
| 256 | } |
||
| 257 | |||
| 258 | $this->assertTrue(TRUE); |
||
| 259 | |||
| 260 | } |
||
| 261 | |||
| 262 | |||
| 263 | public function teststartSession(){ |
||
| 264 | |||
| 265 | $SugarApplication = new SugarApplication(); |
||
| 266 | $SugarApplication->controller = new SugarController(); |
||
| 267 | |||
| 268 | //execute the method and test if it works and does not throws an exception. |
||
| 269 | try { |
||
| 270 | $SugarApplication->startSession(); |
||
| 271 | } |
||
| 272 | catch (Exception $e) { |
||
| 273 | $this->fail(); |
||
| 274 | } |
||
| 275 | |||
| 276 | $this->assertTrue(TRUE); |
||
| 277 | |||
| 278 | |||
| 279 | } |
||
| 280 | |||
| 281 | |||
| 282 | public function testendSession(){ |
||
| 283 | |||
| 284 | |||
| 285 | $SugarApplication = new SugarApplication(); |
||
| 286 | $SugarApplication->controller = new SugarController(); |
||
| 287 | |||
| 288 | //execute the method and test if it works and does not throws an exception. |
||
| 289 | try { |
||
| 290 | $SugarApplication->endSession(); |
||
| 291 | } |
||
| 292 | catch (Exception $e) { |
||
| 293 | $this->fail(); |
||
| 294 | } |
||
| 295 | |||
| 296 | $this->assertTrue(TRUE); |
||
| 297 | |||
| 298 | } |
||
| 299 | |||
| 300 | public function testredirect() |
||
| 301 | { |
||
| 302 | //this method uses exit() which stops execution of phpunit as well so it cannot be tested without additional --process-isolation commandline parameter. |
||
| 303 | /* |
||
| 304 | $SugarApplication = new SugarApplication(); |
||
| 305 | |||
| 306 | //execute the method and check if it works and doesn't throws an exception |
||
| 307 | try { |
||
| 308 | ob_start(); |
||
| 309 | |||
| 310 | $SugarApplication->redirect(); |
||
| 311 | |||
| 312 | $renderedContent = ob_get_contents(); |
||
| 313 | ob_end_clean(); |
||
| 314 | $this->assertGreaterThan(0,strlen($renderedContent)); |
||
| 315 | |||
| 316 | } catch (Exception $e) { |
||
| 317 | $this->fail(); |
||
| 318 | } |
||
| 319 | */ |
||
| 320 | $this->markTestIncomplete('Can Not be implemented'); |
||
| 321 | |||
| 322 | } |
||
| 323 | |||
| 324 | |||
| 325 | public function testappendErrorMessage() |
||
| 326 | { |
||
| 327 | //execute the method and check that the method adds the message to user_error_message array. |
||
| 328 | //there should be one more array element after method execution. |
||
| 329 | $user_error_message_count = count($_SESSION['user_error_message']); |
||
| 330 | SugarApplication::appendErrorMessage('some error'); |
||
| 331 | $this->assertGreaterThan($user_error_message_count , count($_SESSION['user_error_message'])); |
||
| 332 | } |
||
| 333 | |||
| 334 | public function testgetErrorMessages() |
||
| 335 | { |
||
| 336 | //execute the method and check if it returns a array. |
||
| 337 | $errorMessages = SugarApplication::getErrorMessages(); |
||
| 338 | $this->assertTrue(is_array($errorMessages)); |
||
| 339 | |||
| 340 | } |
||
| 341 | |||
| 342 | |||
| 343 | public function testsetCookie( ) |
||
| 344 | { |
||
| 345 | //execute the method and check that the method adds the key value pair to cookies array. |
||
| 346 | SugarApplication::setCookie('key','value'); |
||
| 347 | $this->assertEquals('value' , $_COOKIE['key'] ); |
||
| 348 | |||
| 349 | } |
||
| 350 | |||
| 351 | |||
| 352 | public function testcreateLoginVars() |
||
| 353 | { |
||
| 354 | |||
| 355 | $SugarApplication = new SugarApplication(); |
||
| 356 | |||
| 357 | //execute the method and test if it works and does not throws an exception. |
||
| 358 | try { |
||
| 359 | $vars = $SugarApplication->createLoginVars(); |
||
| 360 | } |
||
| 361 | catch (Exception $e) { |
||
| 362 | $this->fail(); |
||
| 363 | } |
||
| 364 | |||
| 365 | $this->assertTrue(TRUE); |
||
| 366 | |||
| 367 | } |
||
| 368 | |||
| 369 | |||
| 370 | public function testgetLoginVars() |
||
| 371 | { |
||
| 372 | $SugarApplication = new SugarApplication(); |
||
| 373 | |||
| 374 | //execute the method and test that it returns a array. |
||
| 375 | $vars = $SugarApplication->getLoginVars(); |
||
| 376 | $this->assertTrue(is_array($vars)); |
||
| 377 | |||
| 378 | } |
||
| 379 | |||
| 380 | |||
| 381 | public function testgetLoginRedirect() |
||
| 382 | { |
||
| 383 | |||
| 384 | $SugarApplication = new SugarApplication(); |
||
| 385 | |||
| 386 | //execute the method and test that it returns a plus length string |
||
| 387 | $redirect = $SugarApplication->getLoginRedirect(); |
||
| 388 | $this->assertGreaterThan(0, strlen($redirect)); |
||
| 389 | |||
| 390 | } |
||
| 391 | } |
||
| 392 |