diff --git a/src/Expectation.php b/src/Expectation.php index b156d558..fcd54a75 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Pest; use PHPUnit\Framework\Assert; +use PHPUnit\Framework\Constraint\Constraint; /** * @internal @@ -504,6 +505,16 @@ final class Expectation return $this; } + /** + * Asserts that the value matches a constraint. + */ + public function toMatchConstraint(Constraint $constraint): Expectation + { + Assert::assertThat($this->value, $constraint); + + return $this; + } + /** * Dynamically calls methods on the class without any arguments. * diff --git a/tests/Expect/toMatchConstraint.php b/tests/Expect/toMatchConstraint.php new file mode 100644 index 00000000..e5b4b681 --- /dev/null +++ b/tests/Expect/toMatchConstraint.php @@ -0,0 +1,16 @@ +toMatchConstraint(new IsTrue()); +}); + +test('failures', function () { + expect(false)->toMatchConstraint(new IsTrue()); +})->throws(ExpectationFailedException::class); + +test('not failures', function () { + expect(true)->not->toMatchConstraint(new IsTrue()); +})->throws(ExpectationFailedException::class);