AbstractSetTestCase.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 AbstractSetTestCase 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. self::assertSanityString($setName);
  34. self::assertSanityString($setDescription);
  35. self::assertStringEndsWith('.', $setDescription, \sprintf('Ruleset description of "%s" must end with ".", got "%s".', $setName, $setDescription));
  36. self::assertRules($setRules, $factory, $setName);
  37. if (1 === preg_match('/(\d+)(\d)Migration/', \get_class($set), $matches)) {
  38. self::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. self::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. self::assertSame($isRiskySet, $fixer->isRisky(), \sprintf('Is risky mismatch between set "%s" and rule "%s".', $setName, $fixerName));
  52. if (isset($setRules[$fixerName])) {
  53. self::assertTrue(\is_bool($setRules[$fixerName]) || \is_array($setRules[$fixerName]));
  54. }
  55. }
  56. }
  57. protected static function assertSanityString(string $string): void
  58. {
  59. self::assertSame(trim($string), $string);
  60. self::assertNotSame('', $string);
  61. }
  62. protected static function getSet(): RuleSetDescriptionInterface
  63. {
  64. $setClassName = preg_replace('/^(PhpCsFixer)\\\Tests(\\\.+)Test$/', '$1$2', static::class);
  65. return new $setClassName();
  66. }
  67. /**
  68. * @param array<string, array<string, mixed>|bool> $setRules
  69. */
  70. protected static function assertRules(array $setRules, FixerFactory $factory, string $setName): void
  71. {
  72. $sawRule = false;
  73. foreach ($setRules as $rule => $config) {
  74. self::assertIsString($rule, $setName);
  75. if (str_starts_with($rule, '@')) {
  76. self::assertFalse($sawRule, \sprintf('Ruleset "%s" should define all sets it extends first and than list by rule configuration overrides.', $setName));
  77. RuleSets::getSetDefinition($setName);
  78. } else {
  79. $sawRule = true;
  80. self::assertTrue($factory->hasRule($rule), $rule);
  81. }
  82. }
  83. $setRulesSorted = $setRules;
  84. ksort($setRulesSorted);
  85. self::assertSame($setRulesSorted, $setRules);
  86. }
  87. }