Skip to content

Commit d09e152

Browse files
authored
Fix error message for "assertNotEquals" usage referencing "assertSame" and "assertEquals"
1 parent e32ac65 commit d09e152

File tree

3 files changed

+23
-16
lines changed

3 files changed

+23
-16
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ It also contains this strict framework-specific rules (can be enabled separately
2323
* Check that you are not using `assertSame()` with `null` as expected value. `assertNull()` should be used instead.
2424
* Check that you are not using `assertSame()` with `count($variable)` as second parameter. `assertCount($variable)` should be used instead.
2525
* Check that you are not using `assertEquals()` with same types (`assertSame()` should be used)
26+
* Check that you are not using `assertNotEquals()` with same types (`assertNotSame()` should be used)
2627

2728
## How to document mock objects in phpDocs?
2829

src/Rules/PHPUnit/AssertEqualsIsDiscouragedRule.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PHPStan\Type\TypeCombinator;
1212
use function count;
1313
use function in_array;
14+
use function sprintf;
1415
use function strtolower;
1516

1617
/**
@@ -57,7 +58,11 @@ public function processNode(Node $node, Scope $scope): array
5758
) {
5859
return [
5960
RuleErrorBuilder::message(
60-
'You should use assertSame() instead of assertEquals(), because both values are scalars of the same type',
61+
sprintf(
62+
'You should use %s() instead of %s(), because both values are scalars of the same type',
63+
strtolower($node->name->name) === 'assertnotequals' ? 'assertNotSame' : 'assertSame',
64+
$node->name->name,
65+
),
6166
)->identifier('phpunit.assertEquals')->build(),
6267
];
6368
}

tests/Rules/PHPUnit/AssertEqualsIsDiscouragedRuleTest.php

+16-15
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,26 @@
1111
final class AssertEqualsIsDiscouragedRuleTest extends RuleTestCase
1212
{
1313

14-
private const ERROR_MESSAGE = 'You should use assertSame() instead of assertEquals(), because both values are scalars of the same type';
14+
private const ERROR_MESSAGE_EQUALS = 'You should use assertSame() instead of assertEquals(), because both values are scalars of the same type';
15+
private const ERROR_MESSAGE_NOT_EQUALS = 'You should use assertNotSame() instead of assertNotEquals(), because both values are scalars of the same type';
1516

1617
public function testRule(): void
1718
{
1819
$this->analyse([__DIR__ . '/data/assert-equals-is-discouraged.php'], [
19-
[self::ERROR_MESSAGE, 19],
20-
[self::ERROR_MESSAGE, 22],
21-
[self::ERROR_MESSAGE, 23],
22-
[self::ERROR_MESSAGE, 24],
23-
[self::ERROR_MESSAGE, 25],
24-
[self::ERROR_MESSAGE, 26],
25-
[self::ERROR_MESSAGE, 27],
26-
[self::ERROR_MESSAGE, 28],
27-
[self::ERROR_MESSAGE, 29],
28-
[self::ERROR_MESSAGE, 30],
29-
[self::ERROR_MESSAGE, 32],
30-
[self::ERROR_MESSAGE, 37],
31-
[self::ERROR_MESSAGE, 38],
32-
[self::ERROR_MESSAGE, 39],
20+
[self::ERROR_MESSAGE_EQUALS, 19],
21+
[self::ERROR_MESSAGE_EQUALS, 22],
22+
[self::ERROR_MESSAGE_EQUALS, 23],
23+
[self::ERROR_MESSAGE_EQUALS, 24],
24+
[self::ERROR_MESSAGE_EQUALS, 25],
25+
[self::ERROR_MESSAGE_EQUALS, 26],
26+
[self::ERROR_MESSAGE_EQUALS, 27],
27+
[self::ERROR_MESSAGE_EQUALS, 28],
28+
[self::ERROR_MESSAGE_EQUALS, 29],
29+
[self::ERROR_MESSAGE_EQUALS, 30],
30+
[self::ERROR_MESSAGE_EQUALS, 32],
31+
[self::ERROR_MESSAGE_NOT_EQUALS, 37],
32+
[self::ERROR_MESSAGE_NOT_EQUALS, 38],
33+
[self::ERROR_MESSAGE_NOT_EQUALS, 39],
3334
]);
3435
}
3536

0 commit comments

Comments
 (0)