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

NexusPHP / cs-config / 5878190327

16 Aug 2023 11:28AM UTC coverage: 98.569% (-0.05%) from 98.619%
5878190327

push

github

paulbalandan
Update phpstan

8 of 13 new or added lines in 3 files covered. (61.54%)

2 existing lines in 1 file now uncovered.

2892 of 2934 relevant lines covered (98.57%)

24.22 hits per line

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

79.52
/src/Test/AbstractCustomFixerTestCase.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\Fixer\AbstractCustomFixer;
17
use PhpCsFixer\AbstractFixer;
18
use PhpCsFixer\Fixer\ConfigurableFixerInterface;
19
use PhpCsFixer\Fixer\DeprecatedFixerInterface;
20
use PhpCsFixer\FixerConfiguration\FixerOptionInterface;
21
use PhpCsFixer\FixerDefinition\CodeSampleInterface;
22
use PhpCsFixer\FixerDefinition\FileSpecificCodeSampleInterface;
23
use PhpCsFixer\FixerDefinition\VersionSpecificCodeSampleInterface;
24
use PhpCsFixer\FixerNameValidator;
25
use PhpCsFixer\Linter\CachingLinter;
26
use PhpCsFixer\Linter\LinterInterface;
27
use PhpCsFixer\Linter\ProcessLinter;
28
use PhpCsFixer\Preg;
29
use PhpCsFixer\Tokenizer\Token;
30
use PhpCsFixer\Tokenizer\Tokens;
31
use PHPUnit\Framework\TestCase;
32

33
/**
34
 * Used for testing the fixers.
35
 *
36
 * Most of the tests here are directly from `PhpCsFixer\Tests\Test\AbstractFixerTestCase`
37
 * with some modifications and additions, since the test case is not shipped to production.
38
 *
39
 * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
40
 * @author John Paul E. Balandan, CPA <paulbalandan@gmail.com>
41
 */
