UtilsTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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\Fixer\FixerInterface;
  13. use PhpCsFixer\Tokenizer\Token;
  14. use PhpCsFixer\Utils;
  15. /**
  16. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  17. * @author Graham Campbell <graham@alt-three.com>
  18. * @author Odín del Río <odin.drp@gmail.com>
  19. *
  20. * @internal
  21. *
  22. * @covers \PhpCsFixer\Utils
  23. */
  24. final class UtilsTest extends TestCase
  25. {
  26. /**
  27. * @param string $expected Camel case string
  28. * @param string $input Input string
  29. *
  30. * @dataProvider provideCamelCaseToUnderscoreCases
  31. */
  32. public function testCamelCaseToUnderscore($expected, $input = null)
  33. {
  34. if (null !== $input) {
  35. $this->assertSame($expected, Utils::camelCaseToUnderscore($input));
  36. }
  37. $this->assertSame($expected, Utils::camelCaseToUnderscore($expected));
  38. }
  39. /**
  40. * @return array
  41. */
  42. public function provideCamelCaseToUnderscoreCases()
  43. {
  44. return [
  45. [
  46. 'dollar_close_curly_braces',
  47. 'DollarCloseCurlyBraces',
  48. ],
  49. [
  50. 'utf8_encoder_fixer',
  51. 'utf8EncoderFixer',
  52. ],
  53. [
  54. 'terminated_with_number10',
  55. 'TerminatedWithNumber10',
  56. ],
  57. [
  58. 'utf8_encoder_fixer',
  59. ],
  60. ];
  61. }
  62. /**
  63. * @param int $expected
  64. * @param int $left
  65. * @param int $right
  66. *
  67. * @dataProvider provideCmpIntCases
  68. */
  69. public function testCmpInt($expected, $left, $right)
  70. {
  71. $this->assertSame($expected, Utils::cmpInt($left, $right));
  72. }
  73. public function provideCmpIntCases()
  74. {
  75. return [
  76. [0, 1, 1],
  77. [0, -1, -1],
  78. [-1, 10, 20],
  79. [-1, -20, -10],
  80. [1, 20, 10],
  81. [1, -10, -20],
  82. ];
  83. }
  84. /**
  85. * @param array $expected
  86. * @param string $input
  87. *
  88. * @dataProvider provideSplitLinesCases
  89. */
  90. public function testSplitLines(array $expected, $input)
  91. {
  92. $this->assertSame($expected, Utils::splitLines($input));
  93. }
  94. public function provideSplitLinesCases()
  95. {
  96. return [
  97. [
  98. ["\t aaa\n", " bbb\n", "\t"],
  99. "\t aaa\n bbb\n\t",
  100. ],
  101. [
  102. ["aaa\r\n", " bbb\r\n"],
  103. "aaa\r\n bbb\r\n",
  104. ],
  105. [
  106. ["aaa\r\n", " bbb\n"],
  107. "aaa\r\n bbb\n",
  108. ],
  109. [
  110. ["aaa\r\n\n\n\r\n", " bbb\n"],
  111. "aaa\r\n\n\n\r\n bbb\n",
  112. ],
  113. ];
  114. }
  115. /**
  116. * @param string $spaces
  117. * @param array|string $input token prototype
  118. *
  119. * @dataProvider provideCalculateTrailingWhitespaceIndentCases
  120. */
  121. public function testCalculateTrailingWhitespaceIndent($spaces, $input)
  122. {
  123. $token = new Token($input);
  124. $this->assertSame($spaces, Utils::calculateTrailingWhitespaceIndent($token));
  125. }
  126. public function provideCalculateTrailingWhitespaceIndentCases()
  127. {
  128. return [
  129. [' ', [T_WHITESPACE, "\n\n "]],
  130. [' ', [T_WHITESPACE, "\r\n\r\r\r "]],
  131. ["\t", [T_WHITESPACE, "\r\n\t"]],
  132. ['', [T_WHITESPACE, "\t\n\r"]],
  133. ['', [T_WHITESPACE, "\n"]],
  134. ['', ''],
  135. ];
  136. }
  137. public function testCalculateTrailingWhitespaceIndentFail()
  138. {
  139. $this->expectException(\InvalidArgumentException::class);
  140. $this->expectExceptionMessage('The given token must be whitespace, got "T_STRING".');
  141. $token = new Token([T_STRING, 'foo']);
  142. Utils::calculateTrailingWhitespaceIndent($token);
  143. }
  144. /**
  145. * @dataProvider provideStableSortCases
  146. */
  147. public function testStableSort(
  148. array $expected,
  149. array $elements,
  150. callable $getComparableValueCallback,
  151. callable $compareValuesCallback
  152. ) {
  153. $this->assertSame(
  154. $expected,
  155. Utils::stableSort($elements, $getComparableValueCallback, $compareValuesCallback)
  156. );
  157. }
  158. public function provideStableSortCases()
  159. {
  160. return [
  161. [
  162. ['a', 'b', 'c', 'd', 'e'],
  163. ['b', 'd', 'e', 'a', 'c'],
  164. static function ($element) { return $element; },
  165. 'strcmp',
  166. ],
  167. [
  168. ['b', 'd', 'e', 'a', 'c'],
  169. ['b', 'd', 'e', 'a', 'c'],
  170. static function ($element) { return 'foo'; },
  171. 'strcmp',
  172. ],
  173. [
  174. ['b', 'd', 'e', 'a', 'c'],
  175. ['b', 'd', 'e', 'a', 'c'],
  176. static function ($element) { return $element; },
  177. static function ($a, $b) { return 0; },
  178. ],
  179. [
  180. ['bar1', 'baz1', 'foo1', 'bar2', 'baz2', 'foo2'],
  181. ['foo1', 'foo2', 'bar1', 'bar2', 'baz1', 'baz2'],
  182. static function ($element) { return preg_replace('/([a-z]+)(\d+)/', '$2$1', $element); },
  183. 'strcmp',
  184. ],
  185. ];
  186. }
  187. public function testSortFixers()
  188. {
  189. $fixers = [
  190. $this->createFixerDouble('f1', 0),
  191. $this->createFixerDouble('f2', -10),
  192. $this->createFixerDouble('f3', 10),
  193. $this->createFixerDouble('f4', -10),
  194. ];
  195. $this->assertSame(
  196. [
  197. $fixers[2],
  198. $fixers[0],
  199. $fixers[1],
  200. $fixers[3],
  201. ],
  202. Utils::sortFixers($fixers)
  203. );
  204. }
  205. public function testNaturalLanguageJoinWithBackticksThrowsInvalidArgumentExceptionForEmptyArray()
  206. {
  207. $this->expectException(\InvalidArgumentException::class);
  208. Utils::naturalLanguageJoinWithBackticks([]);
  209. }
  210. /**
  211. * @dataProvider provideNaturalLanguageJoinWithBackticksCases
  212. *
  213. * @param string $joined
  214. * @param array $names
  215. */
  216. public function testNaturalLanguageJoinWithBackticks($joined, array $names)
  217. {
  218. $this->assertSame($joined, Utils::naturalLanguageJoinWithBackticks($names));
  219. }
  220. public function provideNaturalLanguageJoinWithBackticksCases()
  221. {
  222. return [
  223. [
  224. '`a`',
  225. ['a'],
  226. ],
  227. [
  228. '`a` and `b`',
  229. ['a', 'b'],
  230. ],
  231. [
  232. '`a`, `b` and `c`',
  233. ['a', 'b', 'c'],
  234. ],
  235. ];
  236. }
  237. private function createFixerDouble($name, $priority)
  238. {
  239. $fixer = $this->prophesize(FixerInterface::class);
  240. $fixer->getName()->willReturn($name);
  241. $fixer->getPriority()->willReturn($priority);
  242. return $fixer->reveal();
  243. }
  244. }