|
11 | 11 |
|
12 | 12 | namespaceSymfony\Component\ExpressionLanguage\Tests\Node; |
13 | 13 |
|
| 14 | +useSymfony\Component\ExpressionLanguage\Compiler; |
14 | 15 | useSymfony\Component\ExpressionLanguage\Node\ArrayNode; |
15 | 16 | useSymfony\Component\ExpressionLanguage\Node\BinaryNode; |
16 | 17 | useSymfony\Component\ExpressionLanguage\Node\ConstantNode; |
| 18 | +useSymfony\Component\ExpressionLanguage\Node\NameNode; |
| 19 | +useSymfony\Component\ExpressionLanguage\SyntaxError; |
17 | 20 |
|
18 | 21 | class BinaryNodeTestextends AbstractNodeTest |
19 | 22 | { |
@@ -111,7 +114,7 @@ public function getCompileData() |
111 | 114 |
|
112 | 115 | ['range(1, 3)',newBinaryNode('..',newConstantNode(1),newConstantNode(3))], |
113 | 116 |
|
114 | | - ['preg_match("/^[a-z]+/i\$/", "abc")',newBinaryNode('matches',newConstantNode('abc'),newConstantNode('/^[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 { returnpreg_match($regexp, $str); } finally { restore_error_handler(); } })("/^[a-z]+\$/", "abc")',newBinaryNode('matches',newConstantNode('abc'),newConstantNode('/^[a-z]+$/'))], |
115 | 118 | ]; |
116 | 119 | } |
117 | 120 |
|
@@ -160,7 +163,42 @@ public function getDumpData() |
160 | 163 |
|
161 | 164 | ['(1 .. 3)',newBinaryNode('..',newConstantNode(1),newConstantNode(3))], |
162 | 165 |
|
163 | | - ['("abc" matches "/^[a-z]+/i$/")',newBinaryNode('matches',newConstantNode('abc'),newConstantNode('/^[a-z]+/i$/'))], |
| 166 | + ['("abc" matches "/^[a-z]+$/")',newBinaryNode('matches',newConstantNode('abc'),newConstantNode('/^[a-z]+$/'))], |
164 | 167 | ]; |
165 | 168 | } |
| 169 | + |
| 170 | +publicfunctiontestEvaluateMatchesWithInvalidRegexp() |
| 171 | + { |
| 172 | +$node =newBinaryNode('matches',newConstantNode('abc'),newConstantNode('this is not a regexp')); |
| 173 | + |
| 174 | +$this->expectExceptionObject(newSyntaxError('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 | +publicfunctiontestEvaluateMatchesWithInvalidRegexpAsExpression() |
| 179 | + { |
| 180 | +$node =newBinaryNode('matches',newConstantNode('abc'),newNameNode('regexp')); |
| 181 | + |
| 182 | +$this->expectExceptionObject(newSyntaxError('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 | +publicfunctiontestCompileMatchesWithInvalidRegexp() |
| 187 | + { |
| 188 | +$node =newBinaryNode('matches',newConstantNode('abc'),newConstantNode('this is not a regexp')); |
| 189 | + |
| 190 | +$this->expectExceptionObject(newSyntaxError('Regexp "this is not a regexp" passed to "matches" is not valid: Delimiter must not be alphanumeric or backslash')); |
| 191 | +$compiler =newCompiler([]); |
| 192 | +$node->compile($compiler); |
| 193 | + } |
| 194 | + |
| 195 | +publicfunctiontestCompileMatchesWithInvalidRegexpAsExpression() |
| 196 | + { |
| 197 | +$node =newBinaryNode('matches',newConstantNode('abc'),newNameNode('regexp')); |
| 198 | + |
| 199 | +$this->expectExceptionObject(newSyntaxError('Regexp "this is not a regexp" passed to "matches" is not valid: Delimiter must not be alphanumeric or backslash')); |
| 200 | +$compiler =newCompiler([]); |
| 201 | +$node->compile($compiler); |
| 202 | +eval('$regexp = "this is not a regexp";'.$compiler->getSource().';'); |
| 203 | + } |
166 | 204 | } |