ConfigTest.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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\Config;
  14. use PhpCsFixer\ConfigurationException\InvalidConfigurationException;
  15. use PhpCsFixer\Console\Application;
  16. use PhpCsFixer\Console\Command\FixCommand;
  17. use PhpCsFixer\Console\ConfigurationResolver;
  18. use PhpCsFixer\Finder;
  19. use PhpCsFixer\Fixer\ArrayNotation\NoWhitespaceBeforeCommaInArrayFixer;
  20. use PhpCsFixer\Fixer\ControlStructure\IncludeFixer;
  21. use PhpCsFixer\Fixer\FixerInterface;
  22. use PhpCsFixer\Runner\Parallel\ParallelConfig;
  23. use PhpCsFixer\ToolInfo;
  24. use Symfony\Component\Console\Output\OutputInterface;
  25. use Symfony\Component\Console\Tester\CommandTester;
  26. use Symfony\Component\Finder\Finder as SymfonyFinder;
  27. /**
  28. * @internal
  29. *
  30. * @covers \PhpCsFixer\Config
  31. */
  32. final class ConfigTest extends TestCase
  33. {
  34. public function testFutureMode(): void
  35. {
  36. $futureMode = getenv('PHP_CS_FIXER_FUTURE_MODE');
  37. putenv('PHP_CS_FIXER_FUTURE_MODE=1');
  38. $config = new Config('test');
  39. self::assertSame('test (future mode)', $config->getName());
  40. self::assertArrayHasKey('@PER-CS', $config->getRules());
  41. putenv('PHP_CS_FIXER_FUTURE_MODE='.$futureMode);
  42. }
  43. public function testConfigRulesUsingSeparateMethod(): void
  44. {
  45. $config = new Config();
  46. $configResolver = new ConfigurationResolver(
  47. $config,
  48. [
  49. 'rules' => 'cast_spaces,statement_indentation',
  50. ],
  51. getcwd(),
  52. new ToolInfo()
  53. );
  54. self::assertSame(
  55. [
  56. 'cast_spaces' => true,
  57. 'statement_indentation' => true,
  58. ],
  59. $configResolver->getRules()
  60. );
  61. }
  62. public function testConfigRulesUsingJsonMethod(): void
  63. {
  64. $config = new Config();
  65. $configResolver = new ConfigurationResolver(
  66. $config,
  67. [
  68. 'rules' => '{"array_syntax": {"syntax": "short"}, "cast_spaces": true}',
  69. ],
  70. getcwd(),
  71. new ToolInfo()
  72. );
  73. self::assertSame(
  74. [
  75. 'array_syntax' => [
  76. 'syntax' => 'short',
  77. ],
  78. 'cast_spaces' => true,
  79. ],
  80. $configResolver->getRules()
  81. );
  82. }
  83. public function testConfigRulesUsingInvalidJson(): void
  84. {
  85. $this->expectException(InvalidConfigurationException::class);
  86. $config = new Config();
  87. $configResolver = new ConfigurationResolver(
  88. $config,
  89. [
  90. 'rules' => '{blah',
  91. ],
  92. getcwd(),
  93. new ToolInfo()
  94. );
  95. $configResolver->getRules();
  96. }
  97. public function testCustomConfig(): void
  98. {
  99. $customConfigFile = __DIR__.'/Fixtures/.php-cs-fixer.custom.php';
  100. $application = new Application();
  101. $application->add(new FixCommand(new ToolInfo()));
  102. $commandTester = new CommandTester($application->find('fix'));
  103. $commandTester->execute(
  104. [
  105. 'path' => [$customConfigFile],
  106. '--dry-run' => true,
  107. '--config' => $customConfigFile,
  108. ],
  109. [
  110. 'decorated' => false,
  111. 'verbosity' => OutputInterface::VERBOSITY_VERY_VERBOSE,
  112. ]
  113. );
  114. self::assertStringMatchesFormat(
  115. \sprintf('%%ALoaded config custom_config_test from "%s".%%A', $customConfigFile),
  116. $commandTester->getDisplay(true)
  117. );
  118. }
  119. public function testThatFinderWorksWithDirSetOnConfig(): void
  120. {
  121. $config = new Config();
  122. $items = iterator_to_array(
  123. $config->getFinder()->in(__DIR__.'/Fixtures/FinderDirectory'),
  124. false
  125. );
  126. self::assertCount(1, $items);
  127. $item = reset($items);
  128. self::assertSame('somefile.php', $item->getFilename());
  129. }
  130. public function testThatCustomFinderWorks(): void
  131. {
  132. $finder = new Finder();
  133. $finder->in(__DIR__.'/Fixtures/FinderDirectory');
  134. $config = (new Config())->setFinder($finder);
  135. $items = iterator_to_array(
  136. $config->getFinder(),
  137. false
  138. );
  139. self::assertCount(1, $items);
  140. self::assertSame('somefile.php', $items[0]->getFilename());
  141. }
  142. public function testThatCustomSymfonyFinderWorks(): void
  143. {
  144. $finder = new SymfonyFinder();
  145. $finder->in(__DIR__.'/Fixtures/FinderDirectory');
  146. $config = (new Config())->setFinder($finder);
  147. $items = iterator_to_array(
  148. $config->getFinder(),
  149. false
  150. );
  151. self::assertCount(1, $items);
  152. self::assertSame('somefile.php', $items[0]->getFilename());
  153. }
  154. public function testThatCacheFileHasDefaultValue(): void
  155. {
  156. $config = new Config();
  157. self::assertSame('.php-cs-fixer.cache', $config->getCacheFile());
  158. }
  159. public function testThatCacheFileCanBeMutated(): void
  160. {
  161. $cacheFile = 'some-directory/some.file';
  162. $config = new Config();
  163. $config->setCacheFile($cacheFile);
  164. self::assertSame($cacheFile, $config->getCacheFile());
  165. }
  166. public function testThatMutatorHasFluentInterface(): void
  167. {
  168. $config = new Config();
  169. self::assertSame($config, $config->setCacheFile('some-directory/some.file'));
  170. }
  171. /**
  172. * @param list<FixerInterface> $expected
  173. * @param iterable<FixerInterface> $suite
  174. *
  175. * @dataProvider provideRegisterCustomFixersCases
  176. */
  177. public function testRegisterCustomFixers(array $expected, iterable $suite): void
  178. {
  179. $config = new Config();
  180. $config->registerCustomFixers($suite);
  181. self::assertSame($expected, $config->getCustomFixers());
  182. }
  183. public function testConfigDefault(): void
  184. {
  185. $config = new Config();
  186. self::assertSame('.php-cs-fixer.cache', $config->getCacheFile());
  187. self::assertSame([], $config->getCustomFixers());
  188. self::assertSame('txt', $config->getFormat());
  189. self::assertFalse($config->getHideProgress());
  190. self::assertSame(' ', $config->getIndent());
  191. self::assertSame("\n", $config->getLineEnding());
  192. self::assertSame('default', $config->getName());
  193. self::assertNull($config->getPhpExecutable());
  194. self::assertFalse($config->getRiskyAllowed());
  195. self::assertSame(['@PSR12' => true], $config->getRules());
  196. self::assertTrue($config->getUsingCache());
  197. $finder = $config->getFinder();
  198. self::assertInstanceOf(Finder::class, $finder);
  199. $config->setFormat('xml');
  200. self::assertSame('xml', $config->getFormat());
  201. $config->setHideProgress(true);
  202. self::assertTrue($config->getHideProgress());
  203. $config->setIndent("\t");
  204. self::assertSame("\t", $config->getIndent());
  205. $finder = new Finder();
  206. $config->setFinder($finder);
  207. self::assertSame($finder, $config->getFinder());
  208. $config->setLineEnding("\r\n");
  209. self::assertSame("\r\n", $config->getLineEnding());
  210. $config->setPhpExecutable(null);
  211. self::assertNull($config->getPhpExecutable());
  212. $config->setUsingCache(false);
  213. self::assertFalse($config->getUsingCache());
  214. }
  215. public static function provideRegisterCustomFixersCases(): iterable
  216. {
  217. $fixers = [
  218. new NoWhitespaceBeforeCommaInArrayFixer(),
  219. new IncludeFixer(),
  220. ];
  221. yield [$fixers, $fixers];
  222. yield [$fixers, new \ArrayIterator($fixers)];
  223. }
  224. public function testConfigConstructorWithName(): void
  225. {
  226. $anonymousConfig = new Config();
  227. $namedConfig = new Config('foo');
  228. self::assertSame($anonymousConfig->getName(), 'default');
  229. self::assertSame($namedConfig->getName(), 'foo');
  230. }
  231. public function testConfigWithDefaultParallelConfig(): void
  232. {
  233. $config = new Config();
  234. self::assertSame(1, $config->getParallelConfig()->getMaxProcesses());
  235. }
  236. public function testConfigWithExplicitParallelConfig(): void
  237. {
  238. $config = new Config();
  239. $config->setParallelConfig(new ParallelConfig(5, 10, 15));
  240. self::assertSame(5, $config->getParallelConfig()->getMaxProcesses());
  241. self::assertSame(10, $config->getParallelConfig()->getFilesPerProcess());
  242. self::assertSame(15, $config->getParallelConfig()->getProcessTimeout());
  243. }
  244. }