RuleSetTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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\ConfigurationException\InvalidForEnvFixerConfigurationException;
  13. use PhpCsFixer\FixerFactory;
  14. use PhpCsFixer\RuleSet;
  15. use PhpCsFixer\Test\AccessibleObject;
  16. use PHPUnit\Framework\TestCase;
  17. /**
  18. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  19. *
  20. * @internal
  21. *
  22. * @covers \PhpCsFixer\RuleSet
  23. */
  24. final class RuleSetTest extends TestCase
  25. {
  26. public function testCreate()
  27. {
  28. $ruleSet = RuleSet::create();
  29. $this->assertInstanceOf('PhpCsFixer\RuleSet', $ruleSet);
  30. }
  31. /**
  32. * @param string $ruleName
  33. * @param string $setName
  34. * @param array|bool $ruleConfig
  35. *
  36. * @dataProvider provideAllRulesFromSetsCases
  37. */
  38. public function testIfAllRulesInSetsExists($setName, $ruleName, $ruleConfig)
  39. {
  40. $factory = new FixerFactory();
  41. $factory->registerBuiltInFixers();
  42. $fixers = array();
  43. foreach ($factory->getFixers() as $fixer) {
  44. $fixers[$fixer->getName()] = $fixer;
  45. }
  46. $this->assertArrayHasKey($ruleName, $fixers, sprintf('RuleSet "%s" contains unknown rule.', $setName));
  47. if (true === $ruleConfig) {
  48. return; // rule doesn't need configuration.
  49. }
  50. $fixer = $fixers[$ruleName];
  51. $this->assertInstanceOf('PhpCsFixer\Fixer\ConfigurableFixerInterface', $fixer, sprintf('RuleSet "%s" contains configuration for rule "%s" which cannot be configured.', $setName, $ruleName));
  52. try {
  53. $fixer->configure($ruleConfig); // test fixer accepts the configuration
  54. } catch (InvalidForEnvFixerConfigurationException $exception) {
  55. // ignore
  56. }
  57. }
  58. public function provideAllRulesFromSetsCases()
  59. {
  60. $cases = array();
  61. foreach (RuleSet::create()->getSetDefinitionNames() as $setName) {
  62. foreach (RuleSet::create(array($setName => true))->getRules() as $rule => $config) {
  63. $cases[] = array(
  64. $setName,
  65. $rule,
  66. $config,
  67. );
  68. }
  69. }
  70. return $cases;
  71. }
  72. public function testGetBuildInSetDefinitionNames()
  73. {
  74. $setNames = RuleSet::create()->getSetDefinitionNames();
  75. $this->assertInternalType('array', $setNames);
  76. $this->assertNotEmpty($setNames);
  77. }
  78. /**
  79. * @dataProvider provideSetDefinitionNameCases
  80. *
  81. * @param mixed $setName
  82. */
  83. public function testBuildInSetDefinitionNames($setName)
  84. {
  85. $this->assertInternalType('string', $setName);
  86. $this->assertSame('@', substr($setName, 0, 1));
  87. }
  88. public function testResolveRulesWithInvalidSet()
  89. {
  90. $this->setExpectedException(
  91. 'InvalidArgumentException',
  92. 'Set "@foo" does not exist.'
  93. );
  94. RuleSet::create(array(
  95. '@foo' => true,
  96. ));
  97. }
  98. public function testResolveRulesWithMissingRuleValue()
  99. {
  100. $this->setExpectedException(
  101. 'InvalidArgumentException',
  102. 'Missing value for "braces" rule/set.'
  103. );
  104. RuleSet::create(array(
  105. 'braces',
  106. ));
  107. }
  108. public function testResolveRulesWithSet()
  109. {
  110. $ruleSet = RuleSet::create(array(
  111. '@PSR1' => true,
  112. 'braces' => true,
  113. 'encoding' => false,
  114. 'line_ending' => true,
  115. 'strict_comparison' => true,
  116. ));
  117. $this->assertSameRules(
  118. array(
  119. 'braces' => true,
  120. 'full_opening_tag' => true,
  121. 'line_ending' => true,
  122. 'strict_comparison' => true,
  123. ),
  124. $ruleSet->getRules()
  125. );
  126. }
  127. public function testResolveRulesWithNestedSet()
  128. {
  129. $ruleSet = RuleSet::create(array(
  130. '@PSR2' => true,
  131. 'strict_comparison' => true,
  132. ));
  133. $this->assertSameRules(
  134. array(
  135. 'blank_line_after_namespace' => true,
  136. 'braces' => true,
  137. 'class_definition' => true,
  138. 'elseif' => true,
  139. 'encoding' => true,
  140. 'full_opening_tag' => true,
  141. 'function_declaration' => true,
  142. 'indentation_type' => true,
  143. 'line_ending' => true,
  144. 'lowercase_constants' => true,
  145. 'lowercase_keywords' => true,
  146. 'method_argument_space' => true,
  147. 'no_closing_tag' => true,
  148. 'no_spaces_after_function_name' => true,
  149. 'no_spaces_inside_parenthesis' => true,
  150. 'no_trailing_whitespace' => true,
  151. 'no_trailing_whitespace_in_comment' => true,
  152. 'single_blank_line_at_eof' => true,
  153. 'single_class_element_per_statement' => array('elements' => array('property')),
  154. 'single_import_per_statement' => true,
  155. 'single_line_after_imports' => true,
  156. 'strict_comparison' => true,
  157. 'switch_case_semicolon_to_colon' => true,
  158. 'switch_case_space' => true,
  159. 'visibility_required' => true,
  160. ),
  161. $ruleSet->getRules()
  162. );
  163. }
  164. public function testResolveRulesWithDisabledSet()
  165. {
  166. $ruleSet = RuleSet::create(array(
  167. '@PSR2' => true,
  168. '@PSR1' => false,
  169. 'encoding' => true,
  170. ));
  171. $this->assertSameRules(
  172. array(
  173. 'blank_line_after_namespace' => true,
  174. 'braces' => true,
  175. 'class_definition' => true,
  176. 'elseif' => true,
  177. 'encoding' => true,
  178. 'function_declaration' => true,
  179. 'indentation_type' => true,
  180. 'line_ending' => true,
  181. 'lowercase_constants' => true,
  182. 'lowercase_keywords' => true,
  183. 'method_argument_space' => true,
  184. 'no_closing_tag' => true,
  185. 'no_spaces_after_function_name' => true,
  186. 'no_spaces_inside_parenthesis' => true,
  187. 'no_trailing_whitespace' => true,
  188. 'no_trailing_whitespace_in_comment' => true,
  189. 'single_blank_line_at_eof' => true,
  190. 'single_class_element_per_statement' => array('elements' => array('property')),
  191. 'single_import_per_statement' => true,
  192. 'single_line_after_imports' => true,
  193. 'switch_case_semicolon_to_colon' => true,
  194. 'switch_case_space' => true,
  195. 'visibility_required' => true,
  196. ),
  197. $ruleSet->getRules()
  198. );
  199. }
  200. /**
  201. * @dataProvider provideSetDefinitionNameCases
  202. *
  203. * @param string $setDefinitionName
  204. */
  205. public function testSetDefinitionsAreSorted($setDefinitionName)
  206. {
  207. $ruleSet = RuleSet::create();
  208. $method = new \ReflectionMethod(
  209. 'PhpCsFixer\RuleSet',
  210. 'getSetDefinition'
  211. );
  212. $method->setAccessible(true);
  213. $setDefinition = $method->invoke(
  214. $ruleSet,
  215. $setDefinitionName
  216. );
  217. $sortedSetDefinition = $setDefinition;
  218. $this->sort($sortedSetDefinition);
  219. $this->assertSame($sortedSetDefinition, $setDefinition, sprintf(
  220. 'Failed to assert that the set definition for "%s" is sorted by key',
  221. $setDefinitionName
  222. ));
  223. }
  224. /**
  225. * @return array
  226. */
  227. public function provideSetDefinitionNameCases()
  228. {
  229. $setDefinitionNames = RuleSet::create()->getSetDefinitionNames();
  230. return array_map(function ($setDefinitionName) {
  231. return array($setDefinitionName);
  232. }, $setDefinitionNames);
  233. }
  234. /**
  235. * @param array $set
  236. * @param bool $safe
  237. *
  238. * @dataProvider provideSafeSetCases
  239. */
  240. public function testRiskyRulesInSet(array $set, $safe)
  241. {
  242. try {
  243. $fixers = FixerFactory::create()
  244. ->registerBuiltInFixers()
  245. ->useRuleSet(new RuleSet($set))
  246. ->getFixers()
  247. ;
  248. } catch (InvalidForEnvFixerConfigurationException $exception) {
  249. $this->markTestSkipped($exception->getMessage());
  250. }
  251. $fixerNames = array();
  252. foreach ($fixers as $fixer) {
  253. if ($safe === $fixer->isRisky()) {
  254. $fixerNames[] = $fixer->getName();
  255. }
  256. }
  257. $this->assertCount(
  258. 0,
  259. $fixerNames,
  260. sprintf(
  261. 'Set should only contain %s fixers, got: \'%s\'.',
  262. $safe ? 'safe' : 'risky',
  263. implode('\', \'', $fixerNames)
  264. )
  265. );
  266. }
  267. public function provideSafeSetCases()
  268. {
  269. $sets = array();
  270. $ruleSet = new RuleSet();
  271. foreach ($ruleSet->getSetDefinitionNames() as $name) {
  272. $sets[$name] = array(
  273. array($name => true),
  274. false === strpos($name, ':risky'),
  275. );
  276. }
  277. $sets['@Symfony:risky_and_@Symfony'] = array(
  278. array(
  279. '@Symfony:risky' => true,
  280. '@Symfony' => false,
  281. ),
  282. false,
  283. );
  284. return $sets;
  285. }
  286. public function testInvalidConfigNestedSets()
  287. {
  288. $this->setExpectedExceptionRegExp(
  289. '\UnexpectedValueException',
  290. '#^Nested rule set "@PSR1" configuration must be a boolean\.$#'
  291. );
  292. new RuleSet(
  293. array('@PSR1' => array('@PSR2' => 'no'))
  294. );
  295. }
  296. public function testGetSetDefinitionNames()
  297. {
  298. $ruleSet = $this->createRuleSetToTestWith(array());
  299. $this->assertSame(
  300. array_keys(self::getRuleSetDefinitionsToTestWith()),
  301. $ruleSet->getSetDefinitionNames()
  302. );
  303. }
  304. /**
  305. * @param array $expected
  306. * @param array $rules
  307. *
  308. * @dataProvider provideResolveRulesCases
  309. */
  310. public function testResolveRules(array $expected, array $rules)
  311. {
  312. $ruleSet = $this->createRuleSetToTestWith($rules);
  313. $this->assertSameRules($expected, $ruleSet->getRules());
  314. }
  315. public function provideResolveRulesCases()
  316. {
  317. return array(
  318. '@Foo + C\' -D' => array(
  319. array('A' => true, 'B' => true, 'C' => 56),
  320. array('@Foo' => true, 'C' => 56, 'D' => false),
  321. ),
  322. '@Foo + @Bar' => array(
  323. array('A' => true, 'B' => true, 'D' => 34, 'E' => true),
  324. array('@Foo' => true, '@Bar' => true),
  325. ),
  326. '@Foo - @Bar' => array(
  327. array('B' => true),
  328. array('@Foo' => true, '@Bar' => false),
  329. ),
  330. '@A - @E (set in set)' => array(
  331. array('AA' => true), // 'AB' => false, 'AC' => false
  332. array('@A' => true, '@E' => false),
  333. ),
  334. '@A + @E (set in set)' => array(
  335. array('AA' => true, 'AB' => '_AB', 'AC' => 'b', 'Z' => true),
  336. array('@A' => true, '@E' => true),
  337. ),
  338. '@E + @A (set in set) + rule override' => array(
  339. array('AC' => 'd', 'AB' => true, 'Z' => true, 'AA' => true),
  340. array('@E' => true, '@A' => true, 'AC' => 'd'),
  341. ),
  342. 'nest single set' => array(
  343. array('AC' => 'b', 'AB' => '_AB', 'Z' => 'E'),
  344. array('@F' => true),
  345. ),
  346. 'Set reconfigure rule in other set, reconfigure rule.' => array(
  347. array(
  348. 'AA' => true,
  349. 'AB' => true,
  350. 'AC' => 'abc',
  351. ),
  352. array(
  353. '@A' => true,
  354. '@D' => true,
  355. 'AC' => 'abc',
  356. ),
  357. ),
  358. 'Set reconfigure rule in other set.' => array(
  359. array(
  360. 'AA' => true,
  361. 'AB' => true,
  362. 'AC' => 'b',
  363. ),
  364. array(
  365. '@A' => true,
  366. '@D' => true,
  367. ),
  368. ),
  369. 'Set minus two sets minus rule' => array(
  370. array(
  371. 'AB' => true,
  372. ),
  373. array(
  374. '@A' => true,
  375. '@B' => false,
  376. '@C' => false,
  377. 'AC' => false,
  378. ),
  379. ),
  380. 'Set minus two sets' => array(
  381. array(
  382. 'AB' => true,
  383. 'AC' => 'a',
  384. ),
  385. array(
  386. '@A' => true,
  387. '@B' => false,
  388. '@C' => false,
  389. ),
  390. ),
  391. 'Set minus rule test.' => array(
  392. array(
  393. 'AA' => true,
  394. 'AC' => 'a',
  395. ),
  396. array(
  397. '@A' => true,
  398. 'AB' => false,
  399. ),
  400. ),
  401. 'Set minus set test.' => array(
  402. array(
  403. 'AB' => true,
  404. 'AC' => 'a',
  405. ),
  406. array(
  407. '@A' => true,
  408. '@B' => false,
  409. ),
  410. ),
  411. 'Set to rules test.' => array(
  412. array(
  413. 'AA' => true,
  414. 'AB' => true,
  415. 'AC' => 'a',
  416. ),
  417. array(
  418. '@A' => true,
  419. ),
  420. ),
  421. '@A - @C' => array(
  422. array(
  423. 'AB' => true,
  424. 'AC' => 'a',
  425. ),
  426. array(
  427. '@A' => true,
  428. '@C' => false,
  429. ),
  430. ),
  431. '@A - @D' => array(
  432. array(
  433. 'AA' => true,
  434. 'AB' => true,
  435. ),
  436. array(
  437. '@A' => true,
  438. '@D' => false,
  439. ),
  440. ),
  441. );
  442. }
  443. public function testGetMissingRuleConfiguration()
  444. {
  445. $ruleSet = new RuleSet();
  446. $this->setExpectedExceptionRegExp(
  447. 'InvalidArgumentException',
  448. '#^Rule "_not_exists" is not in the set\.$#'
  449. );
  450. $ruleSet->getRuleConfiguration('_not_exists');
  451. }
  452. private function assertSameRules(array $expected, array $actual, $message = '')
  453. {
  454. ksort($expected);
  455. ksort($actual);
  456. $this->assertSame($expected, $actual, $message);
  457. }
  458. /**
  459. * Sorts an array of rule set definitions recursively.
  460. *
  461. * Sometimes keys are all string, sometimes they are integers - we need to account for that.
  462. *
  463. * @param array $data
  464. */
  465. private function sort(array &$data)
  466. {
  467. $keys = array_keys($data);
  468. if ($this->allInteger($keys)) {
  469. sort($data);
  470. } else {
  471. ksort($data);
  472. }
  473. foreach ($data as $key => $value) {
  474. if (is_array($value)) {
  475. $this->sort($data[$key]);
  476. }
  477. }
  478. }
  479. /**
  480. * @param array $values
  481. *
  482. * @return bool
  483. */
  484. private function allInteger(array $values)
  485. {
  486. foreach ($values as $value) {
  487. if (!is_int($value)) {
  488. return false;
  489. }
  490. }
  491. return true;
  492. }
  493. private function createRuleSetToTestWith(array $rules)
  494. {
  495. $ruleSet = new RuleSet();
  496. $reflection = new AccessibleObject($ruleSet);
  497. $reflection->setDefinitions = self::getRuleSetDefinitionsToTestWith();
  498. $reflection->set = $rules;
  499. $reflection->resolveSet();
  500. return $ruleSet;
  501. }
  502. private static function getRuleSetDefinitionsToTestWith()
  503. {
  504. static $testSet = array(
  505. '@A' => array(
  506. 'AA' => true,
  507. 'AB' => true,
  508. 'AC' => 'a',
  509. ),
  510. '@B' => array(
  511. 'AA' => true,
  512. ),
  513. '@C' => array(
  514. 'AA' => false,
  515. ),
  516. '@D' => array(
  517. 'AC' => 'b',
  518. ),
  519. '@E' => array(
  520. '@D' => true,
  521. 'AB' => '_AB',
  522. 'Z' => true,
  523. ),
  524. '@F' => array(
  525. '@E' => true,
  526. 'Z' => 'E',
  527. ),
  528. '@Foo' => array('A' => true, 'B' => true, 'C' => true, 'D' => 12),
  529. '@Bar' => array('A' => true, 'C' => false, 'D' => 34, 'E' => true, 'F' => false),
  530. );
  531. return $testSet;
  532. }
  533. }