UtilsTest.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
  16. /**
  17. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  18. * @author Graham Campbell <graham@alt-three.com>
  19. * @author Odín del Río <odin.drp@gmail.com>
  20. *
  21. * @internal
  22. *
  23. * @covers \PhpCsFixer\Utils
  24. */
  25. final class UtilsTest extends TestCase
  26. {
  27. use ExpectDeprecationTrait;
  28. private $originalValueOfFutureMode;
  29. protected function doSetUp()
  30. {
  31. $this->originalValueOfFutureMode = getenv('PHP_CS_FIXER_FUTURE_MODE');
  32. }
  33. protected function doTearDown()
  34. {
  35. putenv("PHP_CS_FIXER_FUTURE_MODE={$this->originalValueOfFutureMode}");
  36. }
  37. /**
  38. * @param string $expected Camel case string
  39. * @param string $input Input string
  40. *
  41. * @dataProvider provideCamelCaseToUnderscoreCases
  42. */
  43. public function testCamelCaseToUnderscore($expected, $input = null)
  44. {
  45. if (null !== $input) {
  46. static::assertSame($expected, Utils::camelCaseToUnderscore($input));
  47. }
  48. static::assertSame($expected, Utils::camelCaseToUnderscore($expected));
  49. }
  50. /**
  51. * @return array
  52. */
  53. public function provideCamelCaseToUnderscoreCases()
  54. {
  55. return [
  56. [
  57. 'dollar_close_curly_braces',
  58. 'DollarCloseCurlyBraces',
  59. ],
  60. [
  61. 'utf8_encoder_fixer',
  62. 'utf8EncoderFixer',
  63. ],
  64. [
  65. 'terminated_with_number10',
  66. 'TerminatedWithNumber10',
  67. ],
  68. [
  69. 'utf8_encoder_fixer',
  70. ],
  71. [
  72. 'a',
  73. 'A',
  74. ],
  75. [
  76. 'aa',
  77. 'AA',
  78. ],
  79. [
  80. 'foo',
  81. 'FOO',
  82. ],
  83. [
  84. 'foo_bar_baz',
  85. 'FooBarBAZ',
  86. ],
  87. [
  88. 'foo_bar_baz',
  89. 'FooBARBaz',
  90. ],
  91. [
  92. 'foo_bar_baz',
  93. 'FOOBarBaz',
  94. ],
  95. [
  96. 'mr_t',
  97. 'MrT',
  98. ],
  99. ];
  100. }
  101. /**
  102. * @param int $expected
  103. * @param int $left
  104. * @param int $right
  105. *
  106. * @dataProvider provideCmpIntCases
  107. */
  108. public function testCmpInt($expected, $left, $right)
  109. {
  110. static::assertSame($expected, Utils::cmpInt($left, $right));
  111. }
  112. public function provideCmpIntCases()
  113. {
  114. return [
  115. [0, 1, 1],
  116. [0, -1, -1],
  117. [-1, 10, 20],
  118. [-1, -20, -10],
  119. [1, 20, 10],
  120. [1, -10, -20],
  121. ];
  122. }
  123. /**
  124. * @param string $spaces
  125. * @param array|string $input token prototype
  126. *
  127. * @dataProvider provideCalculateTrailingWhitespaceIndentCases
  128. */
  129. public function testCalculateTrailingWhitespaceIndent($spaces, $input)
  130. {
  131. $token = new Token($input);
  132. static::assertSame($spaces, Utils::calculateTrailingWhitespaceIndent($token));
  133. }
  134. public function provideCalculateTrailingWhitespaceIndentCases()
  135. {
  136. return [
  137. [' ', [T_WHITESPACE, "\n\n "]],
  138. [' ', [T_WHITESPACE, "\r\n\r\r\r "]],
  139. ["\t", [T_WHITESPACE, "\r\n\t"]],
  140. ['', [T_WHITESPACE, "\t\n\r"]],
  141. ['', [T_WHITESPACE, "\n"]],
  142. ['', ''],
  143. ];
  144. }
  145. public function testCalculateTrailingWhitespaceIndentFail()
  146. {
  147. $this->expectException(\InvalidArgumentException::class);
  148. $this->expectExceptionMessage('The given token must be whitespace, got "T_STRING".');
  149. $token = new Token([T_STRING, 'foo']);
  150. Utils::calculateTrailingWhitespaceIndent($token);
  151. }
  152. /**
  153. * @dataProvider provideStableSortCases
  154. */
  155. public function testStableSort(
  156. array $expected,
  157. array $elements,
  158. callable $getComparableValueCallback,
  159. callable $compareValuesCallback
  160. ) {
  161. static::assertSame(
  162. $expected,
  163. Utils::stableSort($elements, $getComparableValueCallback, $compareValuesCallback)
  164. );
  165. }
  166. public function provideStableSortCases()
  167. {
  168. return [
  169. [
  170. ['a', 'b', 'c', 'd', 'e'],
  171. ['b', 'd', 'e', 'a', 'c'],
  172. static function ($element) { return $element; },
  173. 'strcmp',
  174. ],
  175. [
  176. ['b', 'd', 'e', 'a', 'c'],
  177. ['b', 'd', 'e', 'a', 'c'],
  178. static function ($element) { return 'foo'; },
  179. 'strcmp',
  180. ],
  181. [
  182. ['b', 'd', 'e', 'a', 'c'],
  183. ['b', 'd', 'e', 'a', 'c'],
  184. static function ($element) { return $element; },
  185. static function ($a, $b) { return 0; },
  186. ],
  187. [
  188. ['bar1', 'baz1', 'foo1', 'bar2', 'baz2', 'foo2'],
  189. ['foo1', 'foo2', 'bar1', 'bar2', 'baz1', 'baz2'],
  190. static function ($element) { return preg_replace('/([a-z]+)(\d+)/', '$2$1', $element); },
  191. 'strcmp',
  192. ],
  193. ];
  194. }
  195. public function testSortFixers()
  196. {
  197. $fixers = [
  198. $this->createFixerDouble('f1', 0),
  199. $this->createFixerDouble('f2', -10),
  200. $this->createFixerDouble('f3', 10),
  201. $this->createFixerDouble('f4', -10),
  202. ];
  203. static::assertSame(
  204. [
  205. $fixers[2],
  206. $fixers[0],
  207. $fixers[1],
  208. $fixers[3],
  209. ],
  210. Utils::sortFixers($fixers)
  211. );
  212. }
  213. public function testNaturalLanguageJoinWithBackticksThrowsInvalidArgumentExceptionForEmptyArray()
  214. {
  215. $this->expectException(\InvalidArgumentException::class);
  216. Utils::naturalLanguageJoinWithBackticks([]);
  217. }
  218. /**
  219. * @dataProvider provideNaturalLanguageJoinWithBackticksCases
  220. *
  221. * @param string $joined
  222. */
  223. public function testNaturalLanguageJoinWithBackticks($joined, array $names)
  224. {
  225. static::assertSame($joined, Utils::naturalLanguageJoinWithBackticks($names));
  226. }
  227. public function provideNaturalLanguageJoinWithBackticksCases()
  228. {
  229. return [
  230. [
  231. '`a`',
  232. ['a'],
  233. ],
  234. [
  235. '`a` and `b`',
  236. ['a', 'b'],
  237. ],
  238. [
  239. '`a`, `b` and `c`',
  240. ['a', 'b', 'c'],
  241. ],
  242. ];
  243. }
  244. /**
  245. * @param int $expected
  246. *
  247. * @dataProvider provideCalculateBitmaskCases
  248. */
  249. public function testCalculateBitmask($expected, array $options)
  250. {
  251. static::assertSame($expected, Utils::calculateBitmask($options));
  252. }
  253. public function provideCalculateBitmaskCases()
  254. {
  255. return [
  256. [
  257. JSON_HEX_TAG | JSON_HEX_QUOT,
  258. ['JSON_HEX_TAG', 'JSON_HEX_QUOT'],
  259. ],
  260. [
  261. JSON_HEX_TAG | JSON_HEX_QUOT,
  262. ['JSON_HEX_TAG', 'JSON_HEX_QUOT', 'NON_EXISTENT_CONST'],
  263. ],
  264. [
  265. JSON_HEX_TAG,
  266. ['JSON_HEX_TAG'],
  267. ],
  268. [
  269. JSON_HEX_TAG | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_HEX_APOS,
  270. ['JSON_HEX_TAG', 'JSON_HEX_QUOT', 'JSON_HEX_AMP', 'JSON_HEX_APOS'],
  271. ],
  272. [
  273. 0,
  274. [],
  275. ],
  276. ];
  277. }
  278. /**
  279. * @group legacy
  280. */
  281. public function testTriggerDeprecationWhenFutureModeIsOff()
  282. {
  283. putenv('PHP_CS_FIXER_FUTURE_MODE=0');
  284. $this->expectDeprecation('The message');
  285. Utils::triggerDeprecation('The message', \DomainException::class);
  286. }
  287. public function testTriggerDeprecationWhenFutureModeIsOn()
  288. {
  289. putenv('PHP_CS_FIXER_FUTURE_MODE=1');
  290. $this->expectException(\DomainException::class);
  291. $this->expectExceptionMessage('The message');
  292. Utils::triggerDeprecation('The message', \DomainException::class);
  293. }
  294. private function createFixerDouble($name, $priority)
  295. {
  296. $fixer = $this->prophesize(FixerInterface::class);
  297. $fixer->getName()->willReturn($name);
  298. $fixer->getPriority()->willReturn($priority);
  299. return $fixer->reveal();
  300. }
  301. }