Skip to content

Commit 9545637

Browse files
authored
[PHPUnit60] Never parse PHPUnit and Symfony base test case methods in AssertCallAnalyzer (#833)
1 parent 82a7251 commit 9545637

7 files changed

Lines changed: 220 additions & 5 deletions

File tree

‎.github/workflows/code_analysis.yaml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ jobs:
4848
name: 'Bench own lifecycle methods'
4949
run: php bench/does_not_perform_assertion_own_lifecycle_bench.php
5050

51+
-
52+
name: 'Bench framework test cases'
53+
run: php bench/does_not_perform_assertion_framework_test_case_bench.php
54+
5155
name: ${{ matrix.actions.name }}
5256
runs-on: ubuntu-latest
5357
timeout-minutes: 10
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Benchmark for AddDoesNotPerformAssertionToNonAssertingTestRector on calls into PHPUnit's own classes.
7+
*
8+
* Every test class extends a base class without setUp(), so "parent::setUp()" lands in PHPUnit's TestCase, and its
9+
* tests call "$this->markTestSkipped()", declared in PHPUnit's Assert. These files are huge and must not be parsed.
10+
* Run on both branches to compare. CPU (user+sys) is the reliable metric; wall is noisy.
11+
*/
12+
13+
use PhpParser\Node\Stmt\Class_;
14+
use Rector\DependencyInjection\LazyContainerFactory;
15+
use Rector\PhpParser\Node\BetterNodeFinder;
16+
use Rector\PHPUnit\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector;
17+
use Rector\Testing\TestingParser\TestingParser;
18+
19+
require __DIR__ . '/../vendor/autoload.php';
20+
21+
const TEST_CLASSES = 300;
22+
const TEST_METHODS = 6; // non-asserting test methods per test class
23+
const MAX_CPU_SECONDS = 1.0; // fails in CI above this
24+
25+
$directory = sys_get_temp_dir() . '/rector_phpunit_framework_test_case_bench_' . getmypid();
26+
@mkdir($directory);
27+
28+
// make generated classes visible to PHPStan reflection
29+
spl_autoload_register(static function (string $class) use ($directory): void {
30+
if (! str_starts_with($class, 'Bench\\')) {
31+
return;
32+
}
33+
34+
$filePath = $directory . '/' . substr($class, 6) . '.php';
35+
if (is_file($filePath)) {
36+
require_once $filePath;
37+
}
38+
});
39+
40+
// the base class does not override setUp(), so calls to it resolve to PHPUnit's TestCase
41+
file_put_contents(
42+
$directory . '/AbstractBaseTestCase.php',
43+
"<?php\n\nnamespace Bench;\n\nabstract class AbstractBaseTestCase extends \\PHPUnit\\Framework\\TestCase\n{\n protected \$ready;\n}\n"
44+
);
45+
46+
$testFilePaths = [];
47+
48+
for ($test = 0; $test < TEST_CLASSES; ++$test) {
49+
$className = sprintf('FrameworkTest%d', $test);
50+
51+
$methods = " protected function setUp(): void\n {\n parent::setUp();\n \$this->ready = true;\n }\n\n";
52+
for ($testMethod = 0; $testMethod < TEST_METHODS; ++$testMethod) {
53+
$methods .= " public function testSomething{$testMethod}(): void\n {\n \$this->markTestSkipped('later');\n }\n\n";
54+
}
55+
56+
$filePath = $directory . '/' . $className . '.php';
57+
file_put_contents(
58+
$filePath,
59+
"<?php\n\nnamespace Bench;\n\nfinal class {$className} extends AbstractBaseTestCase\n{\n{$methods}}\n"
60+
);
61+
62+
$testFilePaths[] = $filePath;
63+
}
64+
65+
$rectorConfig = (new LazyContainerFactory())->create();
66+
$rectorConfig->boot();
67+
68+
/** @var TestingParser $testingParser */
69+
$testingParser = $rectorConfig->make(TestingParser::class);
70+
71+
/** @var BetterNodeFinder $betterNodeFinder */
72+
$betterNodeFinder = $rectorConfig->make(BetterNodeFinder::class);
73+
74+
$rector = $rectorConfig->make(AddDoesNotPerformAssertionToNonAssertingTestRector::class);
75+
76+
$classes = [];
77+
foreach ($testFilePaths as $testFilePath) {
78+
$stmts = $testingParser->parseFileToDecoratedNodes($testFilePath);
79+
$classes = [...$classes, ...$betterNodeFinder->findInstanceOf($stmts, Class_::class)];
80+
}
81+
82+
$cpuSeconds = static function (): float {
83+
$usage = getrusage();
84+
return $usage['ru_utime.tv_sec'] + $usage['ru_utime.tv_usec'] / 1_000_000
85+
+ $usage['ru_stime.tv_sec'] + $usage['ru_stime.tv_usec'] / 1_000_000;
86+
};
87+
88+
$wallStart = microtime(true);
89+
$cpuStart = $cpuSeconds();
90+
91+
$changedCount = 0;
92+
foreach ($classes as $class) {
93+
if ($rector->refactor($class) instanceof Class_) {
94+
++$changedCount;
95+
}
96+
}
97+
98+
$cpu = $cpuSeconds() - $cpuStart;
99+
$wall = microtime(true) - $wallStart;
100+
101+
array_map('unlink', glob($directory . '/*.php'));
102+
@rmdir($directory);
103+
104+
printf('test classes: %d, changed: %d%s', count($classes), $changedCount, PHP_EOL);
105+
printf('CPU: %.4f s (%.3f ms/class)%s', $cpu, $cpu / count($classes) * 1000, PHP_EOL);
106+
printf('wall: %.4f s%s', $wall, PHP_EOL);
107+
108+
if ($cpu > MAX_CPU_SECONDS) {
109+
printf('CPU %.4f s is over the %.1f s limit%s', $cpu, MAX_CPU_SECONDS, PHP_EOL);
110+
exit(1);
111+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture;
4+
5+
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
6+
7+
final class KernelTestCaseParentSetUp extends WebTestCase
8+
{
9+
protected function setUp(): void
10+
{
11+
parent::setUp();
12+
}
13+
14+
public function testSomething()
15+
{
16+
$value = 1;
17+
}
18+
}
19+
20+
?>
21+
-----
22+
<?php
23+
24+
namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture;
25+
26+
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
27+
28+
final class KernelTestCaseParentSetUp extends WebTestCase
29+
{
30+
protected function setUp(): void
31+
{
32+
parent::setUp();
33+
}
34+
35+
#[\PHPUnit\Framework\Attributes\DoesNotPerformAssertions]
36+
public function testSomething()
37+
{
38+
$value = 1;
39+
}
40+
}
41+
42+
?>

‎src/Enum/PHPUnitClassName.php‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,27 @@ final class PHPUnitClassName
3434

3535
public const string SYMFONY_TYPE_TEST_CASE = 'Symfony\Component\Form\Test\TypeTestCase';
3636

37+
public const string SYMFONY_KERNEL_TEST_CASE = 'Symfony\Bundle\FrameworkBundle\Test\KernelTestCase';
38+
39+
public const string SYMFONY_WEB_TEST_CASE = 'Symfony\Bundle\FrameworkBundle\Test\WebTestCase';
40+
3741
public const string TWIG_INTEGRATION_TEST_CASE = 'Twig\Test\IntegrationTestCase';
3842

3943
/**
4044
* @var string[]
4145
*/
4246
public const array TEST_CLASSES = [self::TEST_CASE, self::TEST_CASE_LEGACY];
47+
48+
/**
49+
* Framework base test classes, their methods do not count as test assertions and their files are huge to parse
50+
*
51+
* @var string[]
52+
*/
53+
public const array FRAMEWORK_TEST_CASES = [
54+
self::TEST_CASE,
55+
self::ASSERT,
56+
self::SYMFONY_KERNEL_TEST_CASE,
57+
self::SYMFONY_WEB_TEST_CASE,
58+
self::SYMFONY_TYPE_TEST_CASE,
59+
];
4360
}

‎src/NodeAnalyzer/AssertCallAnalyzer.php‎

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PhpParser\Node\Stmt\ClassMethod;
1313
use PHPStan\Analyser\Scope;
1414
use PHPStan\Reflection\ClassReflection;
15+
use PHPStan\Reflection\ReflectionProvider;
1516
use PHPStan\Type\TypeWithClassName;
1617
use Rector\Enum\ObjectReference;
1718
use Rector\NodeNameResolver\NodeNameResolver;
@@ -61,6 +62,7 @@ public function __construct(
6162
private readonly BetterNodeFinder $betterNodeFinder,
6263
private readonly NodeNameResolver $nodeNameResolver,
6364
private readonly NodeTypeResolver $nodeTypeResolver,
65+
private readonly ReflectionProvider $reflectionProvider,
6466
) {
6567
}
6668

@@ -221,9 +223,32 @@ private function resolveClassMethodFromCall(StaticCall|MethodCall $call): ?Class
221223
return null;
222224
}
223225

