Skip to content

Conversation

@TomasVotruba
Copy link
Member

No description provided.

@TomasVotruba TomasVotruba merged commit 4122b94 into main Aug 19, 2025
6 checks passed
@TomasVotruba TomasVotruba deleted the tv-assert-same-type branch August 19, 2025 09:20
if (! $this->testsNodeAnalyzer->isPHPUnitMethodCallNames($node, ['assertSame', 'assertEquals'])) {
return null;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should add check:

  • isFirstClassCallable()
  • count($node->getArgs()) >= 2

I will apply patch for it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


$variableExpr = $node->getArgs()[1]
->value;
$variableType = $this->getType($variableExpr);
Copy link
Member

@samsonasik samsonasik Aug 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be getNativeType(), avoid compare invalid doc:

   public function run()
    {
        $this->assertSame('123', $this->getOrderId());
    }

    /**
     * @return int
     */
    private function getOrderId()
    {
        return '123';
    }

I will apply patch for it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +39 to +47
public function run()
{
$this->assertSame('123', $this->getOrderId());
}

private function getOrderId(): int
{
return 123;
}
Copy link
Member

@samsonasik samsonasik Aug 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TomasVotruba on assertSame, it actually already error on the first usage before its transformation, that means the test already invalid on very beginning:

There was 1 failure:

1) ArrayLookup\Tests\CollectorTest::test
Failed asserting that 123 is identical to '123'.

so it probably should only apply on assertEquals ?

https://github.com/rectorphp/rector-phpunit/pull/510/files#diff-1d628c9179459ab3a6e79445318e1c19281e70418d547ad433a05f35d83cf8be

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TomasVotruba I tested in our project, it cause error:

-        $this->assertEquals(1, $this->getOrderId());
+        $this->assertSame('1', $this->getOrderId);

that compare:

-'1'
+'00000000000000000001'

this should indeed only on assertEquals, assertSame is apply on actual same type, I will create a patch to only apply on assertEquals and rename the rule to:

-MatchAssertSameExpectedTypeRector
+MatchAssertEqualsExpectedTypeRector

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add test fixture here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


if ($expectedType->isLiteralString()->yes() && $directVariableType->isInteger()->yes()) {
// update expected type to provided type
$expectedArg->value = new LNumber((int) $expectedArg->value->value);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LNumber is deprecated, use Int_ instead, I will create new PR for it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants