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,braces',
  40. ],
  41. getcwd(),
  42. new ToolInfo()
  43. );
  44. static::assertSame(
  45. [
  46. 'cast_spaces' => true,
  47. 'braces' => 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. static::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. static::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. static::assertCount(1, $items);
  117. $item = reset($items);
  118. static::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. static::assertCount(1, $items);
  130. static::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. static::assertCount(1, $items);
  142. static::assertSame('somefile.php', $items[0]->getFilename());
  143. }
  144. public function testThatCacheFileHasDefaultValue(): void
  145. {
  146. $config = new Config();
  147. static::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. static::assertSame($cacheFile, $config->getCacheFile());
  155. }
  156. public function testThatMutatorHasFluentInterface(): void
  157. {
  158. $config = new Config();
  159. static::assertSame($config, $config->setCacheFile('some-directory/some.file'));
  160. }
  161. /**
  162. * @param FixerInterface[] $expected
  163. *
  164. * @dataProvider provideRegisterCustomFixersCases
  165. */
  166. public function testRegisterCustomFixers(array $expected, iterable $suite): void
  167. {
  168. $config = new Config();
  169. $config->registerCustomFixers($suite);
  170. static::assertSame($expected, $config->getCustomFixers());
  171. }
  172. public function testConfigDefault(): void
  173. {
  174. $config = new Config();
  175. static::assertSame('.php-cs-fixer.cache', $config->getCacheFile());
  176. static::assertSame([], $config->getCustomFixers());
  177. static::assertSame('txt', $config->getFormat());
  178. static::assertFalse($config->getHideProgress());
  179. static::assertSame(' ', $config->getIndent());
  180. static::assertSame("\n", $config->getLineEnding());
  181. static::assertSame('default', $config->getName());
  182. static::assertNull($config->getPhpExecutable());
  183. static::assertFalse($config->getRiskyAllowed());
  184. static::assertSame(['@PSR12' => true], $config->getRules());
  185. static::assertTrue($config->getUsingCache());
  186. $finder = $config->getFinder();
  187. static::assertInstanceOf(Finder::class, $finder);
  188. $config->setFormat('xml');
  189. static::assertSame('xml', $config->getFormat());
  190. $config->setHideProgress(true);
  191. static::assertTrue($config->getHideProgress());
  192. $config->setIndent("\t");
  193. static::assertSame("\t", $config->getIndent());
  194. $finder = new Finder();
  195. $config->setFinder($finder);
  196. static::assertSame($finder, $config->getFinder());
  197. $config->setLineEnding("\r\n");
  198. static::assertSame("\r\n", $config->getLineEnding());
  199. $config->setPhpExecutable(null);
  200. static::assertNull($config->getPhpExecutable());
  201. $config->setUsingCache(false);
  202. static::assertFalse($config->getUsingCache());
  203. }
  204. public function provideRegisterCustomFixersCases(): array
  205. {
  206. $fixers = [
  207. new NoWhitespaceBeforeCommaInArrayFixer(),
  208. new IncludeFixer(),
  209. ];
  210. return [
  211. [$fixers, $fixers],
  212. [$fixers, new \ArrayIterator($fixers)],
  213. ];
  214. }
  215. public function testConfigConstructorWithName(): void
  216. {
  217. $anonymousConfig = new Config();
  218. $namedConfig = new Config('foo');
  219. static::assertSame($anonymousConfig->getName(), 'default');
  220. static::assertSame($namedConfig->getName(), 'foo');
  221. }
  222. }