ConfigTest.php 5.8 KB

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