php - Best way to organize parameters when adding your own asserts to phpUnit -
almost of phpunit asserts 3 parameters:
- expected result
- actual value
- optional message print on failure
(some, such asserttrue(), have implicit expected result, have 2 parameters.)
but, if expected result not clean? may optional parameters, or may there more test 1 value. 1 concrete example, have asserttimestamp($expected_time,$actual_time,$tolerance=0,$msg='') tolerance allows bit of clock drift. (i use test created timestamp in database, assert record created in last 30 seconds, i.e. previous function in current unit test run.)
does reasonable? i.e. push $msg end, , keep actual second parameter. asserttag has ishtml flag, comes last, after $msg.
as more complex example i've function takes json string $actual parameter. runs json_decode, extracts few values, , checks each of them. have this:
function assertjsonpersonfromdbresponse($name,$gender,$age,$actual,$expectsomething=false,$msg='') would differently? wondering if might better squeeze expected parameters (including optional flags) first parameter, associative array:
function assertjsonpersonfromdbresponse($expected,$actual,$msg='') ... $this->assertjsonpersonfromdbresponse(array('name'=>'darren','age'=>21, 'gender'=>'m','expectsomething'=>true),$s); that feels cleaner. there existing convention these kind of situations?
the pattern see throught phpunit code base is:
[$expected (if needed)], $actual, $message = '', [other optional paramters] for exaples take @ framework/assert.php file
public static function assertequals( $expected, $actual, $message = '', $delta = 0, $maxdepth = 10, $canonicalize = false, $ignorecase = false ) or more precisce since $actual can composed of more 1 variable (class & attribute example):
[$allexpectedparamsifneeded], $allactualparams, $message='', [$allotherswitches] for example:
public static function assertattributeinternaltype( $expected, $attributename, $classorobject, $message = '' ) or
public static function assertselectregexp( $selector, $pattern, $count, $actual, $message = '', $ishtml = true ) general advice assertions:
try minimize amount of parameters , use more assert functions if possible:
$this->assertstuff($a, $b, true, false, 'oh dear', 6, true); is not readable , confusing me.
having
$this->assertstuffforspecificcasewithsubcasesix($a, $b, 'oh dear'); at least "inline documentation" @ every place it's used.
Comments
Post a Comment