diff --git a/src/Expectation.php b/src/Expectation.php index 89d86f53..a45c8892 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -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 $expressions - * - * @return \Pest\Expectation + * @param callable(): TMatchSubject|TMatchSubject $subject + * @param array): 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; } diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index c1597844..175f10e8 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -171,7 +171,7 @@ ✓ it runs with truthy closure condition ✓ it runs with falsy closure condition ✓ 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 PASS Tests\Features\Expect\not diff --git a/tests/Features/Expect/matchExpectation.php b/tests/Features/Expect/matchExpectation.php index f577cedc..2bea1be5 100644 --- a/tests/Features/Expect/matchExpectation.php +++ b/tests/Features/Expect/matchExpectation.php @@ -130,11 +130,9 @@ it('can be passed non-callable values', function () { ); })->throws(ExpectationFailedException::class, 'two strings are equal'); -it('passes with empty data', function () { - expect('foo') - ->match('bar', []) - ->toEqual('foo'); -}); +it('fails with unhandled match', function () { + expect('foo')->match('bar', []); +})->throws(ExpectationFailedException::class, 'Unhandled match value.'); it('can be used in higher order tests') ->expect(true)