Figures out which diff class to use for the input types then
instantiates that class and returns the object.
public static diffIdentical ( mixed $expected, mixed $actual, string $message = '' ) : PHPUnit_Framework_ComparisonFailure | ||
$expected | mixed | Expected value retrieved. |
$actual | mixed | Actual value retrieved. |
$message | string | A string which is prefixed on all returned lines in the difference output. |
return | PHPUnit_Framework_ComparisonFailure |
/** * Returns a string describing the difference between the expected and the * actual object. * * @return string */ public function toString() { $diff = PHPUnit_Util_Diff::diff(print_r($this->expected, TRUE), print_r($this->actual, TRUE)); if ($diff !== FALSE) { return trim($diff); } // Fallback: Either diff is not available or the print_r() output for // the expected and the actual object are equal (but the objects are // not). $expectedClass = get_class($this->expected); $actualClass = get_class($this->actual); if ($expectedClass !== $actualClass) { return sprintf("%s%sexpected class <%s>\n" . '%sgot class <%s>', $this->message, $this->message != '' ? ' ' : '', $expectedClass, $this->message != '' ? str_repeat(' ', strlen($this->message) + 1) : '', $actualClass); } else { $expectedReflection = new ReflectionClass($expectedClass); $actualReflection = new ReflectionClass($actualClass); $diff = "in object of class <{$expectedClass}>:\n"; $i = 0; foreach ($expectedReflection->getProperties() as $expectedAttribute) { if ($expectedAttribute->isPrivate() || $expectedAttribute->isProtected()) { continue; } $actualAttribute = $actualReflection->getProperty($expectedAttribute->getName()); $expectedValue = $expectedAttribute->getValue($this->expected); $actualValue = $actualAttribute->getValue($this->actual); if ($expectedValue !== $actualValue) { if ($i > 0) { $diff .= "\n"; } ++$i; $expectedType = gettype($expectedValue); $actualType = gettype($actualValue); if ($expectedType !== $actualType) { $diffObject = new PHPUnit_Framework_ComparisonFailure_Type($expectedValue, $actualValue, $this->message . 'attribute <' . $expectedAttribute->getName() . '>: '); $diff .= $diffObject->toString(); } elseif (is_object($expectedValue)) { if (get_class($expectedValue) !== get_class($actualValue)) { $diffObject = new PHPUnit_Framework_ComparisonFailure_Type($expectedValue, $actualValue, $this->message . 'attribute <' . $expectedAttribute->getName() . '>: '); $diff .= $diffObject->toString(); } else { $diff .= 'attribute <' . $expectedAttribute->getName() . '> contains object <' . get_class($expectedValue) . '> with different attributes'; } } else { $diffObject = PHPUnit_Framework_ComparisonFailure::diffIdentical($expectedValue, $actualValue, $this->message . 'attribute <' . $expectedAttribute->getName() . '>: '); $diff .= $diffObject->toString(); } } } return $diff; } }
/** * Returns a string describing the difference between the expected and the * actual array. * * @return string */ public function toString() { if (!$this->identical) { ksort($this->expected); ksort($this->actual); } $diff = PHPUnit_Util_Diff::diff(print_r($this->expected, TRUE), print_r($this->actual, TRUE)); if ($diff !== FALSE) { return trim($diff); } // Fallback: Either diff is not available or the print_r() output for // the expected and the actual array are equal (but the arrays are not). $expectedOnly = array(); $actualOnly = array(); $diff = ''; foreach ($this->expected as $expectedKey => $expectedValue) { if (!array_key_exists($expectedKey, $this->actual)) { $expectedOnly[] = $expectedKey; continue; } if ($expectedValue === $this->actual[$expectedKey]) { continue; } $diffObject = PHPUnit_Framework_ComparisonFailure::diffIdentical($expectedValue, $this->actual[$expectedKey], sprintf('%sarray key %s: ', $this->message, PHPUnit_Util_Type::toString($expectedKey))); $diff .= $diffObject->toString() . "\n"; } foreach ($this->actual as $actualKey => $actualValue) { if (!array_key_exists($actualKey, $this->expected)) { $actualOnly[] = $actualKey; continue; } } foreach ($expectedOnly as $expectedKey) { $diff .= sprintf("array key %s: only in expected %s\n", PHPUnit_Util_Type::toString($expectedKey), PHPUnit_Util_Type::toString($this->expected[$expectedKey])); } foreach ($actualOnly as $actualKey) { $diff .= sprintf("array key %s: only in actual %s\n", PHPUnit_Util_Type::toString($actualKey), PHPUnit_Util_Type::toString($this->actual[$actualKey])); } return $diff; }
/** * @param mixed $other The value passed to evaluate() which failed the * constraint check. * @param string $description A string with extra description of what was * going on while the evaluation failed. * @param boolean $not Flag to indicate negation. * @throws PHPUnit_Framework_ExpectationFailedException */ public function fail($other, $description, $not = FALSE) { $failureDescription = $this->failureDescription($other, $description, $not); if (!$not) { throw new PHPUnit_Framework_ExpectationFailedException($failureDescription, PHPUnit_Framework_ComparisonFailure::diffIdentical($this->value, $other), $description); } else { throw new PHPUnit_Framework_ExpectationFailedException($failureDescription, NULL); } }
/** * Returns a string describing the difference between the expected and the * actual array. * * @note Diffing is only done for one level. */ public function toString() { if ($this->hasDiff()) { return $this->diff(print_r($this->expected, TRUE), print_r($this->actual, TRUE)); } $expectedOnly = array(); $actualOnly = array(); $diff = ''; foreach ($this->expected as $expectedKey => $expectedValue) { if (!array_key_exists($expectedKey, $this->actual)) { $expectedOnly[] = $expectedKey; continue; } if ($expectedValue === $this->actual[$expectedKey]) { continue; } $diffObject = PHPUnit_Framework_ComparisonFailure::diffIdentical($expectedValue, $this->actual[$expectedKey], $this->message . "array key <{$expectedKey}>: "); $diff .= $diffObject->toString() . "\n"; } foreach ($this->actual as $actualKey => $actualValue) { if (!array_key_exists($actualKey, $this->expected)) { $actualOnly[] = $actualKey; continue; } } foreach ($expectedOnly as $expectedKey) { $expectedValue = $this->expected[$expectedKey]; $diff .= "array key <{$expectedKey}>: only in expected <{$expectedValue}>\n"; } foreach ($actualOnly as $actualKey) { $actualValue = $this->actual[$actualKey]; $diff .= "array key <{$actualKey}>: only in actual <{$actualValue}>\n"; } return $diff; }