Skip to content

Commit 0d33999

Browse files
Majkl578ondrejmirtes
authored andcommitted
Fix detection of unspecific assert*() with direct Assert::assert*() calls
1 parent 93a78ef commit 0d33999

10 files changed

+32
-5
lines changed

src/Rules/PHPUnit/AssertRuleHelper.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
class AssertRuleHelper
1010
{
1111

12-
public static function isMethodOrStaticCallOnTestCase(Node $node, Scope $scope): bool
12+
public static function isMethodOrStaticCallOnAssert(Node $node, Scope $scope): bool
1313
{
14-
$testCaseType = new ObjectType('PHPUnit\Framework\TestCase');
14+
$testCaseType = new ObjectType('PHPUnit\Framework\Assert');
1515
if ($node instanceof Node\Expr\MethodCall) {
1616
$calledOnType = $scope->getType($node->var);
1717
} elseif ($node instanceof Node\Expr\StaticCall) {

src/Rules/PHPUnit/AssertSameBooleanExpectedRule.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function getNodeType(): string
2121
*/
2222
public function processNode(Node $node, Scope $scope): array
2323
{
24-
if (!AssertRuleHelper::isMethodOrStaticCallOnTestCase($node, $scope)) {
24+
if (!AssertRuleHelper::isMethodOrStaticCallOnAssert($node, $scope)) {
2525
return [];
2626
}
2727

src/Rules/PHPUnit/AssertSameNullExpectedRule.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function getNodeType(): string
2121
*/
2222
public function processNode(Node $node, Scope $scope): array
2323
{
24-
if (!AssertRuleHelper::isMethodOrStaticCallOnTestCase($node, $scope)) {
24+
if (!AssertRuleHelper::isMethodOrStaticCallOnAssert($node, $scope)) {
2525
return [];
2626
}
2727

src/Rules/PHPUnit/AssertSameWithCountRule.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function getNodeType(): string
2020
*/
2121
public function processNode(Node $node, Scope $scope): array
2222
{
23-
if (!AssertRuleHelper::isMethodOrStaticCallOnTestCase($node, $scope)) {
23+
if (!AssertRuleHelper::isMethodOrStaticCallOnAssert($node, $scope)) {
2424
return [];
2525
}
2626

tests/Rules/PHPUnit/AssertSameBooleanExpectedRuleTest.php

+4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ public function testRule(): void
3131
'You should use assertFalse() instead of assertSame() when expecting "false"',
3232
17,
3333
],
34+
[
35+
'You should use assertTrue() instead of assertSame() when expecting "true"',
36+
26,
37+
],
3438
]);
3539
}
3640

tests/Rules/PHPUnit/AssertSameNullExpectedRuleTest.php

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ public function testRule(): void
2323
'You should use assertNull() instead of assertSame(null, $actual).',
2424
13,
2525
],
26+
[
27+
'You should use assertNull() instead of assertSame(null, $actual).',
28+
24,
29+
],
2630
]);
2731
}
2832

tests/Rules/PHPUnit/AssertSameWithCountRuleTest.php

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ public function testRule(): void
1919
'You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, count($variable)).',
2020
10,
2121
],
22+
[
23+
'You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, count($variable)).',
24+
20,
25+
],
2226
]);
2327
}
2428

tests/Rules/PHPUnit/data/assert-same-boolean-expected.php

+5
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,9 @@ public function testAssertSameWithBooleanAsExpected()
2121
$this->assertSame($a, 'b'); // OK
2222
}
2323

24+
public function testAssertSameIsDetectedWithDirectAssertAccess()
25+
{
26+
\PHPUnit\Framework\Assert::assertSame(true, 'foo');
27+
}
28+
2429
}

tests/Rules/PHPUnit/data/assert-same-count.php

+5
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,9 @@ public function testAssertSameWithCountMethodIsOK()
1515
$this->assertSame(5, $this->count()); // OK
1616
}
1717

18+
public function testAssertSameIsDetectedWithDirectAssertAccess()
19+
{
20+
\PHPUnit\Framework\Assert::assertSame(5, count([1, 2, 3]));
21+
}
22+
1823
}

tests/Rules/PHPUnit/data/assert-same-null-expected.php

+5
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,9 @@ public function testAssertSameWithNullAsExpected()
1919
$this->assertSame($c, 'foo'); // nullable is OK
2020
}
2121

22+
public function testAssertSameIsDetectedWithDirectAssertAccess()
23+
{
24+
\PHPUnit\Framework\Assert::assertSame(null, 'foo');
25+
}
26+
2227
}

0 commit comments

Comments
 (0)