Skip to content

Commit

Permalink
Merge pull request #863 from ergebnis/feature/registered
Browse files Browse the repository at this point in the history
Enhancement: Assert that fixers are registered, not necessarily built-in
  • Loading branch information
localheinz authored Sep 15, 2023
2 parents 52ea8ae + 4cd13ab commit 646abb7
Show file tree
Hide file tree
Showing 14 changed files with 65 additions and 25 deletions.
42 changes: 23 additions & 19 deletions test/Unit/RuleSet/AbstractRuleSetTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Ergebnis\PhpCsFixer\Config\Test\Unit\RuleSet;

use Ergebnis\PhpCsFixer\Config\Factory;
use Ergebnis\PhpCsFixer\Config\RuleSet;
use PhpCsFixer\Fixer;
use PhpCsFixer\FixerConfiguration;
Expand Down Expand Up @@ -133,29 +134,29 @@ final public function testDefaults(): void
self::assertSame($this->targetPhpVersion, $ruleSet->targetPhpVersion());
}

final public function testRuleSetDoesNotConfigureRulesThatAreNotBuiltIn(): void
final public function testRuleSetDoesNotConfigureRulesThatAreNotRegistered(): void
{
$rules = self::createRuleSet()->rules();

$fixersThatAreBuiltIn = self::fixersThatAreBuiltIn();
$fixersThatAreRegistered = self::fixersThatAreRegistered();

$rulesWithoutRulesThatAreNotBuiltIn = \array_filter(
$rulesWithoutRulesThatAreNotRegistered = \array_filter(
$rules,
static function (string $nameOfRule) use ($fixersThatAreBuiltIn): bool {
static function (string $nameOfRule) use ($fixersThatAreRegistered): bool {
if (\str_starts_with($nameOfRule, '@')) {
return true;
}

return \array_key_exists(
$nameOfRule,
$fixersThatAreBuiltIn,
$fixersThatAreRegistered,
);
},
\ARRAY_FILTER_USE_KEY,
);

self::assertEquals($rulesWithoutRulesThatAreNotBuiltIn, $rules, \sprintf(
'Failed asserting that rule set "%s" does not configure rules that are not built-in.',
self::assertEquals($rulesWithoutRulesThatAreNotRegistered, $rules, \sprintf(
'Failed asserting that rule set "%s" does not configure rules that are not registered.',
static::className(),
));
}
Expand All @@ -164,16 +165,16 @@ final public function testRuleSetDoesNotConfigureRulesThatAreDeprecated(): void
{
$rules = self::createRuleSet()->rules();

$fixersThatAreBuiltIn = self::fixersThatAreBuiltIn();
$fixersThatAreRegistered = self::fixersThatAreRegistered();

$rulesWithoutRulesThatAreDeprecated = \array_filter(
$rules,
static function (string $nameOfRule) use ($fixersThatAreBuiltIn): bool {
if (!\array_key_exists($nameOfRule, $fixersThatAreBuiltIn)) {
static function (string $nameOfRule) use ($fixersThatAreRegistered): bool {
if (!\array_key_exists($nameOfRule, $fixersThatAreRegistered)) {
return true;
}

$fixer = $fixersThatAreBuiltIn[$nameOfRule];
$fixer = $fixersThatAreRegistered[$nameOfRule];

return !$fixer instanceof Fixer\DeprecatedFixerInterface;
},
Expand All @@ -192,16 +193,16 @@ final public function testRuleSetDoesNotConfigureRulesUsingDeprecatedConfigurati

$namesOfRules = \array_keys($rules);

$fixersThatAreBuiltIn = self::fixersThatAreBuiltIn();
$fixersThatAreRegistered = self::fixersThatAreRegistered();

$rulesWithoutDeprecatedConfigurationOptions = \array_combine(
$namesOfRules,
\array_map(static function (string $nameOfRule, $ruleConfiguration) use ($fixersThatAreBuiltIn) {
\array_map(static function (string $nameOfRule, $ruleConfiguration) use ($fixersThatAreRegistered) {
if (!\is_array($ruleConfiguration)) {
return $ruleConfiguration;
}

$fixer = $fixersThatAreBuiltIn[$nameOfRule];
$fixer = $fixersThatAreRegistered[$nameOfRule];

if ($fixer instanceof Fixer\DeprecatedFixerInterface) {
return $ruleConfiguration;
Expand Down Expand Up @@ -367,24 +368,27 @@ final protected static function createRuleSet(?string $header = null): RuleSet
/**
* @return array<string, Fixer\FixerInterface>
*/
final protected static function fixersThatAreBuiltIn(): array
final protected static function fixersThatAreRegistered(): array
{
$fixerFactory = new FixerFactory();

$fixerFactory->registerBuiltInFixers();

$fixers = $fixerFactory->getFixers();
$fixers = \array_merge(
$fixerFactory->getFixers(),
Factory::fromRuleSet(self::createRuleSet())->getCustomFixers(),
);

$fixersThatAreBuiltIn = \array_combine(
$fixersThatAreRegistered = \array_combine(
\array_map(static function (Fixer\FixerInterface $fixer): string {
return $fixer->getName();
}, $fixers),
$fixers,
);

\ksort($fixersThatAreBuiltIn);
\ksort($fixersThatAreRegistered);

return $fixersThatAreBuiltIn;
return $fixersThatAreRegistered;
}

final protected static function sort(array $data): array
Expand Down
12 changes: 6 additions & 6 deletions test/Unit/RuleSet/ExplicitRuleSetTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ final public function testRuleSetConfiguresAllRulesThatAreNotDeprecated(): void
{
$rules = self::createRuleSet()->rules();

$fixersThatAreBuiltInAndNotDeprecated = \array_filter(self::fixersThatAreBuiltIn(), static function (Fixer\FixerInterface $fixer): bool {
$fixersThatAreRegisteredAndNotDeprecated = \array_filter(self::fixersThatAreRegistered(), static function (Fixer\FixerInterface $fixer): bool {
return !$fixer instanceof Fixer\DeprecatedFixerInterface;
});

$rulesThatAreNotDeprecated = \array_combine(
\array_keys($fixersThatAreBuiltInAndNotDeprecated),
\array_keys($fixersThatAreRegisteredAndNotDeprecated),
\array_fill(
0,
\count($fixersThatAreBuiltInAndNotDeprecated),
\count($fixersThatAreRegisteredAndNotDeprecated),
false,
),
);
Expand All @@ -78,16 +78,16 @@ final public function testRuleSetConfiguresAllRulesThatAreConfigurableAndNotDepr

$namesOfRules = \array_keys($rules);

$fixersThatAreBuiltIn = self::fixersThatAreBuiltIn();
$fixersThatAreRegistered = self::fixersThatAreRegistered();

$rulesWithAllNonDeprecatedConfigurationOptions = \array_combine(
$namesOfRules,
\array_map(static function (string $nameOfRule, $ruleConfiguration) use ($fixersThatAreBuiltIn) {
\array_map(static function (string $nameOfRule, $ruleConfiguration) use ($fixersThatAreRegistered) {
if (false === $ruleConfiguration) {
return false;
}

$fixer = $fixersThatAreBuiltIn[$nameOfRule];
$fixer = $fixersThatAreRegistered[$nameOfRule];

if ($fixer instanceof Fixer\DeprecatedFixerInterface) {
return $ruleConfiguration;
Expand Down
3 changes: 3 additions & 0 deletions test/Unit/RuleSet/Php53Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@

namespace Ergebnis\PhpCsFixer\Config\Test\Unit\RuleSet;

use Ergebnis\PhpCsFixer\Config\Factory;
use Ergebnis\PhpCsFixer\Config\RuleSet;
use PHPUnit\Framework;

#[Framework\Attributes\CoversClass(RuleSet\AbstractRuleSet::class)]
#[Framework\Attributes\CoversClass(RuleSet\Php53::class)]
#[Framework\Attributes\RequiresPhp('>=5.3')]
#[Framework\Attributes\UsesClass(Factory::class)]
final class Php53Test extends ExplicitRuleSetTestCase
{
protected string $name = 'ergebnis (PHP 5.3)';
Expand Down
3 changes: 3 additions & 0 deletions test/Unit/RuleSet/Php54Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@

namespace Ergebnis\PhpCsFixer\Config\Test\Unit\RuleSet;

use Ergebnis\PhpCsFixer\Config\Factory;
use Ergebnis\PhpCsFixer\Config\RuleSet;
use PHPUnit\Framework;

#[Framework\Attributes\CoversClass(RuleSet\AbstractRuleSet::class)]
#[Framework\Attributes\CoversClass(RuleSet\Php54::class)]
#[Framework\Attributes\RequiresPhp('>=5.4')]
#[Framework\Attributes\UsesClass(Factory::class)]
final class Php54Test extends ExplicitRuleSetTestCase
{
protected string $name = 'ergebnis (PHP 5.4)';
Expand Down
3 changes: 3 additions & 0 deletions test/Unit/RuleSet/Php55Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@

namespace Ergebnis\PhpCsFixer\Config\Test\Unit\RuleSet;

use Ergebnis\PhpCsFixer\Config\Factory;
use Ergebnis\PhpCsFixer\Config\RuleSet;
use PHPUnit\Framework;

#[Framework\Attributes\CoversClass(RuleSet\AbstractRuleSet::class)]
#[Framework\Attributes\CoversClass(RuleSet\Php55::class)]
#[Framework\Attributes\RequiresPhp('>=5.5')]
#[Framework\Attributes\UsesClass(Factory::class)]
final class Php55Test extends ExplicitRuleSetTestCase
{
protected string $name = 'ergebnis (PHP 5.5)';
Expand Down
3 changes: 3 additions & 0 deletions test/Unit/RuleSet/Php56Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@

namespace Ergebnis\PhpCsFixer\Config\Test\Unit\RuleSet;

use Ergebnis\PhpCsFixer\Config\Factory;
use Ergebnis\PhpCsFixer\Config\RuleSet;
use PHPUnit\Framework;

#[Framework\Attributes\CoversClass(RuleSet\AbstractRuleSet::class)]
#[Framework\Attributes\CoversClass(RuleSet\Php56::class)]
#[Framework\Attributes\RequiresPhp('>=5.6')]
#[Framework\Attributes\UsesClass(Factory::class)]
final class Php56Test extends ExplicitRuleSetTestCase
{
protected string $name = 'ergebnis (PHP 5.6)';
Expand Down
3 changes: 3 additions & 0 deletions test/Unit/RuleSet/Php70Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@

namespace Ergebnis\PhpCsFixer\Config\Test\Unit\RuleSet;

use Ergebnis\PhpCsFixer\Config\Factory;
use Ergebnis\PhpCsFixer\Config\RuleSet;
use PHPUnit\Framework;

#[Framework\Attributes\CoversClass(RuleSet\AbstractRuleSet::class)]
#[Framework\Attributes\CoversClass(RuleSet\Php70::class)]
#[Framework\Attributes\RequiresPhp('>=7.0')]
#[Framework\Attributes\UsesClass(Factory::class)]
final class Php70Test extends ExplicitRuleSetTestCase
{
protected string $name = 'ergebnis (PHP 7.0)';
Expand Down
3 changes: 3 additions & 0 deletions test/Unit/RuleSet/Php71Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@

namespace Ergebnis\PhpCsFixer\Config\Test\Unit\RuleSet;

use Ergebnis\PhpCsFixer\Config\Factory;
use Ergebnis\PhpCsFixer\Config\RuleSet;
use PHPUnit\Framework;

#[Framework\Attributes\CoversClass(RuleSet\AbstractRuleSet::class)]
#[Framework\Attributes\CoversClass(RuleSet\Php71::class)]
#[Framework\Attributes\RequiresPhp('>=7.1')]
#[Framework\Attributes\UsesClass(Factory::class)]
final class Php71Test extends ExplicitRuleSetTestCase
{
protected string $name = 'ergebnis (PHP 7.1)';
Expand Down
3 changes: 3 additions & 0 deletions test/Unit/RuleSet/Php72Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@

namespace Ergebnis\PhpCsFixer\Config\Test\Unit\RuleSet;

use Ergebnis\PhpCsFixer\Config\Factory;
use Ergebnis\PhpCsFixer\Config\RuleSet;
use PHPUnit\Framework;

#[Framework\Attributes\CoversClass(RuleSet\AbstractRuleSet::class)]
#[Framework\Attributes\CoversClass(RuleSet\Php72::class)]
#[Framework\Attributes\RequiresPhp('>=7.2')]
#[Framework\Attributes\UsesClass(Factory::class)]
final class Php72Test extends ExplicitRuleSetTestCase
{
protected string $name = 'ergebnis (PHP 7.2)';
Expand Down
3 changes: 3 additions & 0 deletions test/Unit/RuleSet/Php73Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@

namespace Ergebnis\PhpCsFixer\Config\Test\Unit\RuleSet;

use Ergebnis\PhpCsFixer\Config\Factory;
use Ergebnis\PhpCsFixer\Config\RuleSet;
use PHPUnit\Framework;

#[Framework\Attributes\CoversClass(RuleSet\AbstractRuleSet::class)]
#[Framework\Attributes\CoversClass(RuleSet\Php73::class)]
#[Framework\Attributes\RequiresPhp('>=7.3')]
#[Framework\Attributes\UsesClass(Factory::class)]
final class Php73Test extends ExplicitRuleSetTestCase
{
protected string $name = 'ergebnis (PHP 7.3)';
Expand Down
3 changes: 3 additions & 0 deletions test/Unit/RuleSet/Php74Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@

namespace Ergebnis\PhpCsFixer\Config\Test\Unit\RuleSet;

use Ergebnis\PhpCsFixer\Config\Factory;
use Ergebnis\PhpCsFixer\Config\RuleSet;
use PHPUnit\Framework;

#[Framework\Attributes\CoversClass(RuleSet\AbstractRuleSet::class)]
#[Framework\Attributes\CoversClass(RuleSet\Php74::class)]
#[Framework\Attributes\RequiresPhp('>=7.4')]
#[Framework\Attributes\UsesClass(Factory::class)]
final class Php74Test extends ExplicitRuleSetTestCase
{
protected string $name = 'ergebnis (PHP 7.4)';
Expand Down
3 changes: 3 additions & 0 deletions test/Unit/RuleSet/Php80Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@

namespace Ergebnis\PhpCsFixer\Config\Test\Unit\RuleSet;

use Ergebnis\PhpCsFixer\Config\Factory;
use Ergebnis\PhpCsFixer\Config\RuleSet;
use PHPUnit\Framework;

#[Framework\Attributes\CoversClass(RuleSet\AbstractRuleSet::class)]
#[Framework\Attributes\CoversClass(RuleSet\Php80::class)]
#[Framework\Attributes\RequiresPhp('>=8.0')]
#[Framework\Attributes\UsesClass(Factory::class)]
final class Php80Test extends ExplicitRuleSetTestCase
{
protected string $name = 'ergebnis (PHP 8.0)';
Expand Down
3 changes: 3 additions & 0 deletions test/Unit/RuleSet/Php81Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@

namespace Ergebnis\PhpCsFixer\Config\Test\Unit\RuleSet;

use Ergebnis\PhpCsFixer\Config\Factory;
use Ergebnis\PhpCsFixer\Config\RuleSet;
use PHPUnit\Framework;

#[Framework\Attributes\CoversClass(RuleSet\AbstractRuleSet::class)]
#[Framework\Attributes\CoversClass(RuleSet\Php81::class)]
#[Framework\Attributes\RequiresPhp('>=8.1')]
#[Framework\Attributes\UsesClass(Factory::class)]
final class Php81Test extends ExplicitRuleSetTestCase
{
protected string $name = 'ergebnis (PHP 8.1)';
Expand Down
3 changes: 3 additions & 0 deletions test/Unit/RuleSet/Php82Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@

namespace Ergebnis\PhpCsFixer\Config\Test\Unit\RuleSet;

use Ergebnis\PhpCsFixer\Config\Factory;
use Ergebnis\PhpCsFixer\Config\RuleSet;
use PHPUnit\Framework;

#[Framework\Attributes\CoversClass(RuleSet\AbstractRuleSet::class)]
#[Framework\Attributes\CoversClass(RuleSet\Php82::class)]
#[Framework\Attributes\RequiresPhp('>=8.2')]
#[Framework\Attributes\UsesClass(Factory::class)]
final class Php82Test extends ExplicitRuleSetTestCase
{
protected string $name = 'ergebnis (PHP 8.2)';
Expand Down

0 comments on commit 646abb7

Please sign in to comment.