Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 8 additions & 0 deletions src/Rules/PHPUnit/AssertEqualsIsDiscouragedRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ public function processNode(Node $node, Scope $scope): array
&& ($leftType->isSuperTypeOf($rightType)->yes())
&& ($rightType->isSuperTypeOf($leftType)->yes())
) {
if (strtolower($node->name->name) === 'assertnotequals') {
return [
RuleErrorBuilder::message(
'You should use assertNotSame() instead of assertNotEquals(), because both values are scalars of the same type',
)->identifier('phpunit.assertEquals')->build(),
];
}

return [
RuleErrorBuilder::message(
'You should use assertSame() instead of assertEquals(), because both values are scalars of the same type',
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