From 76d0f9cfc19ec0fcd3c86c4920af6804b29db1f7 Mon Sep 17 00:00:00 2001 From: Owen Voke Date: Wed, 16 Sep 2020 19:02:33 +0100 Subject: [PATCH 1/2] 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); From 23805cb5d6286caf1f0912a73cabab79840d1a7d Mon Sep 17 00:00:00 2001 From: Owen Voke Date: Wed, 16 Sep 2020 19:03:21 +0100 Subject: [PATCH 2/2] chore: update snapshots --- tests/.snapshots/success.txt | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index aae4b194..1a605a1b 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -195,6 +195,11 @@ PASS Tests\Expect\toHaveProperty ✓ pass ✓ failures + ✓ not failures + + PASS Tests\Expect\toMatchConstraint + ✓ pass + ✓ failures ✓ not failures PASS Tests\Expect\toMatchObject @@ -373,5 +378,5 @@ ✓ depends with defined arguments ✓ depends run test only once - Tests: 6 skipped, 220 passed + Tests: 6 skipped, 223 passed \ No newline at end of file