Skip to content

Commit

Permalink
Clean/clarify prediction failures messages
Browse files Browse the repository at this point in the history
  • Loading branch information
everzet committed Apr 29, 2013
1 parent fc0ec52 commit 54ba238
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 14 deletions.
9 changes: 7 additions & 2 deletions src/Prophecy/Call/CallCenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,19 @@ private function createUnexpectedCallException(ObjectProphecy $prophecy, $method
$classname = get_class($prophecy->reveal());
$argstring = implode(', ', array_map(array($this->util, 'stringify'), $arguments));
$expected = implode("\n", array_map(function($methodProphecy) {
return sprintf(' - %s(%s)',
return sprintf(' - %s(%s)',
$methodProphecy->getMethodName(),
$methodProphecy->getArgumentsWildcard()
);
}, $prophecy->getMethodProphecies($methodName)));

return new UnexpectedCallException(
sprintf("Method call `%s->%s(%s)` was not expected. Expected calls are:\n%s",
sprintf(
"Method call:\n".
" %s->%s(%s)\n".
"was not expected.\n".
"Expected calls are:\n%s",

$classname, $methodName, $argstring, $expected
),
$prophecy, $methodName, $arguments
Expand Down
10 changes: 7 additions & 3 deletions src/Prophecy/Prediction/CallPrediction.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ public function check(array $calls, ObjectProphecy $object, MethodProphecy $meth

if (count($methodCalls)) {
throw new NoCallsException(sprintf(
"No calls been made that match `%s->%s(%s)`, but expected at least one.\n".
"Other calls to `%s(...)` been made:\n%s",
"No calls been made that match:\n".
" %s->%s(%s)\n".
"but expected at least one.\n".
"Recorded `%s(...)` calls:\n%s",

get_class($object->reveal()),
$method->getMethodName(),
Expand All @@ -72,7 +74,9 @@ public function check(array $calls, ObjectProphecy $object, MethodProphecy $meth
}

throw new NoCallsException(sprintf(
'No calls been made that match `%s->%s(%s)`, but expected at least one.',
"No calls been made that match:\n".
" %s->%s(%s)\n".
"but expected at least one.",

get_class($object->reveal()),
$method->getMethodName(),
Expand Down
14 changes: 10 additions & 4 deletions src/Prophecy/Prediction/CallTimesPrediction.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ public function check(array $calls, ObjectProphecy $object, MethodProphecy $meth

if (count($calls)) {
$message = sprintf(
"Expected exactly %d calls that match `%s->%s(%s)`, but %d were made:\n%s",
"Expected exactly %d calls that match:\n".
" %s->%s(%s)\n".
"but %d were made:\n%s",

$this->times,
get_class($object->reveal()),
Expand All @@ -75,8 +77,10 @@ public function check(array $calls, ObjectProphecy $object, MethodProphecy $meth
);
} elseif (count($methodCalls)) {
$message = sprintf(
"Expected exactly %d calls that match `%s->%s(%s)`, but 0 were made.\n".
"Other calls to `%s(...)` been made:\n%s",
"Expected exactly %d calls that match:\n".
" %s->%s(%s)\n".
"but none were made.\n".
"Recorded `%s(...)` calls:\n%s",

$this->times,
get_class($object->reveal()),
Expand All @@ -87,7 +91,9 @@ public function check(array $calls, ObjectProphecy $object, MethodProphecy $meth
);
} else {
$message = sprintf(
"Expected exactly %d calls that match `%s->%s(%s)`, but 0 were made.",
"Expected exactly %d calls that match:\n".
" %s->%s(%s)\n".
"but none were made.",

$this->times,
get_class($object->reveal()),
Expand Down
6 changes: 5 additions & 1 deletion src/Prophecy/Prediction/NoCallsPrediction.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,14 @@ public function check(array $calls, ObjectProphecy $object, MethodProphecy $meth
}

throw new UnexpectedCallsException(sprintf(
"No calls expected that match `%s->%s(%s)`, but some were made:\n%s",
"No calls expected that match:\n".
" %s->%s(%s)\n".
"but %d were made:\n%s",

get_class($object->reveal()),
$method->getMethodName(),
$method->getArgumentsWildcard(),
count($calls),
$this->util->stringifyCalls($calls)
), $method, $calls);
}
Expand Down
14 changes: 10 additions & 4 deletions src/Prophecy/Util/StringUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ public function stringify($value)
return $value ? 'true' : 'false';
}
if (is_string($value)) {
return sprintf('"%s"', str_replace("\n", '\\n', $value));
$str = sprintf('"%s"', str_replace("\n", '\\n', $value));

if (50 <= strlen($str)) {
return substr($str, 0, 50).'"...';
}

return $str;
}
if (null === $value) {
return 'null';
Expand All @@ -60,11 +66,11 @@ public function stringifyCalls(array $calls)
{
$self = $this;

return implode("\n", array_map(function($call) use($self) {
return sprintf('- `%s(%s)` from %s',
return implode(PHP_EOL, array_map(function($call) use($self) {
return sprintf(' - %s(%s) @ %s',
$call->getMethodName(),
implode(', ', array_map(array($self, 'stringify'), $call->getArguments())),
$call->getCallPlace()
str_replace(GETCWD().DIRECTORY_SEPARATOR, '', $call->getCallPlace())
);
}, $calls));
}
Expand Down

0 comments on commit 54ba238

Please sign in to comment.