Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/code_analysis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
111 changes: 111 additions & 0 deletions bench/does_not_perform_assertion_framework_test_case_bench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

declare(strict_types=1);

/**
* Benchmark for AddDoesNotPerformAssertionToNonAssertingTestRector on calls into PHPUnit's own classes.
*
* Every test class extends a base class without setUp(), so "parent::setUp()" lands in PHPUnit's TestCase, and its
* tests call "$this->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',
"<?php\n\nnamespace Bench;\n\nabstract class AbstractBaseTestCase extends \\PHPUnit\\Framework\\TestCase\n{\n protected \$ready;\n}\n"
);

$testFilePaths = [];

for ($test = 0; $test < TEST_CLASSES; ++$test) {
$className = sprintf('FrameworkTest%d', $test);

$methods = " protected function setUp(): void\n {\n parent::setUp();\n \$this->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,
"<?php\n\nnamespace Bench;\n\nfinal class {$className} extends AbstractBaseTestCase\n{\n{$methods}}\n"
);

$testFilePaths[] = $filePath;
}

$rectorConfig = (new LazyContainerFactory())->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);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

final class KernelTestCaseParentSetUp extends WebTestCase
{
protected function setUp(): void
{
parent::setUp();
}

public function testSomething()
{
$value = 1;
}
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

final class KernelTestCaseParentSetUp extends WebTestCase
{
protected function setUp(): void
{
parent::setUp();
}

#[\PHPUnit\Framework\Attributes\DoesNotPerformAssertions]
public function testSomething()
{
$value = 1;
}
}

?>
17 changes: 17 additions & 0 deletions src/Enum/PHPUnitClassName.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
}
30 changes: 25 additions & 5 deletions src/NodeAnalyzer/AssertCallAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -61,6 +62,7 @@ public function __construct(
private readonly BetterNodeFinder $betterNodeFinder,
private readonly NodeNameResolver $nodeNameResolver,
private readonly NodeTypeResolver $nodeTypeResolver,
private readonly ReflectionProvider $reflectionProvider,
) {
}

Expand Down Expand Up @@ -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);
Expand All @@ -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();
}

Expand Down
14 changes: 14 additions & 0 deletions stubs/Symfony/Bundle/FrameworkBundle/Test/KernelTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Symfony\Bundle\FrameworkBundle\Test;

use PHPUnit\Framework\TestCase;

abstract class KernelTestCase extends TestCase
{
protected function setUp(): void
{
// asserts on purpose, to prove the file is never parsed
$this->assertTrue(true);
}
}
7 changes: 7 additions & 0 deletions stubs/Symfony/Bundle/FrameworkBundle/Test/WebTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Symfony\Bundle\FrameworkBundle\Test;

abstract class WebTestCase extends KernelTestCase
{
}
Loading