Skip to content

Commit ecd39d0

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

16 files changed

Lines changed: 665 additions & 95 deletions

‎bench/does_not_perform_assertion_lifecycle_bench.php‎

Lines changed: 117 additions & 70 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
*/
@@ -26,15 +33,30 @@
2633
const FILLER_METHODS = 15; // extra methods per parent class, to make parsing realistic
2734
const MAX_CPU_SECONDS = 1.0; // fails in CI above this
2835

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

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

57+
$directory = sys_get_temp_dir() . '/rector_phpunit_bench_' . getmypid();
58+
@mkdir($directory);
59+
3860
// make generated classes visible to PHPStan reflection
3961
spl_autoload_register(static function (string $class) use ($directory): void {
4062
if (! str_starts_with($class, 'Bench\\')) {
@@ -47,91 +69,116 @@
4769
}
4870
});
4971

50-
$testFilePaths = [];
72+
$isFailed = false;
73+
$scenarioIndex = 0;
5174

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

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

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

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

67-
$writeClass(
68-
$className,
69-
"abstract class {$className} extends {$parentClassName}\n{\n protected \$ready;\n protected \$cleanup;\n\n{$methods}}\n"
70-
);
90+
for ($level = 0; $level < DEPTH; ++$level) {
91+
$className = sprintf('%sAbstractChain%dLevel%d', $prefix, $chain, $level);
7192

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

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

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

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

90-
$rectorConfig = (new LazyContainerFactory())->create();
91-
$rectorConfig->boot();
139+
$rectorConfig = (new LazyContainerFactory())->create();
140+
$rectorConfig->boot();
92141

93-
/** @var TestingParser $testingParser */
94-
$testingParser = $rectorConfig->make(TestingParser::class);
142+
/** @var TestingParser $testingParser */
143+
$testingParser = $rectorConfig->make(TestingParser::class);
95144

96-
/** @var BetterNodeFinder $betterNodeFinder */
97-
$betterNodeFinder = $rectorConfig->make(BetterNodeFinder::class);
145+
/** @var BetterNodeFinder $betterNodeFinder */
146+
$betterNodeFinder = $rectorConfig->make(BetterNodeFinder::class);
98147

99-
$rector = $rectorConfig->make(AddDoesNotPerformAssertionToNonAssertingTestRector::class);
148+
$rector = $rectorConfig->make(AddDoesNotPerformAssertionToNonAssertingTestRector::class);
100149

101-
// parse test files up front, parent files are left for the rule to resolve
102-
$classes = [];
103-
foreach ($testFilePaths as $testFilePath) {
104-
$stmts = $testingParser->parseFileToDecoratedNodes($testFilePath);
105-
$classes = [...$classes, ...$betterNodeFinder->findInstanceOf($stmts, Class_::class)];
106-
}
150+
// parse test files up front, parent files are left for the rule to resolve
151+
$classes = [];
152+
foreach ($testFilePaths as $testFilePath) {
153+
$stmts = $testingParser->parseFileToDecoratedNodes($testFilePath);
154+
$classes = [...$classes, ...$betterNodeFinder->findInstanceOf($stmts, Class_::class)];
155+
}
107156

108-
$cpuSeconds = static function (): float {
109-
$usage = getrusage();
110-
return $usage['ru_utime.tv_sec'] + $usage['ru_utime.tv_usec'] / 1_000_000
111-
+ $usage['ru_stime.tv_sec'] + $usage['ru_stime.tv_usec'] / 1_000_000;
112-
};
157+
$wallStart = microtime(true);
158+
$cpuStart = $cpuSeconds();
113159

114-
$wallStart = microtime(true);
115-
$cpuStart = $cpuSeconds();
160+
$changedCount = 0;
161+
foreach ($classes as $class) {
162+
if ($rector->refactor($class) instanceof Class_) {
163+
++$changedCount;
164+
}
165+
}
166+
167+
$cpu = $cpuSeconds() - $cpuStart;
168+
$wall = microtime(true) - $wallStart;
169+
170+
printf('%s: test classes: %d, changed: %d%s', $scenarioName, count($classes), $changedCount, PHP_EOL);
171+
printf('CPU: %.4f s (%.3f ms/class)%s', $cpu, $cpu / count($classes) * 1000, PHP_EOL);
172+
printf('wall: %.4f s%s', $wall, PHP_EOL);
116173

117-
$changedCount = 0;
118-
foreach ($classes as $class) {
119-
if ($rector->refactor($class) instanceof Class_) {
120-
++$changedCount;
174+
if ($cpu > MAX_CPU_SECONDS) {
175+
printf('CPU %.4f s is over the %.1f s limit%s', $cpu, MAX_CPU_SECONDS, PHP_EOL);
176+
$isFailed = true;
121177
}
122-
}
123178

124-
$cpu = $cpuSeconds() - $cpuStart;
125-
$wall = microtime(true) - $wallStart;
179+
}
126180

127181
array_map('unlink', glob($directory . '/*.php'));
128182
@rmdir($directory);
129183

130-
printf('test classes: %d, changed: %d%s', count($classes), $changedCount, PHP_EOL);
131-
printf('CPU: %.4f s (%.3f ms/class)%s', $cpu, $cpu / count($classes) * 1000, PHP_EOL);
132-
printf('wall: %.4f s%s', $wall, PHP_EOL);
133-
134-
if ($cpu > MAX_CPU_SECONDS) {
135-
printf('CPU %.4f s is over the %.1f s limit%s', $cpu, MAX_CPU_SECONDS, PHP_EOL);
136-
exit(1);
137-
}
184+
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+
}
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\AbstractClassWithAssertingTearDown;
6+
7+
class SkipAssertingTearDownInParent extends AbstractClassWithAssertingTearDown
8+
{
9+
public function testSomething()
10+
{
11+
$value = 5;
12+
}
13+
}

0 commit comments

Comments
 (0)