Skip to content

Commit 3d8a988

Browse files
authored
Make number of tests always the same (#935)
1 parent a06ef9e commit 3d8a988

File tree

6 files changed

+151
-57
lines changed

6 files changed

+151
-57
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
[![Latest stable version](https://img.shields.io/packagist/v/kubawerlos/php-cs-fixer-custom-fixers.svg?label=current%20version)](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)
66
[![PHP version](https://img.shields.io/packagist/php-v/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://php.net)
77
[![License](https://img.shields.io/github/license/kubawerlos/php-cs-fixer-custom-fixers.svg)](LICENSE)
8-
![Tests](https://img.shields.io/badge/tests-3510-brightgreen.svg)
8+
![Tests](https://img.shields.io/badge/tests-3511-brightgreen.svg)
99
[![Downloads](https://img.shields.io/packagist/dt/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)
1010

1111
[![CI Status](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/workflows/CI/badge.svg?branch=main)](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/actions)

tests/Analyzer/Analysis/ConstructorAnalysisTest.php

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,35 @@ public function __construct(bool $b, callable $c, int $i, string $s) {}
6161
public function __construct(Foo $x = null, ?Bar $y = null, float $z = 3.14) {}
6262
}',
6363
];
64+
}
65+
66+
/**
67+
* @param array<string> $expected
68+
*
69+
* @dataProvider provideGettingConstructorParameterNames80Cases
70+
*
71+
* @requires PHP ^8.0
72+
*/
73+
public function testGettingConstructorParameterNames80(array $expected, string $code): void
74+
{
75+
$tokens = Tokens::fromCode($code);
76+
$analysis = new ConstructorAnalysis($tokens, 11);
6477

65-
if (\PHP_VERSION_ID >= 80000) {
66-
yield 'some already promoted' => [
67-
['$a', '$b', '$q', '$s', '$t'],
68-
'<?php class Foo {
78+
self::assertSame(11, $analysis->getConstructorIndex());
79+
self::assertSame($expected, $analysis->getConstructorParameterNames());
80+
}
81+
82+
/**
83+
* @return iterable<array{array<string>, string}>
84+
*/
85+
public static function provideGettingConstructorParameterNames80Cases(): iterable
86+
{
87+
yield 'some already promoted' => [
88+
['$a', '$b', '$q', '$s', '$t'],
89+
'<?php class Foo {
6990
public function __construct(public array $a, bool $b, protected ?Bar\Baz\Qux $q, string $s, private OtherType $t) {}
7091
}',
71-
];
72-
}
92+
];
7393
}
7494

7595
/**
@@ -118,15 +138,35 @@ public function __construct(array $a, bool $b, callable $c1, CALLABLE $c1, int $
118138
public function __construct(int $x, int $y, int ...$z) {}
119139
}',
120140
];
141+
}
121142

122-
if (\PHP_VERSION_ID >= 80000) {
123-
yield 'some already promoted' => [
124-
[22 => '$b', 39 => '$s'],
125-
'<?php class Foo {
143+
/**
144+
* @param array<int, string> $expected
145+
*
146+
* @dataProvider provideGettingConstructorPromotableParameters80Cases
147+
*
148+
* @requires PHP ^8.0
149+
*/
150+
public function testGettingConstructorPromotableParameters80(array $expected, string $code): void
151+
{
152+
$tokens = Tokens::fromCode($code);
153+
$analysis = new ConstructorAnalysis($tokens, 11);
154+
155+
self::assertSame(11, $analysis->getConstructorIndex());
156+
self::assertSame($expected, $analysis->getConstructorPromotableParameters());
157+
}
158+
159+
/**
160+
* @return iterable<array{array<int, string>, string}>
161+
*/
162+
public static function provideGettingConstructorPromotableParameters80Cases(): iterable
163+
{
164+
yield 'some already promoted' => [
165+
[22 => '$b', 39 => '$s'],
166+
'<?php class Foo {
126167
public function __construct(public array $a, bool $b, protected ?Bar\Baz\Qux $q, string $s, private OtherType $t) {}
127168
}',
128-
];
129-
}
169+
];
130170
}
131171

132172
/**

tests/Fixer/AbstractFixerTestCase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ abstract class AbstractFixerTestCase extends TestCase
3737
'testFix',
3838
'testFixPre80',
3939
'testFix80',
40+
'testFix81',
4041
'testFix82',
4142
'testIsRisky',
4243
'testReversingCodeSample',

tests/Fixer/NoUselessParenthesisFixerTest.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,6 @@ public static function provideFixCases(): iterable
7979
yield ['<?php $f = fn &($x) => $x + 2;'];
8080
yield ['<?php $f = fn &($x): int => $x + 2;'];
8181
}
82-
if (\defined('T_MATCH')) {
83-
yield ['<?php return match ($x) { default => 0 };'];
84-
}
8582
yield ['<?php class Foo {
8683
public function createSelf() {
8784
return new self([1, 2]);
@@ -382,4 +379,14 @@ public function testFixPre80(): void
382379
'<?php $foo = $bar{(1)};',
383380
);
384381
}
382+
383+
/**
384+
* @requires PHP 8.0
385+
*/
386+
public function testFix80(): void
387+
{
388+
$this->doTest(
389+
'<?php return match ($x) { default => 0 };',
390+
);
391+
}
385392
}

tests/Fixer/PhpdocNoIncorrectVarAnnotationFixerTest.php

Lines changed: 67 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -259,45 +259,6 @@ class Foo
259259
];
260260
}
261261

262-
if (\PHP_VERSION_ID >= 80000) {
263-
yield 'keep correct PHPDoc for class properties, PHP 8.0' => [
264-
'<?php class Foo
265-
{
266-
/** @var int|string */
267-
private int|string $intOrString;
268-
}',
269-
];
270-
271-
yield 'keep correct PHPDoc for promoted properties, PHP 8.0' => [
272-
'<?php class Foo
273-
{
274-
public function __construct(
275-
/** @var array<Foo> */
276-
public array $a,
277-
/** @var array<Foo> */
278-
public array $b,
279-
/** @var array<Foo> */
280-
protected array $c,
281-
/** @var array<Foo> */
282-
private array $d,
283-
) {}
284-
}',
285-
];
286-
}
287-
288-
if (\PHP_VERSION_ID >= 80100) {
289-
yield 'keep correct PHPDoc for class properties, PHP 8.1' => [
290-
'<?php class Foo
291-
{
292-
/** @var string */
293-
private readonly string $readonlyString;
294-
295-
/** @var Bar&Vendor\\Baz */
296-
private Bar&Vendor\\Baz $barAndBaz;
297-
}',
298-
];
299-
}
300-
301262
yield 'remove PHPDoc for class properties' => [
302263
'<?php
303264
class Foo
@@ -467,4 +428,71 @@ public function f2($x) {
467428
}',
468429
];
469430
}
431+
432+
/**
433+
* @dataProvider provideFix80Cases
434+
*
435+
* @requires PHP ^8.0
436+
*/
437+
public function testFix80(string $expected, ?string $input = null): void
438+
{
439+
$this->doTest($expected, $input);
440+
}
441+
442+
/**
443+
* @return iterable<array{0: string, 1?: string}>
444+
*/
445+
public static function provideFix80Cases(): iterable
446+
{
447+
yield 'keep correct PHPDoc for class properties, PHP 8.0' => [
448+
'<?php class Foo
449+
{
450+
/** @var int|string */
451+
private int|string $intOrString;
452+
}',
453+
];
454+
455+
yield 'keep correct PHPDoc for promoted properties, PHP 8.0' => [
456+
'<?php class Foo
457+
{
458+
public function __construct(
459+
/** @var array<Foo> */
460+
public array $a,
461+
/** @var array<Foo> */
462+
public array $b,
463+
/** @var array<Foo> */
464+
protected array $c,
465+
/** @var array<Foo> */
466+
private array $d,
467+
) {}
468+
}',
469+
];
470+
}
471+
472+
/**
473+
* @dataProvider provideFix81Cases
474+
*
475+
* @requires PHP ^8.1
476+
*/
477+
public function testFix81(string $expected, ?string $input = null): void
478+
{
479+
$this->doTest($expected, $input);
480+
}
481+
482+
/**
483+
* @return iterable<array{0: string, 1?: string}>
484+
*/
485+
public static function provideFix81Cases(): iterable
486+
{
487+
yield 'keep correct PHPDoc for class properties, PHP 8.1' => [
488+
'<?php class Foo
489+
{
490+
/** @var string */
491+
private readonly string $readonlyString;
492+
493+
/** @var Bar&Vendor\\Baz */
494+
private Bar&Vendor\\Baz $barAndBaz;
495+
}',
496+
];
497+
}
470498
}

