Skip to content

Commit c1e5d5b

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

File tree

6 files changed

+177
-0
lines changed

6 files changed

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

0 commit comments

Comments
 (0)