Merge pull request #413 from rectorphp/assert-comparison-smarter

Make AssertComparisonToSpecificMethodRector smarter
This commit is contained in:
Gabriel Caruso 2018-04-12 07:17:38 -03:00 committed by GitHub
commit 035358be00
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 6 deletions

View File

@ -4,8 +4,12 @@ namespace Rector\Rector\Contrib\PHPUnit\SpecificMethod;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\BinaryOp;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar;
use Rector\NodeAnalyzer\MethodCallAnalyzer;
use Rector\NodeChanger\IdentifierRenamer;
use Rector\Rector\AbstractPHPUnitRector;
@ -112,8 +116,13 @@ final class AssertComparisonToSpecificMethodRector extends AbstractPHPUnitRector
/** @var BinaryOp $expression */
$expression = $oldArguments[0]->value;
$firstArgument = new Arg($expression->right);
$secondArgument = new Arg($expression->left);
if ($this->isConstantValue($expression->left)) {
$firstArgument = new Arg($expression->left);
$secondArgument = new Arg($expression->right);
} else {
$firstArgument = new Arg($expression->right);
$secondArgument = new Arg($expression->left);
}
unset($oldArguments[0]);
@ -132,4 +141,11 @@ final class AssertComparisonToSpecificMethodRector extends AbstractPHPUnitRector
'assertFalse' => $falseMethodName,
]);
}
private function isConstantValue(Node $node): bool
{
return in_array(get_class($node), [Array_::class, ConstFetch::class], true)
|| is_subclass_of($node, Scalar::class)
|| $node instanceof Variable && stripos($node->name, 'exp') === 0;
}
}

View File

@ -4,8 +4,12 @@ final class MyTest extends \PHPUnit\Framework\TestCase
{
public function test()
{
$this->assertNotSame($anything, $something);
$this->assertNotSame($expected, $anything);
$this->assertGreaterThan(2, count($something));
$this->assertLessThanOrEqual(5, count($something), 'message');
$this->assertNotEquals(__DIR__, $something, 'message');
$this->assertSame(1.0, $something);
$this->assertNotSame(true, in_array('foo', ['bar', 'baz'], true));
$this->assertNotEquals('string', gettype($foo));
$this->assertEquals(['foo', 'bar'], $something);
}
}

View File

@ -4,8 +4,12 @@ final class MyTest extends \PHPUnit\Framework\TestCase
{
public function test()
{
$this->assertFalse($something === $anything);
$this->assertFalse($expected === $anything);
$this->assertTrue(count($something) > 2);
$this->assertFalse(count($something) >= 5, 'message');
$this->assertTrue(__DIR__ <> $something, 'message');
$this->assertTrue(1.0 === $something);
$this->assertFalse(true === in_array('foo', ['bar', 'baz'], true));
$this->assertTrue('string' != gettype($foo));
$this->assertTrue(['foo', 'bar'] == $something);
}
}