UtilsTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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 <hello@gjcampbell.co.uk>
  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. /**
  30. * @var null|false|string
  31. */
  32. private $originalValueOfFutureMode;
  33. protected function setUp(): void
  34. {
  35. $this->originalValueOfFutureMode = getenv('PHP_CS_FIXER_FUTURE_MODE');
  36. }
  37. protected function tearDown(): void
  38. {
  39. putenv("PHP_CS_FIXER_FUTURE_MODE={$this->originalValueOfFutureMode}");
  40. }
  41. /**
  42. * @param string $expected Camel case string
  43. *
  44. * @dataProvider provideCamelCaseToUnderscoreCases
  45. */
  46. public function testCamelCaseToUnderscore(string $expected, string $input = null): void
  47. {
  48. if (null !== $input) {
  49. self::assertSame($expected, Utils::camelCaseToUnderscore($input));
  50. }
  51. self::assertSame($expected, Utils::camelCaseToUnderscore($expected));
  52. }
  53. public static function provideCamelCaseToUnderscoreCases(): iterable
  54. {
  55. yield [
  56. 'dollar_close_curly_braces',
  57. 'DollarCloseCurlyBraces',
  58. ];
  59. yield [
  60. 'utf8_encoder_fixer',
  61. 'utf8EncoderFixer',
  62. ];
  63. yield [
  64. 'terminated_with_number10',
  65. 'TerminatedWithNumber10',
  66. ];
  67. yield [
  68. 'utf8_encoder_fixer',
  69. ];
  70. yield [
  71. 'a',
  72. 'A',
  73. ];
  74. yield [
  75. 'aa',
  76. 'AA',
  77. ];
  78. yield [
  79. 'foo',
  80. 'FOO',
  81. ];
  82. yield [
  83. 'foo_bar_baz',
  84. 'FooBarBAZ',
  85. ];
  86. yield [
  87. 'foo_bar_baz',
  88. 'FooBARBaz',
  89. ];
  90. yield [
  91. 'foo_bar_baz',
  92. 'FOOBarBaz',
  93. ];
  94. yield [
  95. 'mr_t',
  96. 'MrT',
  97. ];
  98. yield [
  99. 'voyage_éclair',
  100. 'VoyageÉclair',
  101. ];
  102. }
  103. /**
  104. * @param array{int, string}|string $input token prototype
  105. *
  106. * @dataProvider provideCalculateTrailingWhitespaceIndentCases
  107. */
  108. public function testCalculateTrailingWhitespaceIndent(string $spaces, $input): void
  109. {
  110. $token = new Token($input);
  111. self::assertSame($spaces, Utils::calculateTrailingWhitespaceIndent($token));
  112. }
  113. public static function provideCalculateTrailingWhitespaceIndentCases(): iterable
  114. {
  115. yield [' ', [T_WHITESPACE, "\n\n "]];
  116. yield [' ', [T_WHITESPACE, "\r\n\r\r\r "]];
  117. yield ["\t", [T_WHITESPACE, "\r\n\t"]];
  118. yield ['', [T_WHITESPACE, "\t\n\r"]];
  119. yield ['', [T_WHITESPACE, "\n"]];
  120. yield ['', ''];
  121. }
  122. public function testCalculateTrailingWhitespaceIndentFail(): void
  123. {
  124. $this->expectException(\InvalidArgumentException::class);
  125. $this->expectExceptionMessage('The given token must be whitespace, got "T_STRING".');
  126. $token = new Token([T_STRING, 'foo']);
  127. Utils::calculateTrailingWhitespaceIndent($token);
  128. }
  129. /**
  130. * @param list<mixed> $expected
  131. * @param list<mixed> $elements
  132. *
  133. * @dataProvider provideStableSortCases
  134. */
  135. public function testStableSort(
  136. array $expected,
  137. array $elements,
  138. callable $getComparableValueCallback,
  139. callable $compareValuesCallback
  140. ): void {
  141. self::assertSame(
  142. $expected,
  143. Utils::stableSort($elements, $getComparableValueCallback, $compareValuesCallback)
  144. );
  145. }
  146. public static function provideStableSortCases(): iterable
  147. {
  148. yield [
  149. ['a', 'b', 'c', 'd', 'e'],
  150. ['b', 'd', 'e', 'a', 'c'],
  151. static fn ($element) => $element,
  152. 'strcmp',
  153. ];
  154. yield [
  155. ['b', 'd', 'e', 'a', 'c'],
  156. ['b', 'd', 'e', 'a', 'c'],
  157. static fn (): string => 'foo',
  158. 'strcmp',
  159. ];
  160. yield [
  161. ['b', 'd', 'e', 'a', 'c'],
  162. ['b', 'd', 'e', 'a', 'c'],
  163. static fn ($element) => $element,
  164. static fn (): int => 0,
  165. ];
  166. yield [
  167. ['bar1', 'baz1', 'foo1', 'bar2', 'baz2', 'foo2'],
  168. ['foo1', 'foo2', 'bar1', 'bar2', 'baz1', 'baz2'],
  169. static fn ($element) => preg_replace('/([a-z]+)(\d+)/', '$2$1', $element),
  170. 'strcmp',
  171. ];
  172. }
  173. public function testSortFixers(): void
  174. {
  175. $fixers = [
  176. $this->createFixerDouble('f1', 0),
  177. $this->createFixerDouble('f2', -10),
  178. $this->createFixerDouble('f3', 10),
  179. $this->createFixerDouble('f4', -10),
  180. ];
  181. self::assertSame(
  182. [
  183. $fixers[2],
  184. $fixers[0],
  185. $fixers[1],
  186. $fixers[3],
  187. ],
  188. Utils::sortFixers($fixers)
  189. );
  190. }
  191. public function testNaturalLanguageJoinThrowsInvalidArgumentExceptionForEmptyArray(): void
  192. {
  193. $this->expectException(\InvalidArgumentException::class);
  194. $this->expectExceptionMessage('Array of names cannot be empty.');
  195. Utils::naturalLanguageJoin([]);
  196. }
  197. public function testNaturalLanguageJoinThrowsInvalidArgumentExceptionForMoreThanOneCharWrapper(): void
  198. {
  199. $this->expectException(\InvalidArgumentException::class);
  200. $this->expectExceptionMessage('Wrapper should be a single-char string or empty.');
  201. Utils::naturalLanguageJoin(['a', 'b'], 'foo');
  202. }
  203. /**
  204. * @dataProvider provideNaturalLanguageJoinCases
  205. *
  206. * @param list<string> $names
  207. */
  208. public function testNaturalLanguageJoin(string $joined, array $names, string $wrapper = '"'): void
  209. {
  210. self::assertSame($joined, Utils::naturalLanguageJoin($names, $wrapper));
  211. }
  212. /**
  213. * @return iterable<array<null|array<string>|string>>
  214. */
  215. public static function provideNaturalLanguageJoinCases(): iterable
  216. {
  217. yield [
  218. '"a"',
  219. ['a'],
  220. ];
  221. yield [
  222. '"a" and "b"',
  223. ['a', 'b'],
  224. ];
  225. yield [
  226. '"a", "b" and "c"',
  227. ['a', 'b', 'c'],
  228. ];
  229. yield [
  230. '\'a\'',
  231. ['a'],
  232. '\'',
  233. ];
  234. yield [
  235. '\'a\' and \'b\'',
  236. ['a', 'b'],
  237. '\'',
  238. ];
  239. yield [
  240. '\'a\', \'b\' and \'c\'',
  241. ['a', 'b', 'c'],
  242. '\'',
  243. ];
  244. yield [
  245. '?a?',
  246. ['a'],
  247. '?',
  248. ];
  249. yield [
  250. '?a? and ?b?',
  251. ['a', 'b'],
  252. '?',
  253. ];
  254. yield [
  255. '?a?, ?b? and ?c?',
  256. ['a', 'b', 'c'],
  257. '?',
  258. ];
  259. yield [
  260. 'a',
  261. ['a'],
  262. '',
  263. ];
  264. yield [
  265. 'a and b',
  266. ['a', 'b'],
  267. '',
  268. ];
  269. yield [
  270. 'a, b and c',
  271. ['a', 'b', 'c'],
  272. '',
  273. ];
  274. }
  275. public function testNaturalLanguageJoinWithBackticksThrowsInvalidArgumentExceptionForEmptyArray(): void
  276. {
  277. $this->expectException(\InvalidArgumentException::class);
  278. Utils::naturalLanguageJoinWithBackticks([]);
  279. }
  280. /**
  281. * @param list<string> $names
  282. *
  283. * @dataProvider provideNaturalLanguageJoinWithBackticksCases
  284. */
  285. public function testNaturalLanguageJoinWithBackticks(string $joined, array $names): void
  286. {
  287. self::assertSame($joined, Utils::naturalLanguageJoinWithBackticks($names));
  288. }
  289. public static function provideNaturalLanguageJoinWithBackticksCases(): iterable
  290. {
  291. yield [
  292. '`a`',
  293. ['a'],
  294. ];
  295. yield [
  296. '`a` and `b`',
  297. ['a', 'b'],
  298. ];
  299. yield [
  300. '`a`, `b` and `c`',
  301. ['a', 'b', 'c'],
  302. ];
  303. }
  304. /**
  305. * @group legacy
  306. */
  307. public function testTriggerDeprecationWhenFutureModeIsOff(): void
  308. {
  309. putenv('PHP_CS_FIXER_FUTURE_MODE=0');
  310. $message = __METHOD__.'::The message';
  311. $this->expectDeprecation($message);
  312. Utils::triggerDeprecation(new \DomainException($message));
  313. $triggered = Utils::getTriggeredDeprecations();
  314. self::assertContains($message, $triggered);
  315. }
  316. public function testTriggerDeprecationWhenFutureModeIsOn(): void
  317. {
  318. putenv('PHP_CS_FIXER_FUTURE_MODE=1');
  319. $message = __METHOD__.'::The message';
  320. $exception = new \DomainException($message);
  321. $futureModeException = null;
  322. try {
  323. Utils::triggerDeprecation($exception);
  324. } catch (\Exception $futureModeException) {
  325. }
  326. self::assertInstanceOf(\RuntimeException::class, $futureModeException);
  327. self::assertSame($exception, $futureModeException->getPrevious());
  328. $triggered = Utils::getTriggeredDeprecations();
  329. self::assertNotContains($message, $triggered);
  330. }
  331. /**
  332. * @param mixed $input
  333. *
  334. * @dataProvider provideToStringCases
  335. */
  336. public function testToString(string $expected, $input): void
  337. {
  338. self::assertSame($expected, Utils::toString($input));
  339. }
  340. public static function provideToStringCases(): iterable
  341. {
  342. yield ["['a' => 3, 'b' => 'c']", ['a' => 3, 'b' => 'c']];
  343. yield ['[[1], [2]]', [[1], [2]]];
  344. yield ['[0 => [1], \'a\' => [2]]', [[1], 'a' => [2]]];
  345. yield ['[1, 2, \'foo\', null]', [1, 2, 'foo', null]];
  346. yield ['[1, 2]', [1, 2]];
  347. yield ['[]', []];
  348. yield ['1.5', 1.5];
  349. yield ['false', false];
  350. yield ['true', true];
  351. yield ['1', 1];
  352. yield ["'foo'", 'foo'];
  353. }
  354. private function createFixerDouble(string $name, int $priority): FixerInterface
  355. {
  356. $fixer = $this->prophesize(FixerInterface::class);
  357. $fixer->getName()->willReturn($name);
  358. $fixer->getPriority()->willReturn($priority);
  359. return $fixer->reveal();
  360. }
  361. }