diff --git a/src/Expectation.php b/src/Expectation.php index fcd54a75..3b60d52d 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -505,6 +505,16 @@ final class Expectation return $this; } + /** + * Asserts that the value matches a regular expression. + */ + public function toMatch(string $expression): Expectation + { + Assert::assertMatchesRegularExpression($expression, $this->value); + + return $this; + } + /** * Asserts that the value matches a constraint. */ diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 1a605a1b..fe192a07 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\toMatch + ✓ pass + ✓ failures ✓ not failures PASS Tests\Expect\toMatchConstraint @@ -378,5 +383,5 @@ ✓ depends with defined arguments ✓ depends run test only once - Tests: 6 skipped, 223 passed + Tests: 6 skipped, 226 passed \ No newline at end of file diff --git a/tests/Expect/toMatch.php b/tests/Expect/toMatch.php new file mode 100644 index 00000000..9088a3c4 --- /dev/null +++ b/tests/Expect/toMatch.php @@ -0,0 +1,15 @@ +toMatch('/^hello wo.*$/i'); +}); + +test('failures', function () { + expect('Hello World')->toMatch('/^hello$/i'); +})->throws(ExpectationFailedException::class); + +test('not failures', function () { + expect('Hello World')->not->toMatch('/^hello wo.*$/i'); +})->throws(ExpectationFailedException::class);