Skip to content

Commit 745886e

Browse files
authored
[PHPUnit60] Skip test methods in abstract classes in AddDoesNotPerformAssertionToNonAssertingTestRector (#828)
1 parent 282fac4 commit 745886e

5 files changed

Lines changed: 160 additions & 0 deletions

File tree

‎.github/workflows/code_analysis.yaml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ jobs:
4040
name: 'Bench'
4141
run: php bench/does_not_perform_assertion_lifecycle_bench.php
4242

43+
-
44+
name: 'Bench abstract classes'
45+
run: php bench/does_not_perform_assertion_abstract_class_bench.php
46+
4347
name: ${{ matrix.actions.name }}
4448
runs-on: ubuntu-latest
4549
timeout-minutes: 10
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Benchmark for AddDoesNotPerformAssertionToNonAssertingTestRector on abstract and final test classes.
7+
*
8+
* Half of the test classes are abstract, so the abstract class check runs on every test method.
9+
* Run on both branches to compare. CPU (user+sys) is the reliable metric; wall is noisy.
10+
*/
11+
12+
use PhpParser\Node\Stmt\ClassMethod;
13+
use Rector\DependencyInjection\LazyContainerFactory;
14+
use Rector\PhpParser\Node\BetterNodeFinder;
15+
use Rector\PHPUnit\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector;
16+
use Rector\Testing\TestingParser\TestingParser;
17+
18+
require __DIR__ . '/../vendor/autoload.php';
19+
20+
const TEST_CLASSES = 300; // every other one is abstract
21+
const TEST_METHODS = 6; // non-asserting test methods per test class
22+
const MAX_CPU_SECONDS = 1.0; // fails in CI above this
23+
24+
$directory = sys_get_temp_dir() . '/rector_phpunit_abstract_bench_' . getmypid();
25+
@mkdir($directory);
26+
27+
// make generated classes visible to PHPStan reflection
28+
spl_autoload_register(static function (string $class) use ($directory): void {
29+
if (! str_starts_with($class, 'Bench\\')) {
30+
return;
31+
}
32+
33+
$filePath = $directory . '/' . substr($class, 6) . '.php';
34+
if (is_file($filePath)) {
35+
require_once $filePath;
36+
}
37+
});
38+
39+
$testFilePaths = [];
40+
41+
for ($test = 0; $test < TEST_CLASSES; ++$test) {
42+
$isAbstract = $test % 2 === 0;
43+
$className = sprintf('%sTest%d', $isAbstract ? 'Abstract' : 'Final', $test);
44+
45+
$methods = '';
46+
for ($testMethod = 0; $testMethod < TEST_METHODS; ++$testMethod) {
47+
$methods .= " public function testSomething{$testMethod}(): void\n {\n \$value = {$testMethod};\n }\n\n";
48+
}
49+
50+
$filePath = $directory . '/' . $className . '.php';
51+
file_put_contents(
52+
$filePath,
53+
"<?php\n\nnamespace Bench;\n\n" . ($isAbstract ? 'abstract' : 'final')
54+
. " class {$className} extends \PHPUnit\Framework\TestCase\n{\n{$methods}}\n"
55+
);
56+
57+
$testFilePaths[] = $filePath;
58+
}
59+
60+
$rectorConfig = (new LazyContainerFactory())->create();
61+
$rectorConfig->boot();
62+
63+
/** @var TestingParser $testingParser */
64+
$testingParser = $rectorConfig->make(TestingParser::class);
65+
66+
/** @var BetterNodeFinder $betterNodeFinder */
67+
$betterNodeFinder = $rectorConfig->make(BetterNodeFinder::class);
68+
69+
$rector = $rectorConfig->make(AddDoesNotPerformAssertionToNonAssertingTestRector::class);
70+
71+
$classMethods = [];
72+
foreach ($testFilePaths as $testFilePath) {
73+
$stmts = $testingParser->parseFileToDecoratedNodes($testFilePath);
74+
$classMethods = [...$classMethods, ...$betterNodeFinder->findInstanceOf($stmts, ClassMethod::class)];
75+
}
76+
77+
$cpuSeconds = static function (): float {
78+
$usage = getrusage();
79+
return $usage['ru_utime.tv_sec'] + $usage['ru_utime.tv_usec'] / 1_000_000
80+
+ $usage['ru_stime.tv_sec'] + $usage['ru_stime.tv_usec'] / 1_000_000;
81+
};
82+
83+
$wallStart = microtime(true);
84+
$cpuStart = $cpuSeconds();
85+
86+
$changedCount = 0;
87+
foreach ($classMethods as $classMethod) {
88+
if ($rector->refactor($classMethod) instanceof ClassMethod) {
89+
++$changedCount;
90+
}
91+
}
92+
93+
$cpu = $cpuSeconds() - $cpuStart;
94+
$wall = microtime(true) - $wallStart;
95+
96+
array_map('unlink', glob($directory . '/*.php'));
97+
@rmdir($directory);
98+
99+
printf('test classes: %d, test methods: %d, changed: %d%s', count($testFilePaths), count($classMethods), $changedCount, PHP_EOL);
100+
printf('CPU: %.4f s (%.3f ms/method)%s', $cpu, $cpu / count($classMethods) * 1000, PHP_EOL);
101+
printf('wall: %.4f s%s', $wall, PHP_EOL);
102+
103+
if ($cpu > MAX_CPU_SECONDS) {
104+
printf('CPU %.4f s is over the %.1f s limit%s', $cpu, MAX_CPU_SECONDS, PHP_EOL);
105+
exit(1);
106+
}
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 AbstractBase extends TestCase
8+
{
9+
public function testInherited()
10+
{
11+
$value = 1;
12+
}
13+
}
14+
15+
final class AbstractChild extends AbstractBase
16+
{
17+
protected function setUp(): void
18+
{
19+
$this->assertTrue(true);
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
abstract class SkipAbstractClassTestMethod extends TestCase
8+
{
9+
// a child class can add asserting lifecycle methods
10+
public function testSomething()
11+
{
12+
$value = 5;
13+
}
14+
}

‎rules/PHPUnit60/Rector/ClassMethod/AddDoesNotPerformAssertionToNonAssertingTestRector.php‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,11 @@ private function shouldSkipClassMethod(ClassMethod $classMethod): bool
150150
return true;
151151
}
152152

153+
// a child class can add asserting lifecycle methods, that count toward the inherited tests
154+
if ($this->isInAbstractClass($classMethod)) {
155+
return true;
156+
}
157+
153158
// the parent test case asserts in its own integration methods
154159
if ($this->isInTwigIntegrationTestCase($classMethod)) {
155160
return true;
@@ -220,6 +225,16 @@ private function isInTrait(ClassMethod $classMethod): bool
220225
return $classReflection->isTrait();
221226
}
222227

228+
private function isInAbstractClass(ClassMethod $classMethod): bool
229+
{
230+
$classReflection = $this->reflectionResolver->resolveClassReflection($classMethod);
231+
if (! $classReflection instanceof ClassReflection) {
232+
return false;
233+
}
234+
235+
return $classReflection->isAbstract();
236+
}
237+
223238
private function isInTwigIntegrationTestCase(ClassMethod $classMethod): bool
224239
{
225240
$classReflection = $this->reflectionResolver->resolveClassReflection($classMethod);

0 commit comments

Comments
 (0)