mirror of
https://github.com/pestphp/pest.git
synced 2026-03-13 03:07:22 +01:00
add new match() method
This commit is contained in:
@ -177,6 +177,56 @@ final class Expectation
|
|||||||
return $this;
|
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
|
* Asserts that two variables have the same type and
|
||||||
* value. Used on objects, it asserts that two
|
* value. Used on objects, it asserts that two
|
||||||
|
|||||||
Reference in New Issue
Block a user