AbstractSetTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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\RuleSet\Sets;
  13. use PhpCsFixer\ConfigurationException\InvalidForEnvFixerConfigurationException;
  14. use PhpCsFixer\FixerFactory;
  15. use PhpCsFixer\RuleSet\RuleSet;
  16. use PhpCsFixer\RuleSet\RuleSetDescriptionInterface;
  17. use PhpCsFixer\RuleSet\RuleSets;
  18. use PhpCsFixer\Tests\TestCase;
  19. /**
  20. * @internal
  21. */
  22. abstract class AbstractSetTest extends TestCase
  23. {
  24. public function testSet(): void
  25. {
  26. $set = self::getSet();
  27. $setName = $set->getName();
  28. $setDescription = $set->getDescription();
  29. $isRiskySet = $set->isRisky();
  30. $setRules = $set->getRules();
  31. $factory = new FixerFactory();
  32. $factory->registerBuiltInFixers();
  33. static::assertSanityString($setName);
  34. static::assertSanityString($setDescription);
  35. static::assertStringEndsWith('.', $setDescription, sprintf('Ruleset description of "%s" must end with ".", got "%s".', $setName, $setDescription));
  36. static::assertRules($setRules, $factory, $setName);
  37. if (1 === preg_match('/(\d)(\d)Migration/', \get_class($set), $matches)) {
  38. static::assertStringEndsWith(
  39. sprintf(' %d.%d compatibility.', $matches[1], $matches[2]),
  40. $setDescription,
  41. sprintf('Set %s has incorrect description: "%s".', $setName, $setDescription)
  42. );
  43. }
  44. try {
  45. $factory->useRuleSet(new RuleSet($set->getRules()));
  46. } catch (InvalidForEnvFixerConfigurationException $e) {
  47. static::markTestSkipped(sprintf('Cannot test set "%s" on this environment. %s', $setName, $e->getMessage()));
  48. }
  49. foreach ($factory->getFixers() as $fixer) {
  50. $fixerName = $fixer->getName();
  51. static::assertSame($isRiskySet, $fixer->isRisky(), sprintf('Is risky mismatch between set "%s" and rule "%s".', $setName, $fixerName));
  52. if (isset($setRules[$fixerName])) {
  53. static::assertTrue(\is_bool($setRules[$fixerName]) || \is_array($setRules[$fixerName]));
  54. }
  55. }
  56. }
  57. private static function assertRules(array $setRules, FixerFactory $factory, string $setName): void
  58. {
  59. $sawRule = false;
  60. foreach ($setRules as $rule => $config) {
  61. static::assertIsString($rule, $setName);
  62. if (str_starts_with($rule, '@')) {
  63. static::assertFalse($sawRule, sprintf('Ruleset "%s" should define all sets it extends first and than list by rule configuration overrides.', $setName));
  64. RuleSets::getSetDefinition($setName);
  65. } else {
  66. $sawRule = true;
  67. static::assertTrue($factory->hasRule($rule), $rule);
  68. }
  69. }
  70. $setRulesSorted = $setRules;
  71. ksort($setRulesSorted);
  72. static::assertSame($setRulesSorted, $setRules);
  73. }
  74. private static function assertSanityString(string $string): void
  75. {
  76. static::assertSame(trim($string), $string);
  77. static::assertNotSame('', $string);
  78. }
  79. private static function getSet(): RuleSetDescriptionInterface
  80. {
  81. $setClassName = preg_replace('/^(PhpCsFixer)\\\\Tests(\\\\.+)Test$/', '$1$2', static::class);
  82. return new $setClassName();
  83. }
  84. }