Skip to content

Commit d17bdaf

Browse files
bbralaTomasVotruba
authored andcommitted
[PHPUnit60] Skip tests in classes with asserting lifecycle methods in AddDoesNotPerformAssertionToNonAssertingTestRector (rectorphp/rector#9965)
1 parent f34c2b2 commit d17bdaf

17 files changed

Lines changed: 688 additions & 105 deletions

‎bench/does_not_perform_assertion_lifecycle_bench.php‎

Lines changed: 136 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@
66
* Benchmark for AddDoesNotPerformAssertionToNonAssertingTestRector on test classes with deep parent chains.
77
*
88
* Each test class has parent lifecycle hooks (setUp(), tearDown()) and parent::test*() calls. Resolving these
9-
* via AstResolver parses and decorates parent files on first use, so this bench measures one cold pass, as in a real run.
9+
* via AstResolver parses and decorates parent files on first use, so every scenario measures one cold pass, as in a real run.
10+
*
11+
* Scenarios, all with the same shape (see the constants below):
12+
* - inherited: parents have non-asserting hooks, the test classes have none. Every test method changes.
13+
* - own set up: like "inherited", but each test class has its own setUp() calling parent::setUp(). The file of
14+
* the test class is already parsed by Rector, it must not be parsed a second time to look at its hooks.
15+
* - asserting hooks: the top parent of each chain has an asserting hook (setUp(), assertPostConditions() or a
16+
* #[Before] method), so the lifecycle analyzer walks the whole chain and no test method changes.
1017
*
1118
* Run on both branches to compare. CPU (user+sys) is the reliable metric; wall is noisy.
1219
*/
@@ -28,15 +35,30 @@
2835
const FILLER_METHODS = 15; // extra methods per parent class, to make parsing realistic
2936
const MAX_CPU_SECONDS = 1.0; // fails in CI above this
3037

31-
$directory = sys_get_temp_dir() . '/rector_phpunit_bench_' . getmypid();
32-
@mkdir($directory);
38+
const SCENARIOS = [
39+
'inherited' => [
40+
'own_set_up' => false,
41+
'asserting_hooks' => false,
42+
],
43+
'own set up' => [
44+
'own_set_up' => true,
45+
'asserting_hooks' => false,
46+
],
47+
'asserting hooks' => [
48+
'own_set_up' => false,
49+
'asserting_hooks' => true,
50+
],
51+
];
3352

34-
$writeClass = static function (string $className, string $body) use ($directory): string {
35-
$filePath = $directory . '/' . $className . '.php';
36-
file_put_contents($filePath, "<?php\n\nnamespace Bench;\n\n" . $body);
37-
return $filePath;
53+
$cpuSeconds = static function (): float {
54+
$usage = getrusage();
55+
return $usage['ru_utime.tv_sec'] + $usage['ru_utime.tv_usec'] / 1_000_000
56+
+ $usage['ru_stime.tv_sec'] + $usage['ru_stime.tv_usec'] / 1_000_000;
3857
};
3958

59+
$directory = sys_get_temp_dir() . '/rector_phpunit_bench_' . getmypid();
60+
@mkdir($directory);
61+
4062
// make generated classes visible to PHPStan reflection
4163
spl_autoload_register(static function (string $class) use ($directory): void {
4264
if (! str_starts_with($class, 'Bench\\')) {
@@ -49,103 +71,138 @@
4971
}
5072
});
5173

52-
$testFilePaths = [];
74+
$isFailed = false;
75+
$scenarioIndex = 0;
5376

54-
for ($chain = 0; $chain < CHAINS; ++$chain) {
55-
$parentClassName = '\PHPUnit\Framework\TestCase';
77+
foreach (SCENARIOS as $scenarioName => $scenario) {
78+
// own class names per scenario, so no scenario profits from the parsed files of another
79+
$prefix = 'S' . $scenarioIndex++;
5680

57-
for ($level = 0; $level < DEPTH; ++$level) {
58-
$className = sprintf('AbstractChain%dLevel%d', $chain, $level);
81+
$writeClass = static function (string $className, string $body) use ($directory): string {
82+
$filePath = $directory . '/' . $className . '.php';
83+
file_put_contents($filePath, "<?php\n\nnamespace Bench;\n\n" . $body);
84+
return $filePath;
85+
};
5986

60-
$methods = " protected function setUp(): void\n {\n parent::setUp();\n \$this->prepare{$level}();\n }\n\n";
61-
$methods .= " protected function tearDown(): void\n {\n \$this->cleanup = null;\n parent::tearDown();\n }\n\n";
62-
$methods .= " public function testInherited(): void\n {\n \$value = {$level};\n }\n\n";
63-
$methods .= " private function prepare{$level}(): void\n {\n \$this->ready = true;\n }\n\n";
87+
$testFilePaths = [];
6488

65-
for ($filler = 0; $filler < FILLER_METHODS; ++$filler) {
66-
$methods .= " protected function helper{$filler}(array \$items): array\n {\n return array_map(fn (\$item) => \$item * {$filler}, \$items);\n }\n\n";
67-
}
89+
for ($chain = 0; $chain < CHAINS; ++$chain) {
90+
$parentClassName = '\PHPUnit\Framework\TestCase';
6891

69-
$writeClass(
70-
$className,
71-
"abstract class {$className} extends {$parentClassName}\n{\n protected \$ready;\n protected \$cleanup;\n\n{$methods}}\n"
72-
);
92+
for ($level = 0; $level < DEPTH; ++$level) {
93+
$className = sprintf('%sAbstractChain%dLevel%d', $prefix, $chain, $level);
7394

74-
$parentClassName = $className;
75-
}
95+
$methods = " protected function setUp(): void\n {\n parent::setUp();\n \$this->prepare{$level}();\n }\n\n";
96+
$methods .= " protected function tearDown(): void\n {\n \$this->cleanup = null;\n parent::tearDown();\n }\n\n";
97+
$methods .= " public function testInherited(): void\n {\n \$value = {$level};\n }\n\n";
98+
$methods .= " private function prepare{$level}(): void\n {\n \$this->ready = true;\n }\n\n";
99+
100+
if ($scenario['asserting_hooks'] && $level === 0) {
101+
// the asserting hook is in the top parent, furthest away from the test classes
102+
$methods .= match ($chain % 3) {
103+
0 => " protected function assertPostConditions(): void\n {\n \$this->assertTrue(\$this->ready);\n }\n\n",
104+
1 => " #[\PHPUnit\Framework\Attributes\Before]\n protected function checkReady(): void\n {\n \$this->assertNotNull(\$this->cleanup);\n }\n\n",
105+
default => " protected function assertPreConditions(): void\n {\n \$this->assertFalse(\$this->ready);\n }\n\n",
106+
};
107+
}
76108

77-
for ($test = 0; $test < TESTS_PER_CHAIN; ++$test) {
78-
$className = sprintf('Chain%dTest%d', $chain, $test);
109+
for ($filler = 0; $filler < FILLER_METHODS; ++$filler) {
110+
$methods .= " protected function helper{$filler}(array \$items): array\n {\n return array_map(fn (\$item) => \$item * {$filler}, \$items);\n }\n\n";
111+
}
79112

80-
$methods = " public function testInherited(): void\n {\n parent::testInherited();\n }\n\n";
81-
for ($testMethod = 0; $testMethod < TEST_METHODS; ++$testMethod) {
82-
$methods .= " public function testSomething{$testMethod}(): void\n {\n \$value = \$this->helper{$testMethod}([1, 2, 3]);\n }\n\n";
113+
$writeClass(
114+
$className,
115+
"abstract class {$className} extends {$parentClassName}\n{\n protected \$ready;\n protected \$cleanup;\n\n{$methods}}\n"
116+
);
117+
118+
$parentClassName = $className;
83119
}
84120

85-
$testFilePaths[] = $writeClass(
86-
$className,
87-
"final class {$className} extends {$parentClassName}\n{\n{$methods}}\n"
88-
);
121+
for ($test = 0; $test < TESTS_PER_CHAIN; ++$test) {
122+
$className = sprintf('%sChain%dTest%d', $prefix, $chain, $test);
123+
124+
$methods = '';
125+
if ($scenario['own_set_up']) {
126+
$methods .= " protected function setUp(): void\n {\n parent::setUp();\n \$this->ready = true;\n }\n\n";
127+
}
128+
129+
$methods .= " public function testInherited(): void\n {\n parent::testInherited();\n }\n\n";
130+
for ($testMethod = 0; $testMethod < TEST_METHODS; ++$testMethod) {
131+
$methods .= " public function testSomething{$testMethod}(): void\n {\n \$value = \$this->helper{$testMethod}([1, 2, 3]);\n }\n\n";
132+
}
133+
134+
$testFilePaths[] = $writeClass(
135+
$className,
136+
"final class {$className} extends {$parentClassName}\n{\n{$methods}}\n"
137+
);
138+
}
89139
}
90-
}
91140

92-
$rectorConfig = (new LazyContainerFactory())->create();
93-
$rectorConfig->boot();
141+
$rectorConfig = (new LazyContainerFactory())->create();
142+
$rectorConfig->boot();
94143

95-
/** @var TestingParser $testingParser */
96-
$testingParser = $rectorConfig->make(TestingParser::class);
144+
/** @var TestingParser $testingParser */
145+
$testingParser = $rectorConfig->make(TestingParser::class);
97146

98-
/** @var BetterNodeFinder $betterNodeFinder */
99-
$betterNodeFinder = $rectorConfig->make(BetterNodeFinder::class);
147+
/** @var BetterNodeFinder $betterNodeFinder */
148+
$betterNodeFinder = $rectorConfig->make(BetterNodeFinder::class);
100149

101-
$rector = $rectorConfig->make(AddDoesNotPerformAssertionToNonAssertingTestRector::class);
150+
$rector = $rectorConfig->make(AddDoesNotPerformAssertionToNonAssertingTestRector::class);
102151

103-
/** @var CurrentFileProvider $currentFileProvider */
104-
$currentFileProvider = $rectorConfig->make(CurrentFileProvider::class);
152+
// parse test files up front, parent files are left for the rule to resolve
153+
// the rule finds the class of a method in the stmts of the current file, as it does in a real run
154+
/** @var CurrentFileProvider $currentFileProvider */
155+
$currentFileProvider = $rectorConfig->make(CurrentFileProvider::class);
105156

106-
// parse test files up front, parent files are left for the rule to resolve
107-
// the rule finds the class of a method in the stmts of the current file, as in a real run
108-
$classMethodsByFile = [];
109-
foreach ($testFilePaths as $testFilePath) {
110-
$stmts = $testingParser->parseFileToDecoratedNodes($testFilePath);
111-
$file = new File($testFilePath, (string) file_get_contents($testFilePath));
112-
$file->hydrateStmtsAndTokens($stmts, $stmts, []);
113-
$classMethodsByFile[] = [$file, $betterNodeFinder->findInstanceOf($stmts, ClassMethod::class)];
114-
}
157+
$classMethodsByFile = [];
158+
$methodCount = 0;
159+
foreach ($testFilePaths as $testFilePath) {
160+
$stmts = $testingParser->parseFileToDecoratedNodes($testFilePath);
115161

116-
$cpuSeconds = static function (): float {
117-
$usage = getrusage();
118-
return $usage['ru_utime.tv_sec'] + $usage['ru_utime.tv_usec'] / 1_000_000
119-
+ $usage['ru_stime.tv_sec'] + $usage['ru_stime.tv_usec'] / 1_000_000;
120-
};
162+
$file = new File($testFilePath, (string) file_get_contents($testFilePath));
163+
$file->hydrateStmtsAndTokens($stmts, $stmts, []);
121164

122-
$wallStart = microtime(true);
123-
$cpuStart = $cpuSeconds();
165+
$classMethods = $betterNodeFinder->findInstanceOf($stmts, ClassMethod::class);
166+
$classMethodsByFile[] = [$file, $classMethods];
167+
$methodCount += count($classMethods);
168+
}
169+
170+
$wallStart = microtime(true);
171+
$cpuStart = $cpuSeconds();
124172

125-
$changedCount = 0;
126-
$classMethodCount = 0;
127-
foreach ($classMethodsByFile as [$file, $classMethods]) {
128-
$currentFileProvider->setFile($file);
173+
$changedCount = 0;
174+
foreach ($classMethodsByFile as [$file, $classMethods]) {
175+
$currentFileProvider->setFile($file);
129176

130-
foreach ($classMethods as $classMethod) {
131-
++$classMethodCount;
132-
if ($rector->refactor($classMethod) instanceof ClassMethod) {
133-
++$changedCount;
177+
foreach ($classMethods as $classMethod) {
178+
if ($rector->refactor($classMethod) instanceof ClassMethod) {
179+
++$changedCount;
180+
}
134181
}
135182
}
136-
}
137183

138-
$cpu = $cpuSeconds() - $cpuStart;
139-
$wall = microtime(true) - $wallStart;
184+
$cpu = $cpuSeconds() - $cpuStart;
185+
$wall = microtime(true) - $wallStart;
186+
187+
printf(
188+
'%s: test classes: %d, test methods: %d, changed: %d%s',
189+
$scenarioName,
190+
count($testFilePaths),
191+
$methodCount,
192+
$changedCount,
193+
PHP_EOL
194+
);
195+
printf('CPU: %.4f s (%.3f ms/method)%s', $cpu, $cpu / $methodCount * 1000, PHP_EOL);
196+
printf('wall: %.4f s%s', $wall, PHP_EOL);
197+
198+
if ($cpu > MAX_CPU_SECONDS) {
199+
printf('CPU %.4f s is over the %.1f s limit%s', $cpu, MAX_CPU_SECONDS, PHP_EOL);
200+
$isFailed = true;
201+
}
202+
203+
}
140204

141205
array_map('unlink', glob($directory . '/*.php'));
142206
@rmdir($directory);
143207

144-
printf('test classes: %d, test methods: %d, changed: %d%s', count($testFilePaths), $classMethodCount, $changedCount, PHP_EOL);
145-
printf('CPU: %.4f s (%.3f ms/method)%s', $cpu, $cpu / $classMethodCount * 1000, PHP_EOL);
146-
printf('wall: %.4f s%s', $wall, PHP_EOL);
147-
148-
if ($cpu > MAX_CPU_SECONDS) {
149-
printf('CPU %.4f s is over the %.1f s limit%s', $cpu, MAX_CPU_SECONDS, PHP_EOL);
150-
exit(1);
151-
}
208+
exit($isFailed ? 1 : 0);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
trait AssertingBeforeTrait
8+
{
9+
#[\PHPUnit\Framework\Attributes\Before]
10+
protected function check(): void
11+
{
12+
$this->assertTrue(true);
13+
}
14+
}
15+
16+
final class TraitBefore extends TestCase
17+
{
18+
use AssertingBeforeTrait;
19+
20+
public function testSomething()
21+
{
22+
$value = 1;
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
abstract class Deep0 extends TestCase { protected function setUp(): void { $this->assertTrue(true); } }
8+
abstract class Deep1 extends Deep0 { protected function setUp(): void { parent::setUp(); } }
9+
abstract class Deep2 extends Deep1 { protected function setUp(): void { parent::setUp(); } }
10+
abstract class Deep3 extends Deep2 { protected function setUp(): void { parent::setUp(); } }
11+
abstract class Deep4 extends Deep3 { protected function setUp(): void { parent::setUp(); } }
12+
abstract class Deep5 extends Deep4 { protected function setUp(): void { parent::setUp(); } }
13+
abstract class Deep6 extends Deep5 { protected function setUp(): void { parent::setUp(); } }
14+
15+
final class DeepTest extends Deep6
16+
{
17+
public function testSomething()
18+
{
19+
$value = 1;
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
abstract class DeepC0 extends TestCase { protected function setUp(): void { $this->assertTrue(true); } }
8+
abstract class DeepC1 extends DeepC0 { protected function setUp(): void { parent::setUp(); } }
9+
abstract class DeepC2 extends DeepC1 { protected function setUp(): void { parent::setUp(); } }
10+
abstract class DeepC3 extends DeepC2 { protected function setUp(): void { parent::setUp(); } }
11+
abstract class DeepC4 extends DeepC3 { protected function setUp(): void { parent::setUp(); } }
12+
abstract class DeepC5 extends DeepC4 { protected function setUp(): void { parent::setUp(); } }
13+
abstract class DeepC6 extends DeepC5 { protected function setUp(): void { parent::setUp(); } }
14+
15+
final class DeepCTest extends DeepC6
16+
{
17+
public function testSomething()
18+
{
19+
$value = 1;
20+
}
21+
}
22+
23+
final class DeepCShallowTest extends DeepC2
24+
{
25+
public function testOther()
26+
{
27+
$other = 2;
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
trait AssertingSetUpTrait
8+
{
9+
protected function setUp(): void
10+
{
11+
$this->assertTrue(true);
12+
}
13+
}
14+
15+
final class TraitSetUp extends TestCase
16+
{
17+
use AssertingSetUpTrait;
18+
19+
public function testSomething()
20+
{
21+
$value = 1;
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Source\TraitWithAssertingSetUp;
7+
8+
final class SkipAssertingSetUpInTraitFromOtherFile extends TestCase
9+
{
10+
use TraitWithAssertingSetUp;
11+
12+
public function testSomething()
13+
{
14+
$value = 5;
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture;
4+
5+
use Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Source\AbstractClassUsingAssertingTrait;
6+
7+
final class SkipAssertingSetUpInTraitOfParent extends AbstractClassUsingAssertingTrait
8+
{
9+
public function testSomething()
10+
{
11+
$value = 5;
12+
}
13+
}

0 commit comments

Comments
 (0)