RuleSetTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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;
  11. use Symfony\CS\FixerFactory;
  12. use Symfony\CS\RuleSet;
  13. /**
  14. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  15. *
  16. * @internal
  17. */
  18. final class RuleSetTest extends \PHPUnit_Framework_TestCase
  19. {
  20. public function testCreate()
  21. {
  22. $ruleSet = RuleSet::create();
  23. $this->assertInstanceOf('Symfony\CS\RuleSet', $ruleSet);
  24. }
  25. /**
  26. * @dataProvider provideAllRulesFromSets
  27. */
  28. public function testIfAllRulesInSetsExists($rule)
  29. {
  30. $factory = new FixerFactory();
  31. $factory->registerBuiltInFixers();
  32. $fixers = array();
  33. foreach ($factory->getFixers() as $fixer) {
  34. $fixers[$fixer->getName()] = $fixer;
  35. }
  36. $this->assertArrayHasKey($rule, $fixers);
  37. }
  38. public function provideAllRulesFromSets()
  39. {
  40. $cases = array();
  41. foreach (RuleSet::create()->getSetDefinitionNames() as $setName) {
  42. $cases = array_merge($cases, RuleSet::create(array($setName => true))->getRules());
  43. }
  44. return array_map(
  45. function ($item) {
  46. return array($item);
  47. },
  48. array_keys($cases)
  49. );
  50. }
  51. /**
  52. * @expectedException \UnexpectedValueException
  53. * @expectedExceptionMessage Set "@foo" does not exist.
  54. */
  55. public function testResolveRulesWithInvalidSet()
  56. {
  57. RuleSet::create(array(
  58. '@foo' => true,
  59. ));
  60. }
  61. public function testResolveRulesWithSet()
  62. {
  63. $ruleSet = RuleSet::create(array(
  64. 'strict' => true,
  65. '@PSR1' => true,
  66. 'braces' => true,
  67. 'encoding' => false,
  68. 'unix_line_endings' => true,
  69. ));
  70. $this->assertSameRules(
  71. array(
  72. 'strict' => true,
  73. 'full_opening_tag' => true,
  74. 'braces' => true,
  75. 'unix_line_endings' => true,
  76. ),
  77. $ruleSet->getRules()
  78. );
  79. }
  80. public function testResolveRulesWithNestedSet()
  81. {
  82. $ruleSet = RuleSet::create(array(
  83. '@PSR2' => true,
  84. 'strict' => true,
  85. ));
  86. $this->assertSameRules(
  87. array(
  88. 'encoding' => true,
  89. 'full_opening_tag' => true,
  90. 'unix_line_endings' => true,
  91. 'no_tab_indentation' => true,
  92. 'no_trailing_whitespace' => true,
  93. 'no_closing_tag' => true,
  94. 'elseif' => true,
  95. 'visibility_required' => true,
  96. 'lowercase_keywords' => true,
  97. 'single_line_after_imports' => true,
  98. 'switch_case_space' => true,
  99. 'switch_case_semicolon_to_colon' => true,
  100. 'no_spaces_inside_parenthesis' => true,
  101. 'single_import_per_statement' => true,
  102. 'no_spaces_after_function_name' => true,
  103. 'method_argument_space' => true,
  104. 'function_declaration' => true,
  105. 'lowercase_constants' => true,
  106. 'blank_line_after_namespace' => true,
  107. 'braces' => true,
  108. 'class_definition' => true,
  109. 'single_blank_line_at_eof' => true,
  110. 'strict' => true,
  111. ),
  112. $ruleSet->getRules()
  113. );
  114. }
  115. public function testResolveRulesWithDisabledSet()
  116. {
  117. $ruleSet = RuleSet::create(array(
  118. '@PSR2' => true,
  119. '@PSR1' => false,
  120. 'encoding' => true,
  121. ));
  122. $this->assertSameRules(
  123. array(
  124. 'encoding' => true,
  125. 'unix_line_endings' => true,
  126. 'no_tab_indentation' => true,
  127. 'no_trailing_whitespace' => true,
  128. 'no_closing_tag' => true,
  129. 'elseif' => true,
  130. 'visibility_required' => true,
  131. 'lowercase_keywords' => true,
  132. 'single_line_after_imports' => true,
  133. 'switch_case_space' => true,
  134. 'switch_case_semicolon_to_colon' => true,
  135. 'no_spaces_inside_parenthesis' => true,
  136. 'single_import_per_statement' => true,
  137. 'no_spaces_after_function_name' => true,
  138. 'method_argument_space' => true,
  139. 'function_declaration' => true,
  140. 'lowercase_constants' => true,
  141. 'blank_line_after_namespace' => true,
  142. 'braces' => true,
  143. 'class_definition' => true,
  144. 'single_blank_line_at_eof' => true,
  145. ),
  146. $ruleSet->getRules()
  147. );
  148. }
  149. private function assertSameRules(array $expected, array $actual, $message = '')
  150. {
  151. ksort($expected);
  152. ksort($actual);
  153. $this->assertSame($expected, $actual, $message);
  154. }
  155. }