Skip to content

Commit c87ef42

Browse files
authored
[CodeQuality] Skip test method via #[Test] Attribute on DataProviderArrayItemsNewLinedRector (#664)
* [CodeQuality] Skip test method via #[Test] Attribute * fix
1 parent 1aedbbc commit c87ef42

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\DataProviderArrayItemsNewlinedRector\Fixture;
4+
5+
use PHPUnit\Framework\Attributes\Test;
6+
use PHPUnit\Framework\TestCase;
7+
8+
final class SkipTestMethod extends TestCase
9+
{
10+
#[Test]
11+
public function run(): void
12+
{
13+
$importer = self::createMock(Importer::class);
14+
$importer->expects($invokedCount)
15+
->method('processFiles')
16+
->willReturnCallback(function (array $parameters) use ($invokedCount): array {
17+
switch ($invokedCount->numberOfInvocations()) {
18+
case 1:
19+
self::assertSame(range(1, 250), $parameters);
20+
21+
return [233, [], []];
22+
case 2:
23+
self::assertSame(range(251, 500), $parameters);
24+
25+
return [15, [], []];
26+
27+
default:
28+
throw new \OutOfBoundsException('Did not expect this to run thrice.');
29+
}
30+
});
31+
}
32+
}

rules/CodeQuality/Rector/ClassMethod/DataProviderArrayItemsNewLinedRector.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
use PhpParser\Node\Stmt\ClassMethod;
1010
use PhpParser\Node\Stmt\Return_;
1111
use Rector\NodeTypeResolver\Node\AttributeKey;
12+
use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer;
1213
use Rector\PhpParser\Node\BetterNodeFinder;
14+
use Rector\PHPUnit\Enum\PHPUnitAttribute;
1315
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
1416
use Rector\Rector\AbstractRector;
1517
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@@ -22,7 +24,8 @@ final class DataProviderArrayItemsNewLinedRector extends AbstractRector
2224
{
2325
public function __construct(
2426
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
25-
private readonly BetterNodeFinder $betterNodeFinder
27+
private readonly BetterNodeFinder $betterNodeFinder,
28+
private readonly PhpAttributeAnalyzer $phpAttributeAnalyzer
2629
) {
2730
}
2831

@@ -96,7 +99,8 @@ public function refactor(Node $node): ?Node
9699
}
97100

98101
// skip test methods
99-
if (str_starts_with($node->name->toString(), 'test')) {
102+
if (str_starts_with($node->name->toString(), 'test')
103+
|| $this->phpAttributeAnalyzer->hasPhpAttribute($node, PHPUnitAttribute::TEST)) {
100104
return null;
101105
}
102106

0 commit comments

Comments
 (0)