Skip to content

Commit 4820350

Browse files
authored
[phpunit 10] Add ParentTestClassConstructorRector (#484)
* [phpunit 10] Add ParentTestClassConstructorRector * skip test case and test classes as handled by PHPUnit * skip abstract classes and test cases * note
1 parent 27f53d0 commit 4820350

File tree

9 files changed

+231
-0
lines changed

9 files changed

+231
-0
lines changed

config/sets/phpunit100.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Rector\Config\RectorConfig;
66
use Rector\PHPUnit\PHPUnit100\Rector\Class_\AddProphecyTraitRector;
7+
use Rector\PHPUnit\PHPUnit100\Rector\Class_\ParentTestClassConstructorRector;
78
use Rector\PHPUnit\PHPUnit100\Rector\Class_\PublicDataProviderClassMethodRector;
89
use Rector\PHPUnit\PHPUnit100\Rector\Class_\StaticDataProviderClassMethodRector;
910
use Rector\PHPUnit\PHPUnit100\Rector\MethodCall\PropertyExistsWithoutAssertRector;
@@ -23,6 +24,7 @@
2324
WithConsecutiveRector::class,
2425
RemoveSetMethodsMethodCallRector::class,
2526
PropertyExistsWithoutAssertRector::class,
27+
ParentTestClassConstructorRector::class,
2628
]);
2729

2830
$rectorConfig->ruleWithConfiguration(RenameMethodRector::class, [
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit100\Rector\Class_\ParentTestClassConstructorRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class MockingHelper extends TestCase
8+
{
9+
}
10+
11+
?>
12+
-----
13+
<?php
14+
15+
namespace Rector\PHPUnit\Tests\PHPUnit100\Rector\Class_\ParentTestClassConstructorRector\Fixture;
16+
17+
use PHPUnit\Framework\TestCase;
18+
19+
final class MockingHelper extends TestCase
20+
{
21+
public function __construct()
22+
{
23+
parent::__construct(static::class);
24+
}
25+
}
26+
27+
?>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit100\Rector\Class_\ParentTestClassConstructorRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
abstract class SkipAbstractClasses extends TestCase
8+
{
9+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit100\Rector\Class_\ParentTestClassConstructorRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class SkipAlreadyAdded extends TestCase
8+
{
9+
public function __construct()
10+
{
11+
parent::__construct(static::class);
12+
}
13+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit100\Rector\Class_\ParentTestClassConstructorRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class SkipInCaseOfTest extends TestCase
8+
{
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit100\Rector\Class_\ParentTestClassConstructorRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class SkipInCaseOfTestCase extends TestCase
8+
{
9+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\Tests\PHPUnit100\Rector\Class_\ParentTestClassConstructorRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class ParentTestClassConstructorRectorTest extends AbstractRectorTestCase
12+
{
13+
#[DataProvider('provideData')]
14+
public function test(string $filePath): void
15+
{
16+
$this->doTestFile($filePath);
17+
}
18+
19+
public static function provideData(): Iterator
20+
{
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
22+
}
23+
24+
public function provideConfigFilePath(): string
25+
{
26+
return __DIR__ . '/config/configured_rule.php';
27+
}
28+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\PHPUnit\PHPUnit100\Rector\Class_\ParentTestClassConstructorRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rule(ParentTestClassConstructorRector::class);
10+
};
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\PHPUnit100\Rector\Class_;
6+
7+
use PhpParser\Modifiers;
8+
use PhpParser\Node;
9+
use PhpParser\Node\Arg;
10+
use PhpParser\Node\Expr\ClassConstFetch;
11+
use PhpParser\Node\Expr\StaticCall;
12+
use PhpParser\Node\Name;
13+
use PhpParser\Node\Stmt\Class_;
14+
use PhpParser\Node\Stmt\ClassMethod;
15+
use PhpParser\Node\Stmt\Expression;
16+
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
17+
use Rector\Rector\AbstractRector;
18+
use Rector\ValueObject\MethodName;
19+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
20+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
21+
22+
/**
23+
* @see https://github.com/sebastianbergmann/phpunit/issues/3975
24+
* @see https://github.com/sebastianbergmann/phpunit/commit/705874f1b867fd99865e43cb5eaea4e6d141582f
25+
*
26+
* @see \Rector\PHPUnit\Tests\PHPUnit100\Rector\Class_\ParentTestClassConstructorRector\ParentTestClassConstructorRectorTest
27+
*/
28+
final class ParentTestClassConstructorRector extends AbstractRector
29+
{
30+
public function __construct(
31+
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
32+
) {
33+
}
34+
35+
public function getRuleDefinition(): RuleDefinition
36+
{
37+
return new RuleDefinition('PHPUnit\Framework\TestCase requires a parent constructor call', [
38+
new CodeSample(
39+
<<<'CODE_SAMPLE'
40+
use PHPUnit\Framework\TestCase;
41+
42+
final class SomeHelper extends TestCase
43+
{
44+
}
45+
CODE_SAMPLE
46+
47+
,
48+
<<<'CODE_SAMPLE'
49+
use PHPUnit\Framework\TestCase;
50+
51+
final class SomeHelper extends TestCase
52+
{
53+
public function __construct()
54+
{
55+
parent::__construct(static::class);
56+
}
57+
}
58+
CODE_SAMPLE
59+
),
60+
]);
61+
}
62+
63+
/**
64+
* @return array<class-string<Node>>
65+
*/
66+
public function getNodeTypes(): array
67+
{
68+
return [Class_::class];
69+
}
70+
71+
/**
72+
* @param Class_ $node
73+
*/
74+
public function refactor(Node $node): ?Node
75+
{
76+
if (! $this->testsNodeAnalyzer->isInTestClass($node)) {
77+
return null;
78+
}
79+
80+
if ($this->shouldSkipClass($node)) {
81+
return null;
82+
}
83+
84+
// it already has a constructor, skip as it might require specific tweaking
85+
if ($node->getMethod(MethodName::CONSTRUCT)) {
86+
return null;
87+
}
88+
89+
$constructorClassMethod = new ClassMethod(MethodName::CONSTRUCT);
90+
$constructorClassMethod->flags |= Modifiers::PUBLIC;
91+
$constructorClassMethod->stmts[] = new Expression($this->createParentConstructorCall());
92+
93+
$node->stmts = array_merge([$constructorClassMethod], $node->stmts);
94+
95+
return $node;
96+
}
97+
98+
private function createParentConstructorCall(): StaticCall
99+
{
100+
$staticClassConstFetch = new ClassConstFetch(new Name('static'), 'class');
101+
102+
return new StaticCall(new Name('parent'), MethodName::CONSTRUCT, [new Arg($staticClassConstFetch)]);
103+
}
104+
105+
private function shouldSkipClass(Class_ $class): bool
106+
{
107+
if ($class->isAbstract()) {
108+
return true;
109+
}
110+
111+
if ($class->isAnonymous()) {
112+
return true;
113+
}
114+
115+
$className = $this->getName($class);
116+
117+
// loaded automatically by PHPUnit
118+
if (str_ends_with((string) $className, 'Test')) {
119+
return true;
120+
}
121+
122+
return str_ends_with((string) $className, 'TestCase');
123+
}
124+
}

0 commit comments

Comments
 (0)