PregTest.php 8.3 KB

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