ConfigTest.php 7.6 KB

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