Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add non-testcase assert call in FlipAssertRector #446

Merged
merged 1 commit into from
Jan 19, 2025
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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\FlipAssertRector\Fixture;

use PHPUnit\Framework\Assert;

final class CovertExternalStaticCall
{
public function test()
{
$result = '...';
Assert::assertStringContainsString($result, 'expected');
}
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\FlipAssertRector\Fixture;

use PHPUnit\Framework\Assert;

final class CovertExternalStaticCall
{
public function test()
{
$result = '...';
Assert::assertStringContainsString('expected', $result);
}
}

?>
18 changes: 18 additions & 0 deletions src/Enum/PHPUnitClassName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\Enum;

final class PHPUnitClassName
{
/**
* @var string
*/
public const TEST_CASE = 'PHPUnit\Framework\TestCase';

/**
* @var string
*/
public const ASSERT = 'PHPUnit\Framework\Assert';
}
20 changes: 17 additions & 3 deletions src/NodeAnalyzer/TestsNodeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\PHPUnit\Enum\PHPUnitClassName;
use Rector\Reflection\ReflectionResolver;

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

public function isPHPUnitTestCaseCall(Node $node): bool
{
if (! $this->isInTestClass($node)) {
return false;
if ($node instanceof MethodCall) {
return $this->isInTestClass($node);
}

if ($node instanceof StaticCall) {
$classType = $this->nodeTypeResolver->getType($node->class);
if ($classType instanceof ObjectType) {
if ($classType->isInstanceOf(PHPUnitClassName::TEST_CASE)->yes()) {
return true;
}

if ($classType->isInstanceOf(PHPUnitClassName::ASSERT)->yes()) {
return true;
}
}
}

return $node instanceof MethodCall || $node instanceof StaticCall;
return false;
}
}
Loading