PHP, will comparing MD5s of print_r output work for testing results?
I'm using code igniter and trying to write tests for the data returned by my 开发者_开发技巧models; CI's testing library is very limited in what it will check for, so I've been doing this:
$test = "My Test Name";
$expected = md5(print_r(array(array('val','other'), array('new','old','blue')), 1));
$result = md5(print_r($this->model->method($data), 1));
if($expected == $result) {
echo "$test [pass]";
}
else {
echo "$test [fail]";
}
Are there any pitfalls to testing this way? Is there a preferred, simple library that works with CI that I didn't find? The built-in testing library won't allow you to check results with this fine a grain so I find it not terribly helpful.
Yes, that should work just fine, as if both are identical the hashes should be identical as well.
However, why not just examine the output of print_r
directly? Or, for that matter, use a completely separate tool like PhpUnit
for testing?
Assuming the two variables you are comparing are both arrays, have a look at the inbuilt array_diff()
function (http://php.net/manual/en/function.array-diff.php). If you need to compare the indexes of the arrays also, use array_diff_assoc()
instead.
However, if you want a complete check for testing multi-dimentional arrays (as yours is), here's a function that should do that for you:
function compare_arrays($first_array, $second_array, $compare_keys = false){
/* Test if arrays are actually arrays */
if(!is_array($first_array)){
// Elements are not arrays
if(is_array($second_array)){
// Second array is not array
return false;
}
if($first_array == $second_array){
return true;
}
} elseif(!is_array($second_array)){
// Second array is not array (first is)
return false;
}
/* Test same number of elements */
if(count($first_array) != count($second_array)){
return false;
}
$size = count($first_array);
if($compare_keys){
// Load keys to compare
$first_keys = array_keys($first_array);
$second_keys = array_keys($second_array);
}
for($i = 0; $i < $size; $i++){
if($compare_keys && $first_keys[$i] != $second_keys[$i]){
// Keys do not match
return false;
}
if(is_array($first_array[$i])){
// Element is array
if(!is_array($second_array[$i])){
// Matching element in other array is not array
return false;
}
$result = compare_arrays($first_array[$i], $second_array[$i], $compare_keys);
if(!$result){
// Sub-arrays do not match
return false;
}
// Match - skip iteration
continue;
} elseif(is_array($second_array[$i])){
// Second array element is array (first is not)
return false;
}
// Elements are not arrays
if($first_array[$i] != $second_array[$i]){
return false;
}
}
// Match
return true;
}
Usage:
$br = '<br />';
$array_one = array(4, 5, array(2, 5), 'hello', 'element');
if(compare_arrays($array_one, $array_one)){
echo 'Match 1'.$br;
} else {
echo 'No Match 1'.$br;
}
$array_two = array(5, 5, array(2, 5), 'hello', 'element'); // Slightly different
if(compare_arrays($array_one, $array_two)){
echo 'Match 2'.$br;
} else {
echo 'No Match 2'.$br;
}
/* Output:
Match 1
No Match 2
*/
if($array1 === $array2) {
# the arrays are matched with identical keys and values
# no matter how deeply nested
}
No hashing required.
精彩评论