Description
Compare the expected with the result. If `$result` is null `$expected` equals `$type` and `$result` equals `$expected`.
Parameters
- string $type The type of comparison either `'identical'` or `'equal'` (default).
- mixed $expected The expected value.
- mixed $result An optional result value, defaults to `null`
- string $trace An optional trace used internally to track arrays and objects, defaults to `null`.
Returns
array Data with the keys `trace'`, `'expected'` and `'result'`.
protected function _compare($type, $expected, $result = null, $trace = null) {
$types = array(
'trace' => $trace, 'expected' => gettype($expected), 'result' => gettype($result)
);
if ($types['expected'] !== $types['result']) {
return $types;
}
$data = array();
$isObject = false;
if (is_object($expected)) {
$isObject = true;
$expected = (array) $expected;
$result = (array) $result;
}
if (is_array($expected)) {
foreach ($expected as $key => $value) {
$check = array_key_exists($key, $result) ? $result[$key] : false;
$newTrace = (($isObject == true) ? "{$trace}->{$key}" : "{$trace}[{$key}]");
if ($type === 'identical') {
if ($value === $check) {
continue;
}
if ($check === false) {
$trace = $newTrace;
return compact('trace', 'expected', 'result');
}
} else {
if ($value == $check) {
continue;
}
if (!is_array($value)) {
$trace = $newTrace;
return compact('trace', 'expected', 'result');
}
}
$compare = $this->_compare($type, $value, $check, $newTrace);
if ($compare !== true) {
$data[] = $compare;
}
}
if (empty($data)) {
return compact('trace', 'expected', 'result');
}
return $data;
}
if ($type === 'identical') {
if ($expected === $result) {
return true;
}
} else {
if ($expected == $result) {
return true;
}
}
return compact('trace', 'expected', 'result');
}