Skip to content

Commit

Permalink
Fix detection of unspecific assert*() with direct Assert::assert*() c…
Browse files Browse the repository at this point in the history
…alls
  • Loading branch information
Majkl578 authored and ondrejmirtes committed May 10, 2019
1 parent 93a78ef commit 0d33999
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/Rules/PHPUnit/AssertRuleHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
class AssertRuleHelper
{

public static function isMethodOrStaticCallOnTestCase(Node $node, Scope $scope): bool
public static function isMethodOrStaticCallOnAssert(Node $node, Scope $scope): bool
{
$testCaseType = new ObjectType('PHPUnit\Framework\TestCase');
$testCaseType = new ObjectType('PHPUnit\Framework\Assert');
if ($node instanceof Node\Expr\MethodCall) {
$calledOnType = $scope->getType($node->var);
} elseif ($node instanceof Node\Expr\StaticCall) {
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/PHPUnit/AssertSameBooleanExpectedRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function getNodeType(): string
*/
public function processNode(Node $node, Scope $scope): array
{
if (!AssertRuleHelper::isMethodOrStaticCallOnTestCase($node, $scope)) {
if (!AssertRuleHelper::isMethodOrStaticCallOnAssert($node, $scope)) {
return [];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Rules/PHPUnit/AssertSameNullExpectedRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function getNodeType(): string
*/
public function processNode(Node $node, Scope $scope): array
{
if (!AssertRuleHelper::isMethodOrStaticCallOnTestCase($node, $scope)) {
if (!AssertRuleHelper::isMethodOrStaticCallOnAssert($node, $scope)) {
return [];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Rules/PHPUnit/AssertSameWithCountRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function getNodeType(): string
*/
public function processNode(Node $node, Scope $scope): array
{
if (!AssertRuleHelper::isMethodOrStaticCallOnTestCase($node, $scope)) {
if (!AssertRuleHelper::isMethodOrStaticCallOnAssert($node, $scope)) {
return [];
}

Expand Down
4 changes: 4 additions & 0 deletions tests/Rules/PHPUnit/AssertSameBooleanExpectedRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public function testRule(): void
'You should use assertFalse() instead of assertSame() when expecting "false"',
17,
],
[
'You should use assertTrue() instead of assertSame() when expecting "true"',
26,
],
]);
}

Expand Down
4 changes: 4 additions & 0 deletions tests/Rules/PHPUnit/AssertSameNullExpectedRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public function testRule(): void
'You should use assertNull() instead of assertSame(null, $actual).',
13,
],
[
'You should use assertNull() instead of assertSame(null, $actual).',
24,
],
]);
}

Expand Down
4 changes: 4 additions & 0 deletions tests/Rules/PHPUnit/AssertSameWithCountRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ public function testRule(): void
'You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, count($variable)).',
10,
],
[
'You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, count($variable)).',
20,
],
]);
}

Expand Down
5 changes: 5 additions & 0 deletions tests/Rules/PHPUnit/data/assert-same-boolean-expected.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@ public function testAssertSameWithBooleanAsExpected()
$this->assertSame($a, 'b'); // OK
}

public function testAssertSameIsDetectedWithDirectAssertAccess()
{
\PHPUnit\Framework\Assert::assertSame(true, 'foo');
}

}
5 changes: 5 additions & 0 deletions tests/Rules/PHPUnit/data/assert-same-count.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ public function testAssertSameWithCountMethodIsOK()
$this->assertSame(5, $this->count()); // OK
}

public function testAssertSameIsDetectedWithDirectAssertAccess()
{
\PHPUnit\Framework\Assert::assertSame(5, count([1, 2, 3]));
}

}
5 changes: 5 additions & 0 deletions tests/Rules/PHPUnit/data/assert-same-null-expected.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,9 @@ public function testAssertSameWithNullAsExpected()
$this->assertSame($c, 'foo'); // nullable is OK
}

public function testAssertSameIsDetectedWithDirectAssertAccess()
{
\PHPUnit\Framework\Assert::assertSame(null, 'foo');
}

}

0 comments on commit 0d33999

Please sign in to comment.