Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

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

use PHPUnit\Framework\TestCase;

final class FileExists extends TestCase
{
public function test()
{
$this->assertTrue(file_exists($file));
$this->assertFalse(file_exists($file));
}
}

?>
-----
<?php

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

use PHPUnit\Framework\TestCase;

final class FileExists extends TestCase
{
public function test()
{
$this->assertFileExists($file);
$this->assertFileDoesNotExist($file);
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ final class MyTest extends Testing

$this->assertNotIsReadable('...');
$this->assertEmpty('...');
$this->assertFileNotExists('...');
$this->assertFileDoesNotExist('...');
$this->assertDirectoryExists('...');
$this->assertFinite('...');
$this->assertNan('...');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

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

use PHPUnit\Framework\TestCase;

final class IsDir extends TestCase
{
public function test()
{
$this->assertTrue(is_dir($directory));
$this->assertFalse(is_dir($directory));
}
}

?>
-----
<?php

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

use PHPUnit\Framework\TestCase;

final class IsDir extends TestCase
{
public function test()
{
$this->assertDirectoryExists($directory);
$this->assertDirectoryDoesNotExist($directory);
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\StringType;
use Rector\PHPUnit\Enum\PHPUnitClassName;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\PHPUnit\ValueObject\FunctionNameWithAssertMethods;
use Rector\Rector\AbstractRector;
Expand Down Expand Up @@ -42,8 +44,18 @@ final class AssertTrueFalseToSpecificMethodRector extends AbstractRector
'str_contains' => ['str_contains', 'assertStringContainsString', 'assertStringNotContainsString'],
];

/**
* Some assert methods were renamed in newer PHPUnit versions
* @var array<string, string>
*/
private const array RENAMED_ASSERT_METHOD_NAMES = [
'assertFileNotExists' => 'assertFileDoesNotExist',
'assertDirectoryNotExists' => 'assertDirectoryDoesNotExist',
];

public function __construct(
private readonly TestsNodeAnalyzer $testsNodeAnalyzer
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
private readonly ReflectionProvider $reflectionProvider
) {
}

Expand Down Expand Up @@ -145,7 +157,9 @@ private function renameMethod(
$oldMethodName = $identifierNode->toString();

if (in_array($oldMethodName, ['assertTrue', 'assertNotFalse'], true)) {
$node->name = new Identifier($functionNameWithAssertMethods->getAssetMethodName());
$node->name = new Identifier(
$this->resolveExistingAssertMethodName($functionNameWithAssertMethods->getAssetMethodName())
);
}

if ($functionNameWithAssertMethods->getNotAssertMethodName() === '') {
Expand All @@ -156,7 +170,27 @@ private function renameMethod(
return;
}

$node->name = new Identifier($functionNameWithAssertMethods->getNotAssertMethodName());
$node->name = new Identifier(
$this->resolveExistingAssertMethodName($functionNameWithAssertMethods->getNotAssertMethodName())
);
}

/**
* Verify the assert method exists on the PHPUnit Assert class,
* as some methods were renamed in newer PHPUnit versions
*/
private function resolveExistingAssertMethodName(string $methodName): string
{
if (! $this->reflectionProvider->hasClass(PHPUnitClassName::ASSERT)) {
return $methodName;
}

$assertClassReflection = $this->reflectionProvider->getClass(PHPUnitClassName::ASSERT);
if ($assertClassReflection->hasNativeMethod($methodName)) {
return $methodName;
}

return self::RENAMED_ASSERT_METHOD_NAMES[$methodName] ?? $methodName;
}

/**
Expand Down
Loading