ConfigTest.php 5.8 KB

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