mirror of
https://github.com/pestphp/pest.git
synced 2026-03-06 15:57:21 +01:00
add new match() method
This commit is contained in:
@ -177,6 +177,56 @@ final class Expectation
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* If the subject matches one of the expressions, the callback in the expression is run.
|
||||
*
|
||||
* @template TMatchValue
|
||||
*
|
||||
* @param Closure|bool|string $subject
|
||||
* @param array<mixed, callable(self): void|TMatchValue> $expressions
|
||||
*
|
||||
* @return \Pest\Expectation
|
||||
*/
|
||||
public function match($subject, array $expressions): Expectation
|
||||
{
|
||||
$subject = is_callable($subject)
|
||||
? $subject
|
||||
: function () use ($subject) {
|
||||
return $subject;
|
||||
};
|
||||
|
||||
$subject = $subject();
|
||||
$keys = array_keys($expressions);
|
||||
$matched = false;
|
||||
|
||||
if (in_array($subject, ['0', '1', false, true], true)) {
|
||||
$subject = (int) $subject;
|
||||
}
|
||||
|
||||
foreach ($expressions as $key => $callback) {
|
||||
if ($subject !== $key) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$matched = true;
|
||||
|
||||
if (is_callable($callback)) {
|
||||
$callback(new self($this->value));
|
||||
continue;
|
||||
}
|
||||
|
||||
(new self($this->value))->toEqual($callback);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if (!$matched) {
|
||||
test()->addWarning('No item found matching "' . $subject . '".');
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that two variables have the same type and
|
||||
* value. Used on objects, it asserts that two
|
||||
|
||||
Reference in New Issue
Block a user