ConfigurationResolverTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. <?php
  2. /*
  3. * This file is part of the PHP CS utility.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Symfony\CS\Tests\Console;
  11. use Symfony\CS\Config;
  12. use Symfony\CS\Console\ConfigurationResolver;
  13. use Symfony\CS\Fixer;
  14. use Symfony\CS\Test\AccessibleObject;
  15. /**
  16. * @author Katsuhiro Ogawa <ko.fivestar@gmail.com>
  17. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  18. *
  19. * @internal
  20. */
  21. final class ConfigurationResolverTest extends \PHPUnit_Framework_TestCase
  22. {
  23. protected $config;
  24. protected $resolver;
  25. protected function setUp()
  26. {
  27. $fixer = new Fixer();
  28. $this->config = new Config();
  29. $this->resolver = new ConfigurationResolver();
  30. $this->resolver
  31. ->setDefaultConfig($this->config)
  32. ->setFixer($fixer)
  33. ;
  34. }
  35. protected function tearDown()
  36. {
  37. unset(
  38. $this->config,
  39. $this->resolver
  40. );
  41. }
  42. public function testSetOption()
  43. {
  44. $this->resolver->setOption('path', '.');
  45. $property = AccessibleObject::create($this->resolver)->options;
  46. $this->assertSame('.', $property['path']);
  47. }
  48. /**
  49. * @expectedException \Symfony\CS\ConfigurationException\InvalidConfigurationException
  50. * @expectedExceptionMessageRegExp /^Unknown option name: "foo"\.$/
  51. */
  52. public function testSetOptionWithUndefinedOption()
  53. {
  54. $this->resolver->setOption('foo', 'bar');
  55. }
  56. public function testSetOptions()
  57. {
  58. $this->resolver->setOptions(array(
  59. 'path' => '.',
  60. 'config' => 'config.php_cs',
  61. ));
  62. $property = AccessibleObject::create($this->resolver)->options;
  63. $this->assertSame('.', $property['path']);
  64. $this->assertSame('config.php_cs', $property['config']);
  65. }
  66. public function testCwd()
  67. {
  68. $this->resolver->setCwd('foo');
  69. $property = AccessibleObject::create($this->resolver)->cwd;
  70. $this->assertSame('foo', $property);
  71. }
  72. protected function makeFixersTest($expectedFixers, $resolvedFixers)
  73. {
  74. $this->assertCount(count($expectedFixers), $resolvedFixers);
  75. foreach ($expectedFixers as $fixer) {
  76. $this->assertContains($fixer, $resolvedFixers);
  77. }
  78. }
  79. public function testResolveFixersReturnsEmptyArrayByDefault()
  80. {
  81. $this->makeFixersTest(array(), $this->resolver->getFixers());
  82. }
  83. public function testResolveProgressWithPositiveConfigAndPositiveOption()
  84. {
  85. $this->config->hideProgress(true);
  86. $this->resolver
  87. ->setOption('progress', true)
  88. ->resolve()
  89. ;
  90. $this->assertFalse($this->resolver->getProgress());
  91. }
  92. public function testResolveProgressWithPositiveConfigAndNegativeOption()
  93. {
  94. $this->config->hideProgress(true);
  95. $this->resolver
  96. ->setOption('progress', false)
  97. ->resolve()
  98. ;
  99. $this->assertFalse($this->resolver->getProgress());
  100. }
  101. public function testResolveProgressWithNegativeConfigAndPositiveOption()
  102. {
  103. $this->config->hideProgress(false);
  104. $this->resolver
  105. ->setOption('progress', true)
  106. ->resolve()
  107. ;
  108. $this->assertTrue($this->resolver->getProgress());
  109. }
  110. public function testResolveProgressWithNegativeConfigAndNegativeOption()
  111. {
  112. $this->config->hideProgress(false);
  113. $this->resolver
  114. ->setOption('progress', false)
  115. ->resolve()
  116. ;
  117. $this->assertFalse($this->resolver->getProgress());
  118. }
  119. public function testResolveConfigFileDefault()
  120. {
  121. $this->resolver
  122. ->resolve();
  123. $this->assertNull($this->resolver->getConfigFile());
  124. $this->assertInstanceOf('\\Symfony\\CS\\ConfigInterface', $this->resolver->getConfig());
  125. }
  126. public function testResolveConfigFileByPathOfFile()
  127. {
  128. $dir = __DIR__.'/../Fixtures/ConfigurationResolverConfigFile/case_1';
  129. $this->resolver
  130. ->setOption('path', $dir.DIRECTORY_SEPARATOR.'foo.php')
  131. ->resolve();
  132. $this->assertSame($dir.DIRECTORY_SEPARATOR.'.php_cs.dist', $this->resolver->getConfigFile());
  133. $this->assertInstanceOf('Test1Config', $this->resolver->getConfig());
  134. }
  135. public function testResolveConfigFileSpecified()
  136. {
  137. $file = __DIR__.'/../Fixtures/ConfigurationResolverConfigFile/case_4/my.php_cs';
  138. $this->resolver
  139. ->setOption('config', $file)
  140. ->resolve();
  141. $this->assertSame($file, $this->resolver->getConfigFile());
  142. $this->assertInstanceOf('Test4Config', $this->resolver->getConfig());
  143. }
  144. /**
  145. * @dataProvider provideResolveConfigFileDefaultCases
  146. */
  147. public function testResolveConfigFileChooseFile($expectedFile, $expectedClass, $path)
  148. {
  149. $this->resolver
  150. ->setOption('path', $path)
  151. ->resolve();
  152. $this->assertSame($expectedFile, $this->resolver->getConfigFile());
  153. $this->assertInstanceOf($expectedClass, $this->resolver->getConfig());
  154. }
  155. public function provideResolveConfigFileDefaultCases()
  156. {
  157. $dirBase = __DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'ConfigurationResolverConfigFile'.DIRECTORY_SEPARATOR;
  158. return array(
  159. array(
  160. $dirBase.'case_1'.DIRECTORY_SEPARATOR.'.php_cs.dist',
  161. 'Test1Config',
  162. $dirBase.'case_1',
  163. ),
  164. array(
  165. $dirBase.'case_2'.DIRECTORY_SEPARATOR.'.php_cs',
  166. 'Test2Config',
  167. $dirBase.'case_2',
  168. ),
  169. array(
  170. $dirBase.'case_3'.DIRECTORY_SEPARATOR.'.php_cs',
  171. 'Test3Config',
  172. $dirBase.'case_3',
  173. ),
  174. );
  175. }
  176. /**
  177. * @expectedException \Symfony\CS\ConfigurationException\InvalidConfigurationException
  178. * @expectedExceptionMessageRegExp /^The config file: ".+[\/\\]Fixtures[\/\\]ConfigurationResolverConfigFile[\/\\]case_5[\/\\].php_cs.dist" does not return a "Symfony\\CS\\ConfigInterface" instance\. Got: "string"\.$/
  179. */
  180. public function testResolveConfigFileChooseFileWithInvalidFile()
  181. {
  182. $dirBase = __DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'ConfigurationResolverConfigFile'.DIRECTORY_SEPARATOR;
  183. $dirBase = realpath($dirBase);
  184. $this->resolver
  185. ->setOption('path', $dirBase.'/case_5')
  186. ->resolve();
  187. }
  188. public function testResolvePathRelative()
  189. {
  190. $this->resolver
  191. ->setCwd(__DIR__)
  192. ->setOption('path', 'Foo'.DIRECTORY_SEPARATOR.'Bar')
  193. ->resolve();
  194. $this->assertSame(__DIR__.DIRECTORY_SEPARATOR.'Foo'.DIRECTORY_SEPARATOR.'Bar', $this->resolver->getPath());
  195. }
  196. public function testResolveIsDryRunViaStdIn()
  197. {
  198. $this->resolver
  199. ->setOption('path', '-')
  200. ->setOption('dry-run', false)
  201. ->resolve();
  202. $this->assertTrue($this->resolver->isDryRun());
  203. }
  204. public function testResolveIsDryRunViaNegativeOption()
  205. {
  206. $this->resolver
  207. ->setOption('dry-run', false)
  208. ->resolve();
  209. $this->assertFalse($this->resolver->isDryRun());
  210. }
  211. public function testResolveIsDryRunViaPositiveOption()
  212. {
  213. $this->resolver
  214. ->setOption('dry-run', true)
  215. ->resolve();
  216. $this->assertTrue($this->resolver->isDryRun());
  217. }
  218. public function testResolveUsingCacheWithPositiveConfigAndPositiveOption()
  219. {
  220. $this->config->setUsingCache(true);
  221. $this->resolver
  222. ->setOption('using-cache', 'yes')
  223. ->resolve();
  224. $this->assertTrue($this->config->usingCache());
  225. }
  226. public function testResolveUsingCacheWithPositiveConfigAndNegativeOption()
  227. {
  228. $this->config->setUsingCache(true);
  229. $this->resolver
  230. ->setOption('using-cache', 'no')
  231. ->resolve();
  232. $this->assertFalse($this->config->usingCache());
  233. }
  234. public function testResolveUsingCacheWithNegativeConfigAndPositiveOption()
  235. {
  236. $this->config->setUsingCache(false);
  237. $this->resolver
  238. ->setOption('using-cache', 'yes')
  239. ->resolve();
  240. $this->assertTrue($this->config->usingCache());
  241. }
  242. public function testResolveUsingCacheWithNegativeConfigAndNegativeOption()
  243. {
  244. $this->config->setUsingCache(false);
  245. $this->resolver
  246. ->setOption('using-cache', 'no')
  247. ->resolve();
  248. $this->assertFalse($this->config->usingCache());
  249. }
  250. public function testResolveUsingCacheWithPositiveConfigAndNoOption()
  251. {
  252. $this->config->setUsingCache(true);
  253. $this->resolver
  254. ->resolve();
  255. $this->assertTrue($this->config->usingCache());
  256. }
  257. public function testResolveUsingCacheWithNegativeConfigAndNoOption()
  258. {
  259. $this->config->setUsingCache(false);
  260. $this->resolver
  261. ->resolve();
  262. $this->assertFalse($this->config->usingCache());
  263. }
  264. public function testResolveCacheFileWithoutConfigAndOption()
  265. {
  266. $default = $this->config->getCacheFile();
  267. $this->resolver->resolve();
  268. $this->assertSame($default, $this->config->getCacheFile());
  269. }
  270. public function testResolveCacheFileWithConfig()
  271. {
  272. $cacheFile = 'foo/bar.baz';
  273. $this->config->setCacheFile($cacheFile);
  274. $this->resolver->resolve();
  275. $this->assertSame($cacheFile, $this->config->getCacheFile());
  276. }
  277. public function testResolveCacheFileWithOption()
  278. {
  279. $cacheFile = 'bar.baz';
  280. $this->config->setCacheFile($cacheFile);
  281. $this->resolver->setOption('cache-file', $cacheFile);
  282. $this->resolver->resolve();
  283. $this->assertSame($cacheFile, $this->config->getCacheFile());
  284. }
  285. public function testResolveCacheFileWithConfigAndOption()
  286. {
  287. $configCacheFile = 'foo/bar.baz';
  288. $optionCacheFile = 'bar.baz';
  289. $this->config->setCacheFile($configCacheFile);
  290. $this->resolver->setOption('cache-file', $optionCacheFile);
  291. $this->resolver->resolve();
  292. $this->assertSame($optionCacheFile, $this->config->getCacheFile());
  293. }
  294. public function testResolveAllowRiskyWithPositiveConfigAndPositiveOption()
  295. {
  296. $this->config->setRiskyAllowed(true);
  297. $this->resolver
  298. ->setOption('allow-risky', 'yes')
  299. ->resolve();
  300. $this->assertTrue($this->config->getRiskyAllowed());
  301. }
  302. public function testResolveAllowRiskyWithPositiveConfigAndNegativeOption()
  303. {
  304. $this->config->setRiskyAllowed(true);
  305. $this->resolver
  306. ->setOption('allow-risky', 'no')
  307. ->resolve();
  308. $this->assertFalse($this->config->getRiskyAllowed());
  309. }
  310. public function testResolveAllowRiskyWithNegativeConfigAndPositiveOption()
  311. {
  312. $this->config->setRiskyAllowed(false);
  313. $this->resolver
  314. ->setOption('allow-risky', 'yes')
  315. ->resolve();
  316. $this->assertTrue($this->config->getRiskyAllowed());
  317. }
  318. public function testResolveAllowRiskyWithNegativeConfigAndNegativeOption()
  319. {
  320. $this->config->setRiskyAllowed(false);
  321. $this->resolver
  322. ->setOption('allow-risky', 'no')
  323. ->resolve();
  324. $this->assertFalse($this->config->getRiskyAllowed());
  325. }
  326. public function testResolveAllowRiskyWithPositiveConfigAndNoOption()
  327. {
  328. $this->config->setRiskyAllowed(true);
  329. $this->resolver
  330. ->resolve();
  331. $this->assertTrue($this->config->getRiskyAllowed());
  332. }
  333. public function testResolveAllowRiskyWithNegativeConfigAndNoOption()
  334. {
  335. $this->config->setRiskyAllowed(false);
  336. $this->resolver
  337. ->resolve();
  338. $this->assertFalse($this->config->getRiskyAllowed());
  339. }
  340. public function testResolveRulesWithConfig()
  341. {
  342. $this->config->setRules(array(
  343. 'braces' => true,
  344. 'strict' => false,
  345. ));
  346. $this->resolver->resolve();
  347. $this->assertSameRules(
  348. array(
  349. 'braces' => true,
  350. ),
  351. $this->resolver->getRules()
  352. );
  353. }
  354. public function testResolveRulesWithOption()
  355. {
  356. $this->resolver->setOption('rules', 'braces,-strict');
  357. $this->resolver->resolve();
  358. $this->assertSameRules(
  359. array(
  360. 'braces' => true,
  361. ),
  362. $this->resolver->getRules()
  363. );
  364. }
  365. public function testResolveRulesWithConfigAndOption()
  366. {
  367. $this->config->setRules(array(
  368. 'braces' => true,
  369. 'strict' => false,
  370. ));
  371. $this->resolver->setOption('rules', 'blank_line_before_return');
  372. $this->resolver->resolve();
  373. $this->assertSameRules(
  374. array(
  375. 'blank_line_before_return' => true,
  376. ),
  377. $this->resolver->getRules()
  378. );
  379. }
  380. private function assertSameRules(array $expected, array $actual, $message = '')
  381. {
  382. ksort($expected);
  383. ksort($actual);
  384. $this->assertSame($expected, $actual, $message);
  385. }
  386. }