feat(expectations): add toMatchRegEx

This commit is contained in:
Owen Voke
2020-09-16 18:56:16 +01:00
parent 281166475e
commit 16b9f54dc3
2 changed files with 25 additions and 0 deletions

View File

@ -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.
*/

View File

@ -0,0 +1,15 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () {
expect('Hello World')->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);