|
11 | 11 |
|
12 | 12 | namespace Symfony\Component\ExpressionLanguage\Tests\Node;
|
13 | 13 |
|
| 14 | +use Symfony\Component\ExpressionLanguage\Compiler; |
14 | 15 | use Symfony\Component\ExpressionLanguage\Node\ArrayNode;
|
15 | 16 | use Symfony\Component\ExpressionLanguage\Node\BinaryNode;
|
16 | 17 | use Symfony\Component\ExpressionLanguage\Node\ConstantNode;
|
| 18 | +use Symfony\Component\ExpressionLanguage\Node\NameNode; |
| 19 | +use Symfony\Component\ExpressionLanguage\SyntaxError; |
17 | 20 |
|
18 | 21 | class BinaryNodeTest extends AbstractNodeTest
|
19 | 22 | {
|
@@ -111,7 +114,7 @@ public function getCompileData()
|
111 | 114 |
|
112 | 115 | ['range(1, 3)', new BinaryNode('..', new ConstantNode(1), new ConstantNode(3))],
|
113 | 116 |
|
114 |
| - ['preg_match("/^[a-z]+/i\$/", "abc")', new BinaryNode('matches', new ConstantNode('abc'), new ConstantNode('/^[a-z]+/i$/'))], |
| 117 | + ['(static function ($regexp, $str) { set_error_handler(function ($t, $m) use ($regexp, $str) { throw new \Symfony\Component\ExpressionLanguage\SyntaxError(sprintf(\'Regexp "%s" passed to "matches" is not valid\', $regexp).substr($m, 12)); }); try { return preg_match($regexp, $str); } finally { restore_error_handler(); } })("/^[a-z]+\$/", "abc")', new BinaryNode('matches', new ConstantNode('abc'), new ConstantNode('/^[a-z]+$/'))], |
115 | 118 | ];
|
116 | 119 | }
|
117 | 120 |
|
@@ -160,7 +163,42 @@ public function getDumpData()
|
160 | 163 |
|
161 | 164 | ['(1 .. 3)', new BinaryNode('..', new ConstantNode(1), new ConstantNode(3))],
|
162 | 165 |
|
163 |
| - ['("abc" matches "/^[a-z]+/i$/")', new BinaryNode('matches', new ConstantNode('abc'), new ConstantNode('/^[a-z]+/i$/'))], |
| 166 | + ['("abc" matches "/^[a-z]+$/")', new BinaryNode('matches', new ConstantNode('abc'), new ConstantNode('/^[a-z]+$/'))], |
164 | 167 | ];
|
165 | 168 | }
|
| 169 | + |
| 170 | + public function testEvaluateMatchesWithInvalidRegexp() |
| 171 | + { |
| 172 | + $node = new BinaryNode('matches', new ConstantNode('abc'), new ConstantNode('this is not a regexp')); |
| 173 | + |
| 174 | + $this->expectExceptionObject(new SyntaxError('Regexp "this is not a regexp" passed to "matches" is not valid: Delimiter must not be alphanumeric or backslash')); |
| 175 | + $node->evaluate([], []); |
| 176 | + } |
| 177 | + |
| 178 | + public function testEvaluateMatchesWithInvalidRegexpAsExpression() |
| 179 | + { |
| 180 | + $node = new BinaryNode('matches', new ConstantNode('abc'), new NameNode('regexp')); |
| 181 | + |
| 182 | + $this->expectExceptionObject(new SyntaxError('Regexp "this is not a regexp" passed to "matches" is not valid: Delimiter must not be alphanumeric or backslash')); |
| 183 | + $node->evaluate([], ['regexp' => 'this is not a regexp']); |
| 184 | + } |
| 185 | + |
| 186 | + public function testCompileMatchesWithInvalidRegexp() |
| 187 | + { |
| 188 | + $node = new BinaryNode('matches', new ConstantNode('abc'), new ConstantNode('this is not a regexp')); |
| 189 | + |
| 190 | + $this->expectExceptionObject(new SyntaxError('Regexp "this is not a regexp" passed to "matches" is not valid: Delimiter must not be alphanumeric or backslash')); |
| 191 | + $compiler = new Compiler([]); |
| 192 | + $node->compile($compiler); |
| 193 | + } |
| 194 | + |
| 195 | + public function testCompileMatchesWithInvalidRegexpAsExpression() |
| 196 | + { |
| 197 | + $node = new BinaryNode('matches', new ConstantNode('abc'), new NameNode('regexp')); |
| 198 | + |
| 199 | + $this->expectExceptionObject(new SyntaxError('Regexp "this is not a regexp" passed to "matches" is not valid: Delimiter must not be alphanumeric or backslash')); |
| 200 | + $compiler = new Compiler([]); |
| 201 | + $node->compile($compiler); |
| 202 | + eval('$regexp = "this is not a regexp"; '.$compiler->getSource().';'); |
| 203 | + } |
166 | 204 | }
|
0 commit comments