UtilsTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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\FixerDefinition\FixerDefinitionInterface;
  15. use PhpCsFixer\Tokenizer\Token;
  16. use PhpCsFixer\Tokenizer\Tokens;
  17. use PhpCsFixer\Utils;
  18. use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
  19. /**
  20. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  21. * @author Graham Campbell <hello@gjcampbell.co.uk>
  22. * @author Odín del Río <odin.drp@gmail.com>
  23. *
  24. * @internal
  25. *
  26. * @covers \PhpCsFixer\Utils
  27. */
  28. final class UtilsTest extends TestCase
  29. {
  30. use ExpectDeprecationTrait;
  31. /**
  32. * @var null|false|string
  33. */
  34. private $originalValueOfFutureMode;
  35. protected function setUp(): void
  36. {
  37. $this->originalValueOfFutureMode = getenv('PHP_CS_FIXER_FUTURE_MODE');
  38. }
  39. protected function tearDown(): void
  40. {
  41. putenv("PHP_CS_FIXER_FUTURE_MODE={$this->originalValueOfFutureMode}");
  42. }
  43. /**
  44. * @param string $expected Camel case string
  45. *
  46. * @dataProvider provideCamelCaseToUnderscoreCases
  47. */
  48. public function testCamelCaseToUnderscore(string $expected, string $input = null): void
  49. {
  50. if (null !== $input) {
  51. self::assertSame($expected, Utils::camelCaseToUnderscore($input));
  52. }
  53. self::assertSame($expected, Utils::camelCaseToUnderscore($expected));
  54. }
  55. /**
  56. * @return iterable<array{0: string, 1?: string}>
  57. */
  58. public static function provideCamelCaseToUnderscoreCases(): iterable
  59. {
  60. yield [
  61. 'dollar_close_curly_braces',
  62. 'DollarCloseCurlyBraces',
  63. ];
  64. yield [
  65. 'utf8_encoder_fixer',
  66. 'utf8EncoderFixer',
  67. ];
  68. yield [
  69. 'terminated_with_number10',
  70. 'TerminatedWithNumber10',
  71. ];
  72. yield [
  73. 'utf8_encoder_fixer',
  74. ];
  75. yield [
  76. 'a',
  77. 'A',
  78. ];
  79. yield [
  80. 'aa',
  81. 'AA',
  82. ];
  83. yield [
  84. 'foo',
  85. 'FOO',
  86. ];
  87. yield [
  88. 'foo_bar_baz',
  89. 'FooBarBAZ',
  90. ];
  91. yield [
  92. 'foo_bar_baz',
  93. 'FooBARBaz',
  94. ];
  95. yield [
  96. 'foo_bar_baz',
  97. 'FOOBarBaz',
  98. ];
  99. yield [
  100. 'mr_t',
  101. 'MrT',
  102. ];
  103. yield [
  104. 'voyage_éclair',
  105. 'VoyageÉclair',
  106. ];
  107. }
  108. /**
  109. * @param array{int, string}|string $input token prototype
  110. *
  111. * @dataProvider provideCalculateTrailingWhitespaceIndentCases
  112. */
  113. public function testCalculateTrailingWhitespaceIndent(string $spaces, $input): void
  114. {
  115. $token = new Token($input);
  116. self::assertSame($spaces, Utils::calculateTrailingWhitespaceIndent($token));
  117. }
  118. /**
  119. * @return iterable<array{string, array{int, string}|string}>
  120. */
  121. public static function provideCalculateTrailingWhitespaceIndentCases(): iterable
  122. {
  123. yield [' ', [T_WHITESPACE, "\n\n "]];
  124. yield [' ', [T_WHITESPACE, "\r\n\r\r\r "]];
  125. yield ["\t", [T_WHITESPACE, "\r\n\t"]];
  126. yield ['', [T_WHITESPACE, "\t\n\r"]];
  127. yield ['', [T_WHITESPACE, "\n"]];
  128. yield ['', ''];
  129. }
  130. public function testCalculateTrailingWhitespaceIndentFail(): void
  131. {
  132. $this->expectException(\InvalidArgumentException::class);
  133. $this->expectExceptionMessage('The given token must be whitespace, got "T_STRING".');
  134. $token = new Token([T_STRING, 'foo']);
  135. Utils::calculateTrailingWhitespaceIndent($token);
  136. }
  137. /**
  138. * @param list<mixed> $expected
  139. * @param list<mixed> $elements
  140. *
  141. * @dataProvider provideStableSortCases
  142. */
  143. public function testStableSort(
  144. array $expected,
  145. array $elements,
  146. callable $getComparableValueCallback,
  147. callable $compareValuesCallback
  148. ): void {
  149. self::assertSame(
  150. $expected,
  151. Utils::stableSort($elements, $getComparableValueCallback, $compareValuesCallback)
  152. );
  153. }
  154. /**
  155. * @return iterable<array{list<mixed>, list<mixed>, callable, callable}>
  156. */
  157. public static function provideStableSortCases(): iterable
  158. {
  159. yield [
  160. ['a', 'b', 'c', 'd', 'e'],
  161. ['b', 'd', 'e', 'a', 'c'],
  162. static fn ($element) => $element,
  163. 'strcmp',
  164. ];
  165. yield [
  166. ['b', 'd', 'e', 'a', 'c'],
  167. ['b', 'd', 'e', 'a', 'c'],
  168. static fn (): string => 'foo',
  169. 'strcmp',
  170. ];
  171. yield [
  172. ['b', 'd', 'e', 'a', 'c'],
  173. ['b', 'd', 'e', 'a', 'c'],
  174. static fn ($element) => $element,
  175. static fn (): int => 0,
  176. ];
  177. yield [
  178. ['bar1', 'baz1', 'foo1', 'bar2', 'baz2', 'foo2'],
  179. ['foo1', 'foo2', 'bar1', 'bar2', 'baz1', 'baz2'],
  180. static fn ($element) => preg_replace('/([a-z]+)(\d+)/', '$2$1', $element),
  181. 'strcmp',
  182. ];
  183. }
  184. public function testSortFixers(): void
  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. self::assertSame(
  193. [
  194. $fixers[2],
  195. $fixers[0],
  196. $fixers[1],
  197. $fixers[3],
  198. ],
  199. Utils::sortFixers($fixers)
  200. );
  201. }
  202. public function testNaturalLanguageJoinThrowsInvalidArgumentExceptionForEmptyArray(): void
  203. {
  204. $this->expectException(\InvalidArgumentException::class);
  205. $this->expectExceptionMessage('Array of names cannot be empty.');
  206. Utils::naturalLanguageJoin([]);
  207. }
  208. public function testNaturalLanguageJoinThrowsInvalidArgumentExceptionForMoreThanOneCharWrapper(): void
  209. {
  210. $this->expectException(\InvalidArgumentException::class);
  211. $this->expectExceptionMessage('Wrapper should be a single-char string or empty.');
  212. Utils::naturalLanguageJoin(['a', 'b'], 'foo');
  213. }
  214. /**
  215. * @dataProvider provideNaturalLanguageJoinCases
  216. *
  217. * @param list<string> $names
  218. */
  219. public function testNaturalLanguageJoin(string $joined, array $names, string $wrapper = '"'): void
  220. {
  221. self::assertSame($joined, Utils::naturalLanguageJoin($names, $wrapper));
  222. }
  223. /**
  224. * @return iterable<array{0: string, 1: list<string>, 2?: string}>
  225. */
  226. public static function provideNaturalLanguageJoinCases(): iterable
  227. {
  228. yield [
  229. '"a"',
  230. ['a'],
  231. ];
  232. yield [
  233. '"a" and "b"',
  234. ['a', 'b'],
  235. ];
  236. yield [
  237. '"a", "b" and "c"',
  238. ['a', 'b', 'c'],
  239. ];
  240. yield [
  241. '\'a\'',
  242. ['a'],
  243. '\'',
  244. ];
  245. yield [
  246. '\'a\' and \'b\'',
  247. ['a', 'b'],
  248. '\'',
  249. ];
  250. yield [
  251. '\'a\', \'b\' and \'c\'',
  252. ['a', 'b', 'c'],
  253. '\'',
  254. ];
  255. yield [
  256. '?a?',
  257. ['a'],
  258. '?',
  259. ];
  260. yield [
  261. '?a? and ?b?',
  262. ['a', 'b'],
  263. '?',
  264. ];
  265. yield [
  266. '?a?, ?b? and ?c?',
  267. ['a', 'b', 'c'],
  268. '?',
  269. ];
  270. yield [
  271. 'a',
  272. ['a'],
  273. '',
  274. ];
  275. yield [
  276. 'a and b',
  277. ['a', 'b'],
  278. '',
  279. ];
  280. yield [
  281. 'a, b and c',
  282. ['a', 'b', 'c'],
  283. '',
  284. ];
  285. }
  286. public function testNaturalLanguageJoinWithBackticksThrowsInvalidArgumentExceptionForEmptyArray(): void
  287. {
  288. $this->expectException(\InvalidArgumentException::class);
  289. Utils::naturalLanguageJoinWithBackticks([]);
  290. }
  291. /**
  292. * @param list<string> $names
  293. *
  294. * @dataProvider provideNaturalLanguageJoinWithBackticksCases
  295. */
  296. public function testNaturalLanguageJoinWithBackticks(string $joined, array $names): void
  297. {
  298. self::assertSame($joined, Utils::naturalLanguageJoinWithBackticks($names));
  299. }
  300. /**
  301. * @return iterable<array{string, list<string>}>
  302. */
  303. public static function provideNaturalLanguageJoinWithBackticksCases(): iterable
  304. {
  305. yield [
  306. '`a`',
  307. ['a'],
  308. ];
  309. yield [
  310. '`a` and `b`',
  311. ['a', 'b'],
  312. ];
  313. yield [
  314. '`a`, `b` and `c`',
  315. ['a', 'b', 'c'],
  316. ];
  317. }
  318. /**
  319. * @group legacy
  320. */
  321. public function testTriggerDeprecationWhenFutureModeIsOff(): void
  322. {
  323. putenv('PHP_CS_FIXER_FUTURE_MODE=0');
  324. $message = __METHOD__.'::The message';
  325. $this->expectDeprecation($message);
  326. Utils::triggerDeprecation(new \DomainException($message));
  327. $triggered = Utils::getTriggeredDeprecations();
  328. self::assertContains($message, $triggered);
  329. }
  330. public function testTriggerDeprecationWhenFutureModeIsOn(): void
  331. {
  332. putenv('PHP_CS_FIXER_FUTURE_MODE=1');
  333. $message = __METHOD__.'::The message';
  334. $exception = new \DomainException($message);
  335. $futureModeException = null;
  336. try {
  337. Utils::triggerDeprecation($exception);
  338. } catch (\Exception $futureModeException) {
  339. }
  340. self::assertInstanceOf(\RuntimeException::class, $futureModeException);
  341. self::assertSame($exception, $futureModeException->getPrevious());
  342. $triggered = Utils::getTriggeredDeprecations();
  343. self::assertNotContains($message, $triggered);
  344. }
  345. /**
  346. * @param mixed $input
  347. *
  348. * @dataProvider provideToStringCases
  349. */
  350. public function testToString(string $expected, $input): void
  351. {
  352. self::assertSame($expected, Utils::toString($input));
  353. }
  354. /**
  355. * @return iterable<array{string, mixed}>
  356. */
  357. public static function provideToStringCases(): iterable
  358. {
  359. yield ["['a' => 3, 'b' => 'c']", ['a' => 3, 'b' => 'c']];
  360. yield ['[[1], [2]]', [[1], [2]]];
  361. yield ['[0 => [1], \'a\' => [2]]', [[1], 'a' => [2]]];
  362. yield ['[1, 2, \'foo\', null]', [1, 2, 'foo', null]];
  363. yield ['[1, 2]', [1, 2]];
  364. yield ['[]', []];
  365. yield ['1.5', 1.5];
  366. yield ['false', false];
  367. yield ['true', true];
  368. yield ['1', 1];
  369. yield ["'foo'", 'foo'];
  370. }
  371. private function createFixerDouble(string $name, int $priority): FixerInterface
  372. {
  373. return new class($name, $priority) implements FixerInterface {
  374. private string $name;
  375. private int $priority;
  376. public function __construct(string $name, int $priority)
  377. {
  378. $this->name = $name;
  379. $this->priority = $priority;
  380. }
  381. public function isCandidate(Tokens $tokens): bool
  382. {
  383. throw new \LogicException('Not implemented.');
  384. }
  385. public function isRisky(): bool
  386. {
  387. throw new \LogicException('Not implemented.');
  388. }
  389. public function fix(\SplFileInfo $file, Tokens $tokens): void
  390. {
  391. throw new \LogicException('Not implemented.');
  392. }
  393. public function getDefinition(): FixerDefinitionInterface
  394. {
  395. throw new \LogicException('Not implemented.');
  396. }
  397. public function getName(): string
  398. {
  399. return $this->name;
  400. }
  401. public function getPriority(): int
  402. {
  403. return $this->priority;
  404. }
  405. public function supports(\SplFileInfo $file): bool
  406. {
  407. throw new \LogicException('Not implemented.');
  408. }
  409. };
  410. }
  411. }