• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

NexusPHP / cs-config / 14655342589

23 Feb 2025 03:16PM UTC coverage: 95.644%. Remained the same
14655342589

push

github

paulbalandan
Update build badge

1076 of 1125 relevant lines covered (95.64%)

63.51 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/Test/AbstractRulesetTestCase.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of Nexus CS Config.
7
 *
8
 * (c) 2020 John Paul E. Balandan, CPA <paulbalandan@gmail.com>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace Nexus\CsConfig\Test;
15

16
use Nexus\CsConfig\Ruleset\RulesetInterface;
17
use PhpCsFixer\Fixer\ConfigurableFixerInterface;
18
use PhpCsFixer\Fixer\FixerInterface;
19
use PhpCsFixer\FixerConfiguration\DeprecatedFixerOptionInterface;
20
use PhpCsFixer\FixerConfiguration\FixerOptionInterface;
21
use PhpCsFixer\Preg;
22
use PHPUnit\Framework\Attributes\DataProvider;
23
use PHPUnit\Framework\TestCase;
24

25
/**
26
 * Used for testing the rulesets.
27
 */
28
abstract class AbstractRulesetTestCase extends TestCase
29
{
30
    /**
31
     * @var array<string, FixerInterface>
32
     */
33
    private static array $builtInFixers = [];
34

35
    /**
36
     * @var array<int, string>
37
     */
38
    private static array $configuredFixers = [];
39

40
    /**
41
     * @var array<string, array<string, bool|list<string>|string>|bool>
42
     */
43
    private static array $enabledFixers = [];
44

45
    /**
46
     * @codeCoverageIgnore
47
     */
48
    public static function setUpBeforeClass(): void
49
    {
50
        $fixerProvider = FixerProvider::create(static::createRuleset());
51
        self::$builtInFixers = $fixerProvider->builtin();
52
        self::$configuredFixers = $fixerProvider->configured();
53
        self::$enabledFixers = $fixerProvider->enabled();
54
    }
55

56
    /**
57
     * @codeCoverageIgnore
58
     */
59
    public static function tearDownAfterClass(): void
60
    {
61
        FixerProvider::reset();
62
        self::$builtInFixers = [];
63
        self::$configuredFixers = [];
64
        self::$enabledFixers = [];
65
    }
66

67
    // =========================================================================
68
    // TESTS
69
    // =========================================================================
70

71
    final public function testAllConfiguredFixersAreNotUsingPresets(): void
72
    {
73
        $fixersThatArePresets = array_filter(
16✔
74
            self::$enabledFixers,
16✔
75
            static fn(string $fixer): bool => substr($fixer, 0, 1) === '@',
16✔
76
            ARRAY_FILTER_USE_KEY,
16✔
77
        );
16✔
78

79
        self::assertEmpty($fixersThatArePresets, \sprintf(
16✔
80
            '[%s] Ruleset should not be using rule sets (presets) as fixers. Found: "%s".',
16✔
81
            static::createRuleset()->getName(),
16✔
82
            implode('", "', array_keys($fixersThatArePresets)),
16✔
83
        ));
16✔
84
    }
85

86
    final public function testAllBuiltInFixersNotDeprecatedAreConfiguredInThisRuleset(): void
87
    {
88
        $fixersNotConfigured = array_diff(array_keys(self::$builtInFixers), self::$configuredFixers);
16✔
89

90
        sort($fixersNotConfigured);
16✔
91
        $c = \count($fixersNotConfigured);
16✔
92

93
        self::assertEmpty($fixersNotConfigured, \sprintf(
16✔
94
            '[%s] Non-deprecated built-in %s "%s" %s not configured in the ruleset.',
16✔
95
            static::createRuleset()->getName(),
16✔
96
            $c > 1 ? 'fixers' : 'fixer',
16✔
97
            implode('", "', $fixersNotConfigured),
16✔
98
            $c > 1 ? 'are' : 'is',
16✔
99
        ));
16✔
100
    }
101

102
    final public function testAllConfiguredFixersInThisRulesetAreBuiltInAndNotDeprecated(): void
103
    {
104
        $fixersNotBuiltIn = array_diff(self::$configuredFixers, array_keys(self::$builtInFixers));
16✔
105

106
        sort($fixersNotBuiltIn);
16✔
107
        $c = \count($fixersNotBuiltIn);
16✔
108

109
        self::assertEmpty($fixersNotBuiltIn, \sprintf(
16✔
110
            '[%s] Ruleset used %s "%s" which %s unknown and/or deprecated in PhpCsFixer.',
16✔
111
            static::createRuleset()->getName(),
16✔
112
            $c > 1 ? 'fixers' : 'fixer',
16✔
113
            implode('", "', $fixersNotBuiltIn),
16✔
114
            $c > 1 ? 'are' : 'is',
16✔
115
        ));
16✔
116
    }
117

118
    final public function testAllConfiguredFixersInThisRulesetAreSortedByName(): void
119
    {
120
        $fixers = self::$configuredFixers;
16✔
121
        $sorted = $fixers;
16✔
122
        sort($sorted);
16✔
123

124
        self::assertSame($sorted, $fixers, \sprintf(
16✔
125
            '[%s] Fixers are not sorted by name.',
16✔
126
            static::createRuleset()->getName(),
16✔
127
        ));
16✔
128
    }
129

130
    final public function testHeaderCommentFixerIsDisabledByDefault(): void
131
    {
132
        self::assertArrayHasKey('header_comment', self::$enabledFixers);
16✔
133
        self::assertFalse(self::$enabledFixers['header_comment']);
16✔
134
    }
135

136
    /**
137
     * @param list<string> $goodOptions
138
     * @param list<string> $deprecatedOptions
139
     */
140
    #[DataProvider('provideEnabledConfigurableFixerUsesAllAvailableOptionsNotDeprecatedCases')]
141
    final public function testEnabledConfigurableFixerUsesAllAvailableOptionsNotDeprecated(string $name, array $goodOptions, array $deprecatedOptions): void
142
    {
143
        /** @var null|array<string, bool|list<string>|string>|bool $ruleConfiguration */
144
        $ruleConfiguration = self::$enabledFixers[$name] ?? null;
1,952✔
145

146
        if (null === $ruleConfiguration) {
1,952✔
147
            self::markTestSkipped(\sprintf('`%s` is not yet defined in this ruleset.', $name)); // @codeCoverageIgnore
148
        }
149

150
        if (false === $ruleConfiguration) {
1,952✔
151
            // fixer is turned off
152
            $this->addToAssertionCount(1);
144✔
153

154
            return;
144✔
155
        }
156

157
        $ruleConfiguration = \is_array($ruleConfiguration) ? $ruleConfiguration : [];
1,808✔
158
        $ruleConfiguration = array_keys($ruleConfiguration);
1,808✔
159

160
        $missingOptions = array_diff($goodOptions, $ruleConfiguration);
1,808✔
161
        $usedDeprecatedOptions = array_intersect($deprecatedOptions, $ruleConfiguration);
1,808✔
162
        $extraUsedOptions = array_diff($ruleConfiguration, $goodOptions);
1,808✔
163

164
        self::assertEmpty($missingOptions, \sprintf(
1,808✔
165
            'Enabled configurable fixer "%s" does not use its available array %s "%s". Missing %s: "%s".',
1,808✔
166
            $name,
1,808✔
167
            \count($goodOptions) > 1 ? 'options' : 'option',
1,808✔
168
            implode('", "', $goodOptions),
1,808✔
169
            \count($missingOptions) > 1 ? 'options' : 'option',
1,808✔
170
            implode('", "', $missingOptions),
1,808✔
171
        ));
1,808✔
172
        self::assertEmpty($usedDeprecatedOptions, \sprintf(
1,808✔
173
            'Enabled configurable fixer "%s" uses deprecated %s: "%s".',
1,808✔
174
            $name,
1,808✔
175
            \count($usedDeprecatedOptions) > 1 ? 'options' : 'option',
1,808✔
176
            implode('", "', $usedDeprecatedOptions),
1,808✔
177
        ));
1,808✔
178
        self::assertEmpty($extraUsedOptions, \sprintf(
1,808✔
179
            '%s "%s" for enabled configurable fixer "%s" %s not defined by PhpCsFixer.',
1,808✔
180
            \count($extraUsedOptions) > 1 ? 'Options' : 'Option',
1,808✔
181
            implode('", "', $extraUsedOptions),
1,808✔
182
            $name,
1,808✔
183
            \count($extraUsedOptions) > 1 ? 'are' : 'is',
1,808✔
184
        ));
1,808✔
185
    }
186

187
    /**
188
     * @codeCoverageIgnore
189
     *
190
     * @return iterable<string, array{0: string, 1: list<string>, 2: list<string>}>
191
     */
192
    public static function provideEnabledConfigurableFixerUsesAllAvailableOptionsNotDeprecatedCases(): iterable
193
    {
194
        $fixers = FixerProvider::create(static::createRuleset())->builtin();
195
        ksort($fixers);
196

197
        foreach ($fixers as $name => $fixer) {
198
            if ($fixer instanceof ConfigurableFixerInterface) {
199
                $options = $fixer->getConfigurationDefinition()->getOptions();
200

201
                $goodOptions = array_values(array_map(
202
                    static fn(FixerOptionInterface $option): string => $option->getName(),
203
                    array_filter(
204
                        $options,
205
                        static fn(FixerOptionInterface $option): bool => ! $option instanceof DeprecatedFixerOptionInterface,
206
                    ),
207
                ));
208

209
                $deprecatedOptions = array_values(array_map(
210
                    static fn(FixerOptionInterface $option): string => $option->getName(),
211
                    array_filter(
212
                        $options,
213
                        static fn(FixerOptionInterface $option): bool => $option instanceof DeprecatedFixerOptionInterface,
214
                    ),
215
                ));
216

217
                yield $name => [$name, $goodOptions, $deprecatedOptions];
218
            }
219
        }
220
    }
221

222
    protected static function createRuleset(): RulesetInterface
223
    {
224
        /** @phpstan-var class-string<RulesetInterface> $className */
225
        $className = Preg::replace('/^(Nexus\\\\CsConfig)\\\\Tests(\\\\.+)Test$/', '$1$2', static::class);
64✔
226

227
        return new $className();
64✔
228
    }
229
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc