PregTest.php 8.0 KB

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