226+
// e.g. "parent::setUp()" declared in PHPUnit or Symfony, no need to parse their huge classes
227+
if ($this->isDeclaredInFrameworkTestCase($className, $methodName)) {
228+
return null;
229+
}
230+
224231
return $this->astResolver->resolveClassMethod($className, $methodName);
225232
}
226233

234+
private function isDeclaredInFrameworkTestCase(string $className, string $methodName): bool
235+
{
236+
if (! $this->reflectionProvider->hasClass($className)) {
237+
return false;
238+
}
239+
240+
$classReflection = $this->reflectionProvider->getClass($className);
241+
if (! $classReflection->hasNativeMethod($methodName)) {
242+
return false;
243+
}
244+
245+
$declaringClassName = $classReflection->getNativeMethod($methodName)
246+
->getDeclaringClass()
247+
->getName();
248+
249+
return in_array($declaringClassName, PHPUnitClassName::FRAMEWORK_TEST_CASES, true);
250+
}
251+
227252
private function resolveOwnClassName(ClassMethod $classMethod): string
228253
{
229254
$classReflection = $this->resolveClassReflection($classMethod);
@@ -243,11 +268,6 @@ private function resolveClassName(StaticCall|MethodCall $call): ?string
243268
return null;
244269
}
245270

246-
// PHPUnit itself has no asserting test methods, no need to parse its huge classes
247-
if ($parentClassReflection->getName() === PHPUnitClassName::TEST_CASE) {
248-
return null;
249-
}
250-
251271
return $parentClassReflection->getName();
252272
}
253273

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Symfony\Bundle\FrameworkBundle\Test;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
abstract class KernelTestCase extends TestCase
8+
{
9+
protected function setUp(): void
10+
{
11+
// asserts on purpose, to prove the file is never parsed
12+
$this->assertTrue(true);
13+
}
14+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Symfony\Bundle\FrameworkBundle\Test;
4+
5+
abstract class WebTestCase extends KernelTestCase
6+
{
7+
}

0 commit comments

Comments
 (0)