Skip to content

Commit 6d04484

Browse files
authored
Add non-testcase assert call in FlipAssertRector (#446)
1 parent 0238d9c commit 6d04484

File tree

4 files changed

+68
-22
lines changed

4 files changed

+68
-22
lines changed

rules-tests/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector/Fixture/skip.php

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\FlipAssertRector\Fixture;
4+
5+
use PHPUnit\Framework\Assert;
6+
7+
final class CovertExternalStaticCall
8+
{
9+
public function test()
10+
{
11+
$result = '...';
12+
Assert::assertStringContainsString($result, 'expected');
13+
}
14+
}
15+
16+
?>
17+
-----
18+
<?php
19+
20+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\FlipAssertRector\Fixture;
21+
22+
use PHPUnit\Framework\Assert;
23+
24+
final class CovertExternalStaticCall
25+
{
26+
public function test()
27+
{
28+
$result = '...';
29+
Assert::assertStringContainsString('expected', $result);
30+
}
31+
}
32+
33+
?>

src/Enum/PHPUnitClassName.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\Enum;
6+
7+
final class PHPUnitClassName
8+
{
9+
/**
10+
* @var string
11+
*/
12+
public const TEST_CASE = 'PHPUnit\Framework\TestCase';
13+
14+
/**
15+
* @var string
16+
*/
17+
public const ASSERT = 'PHPUnit\Framework\Assert';
18+
}

src/NodeAnalyzer/TestsNodeAnalyzer.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
1414
use Rector\NodeNameResolver\NodeNameResolver;
1515
use Rector\NodeTypeResolver\NodeTypeResolver;
16+
use Rector\PHPUnit\Enum\PHPUnitClassName;
1617
use Rector\Reflection\ReflectionResolver;
1718

1819
final readonly class TestsNodeAnalyzer
@@ -96,10 +97,23 @@ public function isPHPUnitMethodCallNames(Node $node, array $names): bool
9697

9798
public function isPHPUnitTestCaseCall(Node $node): bool
9899
{
99-
if (! $this->isInTestClass($node)) {
100-
return false;
100+
if ($node instanceof MethodCall) {
101+
return $this->isInTestClass($node);
102+
}
103+
104+
if ($node instanceof StaticCall) {
105+
$classType = $this->nodeTypeResolver->getType($node->class);
106+
if ($classType instanceof ObjectType) {
107+
if ($classType->isInstanceOf(PHPUnitClassName::TEST_CASE)->yes()) {
108+
return true;
109+
}
110+
111+
if ($classType->isInstanceOf(PHPUnitClassName::ASSERT)->yes()) {
112+
return true;
113+
}
114+
}
101115
}
102116

103-
return $node instanceof MethodCall || $node instanceof StaticCall;
117+
return false;
104118
}
105119
}

0 commit comments

Comments
 (0)