refactor: expectation match method

This commit is contained in:
Nuno Maduro
2021-09-24 22:02:18 +01:00
parent ae029660e3
commit 457972716f
3 changed files with 16 additions and 17 deletions

View File

@ -178,14 +178,12 @@ final class Expectation
}
/**
* If the subject matches one of the expressions, the callback in the expression is run.
* If the subject matches one of the given "expressions", the expression callback will run.
*
* @template TMatchValue
* @template TMatchSubject of array-key
*
* @param Closure|bool|string $subject
* @param array<mixed, callable(self): void|TMatchValue> $expressions
*
* @return \Pest\Expectation
* @param callable(): TMatchSubject|TMatchSubject $subject
* @param array<TMatchSubject, (callable(Expectation<TValue>): mixed)|TValue> $expressions
*/
public function match($subject, array $expressions): Expectation
{
@ -196,17 +194,16 @@ final class Expectation
};
$subject = $subject();
$keys = array_keys($expressions);
if (in_array($subject, ['0', '1', false, true], true)) {
$subject = (int) $subject;
}
$matched = false;
foreach ($expressions as $key => $callback) {
if ($subject !== $key) {
if ($subject != $key) {
continue;
}
$matched = true;
if (is_callable($callback)) {
$callback(new self($this->value));
continue;
@ -217,6 +214,10 @@ final class Expectation
break;
}
if ($matched === false) {
throw new ExpectationFailedException('Unhandled match value.');
}
return $this;
}