PregTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. /**
  14. * @author Kuba Werłos <werlos@gmail.com>
  15. *
  16. * @covers \PhpCsFixer\Preg
  17. *
  18. * @internal
  19. */
  20. final class PregTest extends TestCase
  21. {
  22. public function testMatchFailing()
  23. {
  24. $this->expectException(
  25. 'PhpCsFixer\\PregException'
  26. );
  27. $this->expectExceptionMessage(
  28. 'Error occurred when calling preg_match.'
  29. );
  30. Preg::match('', 'foo', $matches);
  31. }
  32. /**
  33. * @param string $pattern
  34. * @param string $subject
  35. *
  36. * @dataProvider provideCommonCases
  37. */
  38. public function testMatch($pattern, $subject)
  39. {
  40. $expectedResult = preg_match($pattern, $subject, $expectedMatches);
  41. $actualResult = Preg::match($pattern, $subject, $actualMatches);
  42. $this->assertSame($expectedResult, $actualResult);
  43. $this->assertSame($expectedMatches, $actualMatches);
  44. }
  45. public function testMatchAllFailing()
  46. {
  47. $this->expectException(
  48. 'PhpCsFixer\\PregException'
  49. );
  50. $this->expectExceptionMessage(
  51. 'Error occurred when calling preg_match_all.'
  52. );
  53. Preg::matchAll('', 'foo', $matches);
  54. }
  55. /**
  56. * @param string $pattern
  57. * @param string $subject
  58. *
  59. * @dataProvider provideCommonCases
  60. */
  61. public function testMatchAll($pattern, $subject)
  62. {
  63. $expectedResult = preg_match_all($pattern, $subject, $expectedMatches);
  64. $actualResult = Preg::matchAll($pattern, $subject, $actualMatches);
  65. $this->assertSame($expectedResult, $actualResult);
  66. $this->assertSame($expectedMatches, $actualMatches);
  67. }
  68. public function testReplaceFailing()
  69. {
  70. $this->expectException(
  71. 'PhpCsFixer\\PregException'
  72. );
  73. $this->expectExceptionMessage(
  74. 'Error occurred when calling preg_replace.'
  75. );
  76. Preg::replace('', 'foo', 'bar');
  77. }
  78. /**
  79. * @param string $pattern
  80. * @param string $subject
  81. *
  82. * @dataProvider provideCommonCases
  83. * @dataProvider provideArrayOfPatternsCases
  84. */
  85. public function testReplace($pattern, $subject)
  86. {
  87. $expectedResult = preg_replace($pattern, 'foo', $subject);
  88. $actualResult = Preg::replace($pattern, 'foo', $subject);
  89. $this->assertSame($expectedResult, $actualResult);
  90. }
  91. public function testReplaceCallbackFailing()
  92. {
  93. $this->expectException(
  94. 'PhpCsFixer\\PregException'
  95. );
  96. $this->expectExceptionMessage(
  97. 'Error occurred when calling preg_replace_callback.'
  98. );
  99. Preg::replaceCallback('', 'sort', 'foo');
  100. }
  101. /**
  102. * @param string $pattern
  103. * @param string $subject
  104. *
  105. * @dataProvider provideCommonCases
  106. * @dataProvider provideArrayOfPatternsCases
  107. */
  108. public function testReplaceCallback($pattern, $subject)
  109. {
  110. $callback = function (array $x) { return implode('-', $x); };
  111. $expectedResult = preg_replace_callback($pattern, $callback, $subject);
  112. $actualResult = Preg::replaceCallback($pattern, $callback, $subject);
  113. $this->assertSame($expectedResult, $actualResult);
  114. }
  115. public function provideCommonCases()
  116. {
  117. return [
  118. ['/u/u', 'u'],
  119. ['/u/u', 'u/u'],
  120. ['/./', chr(224).'bc'],
  121. ['/à/', 'àbc'],
  122. ['/'.chr(224).'|í/', 'àbc'],
  123. ];
  124. }
  125. public function provideArrayOfPatternsCases()
  126. {
  127. return [
  128. [['/à/', '/í/'], 'Tàíl'],
  129. [['/'.chr(174).'/', '/'.chr(224).'/'], 'foo'],
  130. ];
  131. }
  132. public function testSplitFailing()
  133. {
  134. $this->expectException(
  135. 'PhpCsFixer\\PregException'
  136. );
  137. $this->expectExceptionMessage(
  138. 'Error occurred when calling preg_split.'
  139. );
  140. Preg::split('', 'foo');
  141. }
  142. /**
  143. * @param string $pattern
  144. *
  145. * @dataProvider provideCommonCases
  146. */
  147. public function testSplit($pattern)
  148. {
  149. $expectedResult = preg_split($pattern, 'foo');
  150. $actualResult = Preg::split($pattern, 'foo');
  151. $this->assertSame($expectedResult, $actualResult);
  152. }
  153. public function testCorrectnessForUtf8String()
  154. {
  155. $pattern = '/./';
  156. $subject = 'àbc';
  157. Preg::match($pattern, $subject, $methodMatches);
  158. preg_match($pattern, $subject, $functionMatches);
  159. $this->assertSame(['à'], $methodMatches);
  160. $this->assertNotSame(['à'], $functionMatches);
  161. }
  162. public function testCorrectnessForNonUtf8String()
  163. {
  164. $pattern = '/./u';
  165. $subject = chr(224).'bc';
  166. Preg::match($pattern, $subject, $methodMatches);
  167. preg_match($pattern, $subject, $functionMatches);
  168. $this->assertSame([chr(224)], $methodMatches);
  169. $this->assertNotSame([chr(224)], $functionMatches);
  170. }
  171. }