Skip to content

Commit

Permalink
Fix error message for "assertNotEquals" usage referencing "assertSame…
Browse files Browse the repository at this point in the history
…" and "assertEquals"
  • Loading branch information
PrinsFrank authored Jan 22, 2025
1 parent e32ac65 commit d09e152
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand Down
7 changes: 6 additions & 1 deletion src/Rules/PHPUnit/AssertEqualsIsDiscouragedRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PHPStan\Type\TypeCombinator;
use function count;
use function in_array;
use function sprintf;
use function strtolower;

/**
Expand Down Expand Up @@ -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(),
];
}
Expand Down
31 changes: 16 additions & 15 deletions tests/Rules/PHPUnit/AssertEqualsIsDiscouragedRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
]);
}

Expand Down

0 comments on commit d09e152

Please sign in to comment.