UtilsTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. static::assertSame($expected, Utils::camelCaseToUnderscore($input));
  36. }
  37. static::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. 'a',
  62. 'A',
  63. ],
  64. [
  65. 'aa',
  66. 'AA',
  67. ],
  68. [
  69. 'foo',
  70. 'FOO',
  71. ],
  72. [
  73. 'foo_bar_baz',
  74. 'FooBarBAZ',
  75. ],
  76. [
  77. 'foo_bar_baz',
  78. 'FooBARBaz',
  79. ],
  80. [
  81. 'foo_bar_baz',
  82. 'FOOBarBaz',
  83. ],
  84. [
  85. 'mr_t',
  86. 'MrT',
  87. ],
  88. ];
  89. }
  90. /**
  91. * @param int $expected
  92. * @param int $left
  93. * @param int $right
  94. *
  95. * @dataProvider provideCmpIntCases
  96. */
  97. public function testCmpInt($expected, $left, $right)
  98. {
  99. static::assertSame($expected, Utils::cmpInt($left, $right));
  100. }
  101. public function provideCmpIntCases()
  102. {
  103. return [
  104. [0, 1, 1],
  105. [0, -1, -1],
  106. [-1, 10, 20],
  107. [-1, -20, -10],
  108. [1, 20, 10],
  109. [1, -10, -20],
  110. ];
  111. }
  112. /**
  113. * @param string $spaces
  114. * @param array|string $input token prototype
  115. *
  116. * @dataProvider provideCalculateTrailingWhitespaceIndentCases
  117. */
  118. public function testCalculateTrailingWhitespaceIndent($spaces, $input)
  119. {
  120. $token = new Token($input);
  121. static::assertSame($spaces, Utils::calculateTrailingWhitespaceIndent($token));
  122. }
  123. public function provideCalculateTrailingWhitespaceIndentCases()
  124. {
  125. return [
  126. [' ', [T_WHITESPACE, "\n\n "]],
  127. [' ', [T_WHITESPACE, "\r\n\r\r\r "]],
  128. ["\t", [T_WHITESPACE, "\r\n\t"]],
  129. ['', [T_WHITESPACE, "\t\n\r"]],
  130. ['', [T_WHITESPACE, "\n"]],
  131. ['', ''],
  132. ];
  133. }
  134. public function testCalculateTrailingWhitespaceIndentFail()
  135. {
  136. $this->expectException(\InvalidArgumentException::class);
  137. $this->expectExceptionMessage('The given token must be whitespace, got "T_STRING".');
  138. $token = new Token([T_STRING, 'foo']);
  139. Utils::calculateTrailingWhitespaceIndent($token);
  140. }
  141. /**
  142. * @dataProvider provideStableSortCases
  143. */
  144. public function testStableSort(
  145. array $expected,
  146. array $elements,
  147. callable $getComparableValueCallback,
  148. callable $compareValuesCallback
  149. ) {
  150. static::assertSame(
  151. $expected,
  152. Utils::stableSort($elements, $getComparableValueCallback, $compareValuesCallback)
  153. );
  154. }
  155. public function provideStableSortCases()
  156. {
  157. return [
  158. [
  159. ['a', 'b', 'c', 'd', 'e'],
  160. ['b', 'd', 'e', 'a', 'c'],
  161. static function ($element) { return $element; },
  162. 'strcmp',
  163. ],
  164. [
  165. ['b', 'd', 'e', 'a', 'c'],
  166. ['b', 'd', 'e', 'a', 'c'],
  167. static function ($element) { return 'foo'; },
  168. 'strcmp',
  169. ],
  170. [
  171. ['b', 'd', 'e', 'a', 'c'],
  172. ['b', 'd', 'e', 'a', 'c'],
  173. static function ($element) { return $element; },
  174. static function ($a, $b) { return 0; },
  175. ],
  176. [
  177. ['bar1', 'baz1', 'foo1', 'bar2', 'baz2', 'foo2'],
  178. ['foo1', 'foo2', 'bar1', 'bar2', 'baz1', 'baz2'],
  179. static function ($element) { return preg_replace('/([a-z]+)(\d+)/', '$2$1', $element); },
  180. 'strcmp',
  181. ],
  182. ];
  183. }
  184. public function testSortFixers()
  185. {
  186. $fixers = [
  187. $this->createFixerDouble('f1', 0),
  188. $this->createFixerDouble('f2', -10),
  189. $this->createFixerDouble('f3', 10),
  190. $this->createFixerDouble('f4', -10),
  191. ];
  192. static::assertSame(
  193. [
  194. $fixers[2],
  195. $fixers[0],
  196. $fixers[1],
  197. $fixers[3],
  198. ],
  199. Utils::sortFixers($fixers)
  200. );
  201. }
  202. public function testNaturalLanguageJoinWithBackticksThrowsInvalidArgumentExceptionForEmptyArray()
  203. {
  204. $this->expectException(\InvalidArgumentException::class);
  205. Utils::naturalLanguageJoinWithBackticks([]);
  206. }
  207. /**
  208. * @dataProvider provideNaturalLanguageJoinWithBackticksCases
  209. *
  210. * @param string $joined
  211. */
  212. public function testNaturalLanguageJoinWithBackticks($joined, array $names)
  213. {
  214. static::assertSame($joined, Utils::naturalLanguageJoinWithBackticks($names));
  215. }
  216. public function provideNaturalLanguageJoinWithBackticksCases()
  217. {
  218. return [
  219. [
  220. '`a`',
  221. ['a'],
  222. ],
  223. [
  224. '`a` and `b`',
  225. ['a', 'b'],
  226. ],
  227. [
  228. '`a`, `b` and `c`',
  229. ['a', 'b', 'c'],
  230. ],
  231. ];
  232. }
  233. /**
  234. * @param int $expected
  235. *
  236. * @dataProvider provideCalculateBitmaskCases
  237. */
  238. public function testCalculateBitmask($expected, array $options)
  239. {
  240. static::assertSame($expected, Utils::calculateBitmask($options));
  241. }
  242. public function provideCalculateBitmaskCases()
  243. {
  244. return [
  245. [
  246. JSON_HEX_TAG | JSON_HEX_QUOT,
  247. ['JSON_HEX_TAG', 'JSON_HEX_QUOT'],
  248. ],
  249. [
  250. JSON_HEX_TAG | JSON_HEX_QUOT,
  251. ['JSON_HEX_TAG', 'JSON_HEX_QUOT', 'NON_EXISTENT_CONST'],
  252. ],
  253. [
  254. JSON_HEX_TAG,
  255. ['JSON_HEX_TAG'],
  256. ],
  257. [
  258. JSON_HEX_TAG | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_HEX_APOS,
  259. ['JSON_HEX_TAG', 'JSON_HEX_QUOT', 'JSON_HEX_AMP', 'JSON_HEX_APOS'],
  260. ],
  261. [
  262. 0,
  263. [],
  264. ],
  265. ];
  266. }
  267. private function createFixerDouble($name, $priority)
  268. {
  269. $fixer = $this->prophesize(FixerInterface::class);
  270. $fixer->getName()->willReturn($name);
  271. $fixer->getPriority()->willReturn($priority);
  272. return $fixer->reveal();
  273. }
  274. }