diff --git a/src/Expectation.php b/src/Expectation.php index fcd54a75..98a743f2 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 toMatchRegEx(string $expression): Expectation + { + Assert::assertMatchesRegularExpression($expression, $this->value); + + return $this; + } + /** * Asserts that the value matches a constraint. */ diff --git a/tests/Expect/toMatchRegEx.php b/tests/Expect/toMatchRegEx.php new file mode 100644 index 00000000..1bc98667 --- /dev/null +++ b/tests/Expect/toMatchRegEx.php @@ -0,0 +1,15 @@ +toMatchRegEx('/^hello wo.*$/i'); +}); + +test('failures', function () { + expect('Hello World')->toMatchRegEx('/^hello$/i'); +})->throws(ExpectationFailedException::class); + +test('not failures', function () { + expect('Hello World')->not->toMatchRegEx('/^hello wo.*$/i'); +})->throws(ExpectationFailedException::class);