diff --git a/rules-tests/CodeQuality/Rector/MethodCall/AssertTrueFalseToSpecificMethodRector/Fixture/file_exists.php.inc b/rules-tests/CodeQuality/Rector/MethodCall/AssertTrueFalseToSpecificMethodRector/Fixture/file_exists.php.inc new file mode 100644 index 00000000..b242270d --- /dev/null +++ b/rules-tests/CodeQuality/Rector/MethodCall/AssertTrueFalseToSpecificMethodRector/Fixture/file_exists.php.inc @@ -0,0 +1,33 @@ +assertTrue(file_exists($file)); + $this->assertFalse(file_exists($file)); + } +} + +?> +----- +assertFileExists($file); + $this->assertFileDoesNotExist($file); + } +} + +?> diff --git a/rules-tests/CodeQuality/Rector/MethodCall/AssertTrueFalseToSpecificMethodRector/Fixture/fixture.php.inc b/rules-tests/CodeQuality/Rector/MethodCall/AssertTrueFalseToSpecificMethodRector/Fixture/fixture.php.inc index d4de65dc..ca255917 100644 --- a/rules-tests/CodeQuality/Rector/MethodCall/AssertTrueFalseToSpecificMethodRector/Fixture/fixture.php.inc +++ b/rules-tests/CodeQuality/Rector/MethodCall/AssertTrueFalseToSpecificMethodRector/Fixture/fixture.php.inc @@ -46,7 +46,7 @@ final class MyTest extends Testing $this->assertNotIsReadable('...'); $this->assertEmpty('...'); - $this->assertFileNotExists('...'); + $this->assertFileDoesNotExist('...'); $this->assertDirectoryExists('...'); $this->assertFinite('...'); $this->assertNan('...'); diff --git a/rules-tests/CodeQuality/Rector/MethodCall/AssertTrueFalseToSpecificMethodRector/Fixture/is_dir.php.inc b/rules-tests/CodeQuality/Rector/MethodCall/AssertTrueFalseToSpecificMethodRector/Fixture/is_dir.php.inc new file mode 100644 index 00000000..99f990a3 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/MethodCall/AssertTrueFalseToSpecificMethodRector/Fixture/is_dir.php.inc @@ -0,0 +1,33 @@ +assertTrue(is_dir($directory)); + $this->assertFalse(is_dir($directory)); + } +} + +?> +----- +assertDirectoryExists($directory); + $this->assertDirectoryDoesNotExist($directory); + } +} + +?> diff --git a/rules/CodeQuality/Rector/MethodCall/AssertTrueFalseToSpecificMethodRector.php b/rules/CodeQuality/Rector/MethodCall/AssertTrueFalseToSpecificMethodRector.php index d33d85a8..8d79c653 100644 --- a/rules/CodeQuality/Rector/MethodCall/AssertTrueFalseToSpecificMethodRector.php +++ b/rules/CodeQuality/Rector/MethodCall/AssertTrueFalseToSpecificMethodRector.php @@ -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; @@ -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 + */ + 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 ) { } @@ -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() === '') { @@ -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; } /**