mirror of
https://github.com/pestphp/pest.git
synced 2026-03-06 07:47:22 +01:00
refactor: expectation match method
This commit is contained in:
@ -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 callable(): TMatchSubject|TMatchSubject $subject
|
||||||
* @param array<mixed, callable(self): void|TMatchValue> $expressions
|
* @param array<TMatchSubject, (callable(Expectation<TValue>): mixed)|TValue> $expressions
|
||||||
*
|
|
||||||
* @return \Pest\Expectation
|
|
||||||
*/
|
*/
|
||||||
public function match($subject, array $expressions): Expectation
|
public function match($subject, array $expressions): Expectation
|
||||||
{
|
{
|
||||||
@ -196,17 +194,16 @@ final class Expectation
|
|||||||
};
|
};
|
||||||
|
|
||||||
$subject = $subject();
|
$subject = $subject();
|
||||||
$keys = array_keys($expressions);
|
|
||||||
|
|
||||||
if (in_array($subject, ['0', '1', false, true], true)) {
|
$matched = false;
|
||||||
$subject = (int) $subject;
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($expressions as $key => $callback) {
|
foreach ($expressions as $key => $callback) {
|
||||||
if ($subject !== $key) {
|
if ($subject != $key) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$matched = true;
|
||||||
|
|
||||||
if (is_callable($callback)) {
|
if (is_callable($callback)) {
|
||||||
$callback(new self($this->value));
|
$callback(new self($this->value));
|
||||||
continue;
|
continue;
|
||||||
@ -217,6 +214,10 @@ final class Expectation
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($matched === false) {
|
||||||
|
throw new ExpectationFailedException('Unhandled match value.');
|
||||||
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -171,7 +171,7 @@
|
|||||||
✓ it runs with truthy closure condition
|
✓ it runs with truthy closure condition
|
||||||
✓ it runs with falsy closure condition
|
✓ it runs with falsy closure condition
|
||||||
✓ it can be passed non-callable values
|
✓ it can be passed non-callable values
|
||||||
✓ it passes with empty data
|
✓ it fails with unhandled match
|
||||||
✓ it can be used in higher order tests
|
✓ it can be used in higher order tests
|
||||||
|
|
||||||
PASS Tests\Features\Expect\not
|
PASS Tests\Features\Expect\not
|
||||||
|
|||||||
@ -130,11 +130,9 @@ it('can be passed non-callable values', function () {
|
|||||||
);
|
);
|
||||||
})->throws(ExpectationFailedException::class, 'two strings are equal');
|
})->throws(ExpectationFailedException::class, 'two strings are equal');
|
||||||
|
|
||||||
it('passes with empty data', function () {
|
it('fails with unhandled match', function () {
|
||||||
expect('foo')
|
expect('foo')->match('bar', []);
|
||||||
->match('bar', [])
|
})->throws(ExpectationFailedException::class, 'Unhandled match value.');
|
||||||
->toEqual('foo');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('can be used in higher order tests')
|
it('can be used in higher order tests')
|
||||||
->expect(true)
|
->expect(true)
|
||||||
|
|||||||
Reference in New Issue
Block a user