From d09e152f403c843998d7a52b5d87040c937525dd Mon Sep 17 00:00:00 2001 From: PrinsFrank <25006490+PrinsFrank@users.noreply.github.com> Date: Wed, 22 Jan 2025 14:07:38 +0100 Subject: [PATCH] Fix error message for "assertNotEquals" usage referencing "assertSame" and "assertEquals" --- README.md | 1 + .../PHPUnit/AssertEqualsIsDiscouragedRule.php | 7 ++++- .../AssertEqualsIsDiscouragedRuleTest.php | 31 ++++++++++--------- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 526085b..3469379 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ It also contains this strict framework-specific rules (can be enabled separately * Check that you are not using `assertSame()` with `null` as expected value. `assertNull()` should be used instead. * Check that you are not using `assertSame()` with `count($variable)` as second parameter. `assertCount($variable)` should be used instead. * Check that you are not using `assertEquals()` with same types (`assertSame()` should be used) +* Check that you are not using `assertNotEquals()` with same types (`assertNotSame()` should be used) ## How to document mock objects in phpDocs? diff --git a/src/Rules/PHPUnit/AssertEqualsIsDiscouragedRule.php b/src/Rules/PHPUnit/AssertEqualsIsDiscouragedRule.php index f4fc89c..bd685dd 100644 --- a/src/Rules/PHPUnit/AssertEqualsIsDiscouragedRule.php +++ b/src/Rules/PHPUnit/AssertEqualsIsDiscouragedRule.php @@ -11,6 +11,7 @@ use PHPStan\Type\TypeCombinator; use function count; use function in_array; +use function sprintf; use function strtolower; /** @@ -57,7 +58,11 @@ public function processNode(Node $node, Scope $scope): array ) { return [ RuleErrorBuilder::message( - 'You should use assertSame() instead of assertEquals(), because both values are scalars of the same type', + sprintf( + 'You should use %s() instead of %s(), because both values are scalars of the same type', + strtolower($node->name->name) === 'assertnotequals' ? 'assertNotSame' : 'assertSame', + $node->name->name, + ), )->identifier('phpunit.assertEquals')->build(), ]; } diff --git a/tests/Rules/PHPUnit/AssertEqualsIsDiscouragedRuleTest.php b/tests/Rules/PHPUnit/AssertEqualsIsDiscouragedRuleTest.php index f1d34d0..568b5b6 100644 --- a/tests/Rules/PHPUnit/AssertEqualsIsDiscouragedRuleTest.php +++ b/tests/Rules/PHPUnit/AssertEqualsIsDiscouragedRuleTest.php @@ -11,25 +11,26 @@ final class AssertEqualsIsDiscouragedRuleTest extends RuleTestCase { - private const ERROR_MESSAGE = 'You should use assertSame() instead of assertEquals(), because both values are scalars of the same type'; + private const ERROR_MESSAGE_EQUALS = 'You should use assertSame() instead of assertEquals(), because both values are scalars of the same type'; + private const ERROR_MESSAGE_NOT_EQUALS = 'You should use assertNotSame() instead of assertNotEquals(), because both values are scalars of the same type'; public function testRule(): void { $this->analyse([__DIR__ . '/data/assert-equals-is-discouraged.php'], [ - [self::ERROR_MESSAGE, 19], - [self::ERROR_MESSAGE, 22], - [self::ERROR_MESSAGE, 23], - [self::ERROR_MESSAGE, 24], - [self::ERROR_MESSAGE, 25], - [self::ERROR_MESSAGE, 26], - [self::ERROR_MESSAGE, 27], - [self::ERROR_MESSAGE, 28], - [self::ERROR_MESSAGE, 29], - [self::ERROR_MESSAGE, 30], - [self::ERROR_MESSAGE, 32], - [self::ERROR_MESSAGE, 37], - [self::ERROR_MESSAGE, 38], - [self::ERROR_MESSAGE, 39], + [self::ERROR_MESSAGE_EQUALS, 19], + [self::ERROR_MESSAGE_EQUALS, 22], + [self::ERROR_MESSAGE_EQUALS, 23], + [self::ERROR_MESSAGE_EQUALS, 24], + [self::ERROR_MESSAGE_EQUALS, 25], + [self::ERROR_MESSAGE_EQUALS, 26], + [self::ERROR_MESSAGE_EQUALS, 27], + [self::ERROR_MESSAGE_EQUALS, 28], + [self::ERROR_MESSAGE_EQUALS, 29], + [self::ERROR_MESSAGE_EQUALS, 30], + [self::ERROR_MESSAGE_EQUALS, 32], + [self::ERROR_MESSAGE_NOT_EQUALS, 37], + [self::ERROR_MESSAGE_NOT_EQUALS, 38], + [self::ERROR_MESSAGE_NOT_EQUALS, 39], ]); }