Skip to content

Commit

Permalink
Add more test, and comment
Browse files Browse the repository at this point in the history
  • Loading branch information
antalaron committed Dec 1, 2017
1 parent d90ee9f commit 16f9950
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 10 deletions.
39 changes: 38 additions & 1 deletion src/Regex.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,50 @@
*/
class Regex extends Constraint
{
/**
* If there was an internal PCRE error.
*/
const INTERNAL_ERROR = '272fcfbe-0b7a-4189-94af-e737ebbb023f';

/**
* If backtrack limit was exhausted.
*/
const BACKTRACK_LIMIT_ERROR = '7607aa8b-8adf-4946-895a-dee1540938d6';

/**
* If recursion limit was exhausted.
*/
const RECURSION_LIMIT_ERROR = 'ebb05dfb-f797-4329-bc22-a0a787187a0c';

/**
* If the last error was caused by malformed UTF-8 data (only when running
* a regex in UTF-8 mode).
*/
const BAD_UTF8_ERROR = '9fc7f69e-7e59-4f39-b58e-ddcb0b507d5c';

/**
* If the offset didn't correspond to the begin of a valid UTF-8 code point
* (only when running a regex in UTF-8 mode).
*/
const BAD_UTF8_OFFSET_ERROR = '517727f0-8422-45a6-911c-8ff7794a3255';
const JIT_STACKLIMIT_ERROR = '2b4859eb-7feb-4672-9e2a-a09c99b0785f'; // >= PHP 7.0

/**
* If the last PCRE function failed due to limited JIT stack space.
*
* >= PHP 7.0
*/
const JIT_STACKLIMIT_ERROR = '2b4859eb-7feb-4672-9e2a-a09c99b0785f';

/**
* Other error.
*/
const OTHER_ERROR = '026d3188-5252-42ad-98c0-b053fa97cf6f';

const MESSAGE = 'Invalid regular expression.';

/**
* @var string[]
*/
protected static $errorNames = [
self::INTERNAL_ERROR => 'INTERNAL_ERROR',
self::BACKTRACK_LIMIT_ERROR => 'BACKTRACK_LIMIT_ERROR',
Expand All @@ -41,5 +75,8 @@ class Regex extends Constraint
self::OTHER_ERROR => 'OTHER_ERROR',
];

/**
* @var string
*/
public $message = self::MESSAGE;
}
24 changes: 15 additions & 9 deletions tests/Tests/RegexValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,14 @@ public function testRegexes($regex, $valid, $code = null)
public function regexProvider()
{
return [
['/abc/', true],
['/abc', false, Regex::OTHER_ERROR],
['/foo/', true],
['/foo/i', true],
['`foo`', true],
['/(?:(?:(?:(?:(?:(?:(?:[ \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]*(?:\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-\x7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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])))?\[((?:(?:[ \t]*(?:\r\n))?[ \t])?(?:(?:[\x01-\x08\x0B\x0C\x0E-\x19\x7F]|[\x21-\x5A\x5E-\x7E])|(?:\\[\x00-\x08\x0B\x0C\x0E-\x7F])))*?(?:(?:[ \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])?(\((?:(?:(?:[ \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])))?)))/', true],
['/(foo/', false, Regex::OTHER_ERROR],
['/foo//', false, Regex::OTHER_ERROR],
['/?:foo/', false, Regex::OTHER_ERROR],
['/?P<foo/', false, Regex::OTHER_ERROR],
];
}

Expand All @@ -80,13 +86,13 @@ public function testRegexErrors($regex, $errorCode, $code)
public function regexErrorProvider()
{
return [
['/abc', \PREG_NO_ERROR, Regex::OTHER_ERROR],
['/abc', \PREG_INTERNAL_ERROR, Regex::INTERNAL_ERROR],
['/abc', \PREG_BACKTRACK_LIMIT_ERROR, Regex::BACKTRACK_LIMIT_ERROR],
['/abc', \PREG_RECURSION_LIMIT_ERROR, Regex::RECURSION_LIMIT_ERROR],
['/abc', \PREG_BAD_UTF8_ERROR, Regex::BAD_UTF8_ERROR],
['/abc', \PREG_BAD_UTF8_OFFSET_ERROR, Regex::BAD_UTF8_OFFSET_ERROR],
['/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],
['/foo', \PREG_NO_ERROR, Regex::OTHER_ERROR],
['/foo', \PREG_INTERNAL_ERROR, Regex::INTERNAL_ERROR],
['/foo', \PREG_BACKTRACK_LIMIT_ERROR, Regex::BACKTRACK_LIMIT_ERROR],
['/foo', \PREG_RECURSION_LIMIT_ERROR, Regex::RECURSION_LIMIT_ERROR],
['/foo', \PREG_BAD_UTF8_ERROR, Regex::BAD_UTF8_ERROR],
['/foo', \PREG_BAD_UTF8_OFFSET_ERROR, Regex::BAD_UTF8_OFFSET_ERROR],
['/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],
];
}
}

0 comments on commit 16f9950

Please sign in to comment.