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
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertThatToDirectAssertRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class AssertThatToDirectAssertRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertThatToDirectAssertRector\Fixture;

final class SimpleAssertThat extends \PHPUnit\Framework\TestCase
{
public function test()
{
$this->assertThat('value', $this->equalTo('expectedValue'));
}
}

?>
-----
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertThatToDirectAssertRector\Fixture;

final class SimpleAssertThat extends \PHPUnit\Framework\TestCase
{
public function test()
{
$this->assertEquals('expectedValue', 'value');
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertThatToDirectAssertRector\Fixture;

final class ThatAssertTrue extends \PHPUnit\Framework\TestCase
{
public function test()
{
$this->assertThat('value', $this->isTrue());
}
}

?>
-----
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertThatToDirectAssertRector\Fixture;

final class ThatAssertTrue extends \PHPUnit\Framework\TestCase
{
public function test()
{
$this->assertTrue('value');
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\AssertThatToDirectAssertRector;

return RectorConfig::configure()
->withRules([AssertThatToDirectAssertRector::class]);
107 changes: 107 additions & 0 deletions rules/CodeQuality/Rector/MethodCall/AssertThatToDirectAssertRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\CodeQuality\Rector\MethodCall;

use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Identifier;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertThatToDirectAssertRector\AssertThatToDirectAssertRectorTest
*/
final class AssertThatToDirectAssertRector extends AbstractRector
{
public function __construct(
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Change $this->assertThat($value, $this->*()) to direct $this->assert*() method',
[
new CodeSample(
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;

final class SomeTest extends TestCase
{
public function test()
{
$this->assertThat($value, $this->isTrue());
}
}
CODE_SAMPLE

,
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;

final class SomeTest extends TestCase
{
public function test()
{
$this->assertTrue($value);
}
}
CODE_SAMPLE
),
]
);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [MethodCall::class];
}

/**
* @param MethodCall $node
*/
public function refactor(Node $node): ?Node
{
if ($node->isFirstClassCallable()) {
return null;
}

if (! $this->testsNodeAnalyzer->isPHPUnitMethodCallNames($node, ['assertThat'])) {
return null;
}

$secondArg = $node->getArgs()[1];
if (! $secondArg->value instanceof MethodCall) {
return null;
}

$exactAssertMethodCall = $secondArg->value;
$exactAssertName = $this->getName($exactAssertMethodCall->name);

if ($exactAssertName === 'equalTo') {
$node->name = new Identifier('assertEquals');

$node->args[1] = $node->args[0];
$node->args[0] = $exactAssertMethodCall->args[0];

return $node;
}

if ($exactAssertName === 'isTrue') {
$node->name = new Identifier('assertTrue');
unset($node->args[1]);

return $node;
}

return null;
}
}