ConfigTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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\ArrayNotation\NoWhitespaceBeforeCommaInArrayFixer;
  18. use PhpCsFixer\Fixer\ControlStructure\IncludeFixer;
  19. use PhpCsFixer\Fixer\FixerInterface;
  20. use PhpCsFixer\ToolInfo;
  21. use Symfony\Component\Console\Output\OutputInterface;
  22. use Symfony\Component\Console\Tester\CommandTester;
  23. use Symfony\Component\Finder\Finder as SymfonyFinder;
  24. /**
  25. * @internal
  26. *
  27. * @covers \PhpCsFixer\Config
  28. */
  29. final class ConfigTest extends TestCase
  30. {
  31. public function testConfigRulesUsingSeparateMethod()
  32. {
  33. $config = new Config();
  34. $configResolver = new ConfigurationResolver(
  35. $config,
  36. [
  37. 'rules' => 'cast_spaces,braces',
  38. ],
  39. getcwd(),
  40. new ToolInfo()
  41. );
  42. static::assertArraySubset(
  43. [
  44. 'cast_spaces' => true,
  45. 'braces' => true,
  46. ],
  47. $configResolver->getRules()
  48. );
  49. }
  50. public function testConfigRulesUsingJsonMethod()
  51. {
  52. $config = new Config();
  53. $configResolver = new ConfigurationResolver(
  54. $config,
  55. [
  56. 'rules' => '{"array_syntax": {"syntax": "short"}, "cast_spaces": true}',
  57. ],
  58. getcwd(),
  59. new ToolInfo()
  60. );
  61. static::assertArraySubset(
  62. [
  63. 'array_syntax' => [
  64. 'syntax' => 'short',
  65. ],
  66. 'cast_spaces' => true,
  67. ],
  68. $configResolver->getRules()
  69. );
  70. }
  71. public function testConfigRulesUsingInvalidJson()
  72. {
  73. $this->expectException(\PhpCsFixer\ConfigurationException\InvalidConfigurationException::class);
  74. $config = new Config();
  75. $configResolver = new ConfigurationResolver(
  76. $config,
  77. [
  78. 'rules' => '{blah',
  79. ],
  80. getcwd(),
  81. new ToolInfo()
  82. );
  83. $configResolver->getRules();
  84. }
  85. public function testCustomConfig()
  86. {
  87. $customConfigFile = __DIR__.'/Fixtures/.php_cs_custom.php';
  88. $application = new Application();
  89. $application->add(new FixCommand(new ToolInfo()));
  90. $commandTester = new CommandTester($application->find('fix'));
  91. $commandTester->execute(
  92. [
  93. 'path' => [$customConfigFile],
  94. '--dry-run' => true,
  95. '--config' => $customConfigFile,
  96. ],
  97. [
  98. 'decorated' => false,
  99. 'verbosity' => OutputInterface::VERBOSITY_VERY_VERBOSE,
  100. ]
  101. );
  102. static::assertStringMatchesFormat(
  103. sprintf('%%ALoaded config custom_config_test from "%s".%%A', $customConfigFile),
  104. $commandTester->getDisplay(true)
  105. );
  106. }
  107. public function testThatFinderWorksWithDirSetOnConfig()
  108. {
  109. $config = new Config();
  110. $items = iterator_to_array(
  111. $config->getFinder()->in(__DIR__.'/Fixtures/FinderDirectory'),
  112. false
  113. );
  114. static::assertCount(1, $items);
  115. static::assertSame('somefile.php', $items[0]->getFilename());
  116. }
  117. public function testThatCustomFinderWorks()
  118. {
  119. $finder = new Finder();
  120. $finder->in(__DIR__.'/Fixtures/FinderDirectory');
  121. $config = (new Config())->setFinder($finder);
  122. $items = iterator_to_array(
  123. $config->getFinder(),
  124. false
  125. );
  126. static::assertCount(1, $items);
  127. static::assertSame('somefile.php', $items[0]->getFilename());
  128. }
  129. public function testThatCustomSymfonyFinderWorks()
  130. {
  131. $finder = new SymfonyFinder();
  132. $finder->in(__DIR__.'/Fixtures/FinderDirectory');
  133. $config = (new Config())->setFinder($finder);
  134. $items = iterator_to_array(
  135. $config->getFinder(),
  136. false
  137. );
  138. static::assertCount(1, $items);
  139. static::assertSame('somefile.php', $items[0]->getFilename());
  140. }
  141. public function testThatCacheFileHasDefaultValue()
  142. {
  143. $config = new Config();
  144. static::assertSame('.php_cs.cache', $config->getCacheFile());
  145. }
  146. public function testThatCacheFileCanBeMutated()
  147. {
  148. $cacheFile = 'some-directory/some.file';
  149. $config = new Config();
  150. $config->setCacheFile($cacheFile);
  151. static::assertSame($cacheFile, $config->getCacheFile());
  152. }
  153. public function testThatMutatorHasFluentInterface()
  154. {
  155. $config = new Config();
  156. static::assertSame($config, $config->setCacheFile('some-directory/some.file'));
  157. }
  158. public function testRegisterCustomFixersWithInvalidArgument()
  159. {
  160. $this->expectException(\InvalidArgumentException::class);
  161. $this->expectExceptionMessageRegExp('/^Argument must be an array or a Traversable, got "\w+"\.$/');
  162. $config = new Config();
  163. $config->registerCustomFixers('foo');
  164. }
  165. /**
  166. * @param FixerInterface[] $expected
  167. * @param iterable $suite
  168. *
  169. * @dataProvider provideRegisterCustomFixersCases
  170. */
  171. public function testRegisterCustomFixers(array $expected, $suite)
  172. {
  173. $config = new Config();
  174. $config->registerCustomFixers($suite);
  175. static::assertSame($expected, $config->getCustomFixers());
  176. }
  177. public function testConfigDefault()
  178. {
  179. $config = new Config();
  180. static::assertSame('.php_cs.cache', $config->getCacheFile());
  181. static::assertSame([], $config->getCustomFixers());
  182. static::assertSame('txt', $config->getFormat());
  183. static::assertFalse($config->getHideProgress());
  184. static::assertSame(' ', $config->getIndent());
  185. static::assertSame("\n", $config->getLineEnding());
  186. static::assertSame('default', $config->getName());
  187. static::assertNull($config->getPhpExecutable());
  188. static::assertFalse($config->getRiskyAllowed());
  189. static::assertSame(['@PSR2' => true], $config->getRules());
  190. static::assertTrue($config->getUsingCache());
  191. $finder = $config->getFinder();
  192. static::assertInstanceOf(Finder::class, $finder);
  193. $config->setFormat('xml');
  194. static::assertSame('xml', $config->getFormat());
  195. $config->setHideProgress(true);
  196. static::assertTrue($config->getHideProgress());
  197. $config->setIndent("\t");
  198. static::assertSame("\t", $config->getIndent());
  199. $finder = new Finder();
  200. $config->setFinder($finder);
  201. static::assertSame($finder, $config->getFinder());
  202. $config->setLineEnding("\r\n");
  203. static::assertSame("\r\n", $config->getLineEnding());
  204. $config->setPhpExecutable(null);
  205. static::assertNull($config->getPhpExecutable());
  206. $config->setUsingCache(false);
  207. static::assertFalse($config->getUsingCache());
  208. }
  209. public function testSetInvalidFinder()
  210. {
  211. $config = new Config();
  212. $this->expectException(\InvalidArgumentException::class);
  213. $this->expectExceptionMessageRegExp('/^Argument must be an array or a Traversable, got "integer"\.$/');
  214. $config->setFinder(123);
  215. }
  216. /**
  217. * @return array
  218. */
  219. public function provideRegisterCustomFixersCases()
  220. {
  221. $fixers = [
  222. new NoWhitespaceBeforeCommaInArrayFixer(),
  223. new IncludeFixer(),
  224. ];
  225. return [
  226. [$fixers, $fixers],
  227. [$fixers, new \ArrayIterator($fixers)],
  228. ];
  229. }
  230. public function testConfigConstructorWithName()
  231. {
  232. $anonymousConfig = new Config();
  233. $namedConfig = new Config('foo');
  234. static::assertSame($anonymousConfig->getName(), 'default');
  235. static::assertSame($namedConfig->getName(), 'foo');
  236. }
  237. }