Skip to content

Fix detection of unspecific assert*() with direct Assert::assert*() calls #47

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 1 commit into from
May 10, 2019
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
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');
}

}