FixerFactoryTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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\Fixer\FixerInterface;
  13. use PhpCsFixer\FixerFactory;
  14. use PhpCsFixer\RuleSet;
  15. use PHPUnit\Framework\TestCase;
  16. /**
  17. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  18. *
  19. * @internal
  20. *
  21. * @covers \PhpCsFixer\FixerFactory
  22. */
  23. final class FixerFactoryTest extends TestCase
  24. {
  25. public function testInterfaceIsFluent()
  26. {
  27. $factory = new FixerFactory();
  28. $testInstance = $factory->registerBuiltInFixers();
  29. $this->assertSame($factory, $testInstance);
  30. $testInstance = $factory->registerCustomFixers(
  31. [$this->createFixerDouble('Foo/f1'), $this->createFixerDouble('Foo/f2')]
  32. );
  33. $this->assertSame($factory, $testInstance);
  34. $testInstance = $factory->registerFixer(
  35. $this->createFixerDouble('f3'),
  36. false
  37. );
  38. $this->assertSame($factory, $testInstance);
  39. $ruleSetProphecy = $this->prophesize(\PhpCsFixer\RuleSetInterface::class);
  40. $ruleSetProphecy->getRules()->willReturn([]);
  41. $testInstance = $factory->useRuleSet(
  42. $ruleSetProphecy->reveal()
  43. );
  44. $this->assertSame($factory, $testInstance);
  45. }
  46. /**
  47. * @covers \PhpCsFixer\FixerFactory::create
  48. */
  49. public function testCreate()
  50. {
  51. $factory = FixerFactory::create();
  52. $this->assertInstanceOf(\PhpCsFixer\FixerFactory::class, $factory);
  53. }
  54. /**
  55. * @covers \PhpCsFixer\FixerFactory::registerBuiltInFixers
  56. */
  57. public function testRegisterBuiltInFixers()
  58. {
  59. $factory = new FixerFactory();
  60. $factory->registerBuiltInFixers();
  61. $this->assertGreaterThan(0, count($factory->getFixers()));
  62. }
  63. /**
  64. * @covers \PhpCsFixer\FixerFactory::getFixers
  65. * @covers \PhpCsFixer\FixerFactory::sortFixers
  66. */
  67. public function testThatFixersAreSorted()
  68. {
  69. $factory = new FixerFactory();
  70. $fxs = [
  71. $this->createFixerDouble('f1', 0),
  72. $this->createFixerDouble('f2', -10),
  73. $this->createFixerDouble('f3', 10),
  74. $this->createFixerDouble('f4', -10),
  75. ];
  76. foreach ($fxs as $fx) {
  77. $factory->registerFixer($fx, false);
  78. }
  79. // There are no rules that forces $fxs[1] to be prioritized before $fxs[3]. We should not test against that
  80. $this->assertSame([$fxs[2], $fxs[0]], array_slice($factory->getFixers(), 0, 2));
  81. }
  82. /**
  83. * @covers \PhpCsFixer\FixerFactory::getFixers
  84. * @covers \PhpCsFixer\FixerFactory::registerCustomFixers
  85. * @covers \PhpCsFixer\FixerFactory::registerFixer
  86. */
  87. public function testThatCanRegisterAndGetFixers()
  88. {
  89. $factory = new FixerFactory();
  90. $f1 = $this->createFixerDouble('f1');
  91. $f2 = $this->createFixerDouble('Foo/f2');
  92. $f3 = $this->createFixerDouble('Foo/f3');
  93. $factory->registerFixer($f1, false);
  94. $factory->registerCustomFixers([$f2, $f3]);
  95. $this->assertTrue(in_array($f1, $factory->getFixers(), true));
  96. $this->assertTrue(in_array($f2, $factory->getFixers(), true));
  97. $this->assertTrue(in_array($f3, $factory->getFixers(), true));
  98. }
  99. /**
  100. * @covers \PhpCsFixer\FixerFactory::registerFixer
  101. */
  102. public function testRegisterFixerWithOccupiedName()
  103. {
  104. $this->setExpectedException(
  105. \UnexpectedValueException::class,
  106. 'Fixer named "non_unique_name" is already registered.'
  107. );
  108. $factory = new FixerFactory();
  109. $f1 = $this->createFixerDouble('non_unique_name');
  110. $f2 = $this->createFixerDouble('non_unique_name');
  111. $factory->registerFixer($f1, false);
  112. $factory->registerFixer($f2, false);
  113. }
  114. /**
  115. * @covers \PhpCsFixer\FixerFactory::useRuleSet
  116. */
  117. public function testUseRuleSet()
  118. {
  119. $factory = FixerFactory::create()
  120. ->registerBuiltInFixers()
  121. ->useRuleSet(new RuleSet([]))
  122. ;
  123. $this->assertCount(0, $factory->getFixers());
  124. $factory = FixerFactory::create()
  125. ->registerBuiltInFixers()
  126. ->useRuleSet(new RuleSet(['strict_comparison' => true, 'blank_line_before_return' => false]))
  127. ;
  128. $fixers = $factory->getFixers();
  129. $this->assertCount(1, $fixers);
  130. $this->assertSame('strict_comparison', $fixers[0]->getName());
  131. }
  132. /**
  133. * @covers \PhpCsFixer\FixerFactory::useRuleSet
  134. */
  135. public function testUseRuleSetWithNonExistingRule()
  136. {
  137. $this->setExpectedException(
  138. \UnexpectedValueException::class,
  139. 'Rule "non_existing_rule" does not exist.'
  140. );
  141. $factory = FixerFactory::create()
  142. ->registerBuiltInFixers()
  143. ->useRuleSet(new RuleSet(['non_existing_rule' => true]))
  144. ;
  145. $fixers = $factory->getFixers();
  146. $this->assertCount(1, $fixers);
  147. $this->assertSame('strict_comparison', $fixers[0]->getName());
  148. }
  149. public function testHasRule()
  150. {
  151. $factory = new FixerFactory();
  152. $f1 = $this->createFixerDouble('f1');
  153. $f2 = $this->createFixerDouble('Foo/f2');
  154. $f3 = $this->createFixerDouble('Foo/f3');
  155. $factory->registerFixer($f1, false);
  156. $factory->registerCustomFixers([$f2, $f3]);
  157. $this->assertTrue($factory->hasRule('f1'), 'Should have f1 fixer');
  158. $this->assertTrue($factory->hasRule('Foo/f2'), 'Should have f2 fixer');
  159. $this->assertTrue($factory->hasRule('Foo/f3'), 'Should have f3 fixer');
  160. $this->assertFalse($factory->hasRule('dummy'), 'Should not have dummy fixer');
  161. }
  162. public function testHasRuleWithChangedRuleSet()
  163. {
  164. $factory = new FixerFactory();
  165. $f1 = $this->createFixerDouble('f1');
  166. $f2 = $this->createFixerDouble('f2');
  167. $factory->registerFixer($f1, false);
  168. $factory->registerFixer($f2, false);
  169. $this->assertTrue($factory->hasRule('f1'), 'Should have f1 fixer');
  170. $this->assertTrue($factory->hasRule('f2'), 'Should have f2 fixer');
  171. $factory->useRuleSet(new RuleSet(['f2' => true]));
  172. $this->assertFalse($factory->hasRule('f1'), 'Should not have f1 fixer');
  173. $this->assertTrue($factory->hasRule('f2'), 'Should have f2 fixer');
  174. }
  175. /**
  176. * @dataProvider provideConflictingFixersRules
  177. */
  178. public function testConflictingFixers(RuleSet $ruleSet)
  179. {
  180. $this->setExpectedExceptionRegExp(
  181. \UnexpectedValueException::class,
  182. '#^Rule contains conflicting fixers:\n#'
  183. );
  184. FixerFactory::create()->registerBuiltInFixers()->useRuleSet($ruleSet);
  185. }
  186. public function provideConflictingFixersRules()
  187. {
  188. return [
  189. [new RuleSet(['no_blank_lines_before_namespace' => true, 'single_blank_line_before_namespace' => true])],
  190. [new RuleSet(['single_blank_line_before_namespace' => true, 'no_blank_lines_before_namespace' => true])],
  191. ];
  192. }
  193. public function testNoDoubleConflictReporting()
  194. {
  195. $factory = new FixerFactory();
  196. $method = new \ReflectionMethod($factory, 'generateConflictMessage');
  197. $method->setAccessible(true);
  198. $this->assertSame(
  199. 'Rule contains conflicting fixers:
  200. - "a" with "b"
  201. - "c" with "d", "e", "f"
  202. - "d" with "g", "h"
  203. - "e" with "a"',
  204. $method->invoke(
  205. $factory,
  206. [
  207. 'a' => ['b'],
  208. 'b' => ['a'],
  209. 'c' => ['d', 'e', 'f'],
  210. 'd' => ['c', 'g', 'h'],
  211. 'e' => ['a'],
  212. ]
  213. )
  214. );
  215. }
  216. private function createFixerDouble($name, $priority = 0)
  217. {
  218. /** @var FixerInterface $fixer */
  219. $fixer = $this->prophesize(\PhpCsFixer\Fixer\FixerInterface::class);
  220. $fixer->getName()->willReturn($name);
  221. $fixer->getPriority()->willReturn($priority);
  222. return $fixer->reveal();
  223. }
  224. }