Merge pull request #191 from owenvoke/feature/assert-regex

feat(expectations): add toMatch
This commit is contained in:
Owen Voke
2020-09-21 20:28:42 +01:00
committed by GitHub
3 changed files with 31 additions and 1 deletions

View File

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

View File

@ -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

15
tests/Expect/toMatch.php Normal file
View File

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