Skip to content

Commit 16f9950

Browse files
committed
Add more test, and comment
1 parent d90ee9f commit 16f9950

File tree

2 files changed

+53
-10
lines changed

2 files changed

+53
-10
lines changed

src/Regex.php

+38-1
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,50 @@
2121
*/
2222
class Regex extends Constraint
2323
{
24+
/**
25+
* If there was an internal PCRE error.
26+
*/
2427
const INTERNAL_ERROR = '272fcfbe-0b7a-4189-94af-e737ebbb023f';
28+
29+
/**
30+
* If backtrack limit was exhausted.
31+
*/
2532
const BACKTRACK_LIMIT_ERROR = '7607aa8b-8adf-4946-895a-dee1540938d6';
33+
34+
/**
35+
* If recursion limit was exhausted.
36+
*/
2637
const RECURSION_LIMIT_ERROR = 'ebb05dfb-f797-4329-bc22-a0a787187a0c';
38+
39+
/**
40+
* If the last error was caused by malformed UTF-8 data (only when running
41+
* a regex in UTF-8 mode).
42+
*/
2743
const BAD_UTF8_ERROR = '9fc7f69e-7e59-4f39-b58e-ddcb0b507d5c';
44+
45+
/**
46+
* If the offset didn't correspond to the begin of a valid UTF-8 code point
47+
* (only when running a regex in UTF-8 mode).
48+
*/
2849
const BAD_UTF8_OFFSET_ERROR = '517727f0-8422-45a6-911c-8ff7794a3255';
29-
const JIT_STACKLIMIT_ERROR = '2b4859eb-7feb-4672-9e2a-a09c99b0785f'; // >= PHP 7.0
50+
51+
/**
52+
* If the last PCRE function failed due to limited JIT stack space.
53+
*
54+
* >= PHP 7.0
55+
*/
56+
const JIT_STACKLIMIT_ERROR = '2b4859eb-7feb-4672-9e2a-a09c99b0785f';
57+
58+
/**
59+
* Other error.
60+
*/
3061
const OTHER_ERROR = '026d3188-5252-42ad-98c0-b053fa97cf6f';
3162

3263
const MESSAGE = 'Invalid regular expression.';
3364

65+
/**
66+
* @var string[]
67+
*/
3468
protected static $errorNames = [
3569
self::INTERNAL_ERROR => 'INTERNAL_ERROR',
3670
self::BACKTRACK_LIMIT_ERROR => 'BACKTRACK_LIMIT_ERROR',
@@ -41,5 +75,8 @@ class Regex extends Constraint
4175
self::OTHER_ERROR => 'OTHER_ERROR',
4276
];
4377

78+
/**
79+
* @var string
80+
*/
4481
public $message = self::MESSAGE;
4582
}

tests/Tests/RegexValidatorTest.php

+15-9
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,14 @@ public function testRegexes($regex, $valid, $code = null)
5555
public function regexProvider()
5656
{
5757
return [
58-
['/abc/', true],
59-
['/abc', false, Regex::OTHER_ERROR],
58+
['/foo/', true],
59+
['/foo/i', true],
60+
['`foo`', true],
61+
['/(?:(?:(?:(?:(?:(?:(?:[ \t]*(?:\r\n))?[ \t])?(\((?:(?:(?:[ \t]*(?:\r\n))?[ \t])|(?:(?:[\x01-\x08\x0B\x0C\x0E-\x19\x7F]|[\x21-\x27\x2A-\x5B\x5D-\x7E])|(?:\\[\x00-\x08\x0B\x0C\x0E-\x7F])|(?1)))*(?:(?:[ \t]*(?:\r\n))?[ \t])?\)))*(?:(?:(?:(?:[ \t]*(?:\r\n))?[ \t])?(\((?:(?:(?:[ \t]*(?:\r\n))?[ \t])|(?:(?:[\x01-\x08\x0B\x0C\x0E-\x19\x7F]|[\x21-\x27\x2A-\x5B\x5D-\x7E])|(?:\\[\x00-\x08\x0B\x0C\x0E-\x7F])|(?1)))*(?:(?:[ \t]*(?:\r\n))?[ \t])?\)))|(?:(?:[ \t]*(?:\r\n))?[ \t])))?(?:[a-zA-Z0-9!#\$%&\'\*\+\-\/=\?\^_`\{\}\|~]+(\.[a-zA-Z0-9!#\$%&\'\*\+\-\/=\?\^_`\{\}\|~]+)*)+(?:(?:(?:(?:[ \t]*(?:\r\n))?[ \t])?(\((?:(?:(?:[ \t]*(?:\r\n))?[ \t])|(?:(?:[\x01-\x08\x0B\x0C\x0E-\x19\x7F]|[\x21-\x27\x2A-\x5B\x5D-\x7E])|(?:\\[\x00-\x08\x0B\x0C\x0E-\x7F])|(?1)))*(?:(?:[ \t]*(?:\r\n))?[ \t])?\)))*(?:(?:(?:(?:[ \t]*(?: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x08\x0B\x0C\x0E-\x7F])|(?1)))*(?:(?:[ \t]*(?:\r\n))?[ \t])?\)))|(?:(?:[ \t]*(?:\r\n))?[ \t])))?)))/', true],
62+
['/(foo/', false, Regex::OTHER_ERROR],
63+
['/foo//', false, Regex::OTHER_ERROR],
64+
['/?:foo/', false, Regex::OTHER_ERROR],
65+
['/?P<foo/', false, Regex::OTHER_ERROR],
6066
];
6167
}
6268

@@ -80,13 +86,13 @@ public function testRegexErrors($regex, $errorCode, $code)
8086
public function regexErrorProvider()
8187
{
8288
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],
89+
['/foo', \PREG_NO_ERROR, Regex::OTHER_ERROR],
90+
['/foo', \PREG_INTERNAL_ERROR, Regex::INTERNAL_ERROR],
91+
['/foo', \PREG_BACKTRACK_LIMIT_ERROR, Regex::BACKTRACK_LIMIT_ERROR],
92+
['/foo', \PREG_RECURSION_LIMIT_ERROR, Regex::RECURSION_LIMIT_ERROR],
93+
['/foo', \PREG_BAD_UTF8_ERROR, Regex::BAD_UTF8_ERROR],
94+
['/foo', \PREG_BAD_UTF8_OFFSET_ERROR, Regex::BAD_UTF8_OFFSET_ERROR],
95+
['/foo', defined('PREG_JIT_STACKLIMIT_ERROR') ? \PREG_JIT_STACKLIMIT_ERROR : \PREG_NO_ERROR, defined('PREG_JIT_STACKLIMIT_ERROR') ? Regex::JIT_STACKLIMIT_ERROR : Regex::OTHER_ERROR],
9096
];
9197
}
9298
}

0 commit comments

Comments
 (0)