|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Rule\Nette; |
| 4 | + |
| 5 | +use PHPStan\Rules\Rule; |
| 6 | +use PHPStan\Testing\RuleTestCase; |
| 7 | +use function sprintf; |
| 8 | +use const PHP_VERSION_ID; |
| 9 | + |
| 10 | +/** |
| 11 | + * @extends RuleTestCase<RegularExpressionPatternRule> |
| 12 | + */ |
| 13 | +class RegularExpressionPatternRuleTest extends RuleTestCase |
| 14 | +{ |
| 15 | + |
| 16 | + protected function getRule(): Rule |
| 17 | + { |
| 18 | + return new RegularExpressionPatternRule(); |
| 19 | + } |
| 20 | + |
| 21 | + public function testValidRegexPatternBefore73(): void |
| 22 | + { |
| 23 | + if (PHP_VERSION_ID >= 70300) { |
| 24 | + self::markTestSkipped('This test requires PHP < 7.3.0'); |
| 25 | + } |
| 26 | + |
| 27 | + $this->analyse( |
| 28 | + [__DIR__ . '/data/valid-regex-pattern.php'], |
| 29 | + [ |
| 30 | + [ |
| 31 | + 'Regex pattern is invalid: Delimiter must not be alphanumeric or backslash in pattern: nok', |
| 32 | + 6, |
| 33 | + ], |
| 34 | + [ |
| 35 | + 'Regex pattern is invalid: Compilation failed: missing ) at offset 1 in pattern: ~(~', |
| 36 | + 7, |
| 37 | + ], |
| 38 | + [ |
| 39 | + 'Regex pattern is invalid: Delimiter must not be alphanumeric or backslash in pattern: nok', |
| 40 | + 11, |
| 41 | + ], |
| 42 | + [ |
| 43 | + 'Regex pattern is invalid: Compilation failed: missing ) at offset 1 in pattern: ~(~', |
| 44 | + 12, |
| 45 | + ], |
| 46 | + [ |
| 47 | + 'Regex pattern is invalid: Delimiter must not be alphanumeric or backslash in pattern: nok', |
| 48 | + 16, |
| 49 | + ], |
| 50 | + [ |
| 51 | + 'Regex pattern is invalid: Compilation failed: missing ) at offset 1 in pattern: ~(~', |
| 52 | + 17, |
| 53 | + ], |
| 54 | + [ |
| 55 | + 'Regex pattern is invalid: Delimiter must not be alphanumeric or backslash in pattern: nok', |
| 56 | + 21, |
| 57 | + ], |
| 58 | + [ |
| 59 | + 'Regex pattern is invalid: Compilation failed: missing ) at offset 1 in pattern: ~(~', |
| 60 | + 22, |
| 61 | + ], |
| 62 | + [ |
| 63 | + 'Regex pattern is invalid: Delimiter must not be alphanumeric or backslash in pattern: nok', |
| 64 | + 26, |
| 65 | + ], |
| 66 | + [ |
| 67 | + 'Regex pattern is invalid: Compilation failed: missing ) at offset 1 in pattern: ~(~', |
| 68 | + 26, |
| 69 | + ], |
| 70 | + ] |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + public function testValidRegexPatternAfter73(): void |
| 75 | + { |
| 76 | + if (PHP_VERSION_ID < 70300) { |
| 77 | + self::markTestSkipped('This test requires PHP >= 7.3.0'); |
| 78 | + } |
| 79 | + |
| 80 | + $messagePart = 'alphanumeric or backslash'; |
| 81 | + if (PHP_VERSION_ID >= 80200) { |
| 82 | + $messagePart = 'alphanumeric, backslash, or NUL'; |
| 83 | + } |
| 84 | + |
| 85 | + $this->analyse( |
| 86 | + [__DIR__ . '/data/valid-regex-pattern.php'], |
| 87 | + [ |
| 88 | + [ |
| 89 | + sprintf('Regex pattern is invalid: Delimiter must not be %s in pattern: nok', $messagePart), |
| 90 | + 6, |
| 91 | + ], |
| 92 | + [ |
| 93 | + 'Regex pattern is invalid: Compilation failed: missing closing parenthesis at offset 1 in pattern: ~(~', |
| 94 | + 7, |
| 95 | + ], |
| 96 | + [ |
| 97 | + sprintf('Regex pattern is invalid: Delimiter must not be %s in pattern: nok', $messagePart), |
| 98 | + 11, |
| 99 | + ], |
| 100 | + [ |
| 101 | + 'Regex pattern is invalid: Compilation failed: missing closing parenthesis at offset 1 in pattern: ~(~', |
| 102 | + 12, |
| 103 | + ], |
| 104 | + [ |
| 105 | + sprintf('Regex pattern is invalid: Delimiter must not be %s in pattern: nok', $messagePart), |
| 106 | + 16, |
| 107 | + ], |
| 108 | + [ |
| 109 | + 'Regex pattern is invalid: Compilation failed: missing closing parenthesis at offset 1 in pattern: ~(~', |
| 110 | + 17, |
| 111 | + ], |
| 112 | + [ |
| 113 | + sprintf('Regex pattern is invalid: Delimiter must not be %s in pattern: nok', $messagePart), |
| 114 | + 21, |
| 115 | + ], |
| 116 | + [ |
| 117 | + 'Regex pattern is invalid: Compilation failed: missing closing parenthesis at offset 1 in pattern: ~(~', |
| 118 | + 22, |
| 119 | + ], |
| 120 | + [ |
| 121 | + sprintf('Regex pattern is invalid: Delimiter must not be %s in pattern: nok', $messagePart), |
| 122 | + 26, |
| 123 | + ], |
| 124 | + [ |
| 125 | + 'Regex pattern is invalid: Compilation failed: missing closing parenthesis at offset 1 in pattern: ~(~', |
| 126 | + 26, |
| 127 | + ], |
| 128 | + ] |
| 129 | + ); |
| 130 | + } |
| 131 | + |
| 132 | +} |
0 commit comments