diff --git a/.github/workflows/code_analysis.yaml b/.github/workflows/code_analysis.yaml index f3cbc44b..6da13444 100644 --- a/.github/workflows/code_analysis.yaml +++ b/.github/workflows/code_analysis.yaml @@ -48,6 +48,10 @@ jobs: name: 'Bench own lifecycle methods' run: php bench/does_not_perform_assertion_own_lifecycle_bench.php + - + name: 'Bench framework test cases' + run: php bench/does_not_perform_assertion_framework_test_case_bench.php + name: ${{ matrix.actions.name }} runs-on: ubuntu-latest timeout-minutes: 10 diff --git a/bench/does_not_perform_assertion_framework_test_case_bench.php b/bench/does_not_perform_assertion_framework_test_case_bench.php new file mode 100644 index 00000000..407c8124 --- /dev/null +++ b/bench/does_not_perform_assertion_framework_test_case_bench.php @@ -0,0 +1,111 @@ +markTestSkipped()", declared in PHPUnit's Assert. These files are huge and must not be parsed. + * Run on both branches to compare. CPU (user+sys) is the reliable metric; wall is noisy. + */ + +use PhpParser\Node\Stmt\Class_; +use Rector\DependencyInjection\LazyContainerFactory; +use Rector\PhpParser\Node\BetterNodeFinder; +use Rector\PHPUnit\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector; +use Rector\Testing\TestingParser\TestingParser; + +require __DIR__ . '/../vendor/autoload.php'; + +const TEST_CLASSES = 300; +const TEST_METHODS = 6; // non-asserting test methods per test class +const MAX_CPU_SECONDS = 1.0; // fails in CI above this + +$directory = sys_get_temp_dir() . '/rector_phpunit_framework_test_case_bench_' . getmypid(); +@mkdir($directory); + +// make generated classes visible to PHPStan reflection +spl_autoload_register(static function (string $class) use ($directory): void { + if (! str_starts_with($class, 'Bench\\')) { + return; + } + + $filePath = $directory . '/' . substr($class, 6) . '.php'; + if (is_file($filePath)) { + require_once $filePath; + } +}); + +// the base class does not override setUp(), so calls to it resolve to PHPUnit's TestCase +file_put_contents( + $directory . '/AbstractBaseTestCase.php', + "ready = true;\n }\n\n"; + for ($testMethod = 0; $testMethod < TEST_METHODS; ++$testMethod) { + $methods .= " public function testSomething{$testMethod}(): void\n {\n \$this->markTestSkipped('later');\n }\n\n"; + } + + $filePath = $directory . '/' . $className . '.php'; + file_put_contents( + $filePath, + "create(); +$rectorConfig->boot(); + +/** @var TestingParser $testingParser */ +$testingParser = $rectorConfig->make(TestingParser::class); + +/** @var BetterNodeFinder $betterNodeFinder */ +$betterNodeFinder = $rectorConfig->make(BetterNodeFinder::class); + +$rector = $rectorConfig->make(AddDoesNotPerformAssertionToNonAssertingTestRector::class); + +$classes = []; +foreach ($testFilePaths as $testFilePath) { + $stmts = $testingParser->parseFileToDecoratedNodes($testFilePath); + $classes = [...$classes, ...$betterNodeFinder->findInstanceOf($stmts, Class_::class)]; +} + +$cpuSeconds = static function (): float { + $usage = getrusage(); + return $usage['ru_utime.tv_sec'] + $usage['ru_utime.tv_usec'] / 1_000_000 + + $usage['ru_stime.tv_sec'] + $usage['ru_stime.tv_usec'] / 1_000_000; +}; + +$wallStart = microtime(true); +$cpuStart = $cpuSeconds(); + +$changedCount = 0; +foreach ($classes as $class) { + if ($rector->refactor($class) instanceof Class_) { + ++$changedCount; + } +} + +$cpu = $cpuSeconds() - $cpuStart; +$wall = microtime(true) - $wallStart; + +array_map('unlink', glob($directory . '/*.php')); +@rmdir($directory); + +printf('test classes: %d, changed: %d%s', count($classes), $changedCount, PHP_EOL); +printf('CPU: %.4f s (%.3f ms/class)%s', $cpu, $cpu / count($classes) * 1000, PHP_EOL); +printf('wall: %.4f s%s', $wall, PHP_EOL); + +if ($cpu > MAX_CPU_SECONDS) { + printf('CPU %.4f s is over the %.1f s limit%s', $cpu, MAX_CPU_SECONDS, PHP_EOL); + exit(1); +} diff --git a/rules-tests/PHPUnit60/Rector/ClassMethod/AddDoesNotPerformAssertionToNonAssertingTestRector/Fixture/kernel_test_case_parent_set_up.php.inc b/rules-tests/PHPUnit60/Rector/ClassMethod/AddDoesNotPerformAssertionToNonAssertingTestRector/Fixture/kernel_test_case_parent_set_up.php.inc new file mode 100644 index 00000000..4c29ef51 --- /dev/null +++ b/rules-tests/PHPUnit60/Rector/ClassMethod/AddDoesNotPerformAssertionToNonAssertingTestRector/Fixture/kernel_test_case_parent_set_up.php.inc @@ -0,0 +1,42 @@ + +----- + diff --git a/src/Enum/PHPUnitClassName.php b/src/Enum/PHPUnitClassName.php index 83044c73..021451ce 100644 --- a/src/Enum/PHPUnitClassName.php +++ b/src/Enum/PHPUnitClassName.php @@ -34,10 +34,27 @@ final class PHPUnitClassName public const string SYMFONY_TYPE_TEST_CASE = 'Symfony\Component\Form\Test\TypeTestCase'; + public const string SYMFONY_KERNEL_TEST_CASE = 'Symfony\Bundle\FrameworkBundle\Test\KernelTestCase'; + + public const string SYMFONY_WEB_TEST_CASE = 'Symfony\Bundle\FrameworkBundle\Test\WebTestCase'; + public const string TWIG_INTEGRATION_TEST_CASE = 'Twig\Test\IntegrationTestCase'; /** * @var string[] */ public const array TEST_CLASSES = [self::TEST_CASE, self::TEST_CASE_LEGACY]; + + /** + * Framework base test classes, their methods do not count as test assertions and their files are huge to parse + * + * @var string[] + */ + public const array FRAMEWORK_TEST_CASES = [ + self::TEST_CASE, + self::ASSERT, + self::SYMFONY_KERNEL_TEST_CASE, + self::SYMFONY_WEB_TEST_CASE, + self::SYMFONY_TYPE_TEST_CASE, + ]; } diff --git a/src/NodeAnalyzer/AssertCallAnalyzer.php b/src/NodeAnalyzer/AssertCallAnalyzer.php index 678321ad..649fc6fe 100644 --- a/src/NodeAnalyzer/AssertCallAnalyzer.php +++ b/src/NodeAnalyzer/AssertCallAnalyzer.php @@ -12,6 +12,7 @@ use PhpParser\Node\Stmt\ClassMethod; use PHPStan\Analyser\Scope; use PHPStan\Reflection\ClassReflection; +use PHPStan\Reflection\ReflectionProvider; use PHPStan\Type\TypeWithClassName; use Rector\Enum\ObjectReference; use Rector\NodeNameResolver\NodeNameResolver; @@ -61,6 +62,7 @@ public function __construct( private readonly BetterNodeFinder $betterNodeFinder, private readonly NodeNameResolver $nodeNameResolver, private readonly NodeTypeResolver $nodeTypeResolver, + private readonly ReflectionProvider $reflectionProvider, ) { } @@ -221,9 +223,32 @@ private function resolveClassMethodFromCall(StaticCall|MethodCall $call): ?Class return null; } + // e.g. "parent::setUp()" declared in PHPUnit or Symfony, no need to parse their huge classes + if ($this->isDeclaredInFrameworkTestCase($className, $methodName)) { + return null; + } + return $this->astResolver->resolveClassMethod($className, $methodName); } + private function isDeclaredInFrameworkTestCase(string $className, string $methodName): bool + { + if (! $this->reflectionProvider->hasClass($className)) { + return false; + } + + $classReflection = $this->reflectionProvider->getClass($className); + if (! $classReflection->hasNativeMethod($methodName)) { + return false; + } + + $declaringClassName = $classReflection->getNativeMethod($methodName) + ->getDeclaringClass() + ->getName(); + + return in_array($declaringClassName, PHPUnitClassName::FRAMEWORK_TEST_CASES, true); + } + private function resolveOwnClassName(ClassMethod $classMethod): string { $classReflection = $this->resolveClassReflection($classMethod); @@ -243,11 +268,6 @@ private function resolveClassName(StaticCall|MethodCall $call): ?string return null; } - // PHPUnit itself has no asserting test methods, no need to parse its huge classes - if ($parentClassReflection->getName() === PHPUnitClassName::TEST_CASE) { - return null; - } - return $parentClassReflection->getName(); } diff --git a/stubs/Symfony/Bundle/FrameworkBundle/Test/KernelTestCase.php b/stubs/Symfony/Bundle/FrameworkBundle/Test/KernelTestCase.php new file mode 100644 index 00000000..549e451d --- /dev/null +++ b/stubs/Symfony/Bundle/FrameworkBundle/Test/KernelTestCase.php @@ -0,0 +1,14 @@ +assertTrue(true); + } +} diff --git a/stubs/Symfony/Bundle/FrameworkBundle/Test/WebTestCase.php b/stubs/Symfony/Bundle/FrameworkBundle/Test/WebTestCase.php new file mode 100644 index 00000000..b46d29bc --- /dev/null +++ b/stubs/Symfony/Bundle/FrameworkBundle/Test/WebTestCase.php @@ -0,0 +1,7 @@ +