ConfigurationResolverTest.php 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471
  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\Console;
  13. use PhpCsFixer\AbstractFixer;
  14. use PhpCsFixer\Cache\NullCacheManager;
  15. use PhpCsFixer\Config;
  16. use PhpCsFixer\ConfigInterface;
  17. use PhpCsFixer\ConfigurationException\InvalidConfigurationException;
  18. use PhpCsFixer\Console\Command\FixCommand;
  19. use PhpCsFixer\Console\ConfigurationResolver;
  20. use PhpCsFixer\Console\Output\Progress\ProgressOutputType;
  21. use PhpCsFixer\Differ\NullDiffer;
  22. use PhpCsFixer\Differ\UnifiedDiffer;
  23. use PhpCsFixer\Finder;
  24. use PhpCsFixer\Fixer\ConfigurableFixerInterface;
  25. use PhpCsFixer\Fixer\DeprecatedFixerInterface;
  26. use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
  27. use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverInterface;
  28. use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
  29. use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
  30. use PhpCsFixer\Linter\LinterInterface;
  31. use PhpCsFixer\Tests\TestCase;
  32. use PhpCsFixer\Tokenizer\Tokens;
  33. use PhpCsFixer\ToolInfoInterface;
  34. use PhpCsFixer\Utils;
  35. use Symfony\Component\Console\Output\OutputInterface;
  36. /**
  37. * @author Katsuhiro Ogawa <ko.fivestar@gmail.com>
  38. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  39. *
  40. * @internal
  41. *
  42. * @covers \PhpCsFixer\Console\ConfigurationResolver
  43. */
  44. final class ConfigurationResolverTest extends TestCase
  45. {
  46. public function testSetOptionWithUndefinedOption(): void
  47. {
  48. $this->expectException(InvalidConfigurationException::class);
  49. $this->expectExceptionMessageMatches('/^Unknown option name: "foo"\.$/');
  50. $this->createConfigurationResolver(['foo' => 'bar']);
  51. }
  52. public function testResolveProgressWithPositiveConfigAndPositiveOption(): void
  53. {
  54. $config = new Config();
  55. $config->setHideProgress(true);
  56. $resolver = $this->createConfigurationResolver([
  57. 'format' => 'txt',
  58. 'verbosity' => OutputInterface::VERBOSITY_VERBOSE,
  59. ], $config);
  60. self::assertSame('none', $resolver->getProgressType());
  61. }
  62. public function testResolveProgressWithPositiveConfigAndNegativeOption(): void
  63. {
  64. $config = new Config();
  65. $config->setHideProgress(true);
  66. $resolver = $this->createConfigurationResolver([
  67. 'format' => 'txt',
  68. 'verbosity' => OutputInterface::VERBOSITY_NORMAL,
  69. ], $config);
  70. self::assertSame('none', $resolver->getProgressType());
  71. }
  72. public function testResolveProgressWithNegativeConfigAndPositiveOption(): void
  73. {
  74. $config = new Config();
  75. $config->setHideProgress(false);
  76. $resolver = $this->createConfigurationResolver([
  77. 'format' => 'txt',
  78. 'verbosity' => OutputInterface::VERBOSITY_VERBOSE,
  79. ], $config);
  80. self::assertSame('bar', $resolver->getProgressType());
  81. }
  82. public function testResolveProgressWithNegativeConfigAndNegativeOption(): void
  83. {
  84. $config = new Config();
  85. $config->setHideProgress(false);
  86. $resolver = $this->createConfigurationResolver([
  87. 'format' => 'txt',
  88. 'verbosity' => OutputInterface::VERBOSITY_NORMAL,
  89. ], $config);
  90. self::assertSame('bar', $resolver->getProgressType());
  91. }
  92. /**
  93. * @dataProvider provideProgressTypeCases
  94. */
  95. public function testResolveProgressWithPositiveConfigAndExplicitProgress(string $progressType): void
  96. {
  97. $config = new Config();
  98. $config->setHideProgress(true);
  99. $resolver = $this->createConfigurationResolver([
  100. 'format' => 'txt',
  101. 'verbosity' => OutputInterface::VERBOSITY_VERBOSE,
  102. 'show-progress' => $progressType,
  103. ], $config);
  104. self::assertSame($progressType, $resolver->getProgressType());
  105. }
  106. /**
  107. * @dataProvider provideProgressTypeCases
  108. */
  109. public function testResolveProgressWithNegativeConfigAndExplicitProgress(string $progressType): void
  110. {
  111. $config = new Config();
  112. $config->setHideProgress(false);
  113. $resolver = $this->createConfigurationResolver([
  114. 'format' => 'txt',
  115. 'verbosity' => OutputInterface::VERBOSITY_VERBOSE,
  116. 'show-progress' => $progressType,
  117. ], $config);
  118. self::assertSame($progressType, $resolver->getProgressType());
  119. }
  120. /**
  121. * @return iterable<string, array{0: ProgressOutputType::*}>
  122. */
  123. public static function provideProgressTypeCases(): iterable
  124. {
  125. foreach (ProgressOutputType::all() as $outputType) {
  126. yield $outputType => [$outputType];
  127. }
  128. }
  129. public function testResolveProgressWithInvalidExplicitProgress(): void
  130. {
  131. $resolver = $this->createConfigurationResolver([
  132. 'format' => 'txt',
  133. 'verbosity' => OutputInterface::VERBOSITY_VERBOSE,
  134. 'show-progress' => 'foo',
  135. ]);
  136. $this->expectException(InvalidConfigurationException::class);
  137. $this->expectExceptionMessage('The progress type "foo" is not defined, supported are "bar", "dots" and "none".');
  138. $resolver->getProgressType();
  139. }
  140. public function testResolveConfigFileDefault(): void
  141. {
  142. $resolver = $this->createConfigurationResolver([]);
  143. self::assertNull($resolver->getConfigFile());
  144. self::assertInstanceOf(ConfigInterface::class, $resolver->getConfig());
  145. }
  146. public function testResolveConfigFileByPathOfFile(): void
  147. {
  148. $dir = __DIR__.'/../Fixtures/ConfigurationResolverConfigFile/case_1';
  149. $resolver = $this->createConfigurationResolver(['path' => [$dir.\DIRECTORY_SEPARATOR.'foo.php']]);
  150. self::assertSame($dir.\DIRECTORY_SEPARATOR.'.php-cs-fixer.dist.php', $resolver->getConfigFile());
  151. self::assertInstanceOf(\Test1Config::class, $resolver->getConfig()); // @phpstan-ignore-line to avoid `Class Test1Config not found.`
  152. }
  153. public function testResolveConfigFileSpecified(): void
  154. {
  155. $file = __DIR__.'/../Fixtures/ConfigurationResolverConfigFile/case_4/my.php-cs-fixer.php';
  156. $resolver = $this->createConfigurationResolver(['config' => $file]);
  157. self::assertSame($file, $resolver->getConfigFile());
  158. self::assertInstanceOf(\Test4Config::class, $resolver->getConfig()); // @phpstan-ignore-line to avoid `Class Test4Config not found.`
  159. }
  160. /**
  161. * @dataProvider provideResolveConfigFileChooseFileCases
  162. */
  163. public function testResolveConfigFileChooseFile(string $expectedFile, string $expectedClass, string $path, ?string $cwdPath = null): void
  164. {
  165. $resolver = $this->createConfigurationResolver(
  166. ['path' => [$path]],
  167. null,
  168. $cwdPath ?? ''
  169. );
  170. self::assertSame($expectedFile, $resolver->getConfigFile());
  171. self::assertInstanceOf($expectedClass, $resolver->getConfig());
  172. }
  173. public static function provideResolveConfigFileChooseFileCases(): iterable
  174. {
  175. $dirBase = self::getFixtureDir();
  176. yield [
  177. $dirBase.'case_1'.\DIRECTORY_SEPARATOR.'.php-cs-fixer.dist.php',
  178. 'Test1Config',
  179. $dirBase.'case_1',
  180. ];
  181. yield [
  182. $dirBase.'case_2'.\DIRECTORY_SEPARATOR.'.php-cs-fixer.php',
  183. 'Test2Config',
  184. $dirBase.'case_2',
  185. ];
  186. yield [
  187. $dirBase.'case_3'.\DIRECTORY_SEPARATOR.'.php-cs-fixer.php',
  188. 'Test3Config',
  189. $dirBase.'case_3',
  190. ];
  191. yield [
  192. $dirBase.'case_6'.\DIRECTORY_SEPARATOR.'.php-cs-fixer.dist.php',
  193. 'Test6Config',
  194. $dirBase.'case_6'.\DIRECTORY_SEPARATOR.'subdir',
  195. $dirBase.'case_6',
  196. ];
  197. yield [
  198. $dirBase.'case_6'.\DIRECTORY_SEPARATOR.'.php-cs-fixer.dist.php',
  199. 'Test6Config',
  200. $dirBase.'case_6'.\DIRECTORY_SEPARATOR.'subdir/empty_file.php',
  201. $dirBase.'case_6',
  202. ];
  203. }
  204. public function testResolveConfigFileChooseFileWithInvalidFile(): void
  205. {
  206. $this->expectException(InvalidConfigurationException::class);
  207. $this->expectExceptionMessageMatches(
  208. '#^The config file: ".+[\/\\\]Fixtures[\/\\\]ConfigurationResolverConfigFile[\/\\\]case_5[\/\\\]\.php-cs-fixer\.dist\.php" does not return a "PhpCsFixer\\\ConfigInterface" instance\. Got: "string"\.$#'
  209. );
  210. $dirBase = self::getFixtureDir();
  211. $resolver = $this->createConfigurationResolver(['path' => [$dirBase.'case_5']]);
  212. $resolver->getConfig();
  213. }
  214. public function testResolveConfigFileChooseFileWithInvalidFormat(): void
  215. {
  216. $this->expectException(InvalidConfigurationException::class);
  217. $this->expectExceptionMessageMatches('/^The format "xls" is not defined, supported are "checkstyle", "gitlab", "json", "junit", "txt" and "xml"\.$/');
  218. $dirBase = self::getFixtureDir();
  219. $resolver = $this->createConfigurationResolver(['path' => [$dirBase.'case_7']]);
  220. $resolver->getReporter();
  221. }
  222. public function testResolveConfigFileChooseFileWithPathArrayWithoutConfig(): void
  223. {
  224. $this->expectException(InvalidConfigurationException::class);
  225. $this->expectExceptionMessageMatches('/^For multiple paths config parameter is required\.$/');
  226. $dirBase = self::getFixtureDir();
  227. $resolver = $this->createConfigurationResolver(['path' => [$dirBase.'case_1/.php-cs-fixer.dist.php', $dirBase.'case_1/foo.php']]);
  228. $resolver->getConfig();
  229. }
  230. public function testResolveConfigFileChooseFileWithPathArrayAndConfig(): void
  231. {
  232. $dirBase = self::getFixtureDir();
  233. $configFile = $dirBase.'case_1/.php-cs-fixer.dist.php';
  234. $resolver = $this->createConfigurationResolver([
  235. 'config' => $configFile,
  236. 'path' => [$configFile, $dirBase.'case_1/foo.php'],
  237. ]);
  238. self::assertSame($configFile, $resolver->getConfigFile());
  239. }
  240. /**
  241. * @param array<int, string> $paths
  242. * @param array<int, string> $expectedPaths
  243. *
  244. * @dataProvider provideResolvePathCases
  245. */
  246. public function testResolvePath(array $paths, string $cwd, array $expectedPaths): void
  247. {
  248. $resolver = $this->createConfigurationResolver(
  249. ['path' => $paths],
  250. null,
  251. $cwd
  252. );
  253. self::assertSame($expectedPaths, $resolver->getPath());
  254. }
  255. public static function provideResolvePathCases(): iterable
  256. {
  257. yield [
  258. ['Command'],
  259. __DIR__,
  260. [__DIR__.\DIRECTORY_SEPARATOR.'Command'],
  261. ];
  262. yield [
  263. [basename(__DIR__)],
  264. \dirname(__DIR__),
  265. [__DIR__],
  266. ];
  267. yield [
  268. [' Command'],
  269. __DIR__,
  270. [__DIR__.\DIRECTORY_SEPARATOR.'Command'],
  271. ];
  272. yield [
  273. ['Command '],
  274. __DIR__,
  275. [__DIR__.\DIRECTORY_SEPARATOR.'Command'],
  276. ];
  277. }
  278. /**
  279. * @param array<string> $paths
  280. *
  281. * @dataProvider provideRejectInvalidPathCases
  282. */
  283. public function testRejectInvalidPath(array $paths, string $expectedMessage): void
  284. {
  285. $resolver = $this->createConfigurationResolver(
  286. ['path' => $paths],
  287. null,
  288. \dirname(__DIR__)
  289. );
  290. $this->expectException(InvalidConfigurationException::class);
  291. $this->expectExceptionMessage($expectedMessage);
  292. $resolver->getPath();
  293. }
  294. public static function provideRejectInvalidPathCases(): iterable
  295. {
  296. yield [
  297. [''],
  298. 'Invalid path: "".',
  299. ];
  300. yield [
  301. [__DIR__, ''],
  302. 'Invalid path: "".',
  303. ];
  304. yield [
  305. ['', __DIR__],
  306. 'Invalid path: "".',
  307. ];
  308. yield [
  309. [' '],
  310. 'Invalid path: " ".',
  311. ];
  312. yield [
  313. [__DIR__, ' '],
  314. 'Invalid path: " ".',
  315. ];
  316. yield [
  317. [' ', __DIR__],
  318. 'Invalid path: " ".',
  319. ];
  320. }
  321. public function testResolvePathWithFileThatIsExcludedDirectlyOverridePathMode(): void
  322. {
  323. $config = new Config();
  324. $config->getFinder()
  325. ->in(__DIR__)
  326. ->notPath(basename(__FILE__))
  327. ;
  328. $resolver = $this->createConfigurationResolver(
  329. ['path' => [__FILE__]],
  330. $config
  331. );
  332. self::assertCount(1, $resolver->getFinder());
  333. }
  334. public function testResolvePathWithFileThatIsExcludedDirectlyIntersectionPathMode(): void
  335. {
  336. $config = new Config();
  337. $config->getFinder()
  338. ->in(__DIR__)
  339. ->notPath(basename(__FILE__))
  340. ;
  341. $resolver = $this->createConfigurationResolver([
  342. 'path' => [__FILE__],
  343. 'path-mode' => 'intersection',
  344. ], $config);
  345. self::assertCount(0, $resolver->getFinder());
  346. }
  347. public function testResolvePathWithFileThatIsExcludedByDirOverridePathMode(): void
  348. {
  349. $dir = \dirname(__DIR__);
  350. $config = new Config();
  351. $config->getFinder()
  352. ->in($dir)
  353. ->exclude(basename(__DIR__))
  354. ;
  355. $resolver = $this->createConfigurationResolver(
  356. ['path' => [__FILE__]],
  357. $config
  358. );
  359. self::assertCount(1, $resolver->getFinder());
  360. }
  361. public function testResolvePathWithFileThatIsExcludedByDirIntersectionPathMode(): void
  362. {
  363. $dir = \dirname(__DIR__);
  364. $config = new Config();
  365. $config->getFinder()
  366. ->in($dir)
  367. ->exclude(basename(__DIR__))
  368. ;
  369. $resolver = $this->createConfigurationResolver([
  370. 'path-mode' => 'intersection',
  371. 'path' => [__FILE__],
  372. ], $config);
  373. self::assertCount(0, $resolver->getFinder());
  374. }
  375. public function testResolvePathWithFileThatIsNotExcluded(): void
  376. {
  377. $dir = __DIR__;
  378. $config = new Config();
  379. $config->getFinder()
  380. ->in($dir)
  381. ->notPath('foo-'.basename(__FILE__))
  382. ;
  383. $resolver = $this->createConfigurationResolver(
  384. ['path' => [__FILE__]],
  385. $config
  386. );
  387. self::assertCount(1, $resolver->getFinder());
  388. }
  389. /**
  390. * @param \Exception|list<string> $expected
  391. * @param list<string> $path
  392. *
  393. * @dataProvider provideResolveIntersectionOfPathsCases
  394. */
  395. public function testResolveIntersectionOfPaths($expected, ?Finder $configFinder, array $path, string $pathMode, ?string $configOption = null): void
  396. {
  397. if ($expected instanceof \Exception) {
  398. $this->expectException(\get_class($expected));
  399. }
  400. if (null !== $configFinder) {
  401. $config = new Config();
  402. $config->setFinder($configFinder);
  403. } else {
  404. $config = null;
  405. }
  406. $resolver = $this->createConfigurationResolver([
  407. 'config' => $configOption,
  408. 'path' => $path,
  409. 'path-mode' => $pathMode,
  410. ], $config);
  411. $intersectionItems = array_map(
  412. static fn (\SplFileInfo $file): string => $file->getRealPath(),
  413. iterator_to_array($resolver->getFinder(), false)
  414. );
  415. sort($expected);
  416. sort($intersectionItems);
  417. self::assertSame($expected, $intersectionItems);
  418. }
  419. public static function provideResolveIntersectionOfPathsCases(): iterable
  420. {
  421. $dir = __DIR__.'/../Fixtures/ConfigurationResolverPathsIntersection';
  422. $cb = static fn (array $items): array => array_map(
  423. static fn (string $item): string => realpath($dir.'/'.$item),
  424. $items
  425. );
  426. yield 'no path at all' => [
  427. new \LogicException(),
  428. Finder::create(),
  429. [],
  430. 'override',
  431. ];
  432. yield 'configured only by finder' => [
  433. // don't override if the argument is empty
  434. $cb(['a1.php', 'a2.php', 'b/b1.php', 'b/b2.php', 'b_b/b_b1.php', 'c/c1.php', 'c/d/cd1.php', 'd/d1.php', 'd/d2.php', 'd/e/de1.php', 'd/f/df1.php']),
  435. Finder::create()
  436. ->in($dir),
  437. [],
  438. 'override',
  439. ];
  440. yield 'configured only by argument' => [
  441. $cb(['a1.php', 'a2.php', 'b/b1.php', 'b/b2.php', 'b_b/b_b1.php', 'c/c1.php', 'c/d/cd1.php', 'd/d1.php', 'd/d2.php', 'd/e/de1.php', 'd/f/df1.php']),
  442. Finder::create(),
  443. [$dir],
  444. 'override',
  445. ];
  446. yield 'configured by finder, intersected with empty argument' => [
  447. [],
  448. Finder::create()
  449. ->in($dir),
  450. [],
  451. 'intersection',
  452. ];
  453. yield 'configured by finder, intersected with dir' => [
  454. $cb(['c/c1.php', 'c/d/cd1.php']),
  455. Finder::create()
  456. ->in($dir),
  457. [$dir.'/c'],
  458. 'intersection',
  459. ];
  460. yield 'configured by finder, intersected with file' => [
  461. $cb(['c/c1.php']),
  462. Finder::create()
  463. ->in($dir),
  464. [$dir.'/c/c1.php'],
  465. 'intersection',
  466. ];
  467. yield 'finder points to one dir while argument to another, not connected' => [
  468. [],
  469. Finder::create()
  470. ->in($dir.'/b'),
  471. [$dir.'/c'],
  472. 'intersection',
  473. ];
  474. yield 'finder with excluded dir, intersected with excluded file' => [
  475. [],
  476. Finder::create()
  477. ->in($dir)
  478. ->exclude('c'),
  479. [$dir.'/c/d/cd1.php'],
  480. 'intersection',
  481. ];
  482. yield 'finder with excluded dir, intersected with dir containing excluded one' => [
  483. $cb(['c/c1.php']),
  484. Finder::create()
  485. ->in($dir)
  486. ->exclude('c/d'),
  487. [$dir.'/c'],
  488. 'intersection',
  489. ];
  490. yield 'finder with excluded file, intersected with dir containing excluded one' => [
  491. $cb(['c/d/cd1.php']),
  492. Finder::create()
  493. ->in($dir)
  494. ->notPath('c/c1.php'),
  495. [$dir.'/c'],
  496. 'intersection',
  497. ];
  498. yield 'configured by finder, intersected with non-existing path' => [
  499. new \LogicException(),
  500. Finder::create()
  501. ->in($dir),
  502. ['non_existing_dir'],
  503. 'intersection',
  504. ];
  505. yield 'configured by config file, overridden by multiple files' => [
  506. $cb(['d/d1.php', 'd/d2.php']),
  507. null,
  508. [$dir.'/d/d1.php', $dir.'/d/d2.php'],
  509. 'override',
  510. $dir.'/d/.php-cs-fixer.php',
  511. ];
  512. yield 'configured by config file, intersected with multiple files' => [
  513. $cb(['d/d1.php', 'd/d2.php']),
  514. null,
  515. [$dir.'/d/d1.php', $dir.'/d/d2.php'],
  516. 'intersection',
  517. $dir.'/d/.php-cs-fixer.php',
  518. ];
  519. yield 'configured by config file, overridden by non-existing dir' => [
  520. new \LogicException(),
  521. null,
  522. [$dir.'/d/fff'],
  523. 'override',
  524. $dir.'/d/.php-cs-fixer.php',
  525. ];
  526. yield 'configured by config file, intersected with non-existing dir' => [
  527. new \LogicException(),
  528. null,
  529. [$dir.'/d/fff'],
  530. 'intersection',
  531. $dir.'/d/.php-cs-fixer.php',
  532. ];
  533. yield 'configured by config file, overridden by non-existing file' => [
  534. new \LogicException(),
  535. null,
  536. [$dir.'/d/fff.php'],
  537. 'override',
  538. $dir.'/d/.php-cs-fixer.php',
  539. ];
  540. yield 'configured by config file, intersected with non-existing file' => [
  541. new \LogicException(),
  542. null,
  543. [$dir.'/d/fff.php'],
  544. 'intersection',
  545. $dir.'/d/.php-cs-fixer.php',
  546. ];
  547. yield 'configured by config file, overridden by multiple files and dirs' => [
  548. $cb(['d/d1.php', 'd/e/de1.php', 'd/f/df1.php']),
  549. null,
  550. [$dir.'/d/d1.php', $dir.'/d/e', $dir.'/d/f/'],
  551. 'override',
  552. $dir.'/d/.php-cs-fixer.php',
  553. ];
  554. yield 'configured by config file, intersected with multiple files and dirs' => [
  555. $cb(['d/d1.php', 'd/e/de1.php', 'd/f/df1.php']),
  556. null,
  557. [$dir.'/d/d1.php', $dir.'/d/e', $dir.'/d/f/'],
  558. 'intersection',
  559. $dir.'/d/.php-cs-fixer.php',
  560. ];
  561. }
  562. /**
  563. * @param array<string, mixed> $options
  564. *
  565. * @dataProvider provideConfigFinderIsOverriddenCases
  566. */
  567. public function testConfigFinderIsOverridden(array $options, bool $expectedResult): void
  568. {
  569. $resolver = $this->createConfigurationResolver($options);
  570. self::assertSame($expectedResult, $resolver->configFinderIsOverridden());
  571. $resolver = $this->createConfigurationResolver($options);
  572. $resolver->getFinder();
  573. self::assertSame($expectedResult, $resolver->configFinderIsOverridden());
  574. }
  575. public static function provideConfigFinderIsOverriddenCases(): iterable
  576. {
  577. $root = __DIR__.'/../..';
  578. yield [
  579. [
  580. 'config' => $root.'/.php-cs-fixer.dist.php',
  581. ],
  582. false,
  583. ];
  584. yield [
  585. [
  586. 'config' => $root.'/.php-cs-fixer.dist.php',
  587. 'path' => [$root.'/src'],
  588. ],
  589. true,
  590. ];
  591. yield [
  592. [],
  593. false,
  594. ];
  595. yield [
  596. [
  597. 'path' => [$root.'/src'],
  598. ],
  599. false,
  600. ];
  601. yield [
  602. [
  603. 'config' => $root.'/.php-cs-fixer.dist.php',
  604. 'path' => [$root.'/src'],
  605. 'path-mode' => ConfigurationResolver::PATH_MODE_INTERSECTION,
  606. ],
  607. false,
  608. ];
  609. // scenario when loaded config is not setting custom finder
  610. yield [
  611. [
  612. 'config' => $root.'/tests/Fixtures/ConfigurationResolverConfigFile/case_3/.php-cs-fixer.dist.php',
  613. 'path' => [$root.'/src'],
  614. ],
  615. false,
  616. ];
  617. // scenario when loaded config contains not fully defined finder
  618. yield [
  619. [
  620. 'config' => $root.'/tests/Fixtures/ConfigurationResolverConfigFile/case_9/.php-cs-fixer.php',
  621. 'path' => [$root.'/src'],
  622. ],
  623. false,
  624. ];
  625. }
  626. public function testResolveIsDryRunViaStdIn(): void
  627. {
  628. $resolver = $this->createConfigurationResolver([
  629. 'dry-run' => false,
  630. 'path' => ['-'],
  631. ]);
  632. self::assertTrue($resolver->isDryRun());
  633. }
  634. public function testResolveIsDryRunViaNegativeOption(): void
  635. {
  636. $resolver = $this->createConfigurationResolver(['dry-run' => false]);
  637. self::assertFalse($resolver->isDryRun());
  638. }
  639. public function testResolveIsDryRunViaPositiveOption(): void
  640. {
  641. $resolver = $this->createConfigurationResolver(['dry-run' => true]);
  642. self::assertTrue($resolver->isDryRun());
  643. }
  644. /**
  645. * @dataProvider provideResolveBooleanOptionCases
  646. */
  647. public function testResolveUsingCacheWithConfigOption(bool $expected, bool $configValue, ?string $passed): void
  648. {
  649. $config = new Config();
  650. $config->setUsingCache($configValue);
  651. $resolver = $this->createConfigurationResolver(
  652. ['using-cache' => $passed],
  653. $config
  654. );
  655. self::assertSame($expected, $resolver->getUsingCache());
  656. }
  657. public function testResolveUsingCacheWithPositiveConfigAndNoOption(): void
  658. {
  659. $config = new Config();
  660. $config->setUsingCache(true);
  661. $resolver = $this->createConfigurationResolver(
  662. [],
  663. $config
  664. );
  665. self::assertTrue($resolver->getUsingCache());
  666. }
  667. public function testResolveUsingCacheWithNegativeConfigAndNoOption(): void
  668. {
  669. $config = new Config();
  670. $config->setUsingCache(false);
  671. $resolver = $this->createConfigurationResolver(
  672. [],
  673. $config
  674. );
  675. self::assertFalse($resolver->getUsingCache());
  676. }
  677. /**
  678. * @dataProvider provideResolveUsingCacheForRuntimesCases
  679. */
  680. public function testResolveUsingCacheForRuntimes(bool $cacheAllowed, bool $installedWithComposer, bool $asPhar, bool $inDocker): void
  681. {
  682. $config = new Config();
  683. $config->setUsingCache(true);
  684. $resolver = $this->createConfigurationResolver(
  685. [],
  686. $config,
  687. '',
  688. new class($installedWithComposer, $asPhar, $inDocker) implements ToolInfoInterface {
  689. private bool $installedWithComposer;
  690. private bool $asPhar;
  691. private bool $inDocker;
  692. public function __construct(bool $installedWithComposer, bool $asPhar, bool $inDocker)
  693. {
  694. $this->installedWithComposer = $installedWithComposer;
  695. $this->asPhar = $asPhar;
  696. $this->inDocker = $inDocker;
  697. }
  698. public function getComposerInstallationDetails(): array
  699. {
  700. throw new \BadMethodCallException();
  701. }
  702. public function getComposerVersion(): string
  703. {
  704. throw new \BadMethodCallException();
  705. }
  706. public function getVersion(): string
  707. {
  708. throw new \BadMethodCallException();
  709. }
  710. public function isInstalledAsPhar(): bool
  711. {
  712. return $this->asPhar;
  713. }
  714. public function isInstalledByComposer(): bool
  715. {
  716. return $this->installedWithComposer;
  717. }
  718. public function isRunInsideDocker(): bool
  719. {
  720. return $this->inDocker;
  721. }
  722. public function getPharDownloadUri(string $version): string
  723. {
  724. throw new \BadMethodCallException();
  725. }
  726. }
  727. );
  728. self::assertSame($cacheAllowed, $resolver->getUsingCache());
  729. }
  730. /**
  731. * @return iterable<array{0: bool, 1: bool, 2: bool, 3: bool}>
  732. */
  733. public static function provideResolveUsingCacheForRuntimesCases(): iterable
  734. {
  735. yield 'none of the allowed runtimes' => [false, false, false, false];
  736. yield 'composer installation' => [true, true, false, false];
  737. yield 'PHAR distribution' => [true, false, true, false];
  738. yield 'Docker runtime' => [true, false, false, true];
  739. }
  740. public function testResolveCacheFileWithoutConfigAndOption(): void
  741. {
  742. $config = new Config();
  743. $default = $config->getCacheFile();
  744. $resolver = $this->createConfigurationResolver(
  745. [],
  746. $config
  747. );
  748. self::assertSame($default, $resolver->getCacheFile());
  749. }
  750. public function testResolveCacheFileWithConfig(): void
  751. {
  752. $cacheFile = 'foo/bar.baz';
  753. $config = new Config();
  754. $config
  755. ->setUsingCache(false)
  756. ->setCacheFile($cacheFile)
  757. ;
  758. $resolver = $this->createConfigurationResolver(
  759. [],
  760. $config
  761. );
  762. self::assertNull($resolver->getCacheFile());
  763. $cacheManager = $resolver->getCacheManager();
  764. self::assertInstanceOf(NullCacheManager::class, $cacheManager);
  765. $linter = $resolver->getLinter();
  766. self::assertInstanceOf(LinterInterface::class, $linter);
  767. }
  768. public function testResolveCacheFileWithOption(): void
  769. {
  770. $cacheFile = 'bar.baz';
  771. $config = new Config();
  772. $config->setCacheFile($cacheFile);
  773. $resolver = $this->createConfigurationResolver(
  774. ['cache-file' => $cacheFile],
  775. $config
  776. );
  777. self::assertSame($cacheFile, $resolver->getCacheFile());
  778. }
  779. public function testResolveCacheFileWithConfigAndOption(): void
  780. {
  781. $configCacheFile = 'foo/bar.baz';
  782. $optionCacheFile = 'bar.baz';
  783. $config = new Config();
  784. $config->setCacheFile($configCacheFile);
  785. $resolver = $this->createConfigurationResolver(
  786. ['cache-file' => $optionCacheFile],
  787. $config
  788. );
  789. self::assertSame($optionCacheFile, $resolver->getCacheFile());
  790. }
  791. /**
  792. * @dataProvider provideResolveBooleanOptionCases
  793. */
  794. public function testResolveAllowRiskyWithConfigOption(bool $expected, bool $configValue, ?string $passed): void
  795. {
  796. $config = new Config();
  797. $config->setRiskyAllowed($configValue);
  798. $resolver = $this->createConfigurationResolver(
  799. ['allow-risky' => $passed],
  800. $config
  801. );
  802. self::assertSame($expected, $resolver->getRiskyAllowed());
  803. }
  804. public function testResolveAllowRiskyWithNegativeConfigAndPositiveOption(): void
  805. {
  806. $config = new Config();
  807. $config->setRiskyAllowed(false);
  808. $resolver = $this->createConfigurationResolver(
  809. ['allow-risky' => 'yes'],
  810. $config
  811. );
  812. self::assertTrue($resolver->getRiskyAllowed());
  813. }
  814. public function testResolveAllowRiskyWithNegativeConfigAndNegativeOption(): void
  815. {
  816. $config = new Config();
  817. $config->setRiskyAllowed(false);
  818. $resolver = $this->createConfigurationResolver(
  819. ['allow-risky' => 'no'],
  820. $config
  821. );
  822. self::assertFalse($resolver->getRiskyAllowed());
  823. }
  824. public function testResolveAllowRiskyWithPositiveConfigAndNoOption(): void
  825. {
  826. $config = new Config();
  827. $config->setRiskyAllowed(true);
  828. $resolver = $this->createConfigurationResolver(
  829. [],
  830. $config
  831. );
  832. self::assertTrue($resolver->getRiskyAllowed());
  833. }
  834. public function testResolveAllowRiskyWithNegativeConfigAndNoOption(): void
  835. {
  836. $config = new Config();
  837. $config->setRiskyAllowed(false);
  838. $resolver = $this->createConfigurationResolver(
  839. [],
  840. $config
  841. );
  842. self::assertFalse($resolver->getRiskyAllowed());
  843. }
  844. public function testResolveRulesWithConfig(): void
  845. {
  846. $config = new Config();
  847. $config->setRules([
  848. 'statement_indentation' => true,
  849. 'strict_comparison' => false,
  850. ]);
  851. $resolver = $this->createConfigurationResolver(
  852. [],
  853. $config
  854. );
  855. self::assertSameRules(
  856. [
  857. 'statement_indentation' => true,
  858. ],
  859. $resolver->getRules()
  860. );
  861. }
  862. public function testResolveRulesWithOption(): void
  863. {
  864. $resolver = $this->createConfigurationResolver(['rules' => 'statement_indentation,-strict_comparison']);
  865. self::assertSameRules(
  866. [
  867. 'statement_indentation' => true,
  868. ],
  869. $resolver->getRules()
  870. );
  871. }
  872. /**
  873. * @param string[] $rules
  874. *
  875. * @dataProvider provideResolveRenamedRulesWithUnknownRulesCases
  876. */
  877. public function testResolveRenamedRulesWithUnknownRules(string $expectedMessage, array $rules): void
  878. {
  879. $this->expectException(InvalidConfigurationException::class);
  880. $this->expectExceptionMessage($expectedMessage);
  881. $resolver = $this->createConfigurationResolver(['rules' => implode(',', $rules)]);
  882. $resolver->getRules();
  883. }
  884. public static function provideResolveRenamedRulesWithUnknownRulesCases(): iterable
  885. {
  886. yield 'with config' => [
  887. 'The rules contain unknown fixers: "blank_line_before_return" is renamed (did you mean "blank_line_before_statement"? (note: use configuration "[\'statements\' => [\'return\']]")).
  888. For more info about updating see: https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/v3.0.0/UPGRADE-v3.md#renamed-ruless.',
  889. ['blank_line_before_return'],
  890. ];
  891. yield 'without config' => [
  892. 'The rules contain unknown fixers: "final_static_access" is renamed (did you mean "self_static_accessor"?).
  893. For more info about updating see: https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/v3.0.0/UPGRADE-v3.md#renamed-ruless.',
  894. ['final_static_access'],
  895. ];
  896. yield [
  897. 'The rules contain unknown fixers: "hash_to_slash_comment" is renamed (did you mean "single_line_comment_style"? (note: use configuration "[\'comment_types\' => [\'hash\']]")).
  898. For more info about updating see: https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/v3.0.0/UPGRADE-v3.md#renamed-ruless.',
  899. ['hash_to_slash_comment'],
  900. ];
  901. yield 'both renamed and unknown' => [
  902. 'The rules contain unknown fixers: "final_static_access" is renamed (did you mean "self_static_accessor"?), "binary_operator_space" (did you mean "binary_operator_spaces"?).
  903. For more info about updating see: https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/v3.0.0/UPGRADE-v3.md#renamed-ruless.',
  904. ['final_static_access', 'binary_operator_space'],
  905. ];
  906. }
  907. public function testResolveRulesWithUnknownRules(): void
  908. {
  909. $this->expectException(InvalidConfigurationException::class);
  910. $this->expectExceptionMessage('The rules contain unknown fixers: "bar", "binary_operator_space" (did you mean "binary_operator_spaces"?).');
  911. $resolver = $this->createConfigurationResolver(['rules' => 'statement_indentation,-bar,binary_operator_space']);
  912. $resolver->getRules();
  913. }
  914. public function testResolveRulesWithConfigAndOption(): void
  915. {
  916. $config = new Config();
  917. $config->setRules([
  918. 'statement_indentation' => true,
  919. 'strict_comparison' => false,
  920. ]);
  921. $resolver = $this->createConfigurationResolver(
  922. ['rules' => 'blank_line_before_statement'],
  923. $config
  924. );
  925. self::assertSameRules(
  926. [
  927. 'blank_line_before_statement' => true,
  928. ],
  929. $resolver->getRules()
  930. );
  931. }
  932. public function testResolveCommandLineInputOverridesDefault(): void
  933. {
  934. $command = new FixCommand($this->createToolInfoDouble());
  935. $definition = $command->getDefinition();
  936. $arguments = $definition->getArguments();
  937. self::assertCount(1, $arguments, 'Expected one argument, possibly test needs updating.');
  938. self::assertArrayHasKey('path', $arguments);
  939. $options = $definition->getOptions();
  940. self::assertSame(
  941. ['path-mode', 'allow-risky', 'config', 'dry-run', 'rules', 'using-cache', 'cache-file', 'diff', 'format', 'stop-on-violation', 'show-progress'],
  942. array_keys($options),
  943. 'Expected options mismatch, possibly test needs updating.'
  944. );
  945. $resolver = $this->createConfigurationResolver([
  946. 'path-mode' => 'intersection',
  947. 'allow-risky' => 'yes',
  948. 'config' => null,
  949. 'dry-run' => true,
  950. 'rules' => 'php_unit_construct',
  951. 'using-cache' => 'no',
  952. 'diff' => true,
  953. 'format' => 'json',
  954. 'stop-on-violation' => true,
  955. ]);
  956. self::assertTrue($resolver->shouldStopOnViolation());
  957. self::assertTrue($resolver->getRiskyAllowed());
  958. self::assertTrue($resolver->isDryRun());
  959. self::assertSame(['php_unit_construct' => true], $resolver->getRules());
  960. self::assertFalse($resolver->getUsingCache());
  961. self::assertNull($resolver->getCacheFile());
  962. self::assertInstanceOf(UnifiedDiffer::class, $resolver->getDiffer());
  963. self::assertSame('json', $resolver->getReporter()->getFormat());
  964. self::assertSame('none', $resolver->getProgressType());
  965. }
  966. /**
  967. * @param null|bool|string $diffConfig
  968. *
  969. * @dataProvider provideResolveDifferCases
  970. */
  971. public function testResolveDiffer(string $expected, $diffConfig): void
  972. {
  973. $resolver = $this->createConfigurationResolver([
  974. 'diff' => $diffConfig,
  975. ]);
  976. self::assertInstanceOf($expected, $resolver->getDiffer());
  977. }
  978. public static function provideResolveDifferCases(): iterable
  979. {
  980. yield [
  981. NullDiffer::class,
  982. false,
  983. ];
  984. yield [
  985. NullDiffer::class,
  986. null,
  987. ];
  988. yield [
  989. UnifiedDiffer::class,
  990. true,
  991. ];
  992. }
  993. public function testResolveConfigFileOverridesDefault(): void
  994. {
  995. $dir = __DIR__.'/../Fixtures/ConfigurationResolverConfigFile/case_8';
  996. $resolver = $this->createConfigurationResolver(['path' => [$dir.\DIRECTORY_SEPARATOR.'.php-cs-fixer.php']]);
  997. self::assertTrue($resolver->getRiskyAllowed());
  998. self::assertSame(['php_unit_construct' => true], $resolver->getRules());
  999. self::assertFalse($resolver->getUsingCache());
  1000. self::assertNull($resolver->getCacheFile());
  1001. self::assertSame('xml', $resolver->getReporter()->getFormat());
  1002. self::assertSame('none', $resolver->getProgressType());
  1003. }
  1004. public function testDeprecationOfPassingOtherThanNoOrYes(): void
  1005. {
  1006. $this->expectException(InvalidConfigurationException::class);
  1007. $this->expectExceptionMessage('Expected "yes" or "no" for option "allow-risky", got "yes please".');
  1008. $resolver = $this->createConfigurationResolver(['allow-risky' => 'yes please']);
  1009. $resolver->getRiskyAllowed();
  1010. }
  1011. public static function provideResolveBooleanOptionCases(): iterable
  1012. {
  1013. yield [true, true, 'yes'];
  1014. yield [true, false, 'yes'];
  1015. yield [false, true, 'no'];
  1016. yield [false, false, 'no'];
  1017. yield [true, true, null];
  1018. yield [false, false, null];
  1019. }
  1020. public function testWithEmptyRules(): void
  1021. {
  1022. $resolver = $this->createConfigurationResolver(['rules' => '']);
  1023. $this->expectException(InvalidConfigurationException::class);
  1024. $this->expectExceptionMessageMatches('/^Empty rules value is not allowed\.$/');
  1025. $resolver->getRules();
  1026. }
  1027. /**
  1028. * @param array<string, mixed>|bool $ruleConfig
  1029. *
  1030. * @dataProvider provideDeprecatedFixerConfiguredCases
  1031. *
  1032. * @group legacy
  1033. */
  1034. public function testDeprecatedFixerConfigured($ruleConfig): void
  1035. {
  1036. $this->expectDeprecation('Rule "Vendor4/foo" is deprecated. Use "testA" and "testB" instead.');
  1037. $fixer = $this->createDeprecatedFixerDouble();
  1038. $config = new Config();
  1039. $config->registerCustomFixers([$fixer]);
  1040. $config->setRules([$fixer->getName() => $ruleConfig]);
  1041. $resolver = $this->createConfigurationResolver([], $config);
  1042. $resolver->getFixers();
  1043. }
  1044. public static function provideDeprecatedFixerConfiguredCases(): iterable
  1045. {
  1046. yield [true];
  1047. yield [['foo' => true]];
  1048. yield [false];
  1049. }
  1050. /**
  1051. * @dataProvider provideDeprecatedRuleSetConfiguredCases
  1052. *
  1053. * @group legacy
  1054. *
  1055. * @param array<string> $successors
  1056. */
  1057. public function testDeprecatedRuleSetConfigured(string $ruleSet, array $successors): void
  1058. {
  1059. $this->expectDeprecation(sprintf(
  1060. 'Rule set "%s" is deprecated. %s.',
  1061. $ruleSet,
  1062. [] === $successors
  1063. ? 'No replacement available'
  1064. : sprintf('Use %s instead', Utils::naturalLanguageJoin($successors))
  1065. ));
  1066. $config = new Config();
  1067. $config->setRules([$ruleSet => true]);
  1068. $config->setRiskyAllowed(true);
  1069. $resolver = $this->createConfigurationResolver([], $config);
  1070. $resolver->getFixers();
  1071. }
  1072. /**
  1073. * @return iterable<array{0: string, 1: list<string>}>
  1074. */
  1075. public static function provideDeprecatedRuleSetConfiguredCases(): iterable
  1076. {
  1077. yield ['@PER', ['@PER-CS']];
  1078. yield ['@PER:risky', ['@PER-CS:risky']];
  1079. }
  1080. public static function provideGetDirectoryCases(): iterable
  1081. {
  1082. yield [null, '/my/path/my/file', 'my/file'];
  1083. yield ['/my/path/.php-cs-fixer.cache', '/my/path/my/file', 'my/file'];
  1084. yield ['/my/path2/dir/.php-cs-fixer.cache', '/my/path2/dir/dir2/file', 'dir2/file'];
  1085. yield ['dir/.php-cs-fixer.cache', '/my/path/dir/dir3/file', 'dir3/file'];
  1086. }
  1087. /**
  1088. * @dataProvider provideGetDirectoryCases
  1089. */
  1090. public function testGetDirectory(?string $cacheFile, string $file, string $expectedPathRelativeToFile): void
  1091. {
  1092. if (null !== $cacheFile) {
  1093. $cacheFile = $this->normalizePath($cacheFile);
  1094. }
  1095. $file = $this->normalizePath($file);
  1096. $expectedPathRelativeToFile = $this->normalizePath($expectedPathRelativeToFile);
  1097. $config = new Config();
  1098. if (null === $cacheFile) {
  1099. $config->setUsingCache(false);
  1100. } else {
  1101. $config->setCacheFile($cacheFile);
  1102. }
  1103. $resolver = new ConfigurationResolver($config, [], $this->normalizePath('/my/path'), $this->createToolInfoDouble());
  1104. $directory = $resolver->getDirectory();
  1105. self::assertSame($expectedPathRelativeToFile, $directory->getRelativePathTo($file));
  1106. }
  1107. private function normalizePath(string $path): string
  1108. {
  1109. return str_replace('/', \DIRECTORY_SEPARATOR, $path);
  1110. }
  1111. /**
  1112. * @param array<string, array<string, mixed>|bool> $expected
  1113. * @param array<string, array<string, mixed>|bool> $actual
  1114. */
  1115. private static function assertSameRules(array $expected, array $actual): void
  1116. {
  1117. ksort($expected);
  1118. ksort($actual);
  1119. self::assertSame($expected, $actual);
  1120. }
  1121. private static function getFixtureDir(): string
  1122. {
  1123. return realpath(__DIR__.\DIRECTORY_SEPARATOR.'..'.\DIRECTORY_SEPARATOR.'Fixtures'.\DIRECTORY_SEPARATOR.'ConfigurationResolverConfigFile'.\DIRECTORY_SEPARATOR).'/';
  1124. }
  1125. /**
  1126. * @param array<string, mixed> $options
  1127. */
  1128. private function createConfigurationResolver(
  1129. array $options,
  1130. Config $config = null,
  1131. string $cwdPath = '',
  1132. ToolInfoInterface $toolInfo = null
  1133. ): ConfigurationResolver {
  1134. return new ConfigurationResolver(
  1135. $config ?? new Config(),
  1136. $options,
  1137. $cwdPath,
  1138. $toolInfo ?? $this->createToolInfoDouble()
  1139. );
  1140. }
  1141. private function createDeprecatedFixerDouble(): DeprecatedFixerInterface
  1142. {
  1143. return new class() extends AbstractFixer implements DeprecatedFixerInterface, ConfigurableFixerInterface {
  1144. public function getDefinition(): FixerDefinitionInterface
  1145. {
  1146. throw new \LogicException('Not implemented.');
  1147. }
  1148. public function isCandidate(Tokens $tokens): bool
  1149. {
  1150. throw new \LogicException('Not implemented.');
  1151. }
  1152. public function getSuccessorsNames(): array
  1153. {
  1154. return ['testA', 'testB'];
  1155. }
  1156. public function getName(): string
  1157. {
  1158. return 'Vendor4/foo';
  1159. }
  1160. protected function applyFix(\SplFileInfo $file, Tokens $tokens): void {}
  1161. protected function createConfigurationDefinition(): FixerConfigurationResolverInterface
  1162. {
  1163. return new FixerConfigurationResolver([
  1164. (new FixerOptionBuilder('foo', 'Foo.'))->getOption(),
  1165. ]);
  1166. }
  1167. };
  1168. }
  1169. private function createToolInfoDouble(): ToolInfoInterface
  1170. {
  1171. return new class() implements ToolInfoInterface {
  1172. public function getComposerInstallationDetails(): array
  1173. {
  1174. throw new \BadMethodCallException();
  1175. }
  1176. public function getComposerVersion(): string
  1177. {
  1178. throw new \BadMethodCallException();
  1179. }
  1180. public function getVersion(): string
  1181. {
  1182. throw new \BadMethodCallException();
  1183. }
  1184. public function isInstalledAsPhar(): bool
  1185. {
  1186. return true;
  1187. }
  1188. public function isInstalledByComposer(): bool
  1189. {
  1190. throw new \BadMethodCallException();
  1191. }
  1192. public function isRunInsideDocker(): bool
  1193. {
  1194. return false;
  1195. }
  1196. public function getPharDownloadUri(string $version): string
  1197. {
  1198. throw new \BadMethodCallException();
  1199. }
  1200. };
  1201. }
  1202. }