FixerFactoryTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer\Tests;
  13. use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
  14. use PhpCsFixer\Fixer\ConfigurableFixerInterface;
  15. use PhpCsFixer\Fixer\FixerInterface;
  16. use PhpCsFixer\FixerFactory;
  17. use PhpCsFixer\RuleSet\RuleSet;
  18. use PhpCsFixer\RuleSet\RuleSetInterface;
  19. use PhpCsFixer\WhitespacesFixerConfig;
  20. /**
  21. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  22. *
  23. * @internal
  24. *
  25. * @covers \PhpCsFixer\FixerFactory
  26. */
  27. final class FixerFactoryTest extends TestCase
  28. {
  29. public function testInterfaceIsFluent(): void
  30. {
  31. $factory = new FixerFactory();
  32. $testInstance = $factory->registerBuiltInFixers();
  33. static::assertSame($factory, $testInstance);
  34. $testInstance = $factory->registerCustomFixers(
  35. [$this->createFixerDouble('Foo/f1'), $this->createFixerDouble('Foo/f2')]
  36. );
  37. static::assertSame($factory, $testInstance);
  38. $testInstance = $factory->registerFixer(
  39. $this->createFixerDouble('f3'),
  40. false
  41. );
  42. static::assertSame($factory, $testInstance);
  43. $ruleSetProphecy = $this->prophesize(RuleSetInterface::class);
  44. $ruleSetProphecy->getRules()->willReturn([]);
  45. $testInstance = $factory->useRuleSet(
  46. $ruleSetProphecy->reveal()
  47. );
  48. static::assertSame($factory, $testInstance);
  49. }
  50. /**
  51. * @covers \PhpCsFixer\FixerFactory::registerBuiltInFixers
  52. */
  53. public function testRegisterBuiltInFixers(): void
  54. {
  55. $factory = new FixerFactory();
  56. $factory->registerBuiltInFixers();
  57. $fixerClasses = array_filter(
  58. get_declared_classes(),
  59. static function (string $className): bool {
  60. $class = new \ReflectionClass($className);
  61. return !$class->isAbstract() && $class->implementsInterface(FixerInterface::class) && str_starts_with($class->getNamespaceName(), 'PhpCsFixer\\Fixer\\');
  62. }
  63. );
  64. sort($fixerClasses);
  65. $fixers = array_map(
  66. static function (FixerInterface $fixer): string {
  67. return \get_class($fixer);
  68. },
  69. $factory->getFixers()
  70. );
  71. sort($fixers);
  72. static::assertSame($fixerClasses, $fixers);
  73. }
  74. /**
  75. * @covers \PhpCsFixer\FixerFactory::getFixers
  76. */
  77. public function testThatFixersAreSorted(): void
  78. {
  79. $factory = new FixerFactory();
  80. $fxs = [
  81. $this->createFixerDouble('f1', 0),
  82. $this->createFixerDouble('f2', -10),
  83. $this->createFixerDouble('f3', 10),
  84. $this->createFixerDouble('f4', -10),
  85. ];
  86. foreach ($fxs as $fx) {
  87. $factory->registerFixer($fx, false);
  88. }
  89. // There are no rules that forces $fxs[1] to be prioritized before $fxs[3]. We should not test against that
  90. static::assertSame([$fxs[2], $fxs[0]], \array_slice($factory->getFixers(), 0, 2));
  91. }
  92. /**
  93. * @covers \PhpCsFixer\FixerFactory::getFixers
  94. * @covers \PhpCsFixer\FixerFactory::registerCustomFixers
  95. * @covers \PhpCsFixer\FixerFactory::registerFixer
  96. */
  97. public function testThatCanRegisterAndGetFixers(): void
  98. {
  99. $factory = new FixerFactory();
  100. $f1 = $this->createFixerDouble('f1');
  101. $f2 = $this->createFixerDouble('Foo/f2');
  102. $f3 = $this->createFixerDouble('Foo/f3');
  103. $factory->registerFixer($f1, false);
  104. $factory->registerCustomFixers([$f2, $f3]);
  105. static::assertTrue(\in_array($f1, $factory->getFixers(), true));
  106. static::assertTrue(\in_array($f2, $factory->getFixers(), true));
  107. static::assertTrue(\in_array($f3, $factory->getFixers(), true));
  108. }
  109. /**
  110. * @covers \PhpCsFixer\FixerFactory::registerFixer
  111. */
  112. public function testRegisterFixerWithOccupiedName(): void
  113. {
  114. $this->expectException(\UnexpectedValueException::class);
  115. $this->expectExceptionMessage('Fixer named "non_unique_name" is already registered.');
  116. $factory = new FixerFactory();
  117. $f1 = $this->createFixerDouble('non_unique_name');
  118. $f2 = $this->createFixerDouble('non_unique_name');
  119. $factory->registerFixer($f1, false);
  120. $factory->registerFixer($f2, false);
  121. }
  122. /**
  123. * @covers \PhpCsFixer\FixerFactory::useRuleSet
  124. */
  125. public function testUseRuleSet(): void
  126. {
  127. $factory = (new FixerFactory())
  128. ->registerBuiltInFixers()
  129. ->useRuleSet(new RuleSet([]))
  130. ;
  131. static::assertCount(0, $factory->getFixers());
  132. $factory = (new FixerFactory())
  133. ->registerBuiltInFixers()
  134. ->useRuleSet(new RuleSet(['strict_comparison' => true, 'blank_line_before_statement' => false]))
  135. ;
  136. $fixers = $factory->getFixers();
  137. static::assertCount(1, $fixers);
  138. static::assertSame('strict_comparison', $fixers[0]->getName());
  139. }
  140. /**
  141. * @covers \PhpCsFixer\FixerFactory::useRuleSet
  142. */
  143. public function testUseRuleSetWithNonExistingRule(): void
  144. {
  145. $this->expectException(\UnexpectedValueException::class);
  146. $this->expectExceptionMessage('Rule "non_existing_rule" does not exist.');
  147. $factory = (new FixerFactory())
  148. ->registerBuiltInFixers()
  149. ->useRuleSet(new RuleSet(['non_existing_rule' => true]))
  150. ;
  151. $factory->getFixers();
  152. }
  153. /**
  154. * @covers \PhpCsFixer\FixerFactory::useRuleSet
  155. */
  156. public function testUseRuleSetWithInvalidConfigForRule(): void
  157. {
  158. $this->expectException(InvalidFixerConfigurationException::class);
  159. $this->expectExceptionMessage('Configuration must be an array and may not be empty.');
  160. $testRuleSet = new class() implements RuleSetInterface {
  161. public function __construct(array $set = [])
  162. {
  163. if ([] !== $set) {
  164. throw new \RuntimeException('Set is not used in test.');
  165. }
  166. }
  167. public function getRuleConfiguration(string $rule): ?array
  168. {
  169. return $this->getRules()[$rule];
  170. }
  171. public function getRules(): array
  172. {
  173. return ['header_comment' => []];
  174. }
  175. public function hasRule(string $rule): bool
  176. {
  177. return isset($this->getRules()[$rule]);
  178. }
  179. };
  180. $factory = (new FixerFactory())
  181. ->registerBuiltInFixers()
  182. ->useRuleSet($testRuleSet)
  183. ;
  184. $factory->getFixers();
  185. }
  186. public function testHasRule(): void
  187. {
  188. $factory = new FixerFactory();
  189. $f1 = $this->createFixerDouble('f1');
  190. $f2 = $this->createFixerDouble('Foo/f2');
  191. $f3 = $this->createFixerDouble('Foo/f3');
  192. $factory->registerFixer($f1, false);
  193. $factory->registerCustomFixers([$f2, $f3]);
  194. static::assertTrue($factory->hasRule('f1'), 'Should have f1 fixer');
  195. static::assertTrue($factory->hasRule('Foo/f2'), 'Should have f2 fixer');
  196. static::assertTrue($factory->hasRule('Foo/f3'), 'Should have f3 fixer');
  197. static::assertFalse($factory->hasRule('dummy'), 'Should not have dummy fixer');
  198. }
  199. public function testHasRuleWithChangedRuleSet(): void
  200. {
  201. $factory = new FixerFactory();
  202. $f1 = $this->createFixerDouble('f1');
  203. $f2 = $this->createFixerDouble('f2');
  204. $factory->registerFixer($f1, false);
  205. $factory->registerFixer($f2, false);
  206. static::assertTrue($factory->hasRule('f1'), 'Should have f1 fixer');
  207. static::assertTrue($factory->hasRule('f2'), 'Should have f2 fixer');
  208. $factory->useRuleSet(new RuleSet(['f2' => true]));
  209. static::assertFalse($factory->hasRule('f1'), 'Should not have f1 fixer');
  210. static::assertTrue($factory->hasRule('f2'), 'Should have f2 fixer');
  211. }
  212. /**
  213. * @dataProvider provideConflictingFixersCases
  214. */
  215. public function testConflictingFixers(RuleSet $ruleSet): void
  216. {
  217. $this->expectException(\UnexpectedValueException::class);
  218. $this->expectExceptionMessageMatches('#^Rule contains conflicting fixers:\n#');
  219. (new FixerFactory())
  220. ->registerBuiltInFixers()->useRuleSet($ruleSet);
  221. }
  222. public function provideConflictingFixersCases(): array
  223. {
  224. return [
  225. [new RuleSet(['no_blank_lines_before_namespace' => true, 'single_blank_line_before_namespace' => true])],
  226. [new RuleSet(['single_blank_line_before_namespace' => true, 'no_blank_lines_before_namespace' => true])],
  227. ];
  228. }
  229. public function testNoDoubleConflictReporting(): void
  230. {
  231. $factory = new FixerFactory();
  232. $method = new \ReflectionMethod($factory, 'generateConflictMessage');
  233. $method->setAccessible(true);
  234. static::assertSame(
  235. 'Rule contains conflicting fixers:
  236. - "a" with "b"
  237. - "c" with "d", "e", "f"
  238. - "d" with "g", "h"
  239. - "e" with "a"',
  240. $method->invoke(
  241. $factory,
  242. [
  243. 'a' => ['b'],
  244. 'b' => ['a'],
  245. 'c' => ['d', 'e', 'f'],
  246. 'd' => ['c', 'g', 'h'],
  247. 'e' => ['a'],
  248. ]
  249. )
  250. );
  251. }
  252. public function testSetWhitespacesConfig(): void
  253. {
  254. $factory = new FixerFactory();
  255. $config = new WhitespacesFixerConfig();
  256. $fixer = $this->prophesize(\PhpCsFixer\Fixer\WhitespacesAwareFixerInterface::class);
  257. $fixer->getName()->willReturn('foo');
  258. $fixer->setWhitespacesConfig($config)->shouldBeCalled();
  259. $factory->registerFixer($fixer->reveal(), false);
  260. $factory->setWhitespacesConfig($config);
  261. }
  262. public function testRegisterFixerInvalidName(): void
  263. {
  264. $factory = new FixerFactory();
  265. $fixer = $this->createFixerDouble('0');
  266. $this->expectException(\UnexpectedValueException::class);
  267. $this->expectExceptionMessage('Fixer named "0" has invalid name.');
  268. $factory->registerFixer($fixer, false);
  269. }
  270. public function testConfigureNonConfigurableFixer(): void
  271. {
  272. $factory = new FixerFactory();
  273. $fixer = $this->createFixerDouble('non_configurable');
  274. $factory->registerFixer($fixer, false);
  275. $this->expectException(InvalidFixerConfigurationException::class);
  276. $this->expectExceptionMessage('[non_configurable] Is not configurable.');
  277. $factory->useRuleSet(new RuleSet([
  278. 'non_configurable' => ['bar' => 'baz'],
  279. ]));
  280. }
  281. /**
  282. * @param mixed $value
  283. *
  284. * @dataProvider provideConfigureFixerWithNonArrayCases
  285. */
  286. public function testConfigureFixerWithNonArray($value): void
  287. {
  288. $factory = new FixerFactory();
  289. $fixer = $this->prophesize(ConfigurableFixerInterface::class);
  290. $fixer->getName()->willReturn('foo');
  291. $factory->registerFixer($fixer->reveal(), false);
  292. $this->expectException(InvalidFixerConfigurationException::class);
  293. $this->expectExceptionMessage(
  294. '[foo] Rule must be enabled (true), disabled (false) or configured (non-empty, assoc array). Other values are not allowed.'
  295. );
  296. $factory->useRuleSet(new RuleSet([
  297. 'foo' => $value,
  298. ]));
  299. }
  300. public function provideConfigureFixerWithNonArrayCases(): array
  301. {
  302. return [
  303. ['bar'],
  304. [new \stdClass()],
  305. [5],
  306. [5.5],
  307. ];
  308. }
  309. public function testConfigurableFixerIsConfigured(): void
  310. {
  311. $fixer = $this->prophesize(ConfigurableFixerInterface::class);
  312. $fixer->getName()->willReturn('foo');
  313. $fixer->configure(['bar' => 'baz'])->shouldBeCalled();
  314. $factory = new FixerFactory();
  315. $factory->registerFixer($fixer->reveal(), false);
  316. $factory->useRuleSet(new RuleSet([
  317. 'foo' => ['bar' => 'baz'],
  318. ]));
  319. }
  320. private function createFixerDouble(string $name, int $priority = 0): FixerInterface
  321. {
  322. $fixer = $this->prophesize(\PhpCsFixer\Fixer\FixerInterface::class);
  323. $fixer->getName()->willReturn($name);
  324. $fixer->getPriority()->willReturn($priority);
  325. return $fixer->reveal();
  326. }
  327. }