PregTest.php 7.8 KB

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