tests/Readme/ReadmeCommandTest.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@
1919
* @internal
2020
*
2121
* @covers \PhpCsFixerCustomFixersDev\Readme\ReadmeCommand
22-
*
23-
* @requires PHP ^8.1
2422
*/
2523
final class ReadmeCommandTest extends TestCase
2624
{
25+
/**
26+
* @requires PHP ^8.1
27+
*/
2728
public function testReadmeIsUpToDate(): void
2829
{
2930
$tester = new CommandTester(new ReadmeCommand());
@@ -37,4 +38,21 @@ public function testReadmeIsUpToDate(): void
3738
'README.md is not up to date, run "composer fix" to update it.',
3839
);
3940
}
41+
42+
public function testNumberOfTests(): void
43+
{
44+
\preg_match(
45+
'~https://img.shields.io/badge/tests-(\d+)-brightgreen.svg~',
46+
(string) \file_get_contents(__DIR__ . '/../../README.md'),
47+
$matches,
48+
);
49+
$expectedNumberOfTests = (int) $matches[1];
50+
51+
$readmeCommand = new \ReflectionClass(ReadmeCommand::class);
52+
$numberOfTests = $readmeCommand->getMethod('numberOfTests');
53+
$numberOfTests->setAccessible(true);
54+
$actualNumberOfTests = $numberOfTests->invoke($readmeCommand->newInstance());
55+
56+
self::assertSame($expectedNumberOfTests, $actualNumberOfTests);
57+
}
4058
}

0 commit comments

Comments
 (0)