diff() public static méthode

Returns the diff between two arrays or strings.
public static diff ( array | string $from, array | string $to ) : string
$from array | string
$to array | string
Résultat string
Exemple #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;
     }
 }
Exemple #2
0
 /**
  * Returns a string describing the difference between
  * the expected and the actual string value.
  */
 public function toString()
 {
     $expected = (string) $this->expected;
     $actual = (string) $this->actual;
     $diff = PHPUnit_Util_Diff::diff($expected, $actual);
     if ($diff === FALSE) {
         $diff = '';
     }
     if (!empty($this->message)) {
         $buffer = $this->message . "\n";
     } else {
         $buffer = '';
     }
     return trim($buffer . $diff);
 }
Exemple #3
0
 /**
  * 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;
 }
 /**
  * Paints the test failure with a breadcrumbs
  * trail of the nesting test suites below the
  * top level test.
  *
  * @param PHPUnit_Framework_AssertionFailedError $message Failure object displayed in
  *   the context of the other tests.
  * @param mixed $test The test case to paint a failure for.
  * @return void
  */
 public function paintFail($message, $test)
 {
     $trace = $this->_getStackTrace($message);
     $className = get_class($test);
     $testName = $className . '::' . $test->getName() . '()';
     $actualMsg = $expectedMsg = null;
     if (method_exists($message, 'getComparisonFailure')) {
         $failure = $message->getComparisonFailure();
         if (is_object($failure)) {
             $actualMsg = $failure->getActualAsString();
             $expectedMsg = $failure->getExpectedAsString();
         }
     }
     echo "<li class='fail'>\n";
     echo "<span>Failed</span>";
     echo "<div class='msg'><pre>" . $this->_htmlEntities($message->toString());
     if (is_string($actualMsg) && is_string($expectedMsg) || is_array($actualMsg) && is_array($expectedMsg)) {
         echo "<br />" . $this->_htmlEntities(PHPUnit_Util_Diff::diff($expectedMsg, $actualMsg));
     }
     echo "</pre></div>\n";
     echo "<div class='msg'>" . __d('cake_dev', 'Test case: %s', $testName) . "</div>\n";
     if (strpos($className, "PHPUnit_") === false) {
         list($show, $query) = $this->_getQueryLink();
         echo "<div class='msg'><a href='" . $this->baseUrl() . $query . "&amp;filter=" . $test->getName() . "'>" . __d('cake_dev', 'Rerun only this test: %s', $testName) . "</a></div>\n";
     }
     echo "<div class='msg'>" . __d('cake_dev', 'Stack trace:') . '<br />' . $trace . "</div>\n";
     echo "</li>\n";
 }
 /**
  * @covers PHPUnit_Util_Diff::diff
  */
 public function testComparisonErrorOverlapingMatches2()
 {
     $this->assertEquals("--- Expected\n+++ Actual\n@@ @@\n-abcdde\n+abcde\n", PHPUnit_Util_Diff::diff('abcdde', 'abcde'));
 }
 /**
  * Paints the test failure with a breadcrumbs
  * trail of the nesting test suites below the
  * top level test.
  *
  * @param PHPUnit_Framework_AssertionFailedError $message Failure object displayed in
  *   the context of the other tests.
  * @param mixed $test
  * @return void
  */
 public function paintFail($message, $test)
 {
     $trace = $this->_getStackTrace($message);
     $testName = get_class($test) . '(' . $test->getName() . ')';
     $actualMsg = $expectedMsg = null;
     if (method_exists($message, 'getComparisonFailure')) {
         $failure = $message->getComparisonFailure();
         if (is_object($failure)) {
             $actualMsg = $failure->getActualAsString();
             $expectedMsg = $failure->getExpectedAsString();
         }
     }
     echo "<li class='fail'>\n";
     echo "<span>Failed</span>";
     echo "<div class='msg'><pre>" . $this->_htmlEntities($message->toString());
     if (is_string($actualMsg) && is_string($expectedMsg) || is_array($actualMsg) && is_array($expectedMsg)) {
         echo "<br />" . $this->_htmlEntities(PHPUnit_Util_Diff::diff($expectedMsg, $actualMsg));
     }
     echo "</pre></div>\n";
     echo "<div class='msg'>" . __d('cake_dev', 'Test case: %s', $testName) . "</div>\n";
     echo "<div class='msg'>" . __d('cake_dev', 'Stack trace:') . '<br />' . $trace . "</div>\n";
     echo "</li>\n";
 }
 /**
  * @return string
  */
 public function getDiff()
 {
     return $this->actualAsString || $this->expectedAsString ? PHPUnit_Util_Diff::diff($this->expectedAsString, $this->actualAsString) : '';
 }
Exemple #8
0
 protected function additionalFailureDescription($other)
 {
     return PHPUnit_Util_Diff::diff($this->string, $other);
 }
 /**
  * @covers \Tests\Constraint\TokensMatch::failureDescription
  */
 public function testFailAndFailureDescriptionWithDifferentLinenumbers()
 {
     $expected = new Token('blub', null, '5');
     $other = new Token('blub', null, '4');
     $tokenMatch = new TokensMatch($expected, true);
     $message = PHP_EOL . \PHPUnit_Util_Diff::diff((string) $expected, (string) $other);
     $message = 'Failed asserting that Tokens are different: [linenumber]' . $message . '.';
     try {
         $tokenMatch->evaluate($other);
         $this->fail('no exception thrown');
     } catch (\PHPUnit_Framework_ExpectationFailedException $e) {
         $this->assertEquals($message, $e->getMessage());
     }
 }
 /**
  * @param mixed   $other
  * @param string  $description
  * @param boolean $not
  * @return string
  */
 protected function failureDescription($other)
 {
     $message = PHP_EOL . \PHPUnit_Util_Diff::diff((string) $this->_expectedToken, (string) $other);
     $difference = $this->_difference;
     return 'Tokens are different: [' . $difference . ']' . $message;
 }
 protected function additionalFailureDescription($other)
 {
     $from = preg_split('(\\r\\n|\\r|\\n)', $this->string);
     $to = preg_split('(\\r\\n|\\r|\\n)', $other);
     foreach ($from as $index => $line) {
         if (isset($to[$index]) && $line !== $to[$index]) {
             $line = $this->createPatternFromFormat($line);
             if (preg_match($line, $to[$index]) > 0) {
                 $from[$index] = $to[$index];
             }
         }
     }
     $this->string = join("\n", $from);
     $other = join("\n", $to);
     return PHPUnit_Util_Diff::diff($this->string, $other);
 }