Skip to content

Commit f34c2b2

Browse files
authored
[PHPUnit60] Skip tests in classes with own asserting lifecycle methods in AddDoesNotPerformAssertionToNonAssertingTestRector (#831)
1 parent 745886e commit f34c2b2

18 files changed

Lines changed: 618 additions & 7 deletions

‎.github/workflows/code_analysis.yaml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ jobs:
4444
name: 'Bench abstract classes'
4545
run: php bench/does_not_perform_assertion_abstract_class_bench.php
4646

47+
-
48+
name: 'Bench own lifecycle methods'
49+
run: php bench/does_not_perform_assertion_own_lifecycle_bench.php
50+
4751
name: ${{ matrix.actions.name }}
4852
runs-on: ubuntu-latest
4953
timeout-minutes: 10

‎bench/does_not_perform_assertion_lifecycle_bench.php‎

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
*/
1313

1414
use PhpParser\Node\Stmt\ClassMethod;
15+
use Rector\Application\Provider\CurrentFileProvider;
1516
use Rector\DependencyInjection\LazyContainerFactory;
1617
use Rector\PhpParser\Node\BetterNodeFinder;
1718
use Rector\PHPUnit\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector;
1819
use Rector\Testing\TestingParser\TestingParser;
20+
use Rector\ValueObject\Application\File;
1921

2022
require __DIR__ . '/../vendor/autoload.php';
2123

@@ -98,11 +100,17 @@
98100

99101
$rector = $rectorConfig->make(AddDoesNotPerformAssertionToNonAssertingTestRector::class);
100102

103+
/** @var CurrentFileProvider $currentFileProvider */
104+
$currentFileProvider = $rectorConfig->make(CurrentFileProvider::class);
105+
101106
// parse test files up front, parent files are left for the rule to resolve
102-
$classMethods = [];
107+
// the rule finds the class of a method in the stmts of the current file, as in a real run
108+
$classMethodsByFile = [];
103109
foreach ($testFilePaths as $testFilePath) {
104110
$stmts = $testingParser->parseFileToDecoratedNodes($testFilePath);
105-
$classMethods = [...$classMethods, ...$betterNodeFinder->findInstanceOf($stmts, ClassMethod::class)];
111+
$file = new File($testFilePath, (string) file_get_contents($testFilePath));
112+
$file->hydrateStmtsAndTokens($stmts, $stmts, []);
113+
$classMethodsByFile[] = [$file, $betterNodeFinder->findInstanceOf($stmts, ClassMethod::class)];
106114
}
107115

108116
$cpuSeconds = static function (): float {
@@ -115,9 +123,15 @@
115123
$cpuStart = $cpuSeconds();
116124

117125
$changedCount = 0;
118-
foreach ($classMethods as $classMethod) {
119-
if ($rector->refactor($classMethod) instanceof ClassMethod) {
120-
++$changedCount;
126+
$classMethodCount = 0;
127+
foreach ($classMethodsByFile as [$file, $classMethods]) {
128+
$currentFileProvider->setFile($file);
129+
130+
foreach ($classMethods as $classMethod) {
131+
++$classMethodCount;
132+
if ($rector->refactor($classMethod) instanceof ClassMethod) {
133+
++$changedCount;
134+
}
121135
}
122136
}
123137

@@ -127,8 +141,8 @@
127141
array_map('unlink', glob($directory . '/*.php'));
128142
@rmdir($directory);
129143

130-
printf('test classes: %d, test methods: %d, changed: %d%s', count($testFilePaths), count($classMethods), $changedCount, PHP_EOL);
131-
printf('CPU: %.4f s (%.3f ms/method)%s', $cpu, $cpu / count($classMethods) * 1000, PHP_EOL);
144+
printf('test classes: %d, test methods: %d, changed: %d%s', count($testFilePaths), $classMethodCount, $changedCount, PHP_EOL);
145+
printf('CPU: %.4f s (%.3f ms/method)%s', $cpu, $cpu / $classMethodCount * 1000, PHP_EOL);
132146
printf('wall: %.4f s%s', $wall, PHP_EOL);
133147

134148
if ($cpu > MAX_CPU_SECONDS) {
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Benchmark for AddDoesNotPerformAssertionToNonAssertingTestRector on test classes with own lifecycle methods.
7+
*
8+
* Every test class has its own setUp() and tearDown(), every other one asserts in setUp(). The class is taken from
9+
* the current file, so the rule looks into its lifecycle methods for every test method.
10+
* Run on both branches to compare. CPU (user+sys) is the reliable metric; wall is noisy.
11+
*/
12+
13+
use PhpParser\Node\Stmt\ClassMethod;
14+
use Rector\Application\Provider\CurrentFileProvider;
15+
use Rector\DependencyInjection\LazyContainerFactory;
16+
use Rector\PhpParser\Node\BetterNodeFinder;
17+
use Rector\PHPUnit\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector;
18+
use Rector\Testing\TestingParser\TestingParser;
19+
use Rector\ValueObject\Application\File;
20+
21+
require __DIR__ . '/../vendor/autoload.php';
22+
23+
const TEST_CLASSES = 300; // every other one asserts in setUp()
24+
const TEST_METHODS = 6; // non-asserting test methods per test class
25+
const MAX_CPU_SECONDS = 1.0; // fails in CI above this
26+
27+
$directory = sys_get_temp_dir() . '/rector_phpunit_own_lifecycle_bench_' . getmypid();
28+
@mkdir($directory);
29+
30+
// make generated classes visible to PHPStan reflection
31+
spl_autoload_register(static function (string $class) use ($directory): void {
32+
if (! str_starts_with($class, 'Bench\\')) {
33+
return;
34+
}
35+
36+
$filePath = $directory . '/' . substr($class, 6) . '.php';
37+
if (is_file($filePath)) {
38+
require_once $filePath;
39+
}
40+
});
41+
42+
$testFilePaths = [];
43+
44+
for ($test = 0; $test < TEST_CLASSES; ++$test) {
45+
$isAsserting = $test % 2 === 0;
46+
$className = sprintf('%sTest%d', $isAsserting ? 'Asserting' : 'NonAsserting', $test);
47+
48+
$setUpBody = $isAsserting ? '$this->assertTrue(true);' : '$this->ready = true;';
49+
$methods = " protected function setUp(): void\n {\n parent::setUp();\n {$setUpBody}\n }\n\n";
50+
$methods .= " protected function tearDown(): void\n {\n \$this->ready = null;\n parent::tearDown();\n }\n\n";
51+
for ($testMethod = 0; $testMethod < TEST_METHODS; ++$testMethod) {
52+
$methods .= " public function testSomething{$testMethod}(): void\n {\n \$value = {$testMethod};\n }\n\n";
53+
}
54+
55+
$filePath = $directory . '/' . $className . '.php';
56+
file_put_contents(
57+
$filePath,
58+
"<?php\n\nnamespace Bench;\n\nfinal class {$className} extends \\PHPUnit\\Framework\\TestCase\n{\n protected \$ready;\n\n{$methods}}\n"
59+
);
60+
61+
$testFilePaths[] = $filePath;
62+
}
63+
64+
$rectorConfig = (new LazyContainerFactory())->create();
65+
$rectorConfig->boot();
66+
67+
/** @var TestingParser $testingParser */
68+
$testingParser = $rectorConfig->make(TestingParser::class);
69+
70+
/** @var BetterNodeFinder $betterNodeFinder */
71+
$betterNodeFinder = $rectorConfig->make(BetterNodeFinder::class);
72+
73+
$rector = $rectorConfig->make(AddDoesNotPerformAssertionToNonAssertingTestRector::class);
74+
75+
/** @var CurrentFileProvider $currentFileProvider */
76+
$currentFileProvider = $rectorConfig->make(CurrentFileProvider::class);
77+
78+
// the rule finds the class of a method in the stmts of the current file, as in a real run
79+
$classMethodsByFile = [];
80+
foreach ($testFilePaths as $testFilePath) {
81+
$stmts = $testingParser->parseFileToDecoratedNodes($testFilePath);
82+
$file = new File($testFilePath, (string) file_get_contents($testFilePath));
83+
$file->hydrateStmtsAndTokens($stmts, $stmts, []);
84+
$classMethodsByFile[] = [$file, $betterNodeFinder->findInstanceOf($stmts, ClassMethod::class)];
85+
}
86+
87+
$cpuSeconds = static function (): float {
88+
$usage = getrusage();
89+
return $usage['ru_utime.tv_sec'] + $usage['ru_utime.tv_usec'] / 1_000_000
90+
+ $usage['ru_stime.tv_sec'] + $usage['ru_stime.tv_usec'] / 1_000_000;
91+
};
92+
93+
$wallStart = microtime(true);
94+
$cpuStart = $cpuSeconds();
95+
96+
$changedCount = 0;
97+
$classMethodCount = 0;
98+
foreach ($classMethodsByFile as [$file, $classMethods]) {
99+
$currentFileProvider->setFile($file);
100+
101+
foreach ($classMethods as $classMethod) {
102+
++$classMethodCount;
103+
if ($rector->refactor($classMethod) instanceof ClassMethod) {
104+
++$changedCount;
105+
}
106+
}
107+
}
108+
109+
$cpu = $cpuSeconds() - $cpuStart;
110+
$wall = microtime(true) - $wallStart;
111+
112+
array_map('unlink', glob($directory . '/*.php'));
113+
@rmdir($directory);
114+
115+
printf('test classes: %d, test methods: %d, changed: %d%s', count($testFilePaths), $classMethodCount, $changedCount, PHP_EOL);
116+
printf('CPU: %.4f s (%.3f ms/method)%s', $cpu, $cpu / $classMethodCount * 1000, PHP_EOL);
117+
printf('wall: %.4f s%s', $wall, PHP_EOL);
118+
119+
if ($cpu > MAX_CPU_SECONDS) {
120+
printf('CPU %.4f s is over the %.1f s limit%s', $cpu, MAX_CPU_SECONDS, PHP_EOL);
121+
exit(1);
122+
}
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 PHPUnit\Framework\TestCase;
6+
7+
class NonAssertingSetUp extends TestCase
8+
{
9+
protected function setUp(): void
10+
{
11+
$this->value = 1;
12+
}
13+
14+
public function testSomething()
15+
{
16+
$value = 5;
17+
}
18+
}
19+
20+
?>
21+
-----
22+
<?php
23+
24+
namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture;
25+
26+
use PHPUnit\Framework\TestCase;
27+
28+
class NonAssertingSetUp extends TestCase
29+
{
30+
protected function setUp(): void
31+
{
32+
$this->value = 1;
33+
}
34+
35+
#[\PHPUnit\Framework\Attributes\DoesNotPerformAssertions]
36+
public function testSomething()
37+
{
38+
$value = 5;
39+
}
40+
}
41+
42+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
abstract class ShadowBase extends TestCase
8+
{
9+
protected function setUp(): void
10+
{
11+
$this->assertTrue(true);
12+
}
13+
}
14+
15+
final class ShadowChild extends ShadowBase
16+
{
17+
protected function setUp(): void
18+
{
19+
$value = 1;
20+
}
21+
22+
public function testSomething()
23+
{
24+
$value = 1;
25+
}
26+
}
27+
?>
28+
-----
29+
<?php
30+
31+
namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture;
32+
33+
use PHPUnit\Framework\TestCase;
34+
35+
abstract class ShadowBase extends TestCase
36+
{
37+
protected function setUp(): void
38+
{
39+
$this->assertTrue(true);
40+
}
41+
}
42+
43+
final class ShadowChild extends ShadowBase
44+
{
45+
protected function setUp(): void
46+
{
47+
$value = 1;
48+
}
49+
50+
#[\PHPUnit\Framework\Attributes\DoesNotPerformAssertions]
51+
public function testSomething()
52+
{
53+
$value = 1;
54+
}
55+
}
56+
?>
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+
class SkipAssertingAfterAnnotation extends TestCase
8+
{
9+
/**
10+
* @after
11+
*/
12+
protected function cleanup(): void
13+
{
14+
$this->assertTrue(true);
15+
}
16+
17+
public function testSomething()
18+
{
19+
$value = 5;
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use PHPUnit\Framework\Attributes as PU;
7+
8+
final class AliasBefore extends TestCase
9+
{
10+
#[PU\Before]
11+
protected function check(): void
12+
{
13+
$this->assertTrue(true);
14+
}
15+
16+
public function testSomething()
17+
{
18+
$value = 1;
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture;
4+
5+
use PHPUnit\Framework\Attributes\Before;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class SkipAssertingBeforeAttribute extends TestCase
9+
{
10+
#[Before]
11+
protected function prepare(): void
12+
{
13+
$this->assertTrue(true);
14+
}
15+
16+
public function testSomething()
17+
{
18+
$value = 5;
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class AnonThenHook extends TestCase
8+
{
9+
public function testFirst()
10+
{
11+
$object = new class {
12+
public function run() {}
13+
};
14+
$this->assertNotNull($object);
15+
}
16+
17+
public function testSecond()
18+
{
19+
$value = 1;
20+
}
21+
22+
protected function tearDown(): void
23+
{
24+
$this->assertTrue(true);
25+
}
26+
}

0 commit comments

Comments
 (0)