PregTest.php 8.1 KB

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