PHPUnit_Framework_ComparisonFailure::diffIdentical PHP Method

diffIdentical() public static method

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
    public static function diffIdentical($expected, $actual, $message = '')
    {
        if (gettype($expected) !== gettype($actual)) {
            return new PHPUnit_Framework_ComparisonFailure_Type($expected, $actual, TRUE, $message);
        } else {
            if (is_array($expected) && is_array($actual)) {
                return new PHPUnit_Framework_ComparisonFailure_Array($expected, $actual, TRUE, $message);
            } else {
                if (is_object($expected) && is_object($actual)) {
                    return new PHPUnit_Framework_ComparisonFailure_Object($expected, $actual, TRUE, $message);
                } else {
                    if (is_string($expected) && !is_object($actual)) {
                        return new PHPUnit_Framework_ComparisonFailure_String($expected, $actual, TRUE, $message);
                    } else {
                        if (is_null($expected) || is_scalar($expected)) {
                            return new PHPUnit_Framework_ComparisonFailure_Scalar($expected, $actual, TRUE, $message);
                        }
                    }
                }
            }
        }
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * 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;
     }
 }
All Usage Examples Of PHPUnit_Framework_ComparisonFailure::diffIdentical