Skip to content

Commit d991406

Browse files
authored
[fix] Keep original message in AssertEqualsOrAssertSameFloatParameterToSpecificMethodsTypeRector (#406)
1 parent 464dec2 commit d991406

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertEqualsOrAssertSameFloatParameterToSpecificMethodsTypeRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class KeepDeltaMessage extends TestCase
8+
{
9+
public function test()
10+
{
11+
$value = 10.20001;
12+
$this->assertSame(10.20, $value, 'Some message');
13+
}
14+
}
15+
16+
?>
17+
-----
18+
<?php
19+
20+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertEqualsOrAssertSameFloatParameterToSpecificMethodsTypeRector\Fixture;
21+
22+
use PHPUnit\Framework\TestCase;
23+
24+
final class KeepDeltaMessage extends TestCase
25+
{
26+
public function test()
27+
{
28+
$value = 10.20001;
29+
$this->assertEqualsWithDelta(10.20, $value, PHP_FLOAT_EPSILON, 'Some message');
30+
}
31+
}
32+
33+
?>

rules/CodeQuality/Rector/MethodCall/AssertEqualsOrAssertSameFloatParameterToSpecificMethodsTypeRector.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,12 @@ public function getRuleDefinition(): RuleDefinition
3838
// code before
3939
<<<'CODE_SAMPLE'
4040
$this->assertSame(10.20, $value);
41-
$this->assertEquals(10.20, $value);
4241
$this->assertEquals(10.200, $value);
43-
$this->assertSame(10, $value);
4442
CODE_SAMPLE
4543
,
4644
<<<'CODE_SAMPLE'
4745
$this->assertEqualsWithDelta(10.20, $value, PHP_FLOAT_EPSILON);
48-
$this->assertEqualsWithDelta(10.20, $value, PHP_FLOAT_EPSILON);
4946
$this->assertEqualsWithDelta(10.200, $value, PHP_FLOAT_EPSILON);
50-
$this->assertSame(10, $value);
5147
CODE_SAMPLE
5248
),
5349
]
@@ -82,11 +78,17 @@ public function refactor(Node $node): ?Node
8278
return null;
8379
}
8480

81+
$customMessageArg = $args[2] ?? null;
82+
8583
$newMethodCall = $this->assertCallFactory->createCallWithName($node, 'assertEqualsWithDelta');
8684
$newMethodCall->args[0] = $args[0];
8785
$newMethodCall->args[1] = $args[1];
8886
$newMethodCall->args[2] = new Arg(new ConstFetch(new Name('PHP_FLOAT_EPSILON')));
8987

88+
if ($customMessageArg instanceof Arg) {
89+
$newMethodCall->args[] = $customMessageArg;
90+
}
91+
9092
return $newMethodCall;
9193
}
9294

0 commit comments

Comments
 (0)