PregTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 = 1 === preg_match($pattern, $subject, $expectedMatches);
  36. $actualResult = Preg::match($pattern, $subject, $actualMatches);
  37. self::assertSame($expectedResult, $actualResult);
  38. self::assertSame($expectedMatches, $actualMatches);
  39. }
  40. public static function providePatternValidationCases(): iterable
  41. {
  42. yield 'invalid_blank' => ['', null, PregException::class];
  43. yield 'invalid_open' => ["\1", null, PregException::class, "'\1' found"];
  44. yield 'valid_control_character_delimiter' => ["\1\1", true];
  45. yield 'invalid_control_character_modifier' => ["\1\1\1", null, PregException::class, ' Unknown modifier '];
  46. yield 'valid_slate' => ['//', true];
  47. yield 'valid_paired' => ['()', true];
  48. yield 'paired_non_utf8_only' => ["((*UTF8)\xFF)", null, PregException::class, 'UTF-8'];
  49. yield 'valid_paired_non_utf8_only' => ["(\xFF)", true];
  50. yield 'php_version_dependent' => ['([\\R])', false, PregException::class, 'Compilation failed: escape sequence is invalid '];
  51. $nullByteMessage = \PHP_VERSION_ID >= 8_02_00 ? 'NUL is not a valid modifier' : 'Null byte in regex';
  52. yield 'null_byte_injection' => ['()'."\0", null, PregException::class, " {$nullByteMessage} "];
  53. }
  54. /**
  55. * @dataProvider providePatternValidationCases
  56. */
  57. public function testPatternValidation(string $pattern, ?bool $expected = null, ?string $expectedException = null, ?string $expectedMessage = null): void
  58. {
  59. $setup = function () use ($expectedException, $expectedMessage): bool {
  60. $i = 0;
  61. if (null !== $expectedException) {
  62. ++$i;
  63. $this->expectException($expectedException);
  64. }
  65. if (null !== $expectedMessage) {
  66. ++$i;
  67. $this->expectExceptionMessage($expectedMessage);
  68. }
  69. return (bool) $i;
  70. };
  71. try {
  72. $actual = Preg::match($pattern, "The quick brown \xFF\x00\\xXX jumps over the lazy dog\n");
  73. } catch (\Exception $ex) {
  74. $setup();
  75. throw $ex;
  76. }
  77. if (null !== $expected) {
  78. self::assertSame($expected, $actual);
  79. return;
  80. }
  81. if (!$setup()) {
  82. $this->addToAssertionCount(1);
  83. }
  84. }
  85. /**
  86. * @dataProvider providePatternValidationCases
  87. */
  88. public function testPatternsValidation(string $pattern, ?bool $expected = null, ?string $expectedException = null, ?string $expectedMessage = null): void
  89. {
  90. $setup = function () use ($expectedException, $expectedMessage): bool {
  91. $i = 0;
  92. if (null !== $expectedException) {
  93. ++$i;
  94. $this->expectException($expectedException);
  95. }
  96. if (null !== $expectedMessage) {
  97. ++$i;
  98. $this->expectExceptionMessage($expectedMessage);
  99. }
  100. return (bool) $i;
  101. };
  102. try {
  103. $buffer = "The quick brown \xFF\x00\\xXX jumps over the lazy dog\n";
  104. $actual = $buffer !== Preg::replace($pattern, 'abc', $buffer);
  105. } catch (\Exception $ex) {
  106. $setup();
  107. throw $ex;
  108. }
  109. if (null !== $expected) {
  110. self::assertSame($expected, $actual);
  111. return;
  112. }
  113. if (!$setup()) {
  114. $this->addToAssertionCount(1);
  115. }
  116. }
  117. public function testMatchAllFailing(): void
  118. {
  119. $this->expectException(PregException::class);
  120. $this->expectExceptionMessage('Preg::matchAll(): Invalid PCRE pattern ""');
  121. Preg::matchAll('', 'foo', $matches);
  122. }
  123. /**
  124. * @dataProvider provideCommonCases
  125. */
  126. public function testMatchAll(string $pattern, string $subject): void
  127. {
  128. $expectedResult = preg_match_all($pattern, $subject, $expectedMatches);
  129. $actualResult = Preg::matchAll($pattern, $subject, $actualMatches);
  130. self::assertSame($expectedResult, $actualResult);
  131. self::assertSame($expectedMatches, $actualMatches);
  132. }
  133. public function testReplaceFailing(): void
  134. {
  135. $this->expectException(PregException::class);
  136. $this->expectExceptionMessageMatches('~\Q\Preg::replace()\E: Invalid PCRE pattern "": \(code: \d+\) [^(]+ \(version: \d+~');
  137. Preg::replace('', 'foo', 'bar');
  138. }
  139. /**
  140. * @param string|string[] $pattern
  141. * @param string|string[] $subject
  142. *
  143. * @dataProvider provideCommonCases
  144. */
  145. public function testReplace($pattern, $subject): void
  146. {
  147. $expectedResult = preg_replace($pattern, 'foo', $subject);
  148. $actualResult = Preg::replace($pattern, 'foo', $subject);
  149. self::assertSame($expectedResult, $actualResult);
  150. }
  151. public function testReplaceCallbackFailing(): void
  152. {
  153. $this->expectException(PregException::class);
  154. $this->expectExceptionMessage('Preg::replaceCallback(): Invalid PCRE pattern ""');
  155. Preg::replaceCallback('', 'sort', 'foo');
  156. }
  157. /**
  158. * @param string|string[] $pattern
  159. * @param string|string[] $subject
  160. *
  161. * @dataProvider provideCommonCases
  162. */
  163. public function testReplaceCallback($pattern, $subject): void
  164. {
  165. $callback = static fn (array $x): string => implode('-', $x);
  166. $expectedResult = preg_replace_callback($pattern, $callback, $subject);
  167. $actualResult = Preg::replaceCallback($pattern, $callback, $subject);
  168. self::assertSame($expectedResult, $actualResult);
  169. }
  170. public static function provideCommonCases(): iterable
  171. {
  172. yield ['/u/u', 'u'];
  173. yield ['/u/u', 'u/u'];
  174. yield ['/./', \chr(224).'bc'];
  175. yield ['/à/', 'àbc'];
  176. yield ['/'.\chr(224).'|í/', 'àbc'];
  177. }
  178. public function testSplitFailing(): void
  179. {
  180. $this->expectException(PregException::class);
  181. $this->expectExceptionMessage('Preg::split(): Invalid PCRE pattern ""');
  182. Preg::split('', 'foo');
  183. }
  184. /**
  185. * @dataProvider provideCommonCases
  186. */
  187. public function testSplit(string $pattern, string $subject): void
  188. {
  189. $expectedResult = preg_split($pattern, $subject);
  190. $actualResult = Preg::split($pattern, $subject);
  191. self::assertSame($expectedResult, $actualResult);
  192. }
  193. public function testCorrectnessForUtf8String(): void
  194. {
  195. $pattern = '/./';
  196. $subject = 'àbc';
  197. Preg::match($pattern, $subject, $methodMatches);
  198. preg_match($pattern, $subject, $functionMatches);
  199. self::assertSame(['à'], $methodMatches);
  200. self::assertNotSame(['à'], $functionMatches);
  201. }
  202. public function testCorrectnessForNonUtf8String(): void
  203. {
  204. $pattern = '/./u';
  205. $subject = \chr(224).'bc';
  206. Preg::match($pattern, $subject, $methodMatches);
  207. preg_match($pattern, $subject, $functionMatches);
  208. self::assertSame([\chr(224)], $methodMatches);
  209. self::assertNotSame([\chr(224)], $functionMatches);
  210. }
  211. }