public static function PHPUnit_Util_Diff::diff

Returns the diff between two arrays or strings as string.

Parameters

array|string $from:

array|string $to:

Return value

string

10 calls to PHPUnit_Util_Diff::diff()
PHPUnit_Framework_ComparisonFailure::getDiff in drupal/core/vendor/phpunit/phpunit/PHPUnit/Framework/ComparisonFailure.php
PHPUnit_Framework_Constraint_StringMatches::additionalFailureDescription in drupal/core/vendor/phpunit/phpunit/PHPUnit/Framework/Constraint/StringMatches.php
Return additional failure description where needed
Util_DiffTest::testComparisonErrorEndSame in drupal/core/vendor/phpunit/phpunit/Tests/Util/DiffTest.php
@covers PHPUnit_Util_Diff::diff
Util_DiffTest::testComparisonErrorEndSameComplete in drupal/core/vendor/phpunit/phpunit/Tests/Util/DiffTest.php
@covers PHPUnit_Util_Diff::diff
Util_DiffTest::testComparisonErrorMessage in drupal/core/vendor/phpunit/phpunit/Tests/Util/DiffTest.php
@covers PHPUnit_Util_Diff::diff

... See full list

File

drupal/core/vendor/phpunit/phpunit/PHPUnit/Util/Diff.php, line 68

Class

PHPUnit_Util_Diff
Diff implementation.

Code

public static function diff($from, $to) {
  $buffer = "--- Expected\n+++ Actual\n";
  $diff = self::diffToArray($from, $to);
  $inOld = FALSE;
  $i = 0;
  $old = array();
  foreach ($diff as $line) {
    if ($line[1] === 0) {
      if ($inOld === FALSE) {
        $inOld = $i;
      }
    }
    else {
      if ($inOld !== FALSE) {
        if ($i - $inOld > 5) {
          $old[$inOld] = $i - 1;
        }
        $inOld = FALSE;
      }
    }
    ++$i;
  }
  $start = isset($old[0]) ? $old[0] : 0;
  $end = count($diff);
  $i = 0;
  if ($tmp = array_search($end, $old)) {
    $end = $tmp;
  }
  $newChunk = TRUE;
  for ($i = $start; $i < $end; $i++) {
    if (isset($old[$i])) {
      $buffer .= "\n";
      $newChunk = TRUE;
      $i = $old[$i];
    }
    if ($newChunk) {
      $buffer .= "@@ @@\n";
      $newChunk = FALSE;
    }
    if ($diff[$i][1] === 1) {
      $buffer .= '+' . $diff[$i][0] . "\n";
    }
    else {
      if ($diff[$i][1] === 2) {
        $buffer .= '-' . $diff[$i][0] . "\n";
      }
      else {
        $buffer .= ' ' . $diff[$i][0] . "\n";
      }
    }
  }
  return $buffer;
}