Skip to content
Closed
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,31 @@
<?php

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

final class AssertSameNull extends \PHPUnit\Framework\TestCase
{
public function test()
{
$result = '...';
$this->assertSame($result, null);
$this->assertNotSame($result, false);
}
}

?>
-----
<?php

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

final class AssertSameNull extends \PHPUnit\Framework\TestCase
{
public function test()
{
$result = '...';
$this->assertSame(null, $result);
$this->assertNotSame(false, $result);
}
}

?>
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);
}
}

?>
5 changes: 5 additions & 0 deletions rules/CodeQuality/Rector/MethodCall/FlipAssertRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Scalar;
Expand Down Expand Up @@ -119,6 +120,10 @@ private function isScalarValue(Expr $expr): bool
return true;
}

if ($expr instanceof ConstFetch) {
return true;
}

return $expr instanceof ClassConstFetch;
}
}
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