ConfigTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /*
  3. * This file is part of PHP CS Fixer.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. namespace PhpCsFixer\Tests;
  12. use PhpCsFixer\Config;
  13. use PhpCsFixer\Console\Application;
  14. use PhpCsFixer\Console\Command\FixCommand;
  15. use PhpCsFixer\Console\ConfigurationResolver;
  16. use PhpCsFixer\Finder;
  17. use PhpCsFixer\Fixer\FixerInterface;
  18. use PhpCsFixer\ToolInfo;
  19. use Symfony\Component\Console\Output\OutputInterface;
  20. use Symfony\Component\Console\Tester\CommandTester;
  21. use Symfony\Component\Finder\Finder as SymfonyFinder;
  22. /**
  23. * @internal
  24. *
  25. * @covers \PhpCsFixer\Config
  26. */
  27. final class ConfigTest extends TestCase
  28. {
  29. public function testConfigRulesUsingSeparateMethod()
  30. {
  31. $config = new Config();
  32. $configResolver = new ConfigurationResolver(
  33. $config,
  34. [
  35. 'rules' => 'cast_spaces,braces',
  36. ],
  37. getcwd(),
  38. new ToolInfo()
  39. );
  40. static::assertArraySubset(
  41. [
  42. 'cast_spaces' => true,
  43. 'braces' => true,
  44. ],
  45. $configResolver->getRules()
  46. );
  47. }
  48. public function testConfigRulesUsingJsonMethod()
  49. {
  50. $config = new Config();
  51. $configResolver = new ConfigurationResolver(
  52. $config,
  53. [
  54. 'rules' => '{"array_syntax": {"syntax": "short"}, "cast_spaces": true}',
  55. ],
  56. getcwd(),
  57. new ToolInfo()
  58. );
  59. static::assertArraySubset(
  60. [
  61. 'array_syntax' => [
  62. 'syntax' => 'short',
  63. ],
  64. 'cast_spaces' => true,
  65. ],
  66. $configResolver->getRules()
  67. );
  68. }
  69. public function testConfigRulesUsingInvalidJson()
  70. {
  71. $this->expectException(\PhpCsFixer\ConfigurationException\InvalidConfigurationException::class);
  72. $config = new Config();
  73. $configResolver = new ConfigurationResolver(
  74. $config,
  75. [
  76. 'rules' => '{blah',
  77. ],
  78. getcwd(),
  79. new ToolInfo()
  80. );
  81. $configResolver->getRules();
  82. }
  83. public function testCustomConfig()
  84. {
  85. $customConfigFile = __DIR__.'/Fixtures/.php_cs_custom.php';
  86. $application = new Application();
  87. $application->add(new FixCommand(new ToolInfo()));
  88. $commandTester = new CommandTester($application->find('fix'));
  89. $commandTester->execute(
  90. [
  91. 'path' => [$customConfigFile],
  92. '--dry-run' => true,
  93. '--config' => $customConfigFile,
  94. ],
  95. [
  96. 'decorated' => false,
  97. 'verbosity' => OutputInterface::VERBOSITY_VERY_VERBOSE,
  98. ]
  99. );
  100. static::assertStringMatchesFormat(
  101. sprintf('%%ALoaded config custom_config_test from "%s".%%A', $customConfigFile),
  102. $commandTester->getDisplay(true)
  103. );
  104. }
  105. public function testThatFinderWorksWithDirSetOnConfig()
  106. {
  107. $config = new Config();
  108. $items = iterator_to_array(
  109. $config->getFinder()->in(__DIR__.'/Fixtures/FinderDirectory'),
  110. false
  111. );
  112. static::assertCount(1, $items);
  113. static::assertSame('somefile.php', $items[0]->getFilename());
  114. }
  115. public function testThatCustomFinderWorks()
  116. {
  117. $finder = new Finder();
  118. $finder->in(__DIR__.'/Fixtures/FinderDirectory');
  119. $config = Config::create()->setFinder($finder);
  120. $items = iterator_to_array(
  121. $config->getFinder(),
  122. false
  123. );
  124. static::assertCount(1, $items);
  125. static::assertSame('somefile.php', $items[0]->getFilename());
  126. }
  127. public function testThatCustomSymfonyFinderWorks()
  128. {
  129. $finder = new SymfonyFinder();
  130. $finder->in(__DIR__.'/Fixtures/FinderDirectory');
  131. $config = Config::create()->setFinder($finder);
  132. $items = iterator_to_array(
  133. $config->getFinder(),
  134. false
  135. );
  136. static::assertCount(1, $items);
  137. static::assertSame('somefile.php', $items[0]->getFilename());
  138. }
  139. public function testThatCacheFileHasDefaultValue()
  140. {
  141. $config = new Config();
  142. static::assertSame('.php_cs.cache', $config->getCacheFile());
  143. }
  144. public function testThatCacheFileCanBeMutated()
  145. {
  146. $cacheFile = 'some-directory/some.file';
  147. $config = new Config();
  148. $config->setCacheFile($cacheFile);
  149. static::assertSame($cacheFile, $config->getCacheFile());
  150. }
  151. public function testThatMutatorHasFluentInterface()
  152. {
  153. $config = new Config();
  154. static::assertSame($config, $config->setCacheFile('some-directory/some.file'));
  155. }
  156. public function testRegisterCustomFixersWithInvalidArgument()
  157. {
  158. $this->expectException(\InvalidArgumentException::class);
  159. $this->expectExceptionMessageRegExp('/^Argument must be an array or a Traversable, got "\w+"\.$/');
  160. $config = new Config();
  161. $config->registerCustomFixers('foo');
  162. }
  163. /**
  164. * @param FixerInterface[] $expected
  165. * @param iterable $suite
  166. *
  167. * @dataProvider provideRegisterCustomFixersCases
  168. */
  169. public function testRegisterCustomFixers(array $expected, $suite)
  170. {
  171. $config = new Config();
  172. $config->registerCustomFixers($suite);
  173. static::assertSame($expected, $config->getCustomFixers());
  174. }
  175. /**
  176. * @return array
  177. */
  178. public function provideRegisterCustomFixersCases()
  179. {
  180. $fixers = [
  181. new \PhpCsFixer\Fixer\ArrayNotation\NoWhitespaceBeforeCommaInArrayFixer(),
  182. new \PhpCsFixer\Fixer\ControlStructure\IncludeFixer(),
  183. ];
  184. return [
  185. [$fixers, $fixers],
  186. [$fixers, new \ArrayIterator($fixers)],
  187. ];
  188. }
  189. }