Skip to content

Commit 0fcb26e

Browse files
committed
[phpunit 10] Add ParentTestClassConstructorRector
1 parent 27f53d0 commit 0fcb26e

6 files changed

Lines changed: 179 additions & 0 deletions

File tree

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: 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: 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: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\PHPUnit100\Rector\Class_;
6+
7+
use PhpParser\Node\Expr\ClassConstFetch;
8+
use PhpParser\Node\Arg;
9+
use PhpParser\Modifiers;
10+
use PhpParser\Node;
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/commit/705874f1b867fd99865e43cb5eaea4e6d141582f
24+
*
25+
* @see \Rector\PHPUnit\Tests\PHPUnit100\Rector\Class_\ParentTestClassConstructorRector\ParentTestClassConstructorRectorTest
26+
*/
27+
final class ParentTestClassConstructorRector extends AbstractRector
28+
{
29+
public function __construct(
30+
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
31+
) {
32+
}
33+
34+
public function getRuleDefinition(): RuleDefinition
35+
{
36+
return new RuleDefinition('PHPUnit\Framework\TestCase requires a parent constructor call', [
37+
new CodeSample(
38+
<<<'CODE_SAMPLE'
39+
use PHPUnit\Framework\TestCase;
40+
41+
final class SomeHelper extends TestCase
42+
{
43+
}
44+
CODE_SAMPLE
45+
46+
,
47+
<<<'CODE_SAMPLE'
48+
use PHPUnit\Framework\TestCase;
49+
50+
final class SomeHelper extends TestCase
51+
{
52+
public function __construct()
53+
{
54+
parent::__construct(static::class);
55+
}
56+
}
57+
CODE_SAMPLE
58+
),
59+
]);
60+
}
61+
62+
/**
63+
* @return array<class-string<Node>>
64+
*/
65+
public function getNodeTypes(): array
66+
{
67+
return [Class_::class];
68+
}
69+
70+
/**
71+
* @param Class_ $node
72+
*/
73+
public function refactor(Node $node): ?Node
74+
{
75+
if (! $this->testsNodeAnalyzer->isInTestClass($node)) {
76+
return null;
77+
}
78+
79+
// it already has a constructor, skip as it might require specific tweaking
80+
if ($node->getMethod(MethodName::CONSTRUCT)) {
81+
return null;
82+
}
83+
84+
$constructorClassMethod = new ClassMethod(MethodName::CONSTRUCT);
85+
$constructorClassMethod->flags |= Modifiers::PUBLIC;
86+
$constructorClassMethod->stmts[] = new Expression($this->createParentConstructorCall());
87+
88+
$node->stmts = array_merge([$constructorClassMethod], $node->stmts);
89+
90+
return $node;
91+
}
92+
93+
private function createParentConstructorCall(): StaticCall
94+
{
95+
$staticClassConstFetch = new ClassConstFetch(new Name('static'), 'class');
96+
97+
return new StaticCall(new Name('parent'), MethodName::CONSTRUCT, [new Arg($staticClassConstFetch)]);
98+
}
99+
}

0 commit comments

Comments
 (0)