ConfigTest.php 5.8 KB

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