42
abstract class AbstractCustomFixerTestCase extends TestCase
43
{
44
    protected AbstractCustomFixer $fixer;
45
    protected LinterInterface $linter;
46

47
    protected function setUp(): void
48
    {
49
        parent::setUp();
69✔
50

51
        $this->fixer = $this->createFixer();
69✔
52
        $this->linter = $this->getLinter();
69✔
53
    }
54

55
    final public function testIsRisky(): void
56
    {
57
        $riskyDescription = $this->fixer->getDefinition()->getRiskyDescription();
6✔
58

59
        if ($this->fixer->isRisky()) {
6✔
60
            self::assertIsString($riskyDescription);
×
NEW
61
            self::assertValidDescription($this->fixer->getName(), 'risky description', $riskyDescription);
×
62
        } else {
63
            self::assertNull($riskyDescription, sprintf('[%s] Fixer is not risky so no description of it is expected.', $this->fixer->getName()));
6✔
64
        }
65

66
        $reflection = new \ReflectionMethod($this->fixer, 'isRisky');
6✔
67

68
        self::assertSame(
6✔
69
            ! $this->fixer->isRisky(),
6✔
70
            $reflection->getDeclaringClass()->getName() === AbstractFixer::class,
6✔
71
            sprintf(
6✔
72
                '[%s] Fixer is %s so the method "AbstractFixer::isRisky()" must be %s.',
6✔
73
                $this->fixer->getName(),
6✔
74
                $this->fixer->isRisky() ? 'risky' : 'not risky',
6✔
75
                $this->fixer->isRisky() ? 'overridden' : 'used',
6✔
76
            ),
6✔
77
        );
6✔
78
    }
79

80
    final public function testNameIsValid(): void
81
    {
82
        $nameValidator = new FixerNameValidator();
6✔
83
        $customFixerName = $this->fixer->getName();
6✔
84

85
        self::assertTrue(
6✔
86
            $nameValidator->isValid($customFixerName, true),
6✔
87
            sprintf('Fixer name "%s" is not valid.', $customFixerName),
6✔
88
        );
6✔
89
    }
90

91
    final public function testFixerIsFinal(): void
92
    {
93
        self::assertTrue(
6✔
94
            (new \ReflectionClass($this->fixer))->isFinal(),
6✔
95
            sprintf('Fixer "%s" must be declared "final".', $this->fixer->getName()),
6✔
96
        );
6✔
97
    }
98

99
    final public function testDeprecatedFixersHaveCorrectSummary(): void
100
    {
101
        self::assertStringNotContainsString(
6✔
102
            'DEPRECATED',
6✔
103
            $this->fixer->getDefinition()->getSummary(),
6✔
104
            'Fixer cannot contain word "DEPRECATED" in summary',
6✔
105
        );
6✔
106

107
        $comment = (new \ReflectionClass($this->fixer))->getDocComment();
6✔
108
        self::assertIsString($comment, sprintf('[%s] Fixer is missing a class-level PHPDoc.', $this->fixer->getName()));
6✔
109

110
        if ($this->fixer instanceof DeprecatedFixerInterface) {
6✔
111
            self::assertStringContainsString('@deprecated', $comment);
3✔
112
        } else {
113
            self::assertStringNotContainsString('@deprecated', $comment);
3✔
114
        }
115
    }
116

117
    final public function testFixerConfigurationDefinitions(): void
118
    {
119
        if (! $this->fixer instanceof ConfigurableFixerInterface) {
6✔
120
            $this->addToAssertionCount(1); // not applied to the fixer without configuration
6✔
121

122
            return;
6✔
123
        }
124

125
        $configurationDefinition = $this->fixer->getConfigurationDefinition();
×
126

127
        foreach ($configurationDefinition->getOptions() as $option) {
×
UNCOV
128
            self::assertInstanceOf(FixerOptionInterface::class, $option);
×
129
            self::assertNotEmpty($option->getDescription());
×
130

131
            self::assertTrue(
×
132
                $option->hasDefault(),
×
133
                sprintf(
×
134
                    'Option `%s` of fixer `%s` should have a default value.',
×
135
                    $option->getName(),
×
136
                    $this->fixer->getName(),
×
137
                ),
×
138
            );
×
139

140
            self::assertStringNotContainsString(
×
141
                'DEPRECATED',
×
142
                $option->getDescription(),
×
143
                'Option description cannot contain word "DEPRECATED"',
×
144
            );
×
145
        }
146
    }
147

148
    final public function testFixerDefinitions(): void
149
    {
150
        $fixerName = $this->fixer->getName();
6✔
151
        $definition = $this->fixer->getDefinition();
6✔
152
        $fixerIsConfigurable = $this->fixer instanceof ConfigurableFixerInterface;
6✔
153

154
        self::assertValidDescription($fixerName, 'summary', $definition->getSummary());
6✔
155

156
        $samples = $definition->getCodeSamples();
6✔
157
        self::assertNotEmpty($samples, sprintf('[%s] Code samples are required.', $fixerName));
6✔
158

159
        $configSamplesProvided = [];
6✔
160
        $dummyFileInfo = new \SplFileInfo(__FILE__);
6✔
161

162
        foreach ($samples as $counter => $sample) {
6✔
163
            self::assertIsInt($counter);
6✔
164

165
            ++$counter;
6✔
166
            self::assertInstanceOf(CodeSampleInterface::class, $sample, sprintf('[%s] Sample #%d must be an instance of "%s".', $fixerName, $counter, CodeSampleInterface::class));
6✔
167

168
            $code = $sample->getCode();
6✔
169
            self::assertNotEmpty($code, sprintf('[%s] Code provided by sample #%d must not be empty.', $fixerName, $counter));
6✔
170
            self::assertSame("\n", substr($code, -1), sprintf('[%s] Sample #%d must end with linebreak', $fixerName, $counter));
6✔
171

172
            $config = $sample->getConfiguration();
6✔
173

174
            if (null !== $config) {
6✔
175
                self::assertTrue($fixerIsConfigurable, sprintf('[%s] Sample #%d has configuration, but the fixer is not configurable.', $fixerName, $counter));
×
176

UNCOV
177
                $configSamplesProvided[$counter] = $config;
×
178
            } elseif ($fixerIsConfigurable) {
6✔
179
                if (! $sample instanceof VersionSpecificCodeSampleInterface) {
×
180
                    self::assertArrayNotHasKey('default', $configSamplesProvided, sprintf('[%s] Multiple non-versioned samples with default configuration.', $fixerName));
×
181
                }
182

183
                $configSamplesProvided['default'] = true;
×
184
            }
185

186
            if ($sample instanceof VersionSpecificCodeSampleInterface && ! $sample->isSuitableFor(\PHP_VERSION_ID)) {
6✔
187
                continue;
×
188
            }
189

190
            if ($fixerIsConfigurable) {
6✔
191
                // always re-configure as the fixer might have been configured with diff. configuration from previous sample
192
                $this->fixer->configure(null === $config ? [] : $config);
×
193
            }
194

195
            Tokens::clearCache();
6✔
196
            $tokens = Tokens::fromCode($code);
6✔
197

198
            $this->fixer->fix(
6✔
199
                $sample instanceof FileSpecificCodeSampleInterface ? $sample->getSplFileInfo() : $dummyFileInfo,
6✔
200
                $tokens,
6✔
201
            );
6✔
202

203
            self::assertTrue($tokens->isChanged(), sprintf('[%s] Sample #%d is not changed during fixing.', $fixerName, $counter));
6✔
204

205
            $duplicatedCodeSample = array_search(
6✔
206
                $sample,
6✔
207
                \array_slice($samples, 0, $counter - 1),
6✔
208
                true,
6✔
209
            );
6✔
210

211
            self::assertFalse(
6✔
212
                $duplicatedCodeSample,
6✔
213
                sprintf('[%s] Sample #%d duplicates #%d.', $fixerName, $counter, ++$duplicatedCodeSample),
6✔
214
            );
6✔
215
        }
216

217
        if ($fixerIsConfigurable) {
6✔
218
            if (isset($configSamplesProvided['default'])) {
×
219
                reset($configSamplesProvided);
×
220
                self::assertSame('default', key($configSamplesProvided), sprintf('[%s] First sample must be for the default configuration.', $fixerName));
×
221
            }
222

223
            if (\count($configSamplesProvided) < 2) {
×
224
                self::fail(sprintf('[%s] Configurable fixer only provides a default configuration sample and none for its configuration options.', $fixerName));
×
225
            }
226

227
            $options = $this->fixer->getConfigurationDefinition()->getOptions();
×
228

229
            foreach ($options as $option) {
×
230
                self::assertMatchesRegularExpression('/^[a-z_]+[a-z]$/', $option->getName(), sprintf('[%s] Option %s is not snake_case.', $fixerName, $option->getName()));
×
231
            }
232
        }
233
    }
234

235
    /**
236
     * Tests if a fixer fixes a given string to match the expected result.
237
     *
238
     * It is used both if you want to test if something is fixed or if it is not touched by the fixer.
239
     *
240
     * It also makes sure that the expected output does not change when run through the fixer. That means that you
241
     * do not need two test cases like [$expected] and [$expected, $input] (where $expected is the same in both cases)
242
     * as the latter covers both of them.
243
     *
244
     * This method throws an exception if $expected and $input are equal to prevent test cases that accidentally do
245
     * not test anything.
246
     *
247
     * @param string      $expected The expected fixer output
248
     * @param null|string $input    The fixer input, or null if it should intentionally be equal to the output
249
     */
250
    protected function doTest(string $expected, ?string $input = null): void
251
    {
252
        if ($expected === $input) {
33✔
253
            throw new \LogicException('Input parameter must not be equal to expected parameter.'); // @codeCoverageIgnore
254
        }
255

256
        $file = new \SplFileInfo(__FILE__);
33✔
257

258
        if (null !== $input) {
33✔
259
            self::assertNull($this->lintSource($input));
18✔
260

261
            Tokens::clearCache();
18✔
262
            $tokens = Tokens::fromCode($input);
18✔
263

264
            self::assertTrue($this->fixer->isCandidate($tokens), 'Fixer must be a candidate for input code.');
18✔
265
            self::assertFalse($tokens->isChanged(), 'Fixer must not touch Tokens on candidate check.');
18✔
266
            $this->fixer->fix($file, $tokens);
18✔
267

268
            self::assertSame(
18✔
269
                $expected,
18✔
270
                $tokens->generateCode(),
18✔
271
                'Code build on input code must match expected code.',
18✔
272
            );
18✔
273
            self::assertTrue($tokens->isChanged(), 'Tokens collection built on input code must be marked as changed after fixing.');
18✔
274

275
            $tokens->clearEmptyTokens();
18✔
276

277
            /** @var Token[] $tokensArray */
278
            $tokensArray = $tokens->toArray();
18✔
279

280
            self::assertCount(
18✔
281
                \count($tokens),
18✔
282
                array_unique(array_map(static fn(Token $token): string => spl_object_hash($token), $tokensArray)),
18✔
283
                'Token items inside Tokens collection must be unique.',
18✔
284
            );
18✔
285

286
            unset($tokensArray);
18✔
287
            Tokens::clearCache();
18✔
288
            $expectedTokens = Tokens::fromCode($expected);
18✔
289
            self::assertTokens($expectedTokens, $tokens);
18✔
290
        }
291

292
        self::assertNull($this->lintSource($expected));
33✔
293

294
        Tokens::clearCache();
33✔
295
        $tokens = Tokens::fromCode($expected);
33✔
296
        $this->fixer->fix($file, $tokens);
33✔
297

298
        self::assertSame(
33✔
299
            $expected,
33✔
300
            $tokens->generateCode(),
33✔
301
            'Code build on expected code must not change.',
33✔
302
        );
33✔
303
        self::assertFalse($tokens->isChanged(), 'Tokens collection built on expected code must not be marked as changed after fixing.');
33✔
304
    }
305

306
    protected function createFixer(): AbstractCustomFixer
307
    {
308
        /** @phpstan-var class-string<AbstractCustomFixer> $customFixer */
309
        $customFixer = Preg::replace('/^(Nexus\\\\CsConfig)\\\\Tests(\\\\.+)Test$/', '$1$2', static::class);
69✔
310

311
        return new $customFixer();
69✔
312
    }
313

314
    /**
315
     * @codeCoverageIgnore
316
     */
317
    protected function lintSource(string $source): ?string
318
    {
319
        try {
320
            $this->linter->lintSource($source)->check();
321

322
            return null;
323
        } catch (\Throwable $e) {
324
            return sprintf('Linting "%s" failed with message: %s.', $source, $e->getMessage());
325
        }
326
    }
327

328
    private function getLinter(): LinterInterface
329
    {
330
        static $linter = null;
69✔
331

332
        if (null === $linter) {
69✔
333
            $linter = new CachingLinter(new ProcessLinter());
4✔
334
        }
335

336
        return $linter;
69✔
337
    }
338

339
    private static function assertTokens(Tokens $expectedTokens, Tokens $inputTokens): void
340
    {
341
        self::assertCount($expectedTokens->count(), $inputTokens, 'Both Tokens collections should have the same size.');
18✔
342

343
        /** @var Token $expectedToken */
344
        foreach ($expectedTokens as $index => $expectedToken) {
18✔
345
            /** @var Token $inputToken */
346
            $inputToken = $inputTokens[$index];
18✔
347

348
            self::assertTrue(
18✔
349
                $expectedToken->equals($inputToken),
18✔
350
                sprintf("Token at index %d must be:\n%s,\ngot:\n%s.", $index, $expectedToken->toJson(), $inputToken->toJson()),
18✔
351
            );
18✔
352
        }
353
    }
354

355
    private static function assertValidDescription(string $fixerName, string $descriptionType, string $description): void
356
    {
357
        self::assertMatchesRegularExpression('/^[A-Z`][^"]+\.$/', $description, sprintf('[%s] The %s must start with capital letter or a ` and end with dot.', $fixerName, $descriptionType));
6✔
358
        self::assertStringNotContainsString('phpdocs', $description, sprintf('[%s] `PHPDoc` must not be in the plural in %s.', $fixerName, $descriptionType));
6✔
359
        self::assertCorrectCasing($description, 'PHPDoc', sprintf('[%s] `PHPDoc` must be in correct casing in %s.', $fixerName, $descriptionType));
6✔
360
        self::assertCorrectCasing($description, 'PHPUnit', sprintf('[%s] `PHPUnit` must be in correct casing in %s.', $fixerName, $descriptionType));
6✔
361
        self::assertFalse(strpos($descriptionType, '``'), sprintf('[%s] The %s must not contain sequential backticks.', $fixerName, $descriptionType));
6✔
362
    }
363

364
    private static function assertCorrectCasing(string $needle, string $haystack, string $message): void
365
    {
366
        self::assertSame(substr_count(strtolower($haystack), strtolower($needle)), substr_count($haystack, $needle), $message);
6✔
367
    }
368
}
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