mirror of
https://gitlab.com/TheGamecraft/c-cms.git
synced 2026-04-25 12:19:11 -04:00
ALPHA 3.0.2a
This commit is contained in:
@@ -12,6 +12,7 @@
|
||||
namespace Prophecy\Call;
|
||||
|
||||
use Exception;
|
||||
use Prophecy\Argument\ArgumentsWildcard;
|
||||
|
||||
/**
|
||||
* Call object.
|
||||
@@ -26,6 +27,7 @@ class Call
|
||||
private $exception;
|
||||
private $file;
|
||||
private $line;
|
||||
private $scores;
|
||||
|
||||
/**
|
||||
* Initializes call.
|
||||
@@ -44,6 +46,7 @@ class Call
|
||||
$this->arguments = $arguments;
|
||||
$this->returnValue = $returnValue;
|
||||
$this->exception = $exception;
|
||||
$this->scores = new \SplObjectStorage();
|
||||
|
||||
if ($file) {
|
||||
$this->file = $file;
|
||||
@@ -124,4 +127,36 @@ class Call
|
||||
|
||||
return sprintf('%s:%d', $this->file, $this->line);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the wildcard match score for the provided wildcard.
|
||||
*
|
||||
* @param ArgumentsWildcard $wildcard
|
||||
* @param false|int $score
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function addScore(ArgumentsWildcard $wildcard, $score)
|
||||
{
|
||||
$this->scores[$wildcard] = $score;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns wildcard match score for the provided wildcard. The score is
|
||||
* calculated if not already done.
|
||||
*
|
||||
* @param ArgumentsWildcard $wildcard
|
||||
*
|
||||
* @return false|int False OR integer score (higher - better)
|
||||
*/
|
||||
public function getScore(ArgumentsWildcard $wildcard)
|
||||
{
|
||||
if (isset($this->scores[$wildcard])) {
|
||||
return $this->scores[$wildcard];
|
||||
}
|
||||
|
||||
return $this->scores[$wildcard] = $wildcard->scoreArguments($this->getArguments());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,6 +96,7 @@ class CallCenter
|
||||
// Sort matches by their score value
|
||||
@usort($matches, function ($match1, $match2) { return $match2[0] - $match1[0]; });
|
||||
|
||||
$score = $matches[0][0];
|
||||
// If Highest rated method prophecy has a promise - execute it or return null instead
|
||||
$methodProphecy = $matches[0][1];
|
||||
$returnValue = null;
|
||||
@@ -115,9 +116,10 @@ class CallCenter
|
||||
);
|
||||
}
|
||||
|
||||
$this->recordedCalls[] = new Call(
|
||||
$this->recordedCalls[] = $call = new Call(
|
||||
$methodName, $arguments, $returnValue, $exception, $file, $line
|
||||
);
|
||||
$call->addScore($methodProphecy->getArgumentsWildcard(), $score);
|
||||
|
||||
if (null !== $exception) {
|
||||
throw $exception;
|
||||
@@ -139,7 +141,7 @@ class CallCenter
|
||||
return array_values(
|
||||
array_filter($this->recordedCalls, function (Call $call) use ($methodName, $wildcard) {
|
||||
return $methodName === $call->getMethodName()
|
||||
&& 0 < $wildcard->scoreArguments($call->getArguments())
|
||||
&& 0 < $call->getScore($wildcard)
|
||||
;
|
||||
})
|
||||
);
|
||||
@@ -149,23 +151,79 @@ class CallCenter
|
||||
array $arguments)
|
||||
{
|
||||
$classname = get_class($prophecy->reveal());
|
||||
$argstring = implode(', ', array_map(array($this->util, 'stringify'), $arguments));
|
||||
$expected = implode("\n", array_map(function (MethodProphecy $methodProphecy) {
|
||||
return sprintf(' - %s(%s)',
|
||||
$indentationLength = 8; // looks good
|
||||
$argstring = implode(
|
||||
",\n",
|
||||
$this->indentArguments(
|
||||
array_map(array($this->util, 'stringify'), $arguments),
|
||||
$indentationLength
|
||||
)
|
||||
);
|
||||
|
||||
$expected = array();
|
||||
|
||||
foreach (call_user_func_array('array_merge', $prophecy->getMethodProphecies()) as $methodProphecy) {
|
||||
$expected[] = sprintf(
|
||||
" - %s(\n" .
|
||||
"%s\n" .
|
||||
" )",
|
||||
$methodProphecy->getMethodName(),
|
||||
$methodProphecy->getArgumentsWildcard()
|
||||
implode(
|
||||
",\n",
|
||||
$this->indentArguments(
|
||||
array_map('strval', $methodProphecy->getArgumentsWildcard()->getTokens()),
|
||||
$indentationLength
|
||||
)
|
||||
)
|
||||
);
|
||||
}, call_user_func_array('array_merge', $prophecy->getMethodProphecies())));
|
||||
}
|
||||
|
||||
return new UnexpectedCallException(
|
||||
sprintf(
|
||||
"Method call:\n".
|
||||
" - %s(%s)\n".
|
||||
"on %s was not expected, expected calls were:\n%s",
|
||||
"Unexpected method call on %s:\n".
|
||||
" - %s(\n".
|
||||
"%s\n".
|
||||
" )\n".
|
||||
"expected calls were:\n".
|
||||
"%s",
|
||||
|
||||
$methodName, $argstring, $classname, $expected
|
||||
$classname, $methodName, $argstring, implode("\n", $expected)
|
||||
),
|
||||
$prophecy, $methodName, $arguments
|
||||
|
||||
);
|
||||
}
|
||||
|
||||
private function formatExceptionMessage(MethodProphecy $methodProphecy)
|
||||
{
|
||||
return sprintf(
|
||||
" - %s(\n".
|
||||
"%s\n".
|
||||
" )",
|
||||
$methodProphecy->getMethodName(),
|
||||
implode(
|
||||
",\n",
|
||||
$this->indentArguments(
|
||||
array_map(
|
||||
function ($token) {
|
||||
return (string) $token;
|
||||
},
|
||||
$methodProphecy->getArgumentsWildcard()->getTokens()
|
||||
),
|
||||
$indentationLength
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
private function indentArguments(array $arguments, $indentationLength)
|
||||
{
|
||||
return preg_replace_callback(
|
||||
'/^/m',
|
||||
function () use ($indentationLength) {
|
||||
return str_repeat(' ', $indentationLength);
|
||||
},
|
||||
$arguments
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user