Skip to content

Commit 5bc3006

Browse files
committed
Make AssertSameBooleanExpectedRule auto-fixable
1 parent 450bfc0 commit 5bc3006

File tree

5 files changed

+72
-9
lines changed

5 files changed

+72
-9
lines changed

src/Rules/PHPUnit/AssertEqualsIsDiscouragedRule.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,7 @@ public function processNode(Node $node, Scope $scope): array
7373
),
7474
)->identifier('phpunit.assertEquals')
7575
->fixNode($node, static function (CallLike $node) use ($correctName) {
76-
if ($node instanceof Node\Expr\MethodCall) {
77-
$node->name = new Node\Identifier($correctName);
78-
}
79-
80-
if ($node instanceof Node\Expr\StaticCall) {
81-
$node->name = new Node\Identifier($correctName);
82-
}
76+
$node->name = new Node\Identifier($correctName);
8377

8478
return $node;
8579
})

src/Rules/PHPUnit/AssertSameBooleanExpectedRule.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,35 @@ public function processNode(Node $node, Scope $scope): array
4747

4848
if ($expectedArgumentValue->name->toLowerString() === 'true') {
4949
return [
50-
RuleErrorBuilder::message('You should use assertTrue() instead of assertSame() when expecting "true"')->identifier('phpunit.assertTrue')->build(),
50+
RuleErrorBuilder::message('You should use assertTrue() instead of assertSame() when expecting "true"')
51+
->identifier('phpunit.assertTrue')
52+
->fixNode($node, static function (CallLike $node) {
53+
$node->name = new Node\Identifier('assertTrue');
54+
55+
$args = $node->getArgs();
56+
unset($args[0]);
57+
$node->args = $args;
58+
59+
return $node;
60+
})
61+
->build(),
5162
];
5263
}
5364

5465
if ($expectedArgumentValue->name->toLowerString() === 'false') {
5566
return [
56-
RuleErrorBuilder::message('You should use assertFalse() instead of assertSame() when expecting "false"')->identifier('phpunit.assertFalse')->build(),
67+
RuleErrorBuilder::message('You should use assertFalse() instead of assertSame() when expecting "false"')
68+
->identifier('phpunit.assertFalse')
69+
->fixNode($node, static function (CallLike $node) {
70+
$node->name = new Node\Identifier('assertFalse');
71+
72+
$args = $node->getArgs();
73+
unset($args[0]);
74+
$node->args = $args;
75+
76+
return $node;
77+
})
78+
->build(),
5779
];
5880
}
5981

tests/Rules/PHPUnit/AssertSameBooleanExpectedRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ public function testRule(): void
4242
]);
4343
}
4444

45+
public function testFix(): void
46+
{
47+
$this->fix(__DIR__ . '/data/assert-same-boolean-expected-fixable.php', __DIR__ . '/data/assert-same-boolean-expected-fixable.php.fixed');
48+
}
49+
4550
/**
4651
* @return string[]
4752
*/
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace AssertSameBooleanTestCaseFix;
4+
5+
class AssertSameBooleanExpectedTestCase extends \PHPUnit\Framework\TestCase
6+
{
7+
public function returnsBool(): bool
8+
{
9+
if (rand(0, 1)) {
10+
return false;
11+
}
12+
return true;
13+
}
14+
15+
public function doFoo(): void
16+
{
17+
$this->assertSame(true, $this->returnBool());
18+
self::assertSame(false, $this->returnBool());
19+
}
20+
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace AssertSameBooleanTestCaseFix;
4+
5+
class AssertSameBooleanExpectedTestCase extends \PHPUnit\Framework\TestCase
6+
{
7+
public function returnsBool(): bool
8+
{
9+
if (rand(0, 1)) {
10+
return false;
11+
}
12+
return true;
13+
}
14+
15+
public function doFoo(): void
16+
{
17+
$this->assertTrue($this->returnBool());
18+
self::assertFalse($this->returnBool());
19+
}
20+
21+
}

0 commit comments

Comments
 (0)