From 76d0f9cfc19ec0fcd3c86c4920af6804b29db1f7 Mon Sep 17 00:00:00 2001 From: Owen Voke Date: Wed, 16 Sep 2020 19:02:33 +0100 Subject: [PATCH] feat(expectations): add toMatchConstraint --- src/Expectation.php | 11 +++++++++++ tests/Expect/toMatchConstraint.php | 16 ++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 tests/Expect/toMatchConstraint.php 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);