RuleSetTest.php 17 KB

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