|
9 | 9 |
|
10 | 10 | namespace Antalaron\RegexValidator\Tests; |
11 | 11 |
|
| 12 | +require __DIR__.'/../Fixtures/preg-last-error.php'; |
| 13 | + |
12 | 14 | use Antalaron\RegexValidator\Regex; |
13 | 15 | use Antalaron\RegexValidator\RegexValidator; |
| 16 | +use Antalaron\RegexValidator\ValidatorMock; |
14 | 17 |
|
15 | 18 | class RegexValidatorTest extends AbstractConstraintValidatorTest |
16 | 19 | { |
| 20 | + public function setUp() |
| 21 | + { |
| 22 | + parent::setUp(); |
| 23 | + |
| 24 | + ValidatorMock::$pregLastErrorMock = false; |
| 25 | + } |
| 26 | + |
17 | 27 | protected function createValidator() |
18 | 28 | { |
19 | 29 | return new RegexValidator(); |
@@ -49,4 +59,34 @@ public function regexProvider() |
49 | 59 | ['/abc', false, Regex::OTHER_ERROR], |
50 | 60 | ]; |
51 | 61 | } |
| 62 | + |
| 63 | + /** |
| 64 | + * @dataProvider regexErrorProvider |
| 65 | + */ |
| 66 | + public function testRegexErrors($regex, $errorCode, $code) |
| 67 | + { |
| 68 | + ValidatorMock::$pregLastErrorMock = true; |
| 69 | + ValidatorMock::$pregLastError = $errorCode; |
| 70 | + |
| 71 | + $this->validator->validate($regex, new Regex()); |
| 72 | + |
| 73 | + $this->assertSame(ValidatorMock::$pregLastErrorMock, true); |
| 74 | + |
| 75 | + $this->buildViolation(Regex::MESSAGE) |
| 76 | + ->setCode($code) |
| 77 | + ->assertRaised(); |
| 78 | + } |
| 79 | + |
| 80 | + public function regexErrorProvider() |
| 81 | + { |
| 82 | + return [ |
| 83 | + ['/abc', \PREG_NO_ERROR, Regex::OTHER_ERROR], |
| 84 | + ['/abc', \PREG_INTERNAL_ERROR, Regex::INTERNAL_ERROR], |
| 85 | + ['/abc', \PREG_BACKTRACK_LIMIT_ERROR, Regex::BACKTRACK_LIMIT_ERROR], |
| 86 | + ['/abc', \PREG_RECURSION_LIMIT_ERROR, Regex::RECURSION_LIMIT_ERROR], |
| 87 | + ['/abc', \PREG_BAD_UTF8_ERROR, Regex::BAD_UTF8_ERROR], |
| 88 | + ['/abc', \PREG_BAD_UTF8_OFFSET_ERROR, Regex::BAD_UTF8_OFFSET_ERROR], |
| 89 | + ['/abc', defined('PREG_JIT_STACKLIMIT_ERROR') ? \PREG_JIT_STACKLIMIT_ERROR : \PREG_NO_ERROR, defined('PREG_JIT_STACKLIMIT_ERROR') ? Regex::JIT_STACKLIMIT_ERROR : Regex::OTHER_ERROR], |
| 90 | + ]; |
| 91 | + } |
52 | 92 | } |
0 commit comments