UtilsTest.php 7.7 KB

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