| Conditions | 1 |
| Paths | 1 |
| Total Lines | 112 |
| Code Lines | 72 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 236 | public function testFromAModelWithExtraFields() |
||
| 237 | { |
||
| 238 | // create a model session |
||
| 239 | $modelSessionId = SessionManager::create_session( |
||
| 240 | 'Model session'.time(), |
||
| 241 | '2019-01-01 00:00', |
||
| 242 | '2019-08-31 00:00', |
||
| 243 | '2019-01-01 00:00', |
||
| 244 | '2019-08-31 00:00', |
||
| 245 | '2019-01-01 00:00', |
||
| 246 | '2019-08-31 00:00', |
||
| 247 | null, |
||
| 248 | null |
||
| 249 | ); |
||
| 250 | |||
| 251 | // create an extra field and set its value in the model session |
||
| 252 | // the new session will be given a different value in this field |
||
| 253 | $extraFieldModel = new ExtraField('session'); |
||
| 254 | $firstExtraFieldName = 'extraField'.time(); |
||
| 255 | $firstExtraFieldNameForModelSession = 'extra field value for model'; |
||
| 256 | $firstExtraFieldNameForNewSession = 'extra field value for new session'; |
||
| 257 | $firstExtraFieldId = $extraFieldModel->save( |
||
| 258 | [ |
||
| 259 | 'field_type' => ExtraField::FIELD_TYPE_TEXT, |
||
| 260 | 'variable' => $firstExtraFieldName, |
||
| 261 | 'display_text' => $firstExtraFieldName, |
||
| 262 | 'visible_to_self' => 1, |
||
| 263 | 'visible_to_others' => 1, |
||
| 264 | 'changeable' => 1, |
||
| 265 | 'filter' => 1, |
||
| 266 | ] |
||
| 267 | ); |
||
| 268 | SessionManager::update_session_extra_field_value( |
||
| 269 | $modelSessionId, |
||
| 270 | $firstExtraFieldName, |
||
| 271 | $firstExtraFieldNameForModelSession |
||
| 272 | ); |
||
| 273 | |||
| 274 | // create a second extra field and set its value in the model session |
||
| 275 | // the new session will inherit the same value in this field |
||
| 276 | $secondExtraFieldName = 'secondExtraField'.time(); |
||
| 277 | $secondExtraFieldValue = 'second extra field value'; |
||
| 278 | $secondExtraFieldId = $extraFieldModel->save( |
||
| 279 | [ |
||
| 280 | 'field_type' => ExtraField::FIELD_TYPE_TEXT, |
||
| 281 | 'variable' => $secondExtraFieldName, |
||
| 282 | 'display_text' => $secondExtraFieldName, |
||
| 283 | 'visible_to_self' => 1, |
||
| 284 | 'visible_to_others' => 1, |
||
| 285 | 'changeable' => 1, |
||
| 286 | 'filter' => 1, |
||
| 287 | ] |
||
| 288 | ); |
||
| 289 | SessionManager::update_session_extra_field_value( |
||
| 290 | $modelSessionId, |
||
| 291 | $secondExtraFieldName, |
||
| 292 | $secondExtraFieldValue |
||
| 293 | ); |
||
| 294 | |||
| 295 | // call the webservice to create the new session from the model session, |
||
| 296 | // specifying a different value in the first extra field |
||
| 297 | // and assert it returns an integer |
||
| 298 | $newSessionId = $this->integer( |
||
| 299 | [ |
||
| 300 | 'modelSessionId' => $modelSessionId, |
||
| 301 | 'sessionName' => 'Name of the new session'.time(), |
||
| 302 | 'startDate' => '2019-09-01 00:00', |
||
| 303 | 'endDate' => '2019-12-31 00:00', |
||
| 304 | 'extraFields' => [ |
||
| 305 | $firstExtraFieldName => $firstExtraFieldNameForNewSession, |
||
| 306 | ], |
||
| 307 | ] |
||
| 308 | ); |
||
| 309 | |||
| 310 | // assert the new session has its own new value in the first extra field |
||
| 311 | $extraFieldValueModel = new ExtraFieldValue('session'); |
||
| 312 | $extraFieldValue = $extraFieldValueModel->get_values_by_handler_and_field_variable( |
||
| 313 | $newSessionId, |
||
| 314 | $firstExtraFieldName |
||
| 315 | ); |
||
| 316 | $this->assertNotFalse($extraFieldValue); |
||
| 317 | $this->assertSame($firstExtraFieldNameForNewSession, $extraFieldValue['value']); |
||
| 318 | |||
| 319 | // assert the model session still has its own original value in the first extra field |
||
| 320 | $extraFieldValue = $extraFieldValueModel->get_values_by_handler_and_field_variable( |
||
| 321 | $modelSessionId, |
||
| 322 | $firstExtraFieldName |
||
| 323 | ); |
||
| 324 | $this->assertNotFalse($extraFieldValue); |
||
| 325 | $this->assertSame($firstExtraFieldNameForModelSession, $extraFieldValue['value']); |
||
| 326 | |||
| 327 | // assert the new session has inherited the model session value in the second extra field |
||
| 328 | $extraFieldValue = $extraFieldValueModel->get_values_by_handler_and_field_variable( |
||
| 329 | $newSessionId, |
||
| 330 | $secondExtraFieldName |
||
| 331 | ); |
||
| 332 | $this->assertNotFalse($extraFieldValue); |
||
| 333 | $this->assertSame($secondExtraFieldValue, $extraFieldValue['value']); |
||
| 334 | |||
| 335 | // assert the model session still has the same value in the second extra field |
||
| 336 | $extraFieldValue = $extraFieldValueModel->get_values_by_handler_and_field_variable( |
||
| 337 | $modelSessionId, |
||
| 338 | $secondExtraFieldName |
||
| 339 | ); |
||
| 340 | $this->assertNotFalse($extraFieldValue); |
||
| 341 | $this->assertSame($secondExtraFieldValue, $extraFieldValue['value']); |
||
| 342 | |||
| 343 | // clean up |
||
| 344 | SessionManager::delete($modelSessionId); |
||
| 345 | SessionManager::delete($newSessionId); |
||
| 346 | $extraFieldModel->delete($firstExtraFieldId); |
||
| 347 | $extraFieldModel->delete($secondExtraFieldId); |
||
| 348 | } |
||
| 350 |