Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ final class ThatAssertTrue extends \PHPUnit\Framework\TestCase
public function test()
{
$this->assertThat('value', $this->isTrue());

$this->assertThat('name', $this->isType('string'));
$this->assertThat('age', $this->isString());
}
}

Expand All @@ -25,6 +28,9 @@ final class ThatAssertTrue extends \PHPUnit\Framework\TestCase
public function test()
{
$this->assertTrue('value');

$this->assertIsString('name');
$this->assertIsString('age');
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Identifier;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand All @@ -17,8 +18,25 @@
*/
final class AssertThatToDirectAssertRector extends AbstractRector
{
/**
* @var array<string, string>
*/
private const array IS_TO_ASSERT_METHOD_MAP = [
'isTrue' => 'assertTrue',
'isFalse' => 'assertFalse',
'isNull' => 'assertNull',
'isEmpty' => 'assertEmpty',
'isCountable' => 'assertIsCountable',
'isArray' => 'assertIsArray',
'isString' => 'assertIsString',
'isInt' => 'assertIsInt',
'isFloat' => 'assertIsFloat',
'isBool' => 'assertIsBool',
];

public function __construct(
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
private readonly ValueResolver $valueResolver,
) {
}

Expand Down Expand Up @@ -95,8 +113,25 @@ public function refactor(Node $node): ?Node
return $node;
}

if ($exactAssertName === 'isTrue') {
$node->name = new Identifier('assertTrue');
foreach (self::IS_TO_ASSERT_METHOD_MAP as $isName => $assertName) {
if (! $this->isName($exactAssertMethodCall->name, $isName)) {
continue;
}

$node->name = new Identifier($assertName);
unset($node->args[1]);

return $node;
}

if ($this->isName($exactAssertMethodCall->name, 'isType')) {
$exactFirstArg = $exactAssertMethodCall->getArgs()[0];
$expectedType = $this->valueResolver->getValue($exactFirstArg->value);
if (! is_string($expectedType)) {
return null;
}

$node->name = new Identifier('assertIs' . ucfirst($expectedType));
unset($node->args[1]);

return $node;
Expand Down