From f452fac4d5bf9cac39b827728d19dbb4f96ff4ea Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sun, 19 Jan 2025 18:34:36 +0000 Subject: [PATCH] Add non-testcase assert call in FlipAssertRector --- .../AssertEqualsToSameRector/Fixture/skip.php | 19 ----------- .../cover_external_static_call.php.inc | 33 +++++++++++++++++++ src/Enum/PHPUnitClassName.php | 18 ++++++++++ src/NodeAnalyzer/TestsNodeAnalyzer.php | 20 +++++++++-- 4 files changed, 68 insertions(+), 22 deletions(-) delete mode 100644 rules-tests/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector/Fixture/skip.php create mode 100644 rules-tests/CodeQuality/Rector/MethodCall/FlipAssertRector/Fixture/cover_external_static_call.php.inc create mode 100644 src/Enum/PHPUnitClassName.php diff --git a/rules-tests/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector/Fixture/skip.php b/rules-tests/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector/Fixture/skip.php deleted file mode 100644 index 1a7b0b71..00000000 --- a/rules-tests/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector/Fixture/skip.php +++ /dev/null @@ -1,19 +0,0 @@ -assertEquals($expectedNull, $null); - - $bool = true; - $expectedBool = true; - $this->assertEquals($expectedBool, $bool); - } -} diff --git a/rules-tests/CodeQuality/Rector/MethodCall/FlipAssertRector/Fixture/cover_external_static_call.php.inc b/rules-tests/CodeQuality/Rector/MethodCall/FlipAssertRector/Fixture/cover_external_static_call.php.inc new file mode 100644 index 00000000..b46bac6e --- /dev/null +++ b/rules-tests/CodeQuality/Rector/MethodCall/FlipAssertRector/Fixture/cover_external_static_call.php.inc @@ -0,0 +1,33 @@ + +----- + diff --git a/src/Enum/PHPUnitClassName.php b/src/Enum/PHPUnitClassName.php new file mode 100644 index 00000000..51708c6a --- /dev/null +++ b/src/Enum/PHPUnitClassName.php @@ -0,0 +1,18 @@ +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; } }