Skip to content

Commit

Permalink
Optimize tests' execution times
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbalandan committed May 7, 2021
1 parent e7f6140 commit 0ad4fd5
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 65 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [v3.0.3](https://github.com/NexusPHP/cs-config/compare/v3.0.2...v3.0.3) - 2021-05-07

- Add test for checking missing built-in rules in ruleset
- Optimize tests' execution times

## [v3.0.2](https://github.com/NexusPHP/cs-config/compare/v3.0.1...v3.0.2) - 2021-05-07

Expand Down
157 changes: 92 additions & 65 deletions src/Test/AbstractRulesetTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,48 @@
use PhpCsFixer\RuleSet\RuleSet;
use PHPUnit\Framework\TestCase;

/**
* Used for testing the rulesets.
*
* @codeCoverageIgnore
*/
abstract class AbstractRulesetTestCase extends TestCase
{
/** @var string[] */
private static $configured = [];

/** @var string[] */
private static $builtInFixers = [];

/** @var string[] */
private static $cleanBuiltInFixers = [];

/** @var string[] */
private static $deprecatedBuiltInFixers = [];

public static function setUpBeforeClass(): void
{
self::reset();

self::$configured = self::configuredFixers();
self::$builtInFixers = self::builtInFixers(); // @phpstan-ignore-line
self::$cleanBuiltInFixers = self::builtInFixers(false); // @phpstan-ignore-line
self::$deprecatedBuiltInFixers = array_diff(self::$builtInFixers, self::$cleanBuiltInFixers); // @phpstan-ignore-line
}

public static function tearDownAfterClass(): void
{
self::reset();
}

final public static function reset(): void
{
self::$configured = [];
self::$builtInFixers = [];
self::$cleanBuiltInFixers = [];
self::$deprecatedBuiltInFixers = [];
}

/**
* @codeCoverageIgnore
*
Expand All @@ -45,11 +85,56 @@ protected static function createRuleset(): RulesetInterface
return new $className();
}

/**
* Rules defined by PhpCsFixer.
*
* @param bool $withDeprecated
* @param bool $withFixers
*
* @return array<string, FixerInterface>|string[]
*/
private static function builtInFixers(bool $withDeprecated = true, bool $withFixers = false): array
{
$fixerFactory = new FixerFactory();
$fixerFactory->registerBuiltInFixers();
$fixers = $fixerFactory->getFixers();

if (! $withDeprecated) {
$fixers = array_filter($fixers, static function (FixerInterface $fixer): bool {
return ! $fixer instanceof DeprecatedFixerInterface;
});
}

$builtInFixers = array_map(static function (FixerInterface $fixer): string {
return $fixer->getName();
}, $fixers);

if ($withFixers) {
return array_combine($builtInFixers, $fixers) ?: [];
}

return $builtInFixers;
}

/**
* Rules defined by this ruleset.
*
* @return string[]
*/
private static function configuredFixers(): array
{
$rules = array_map(static function ($ruleConfiguration): bool {
return true;
}, static::createRuleset()->getRules());

return array_keys((new RuleSet($rules))->getRules());
}

final public function testAllNondeprecatedBuiltInRulesAreConfigured(): void
{
$fixersNotConfigured = array_diff(
$this->builtInFixers(false),
$this->configuredFixers()
self::$cleanBuiltInFixers,
self::$configured
);

sort($fixersNotConfigured);
Expand All @@ -67,8 +152,8 @@ final public function testAllNondeprecatedBuiltInRulesAreConfigured(): void
final public function testAllConfiguredRulesAreBuiltIn(): void
{
$fixersNotBuiltIn = array_diff(
$this->configuredFixers(),
$this->builtInFixers(false)
self::$configured,
self::$cleanBuiltInFixers
);

sort($fixersNotBuiltIn);
Expand Down Expand Up @@ -113,12 +198,11 @@ final public function testHeaderCommentFixerIsDisabledByDefault(): void
final public function testDeprecatedFixersAreNoLongerUsed(): void
{
$rules = array_keys(static::createRuleset()->getRules());
$fixers = $this->deprecatedBuiltInFixers();

$rulesWithoutDeprecatedRules = array_filter(
$rules,
static function (string $fixer) use ($fixers): bool {
return ! \in_array($fixer, $fixers, true);
static function (string $fixer): bool {
return ! \in_array($fixer, self::$deprecatedBuiltInFixers, true);
}
);

Expand All @@ -137,7 +221,7 @@ final public function testRulesetDoesNotUseDeprecatedConfigurationOptions(): voi

$rules = $ruleset->getRules();
$names = array_keys($rules);
$fixers = $this->builtInFixers(false, true);
$fixers = self::builtInFixers(false, true);

$filtered = array_filter(array_map(static function (string $name, $rule) use ($fixers) {
if (! \is_array($rule)) {
Expand Down Expand Up @@ -180,61 +264,4 @@ static function (FixerOptionInterface $option): bool {
\count($filtered) > 1 ? 'their' : 'its'
));
}

/** @return string[] */
private function deprecatedBuiltInFixers(): array
{
/** @var string[] $builtInFixers */
$builtInFixers = $this->builtInFixers();

/** @var string[] $cleanBuiltInFixers */
$cleanBuiltInFixers = $this->builtInFixers(false);

return array_diff($builtInFixers, $cleanBuiltInFixers);
}

/**
* Rules defined by PhpCsFixer.
*
* @param bool $withDeprecated
* @param bool $withFixers
*
* @return array<string, FixerInterface>|string[]
*/
private function builtInFixers(bool $withDeprecated = true, bool $withFixers = false): array
{
$fixerFactory = new FixerFactory();
$fixerFactory->registerBuiltInFixers();
$fixers = $fixerFactory->getFixers();

if (! $withDeprecated) {
$fixers = array_filter($fixers, static function (FixerInterface $fixer): bool {
return ! $fixer instanceof DeprecatedFixerInterface;
});
}

$builtInFixers = array_map(static function (FixerInterface $fixer): string {
return $fixer->getName();
}, $fixers);

if ($withFixers) {
return array_combine($builtInFixers, $fixers) ?: [];
}

return $builtInFixers;
}

/**
* Rules defined by this ruleset.
*
* @return string[]
*/
private function configuredFixers(): array
{
$rules = array_map(static function ($ruleConfiguration): bool {
return true;
}, static::createRuleset()->getRules());

return array_keys((new RuleSet($rules))->getRules());
}
}

0 comments on commit 0ad4fd5

Please sign in to comment.