Skip to content

Fix error message for "assertNotEquals" usage referencing "assertSame" and "assertEquals" #220

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
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
Loading