PregTest.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer\Tests;
  13. use PhpCsFixer\Preg;
  14. use PhpCsFixer\PregException;
  15. /**
  16. * @author Kuba Werłos <werlos@gmail.com>
  17. *
  18. * @covers \PhpCsFixer\Preg
  19. *
  20. * @internal
  21. */
  22. final class PregTest extends TestCase
  23. {
  24. public function testMatchFailing(): void
  25. {
  26. $this->expectException(PregException::class);
  27. $this->expectExceptionMessage('Preg::match(): Invalid PCRE pattern ""');
  28. Preg::match('', 'foo', $matches);
  29. }
  30. /**
  31. * @dataProvider provideCommonCases
  32. */
  33. public function testMatch(string $pattern, string $subject): void
  34. {
  35. $expectedResult = preg_match($pattern, $subject, $expectedMatches);
  36. $actualResult = Preg::match($pattern, $subject, $actualMatches);
  37. static::assertSame($expectedResult, $actualResult);
  38. static::assertSame($expectedMatches, $actualMatches);
  39. }
  40. public static function providePatternValidationCases(): iterable
  41. {
  42. yield from [
  43. 'invalid_blank' => ['', null, PregException::class],
  44. 'invalid_open' => ["\1", null, PregException::class, "'\1' found"],
  45. 'valid_control_character_delimiter' => ["\1\1", 1],
  46. 'invalid_control_character_modifier' => ["\1\1\1", null, PregException::class, ' Unknown modifier '],
  47. 'valid_slate' => ['//', 1],
  48. 'valid_paired' => ['()', 1],
  49. 'paired_non_utf8_only' => ["((*UTF8)\xFF)", null, PregException::class, 'UTF-8'],
  50. 'valid_paired_non_utf8_only' => ["(\xFF)", 1],
  51. 'php_version_dependent' => ['([\\R])', 0, PregException::class, 'Compilation failed: escape sequence is invalid '],
  52. ];
  53. $nullByteMessage = \PHP_VERSION_ID >= 8_02_00 ? 'NUL is not a valid modifier' : 'Null byte in regex';
  54. yield 'null_byte_injection' => ['()'."\0", null, PregException::class, " {$nullByteMessage} "];
  55. }
  56. /**
  57. * @dataProvider providePatternValidationCases
  58. */
  59. public function testPatternValidation(string $pattern, ?int $expected = null, ?string $expectedException = null, ?string $expectedMessage = null): void
  60. {
  61. $setup = function () use ($expectedException, $expectedMessage): bool {
  62. $i = 0;
  63. if (null !== $expectedException) {
  64. ++$i;
  65. $this->expectException($expectedException);
  66. }
  67. if (null !== $expectedMessage) {
  68. ++$i;
  69. $this->expectExceptionMessage($expectedMessage);
  70. }
  71. return (bool) $i;
  72. };
  73. try {
  74. $actual = Preg::match($pattern, "The quick brown \xFF\x00\\xXX jumps over the lazy dog\n");
  75. } catch (\Exception $ex) {
  76. $setup();
  77. throw $ex;
  78. }
  79. if (null !== $expected) {
  80. static::assertSame($expected, $actual);
  81. return;
  82. }
  83. if (!$setup()) {
  84. $this->addToAssertionCount(1);
  85. }
  86. }
  87. /**
  88. * @dataProvider providePatternValidationCases
  89. */
  90. public function testPatternsValidation(string $pattern, ?int $expected = null, ?string $expectedException = null, ?string $expectedMessage = null): void
  91. {
  92. $setup = function () use ($expectedException, $expectedMessage): bool {
  93. $i = 0;
  94. if (null !== $expectedException) {
  95. ++$i;
  96. $this->expectException($expectedException);
  97. }
  98. if (null !== $expectedMessage) {
  99. ++$i;
  100. $this->expectExceptionMessage($expectedMessage);
  101. }
  102. return (bool) $i;
  103. };
  104. try {
  105. $buffer = "The quick brown \xFF\x00\\xXX jumps over the lazy dog\n";
  106. $actual = $buffer !== Preg::replace($pattern, 'abc', $buffer);
  107. } catch (\Exception $ex) {
  108. $setup();
  109. throw $ex;
  110. }
  111. if (null !== $expected) {
  112. static::assertSame((bool) $expected, $actual);
  113. return;
  114. }
  115. if (!$setup()) {
  116. $this->addToAssertionCount(1);
  117. }
  118. }
  119. public function testMatchAllFailing(): void
  120. {
  121. $this->expectException(PregException::class);
  122. $this->expectExceptionMessage('Preg::matchAll(): Invalid PCRE pattern ""');
  123. Preg::matchAll('', 'foo', $matches);
  124. }
  125. /**
  126. * @dataProvider provideCommonCases
  127. */
  128. public function testMatchAll(string $pattern, string $subject): void
  129. {
  130. $expectedResult = preg_match_all($pattern, $subject, $expectedMatches);
  131. $actualResult = Preg::matchAll($pattern, $subject, $actualMatches);
  132. static::assertSame($expectedResult, $actualResult);
  133. static::assertSame($expectedMatches, $actualMatches);
  134. }
  135. public function testReplaceFailing(): void
  136. {
  137. $this->expectException(PregException::class);
  138. $this->expectExceptionMessageMatches('~\Q\Preg::replace()\E: Invalid PCRE pattern "": \(code: \d+\) [^(]+ \(version: \d+~');
  139. Preg::replace('', 'foo', 'bar');
  140. }
  141. /**
  142. * @param string|string[] $pattern
  143. * @param string|string[] $subject
  144. *
  145. * @dataProvider provideCommonCases
  146. */
  147. public function testReplace($pattern, $subject): void
  148. {
  149. $expectedResult = preg_replace($pattern, 'foo', $subject);
  150. $actualResult = Preg::replace($pattern, 'foo', $subject);
  151. static::assertSame($expectedResult, $actualResult);
  152. }
  153. public function testReplaceCallbackFailing(): void
  154. {
  155. $this->expectException(PregException::class);
  156. $this->expectExceptionMessage('Preg::replaceCallback(): Invalid PCRE pattern ""');
  157. Preg::replaceCallback('', 'sort', 'foo');
  158. }
  159. /**
  160. * @param string|string[] $pattern
  161. * @param string|string[] $subject
  162. *
  163. * @dataProvider provideCommonCases
  164. */
  165. public function testReplaceCallback($pattern, $subject): void
  166. {
  167. $callback = static fn (array $x): string => implode('-', $x);
  168. $expectedResult = preg_replace_callback($pattern, $callback, $subject);
  169. $actualResult = Preg::replaceCallback($pattern, $callback, $subject);
  170. static::assertSame($expectedResult, $actualResult);
  171. }
  172. public static function provideCommonCases(): array
  173. {
  174. return [
  175. ['/u/u', 'u'],
  176. ['/u/u', 'u/u'],
  177. ['/./', \chr(224).'bc'],
  178. ['/à/', 'àbc'],
  179. ['/'.\chr(224).'|í/', 'àbc'],
  180. ];
  181. }
  182. public function testSplitFailing(): void
  183. {
  184. $this->expectException(PregException::class);
  185. $this->expectExceptionMessage('Preg::split(): Invalid PCRE pattern ""');
  186. Preg::split('', 'foo');
  187. }
  188. /**
  189. * @dataProvider provideCommonCases
  190. */
  191. public function testSplit(string $pattern, string $subject): void
  192. {
  193. $expectedResult = preg_split($pattern, $subject);
  194. $actualResult = Preg::split($pattern, $subject);
  195. static::assertSame($expectedResult, $actualResult);
  196. }
  197. public function testCorrectnessForUtf8String(): void
  198. {
  199. $pattern = '/./';
  200. $subject = 'àbc';
  201. Preg::match($pattern, $subject, $methodMatches);
  202. preg_match($pattern, $subject, $functionMatches);
  203. static::assertSame(['à'], $methodMatches);
  204. static::assertNotSame(['à'], $functionMatches);
  205. }
  206. public function testCorrectnessForNonUtf8String(): void
  207. {
  208. $pattern = '/./u';
  209. $subject = \chr(224).'bc';
  210. Preg::match($pattern, $subject, $methodMatches);
  211. preg_match($pattern, $subject, $functionMatches);
  212. static::assertSame([\chr(224)], $methodMatches);
  213. static::assertNotSame([\chr(224)], $functionMatches);
  214. }
  215. }