diff --git a/src/Formatter/Formatter.php b/src/Formatter/Formatter.php old mode 100644 new mode 100755 index 68ad935..f1164db --- a/src/Formatter/Formatter.php +++ b/src/Formatter/Formatter.php @@ -95,9 +95,11 @@ public function objectToString($obj) */ protected function getTemplateVars(TemplateInterface $template) { + $differExists = (property_exists($this->match, "differ") && is_callable($this->match->differ)); $vars = [ 'expected' => $this->match->getExpected(), 'actual' => $this->match->getActual(), + 'diff' => $differExists?call_user_func($this->match->differ, $this->match->getActual(), $this->match->getExpected()):"", ]; if ($tplVars = $template->getTemplateVars()) { diff --git a/src/Matcher/AbstractMatcher.php b/src/Matcher/AbstractMatcher.php index 033ff79..57d9889 100644 --- a/src/Matcher/AbstractMatcher.php +++ b/src/Matcher/AbstractMatcher.php @@ -31,8 +31,13 @@ public function match($actual = '') { $isMatch = $this->doMatch($actual); $isNegated = $this->isNegated(); + $fulfillsExpectation = ($isMatch xor $isNegated); - return new Match($isMatch xor $isNegated, $this->expected, $actual, $isNegated); + $match = new Match($fulfillsExpectation, $this->expected, $actual, $isNegated); + if ((!$fulfillsExpectation) && method_exists($this, "differ")) { + $match->differ = [$this, "differ"]; + } + return $match; } /** diff --git a/src/Matcher/SameMatcher.php b/src/Matcher/SameMatcher.php index 62a64dc..118c249 100644 --- a/src/Matcher/SameMatcher.php +++ b/src/Matcher/SameMatcher.php @@ -11,6 +11,10 @@ */ class SameMatcher extends AbstractMatcher { + public function differ($actual, $expected) { + return "THE CALCULATED DIFF"; + } + /** * Match if the actual value is identical to the expected value using an === * comparison. @@ -31,7 +35,7 @@ public function doMatch($actual) public function getDefaultTemplate() { return new ArrayTemplate([ - 'default' => 'Expected {{actual}} to be identical to {{expected}}', + 'default' => 'Expected {{actual}} to be identical to {{expected}}. Difference: {{diff}}', 'negated' => 'Expected {{actual}} not to be identical to {{expected}}', ]); }