rectorphp / rector-phpunit

Rector upgrade rules for PHPUnit
https://getrector.com/find-rule?activeRectorSetGroup=phpunit
MIT License
64 stars 47 forks source link

Rule to replace assertObjectHasAttribute and assertObjectNotHasAttribute with property_exists #341

Closed alexander-schranz closed 3 months ago

alexander-schranz commented 3 months ago

The assertObjectHasAttribute and assertObjectNotHasAttribute are deprecated in PHPUnit 9. To silence the deprecation error they can be replace with:

$this->assertTrue(\property_exists($object, 'propertyName'));
$this->assertFalse(\property_exists($object, 'propertyName'));

The inverted version of AssertPropertyExistsRector need to be created. For PHPUnit 10.1 a new version converting to the new assertObjectHasProperty could be added but on PHPUnit 9 this das sadly not exists.

samsonasik commented 3 months ago

that should already covered at PR:

please try latest dev-main

alexander-schranz commented 3 months ago

This seems only a upgrade rule if you want to upgrade to PHPUnit 10.1 not one if you keep 9.5 and want just remove the deprecation message if I understand correctly?

alexander-schranz commented 3 months ago

Looks like the PropertyExistsWithoutAssertRector was for that but it got removed again.

samsonasik commented 3 months ago

phpunit is changing method from one version to another, so the last one is used, that's the reason why, for example, this PR exists:

TomasVotruba commented 3 months ago

@alexander-schranz This is caused by back and forth PHPUnit 9/10 removal added methods :) we don't have such a detailed sets, to counter that. So the best way is to ignore these methods for now and do the upgrade on PHPUnit 10.

What is the blocker for your to go PHPUnit 10?

alexander-schranz commented 3 months ago

PHPUnit 10 requires PHP 8.1 we still support lower version, but want to keep the code based prepared for PHPUnit 10.

TomasVotruba commented 3 months ago

I see :+1:

In that case I'd go for https://github.com/Yoast/PHPUnit-Polyfills and run the PHPUnit 10 rule explicitly

alexander-schranz commented 3 months ago

The polyfills sounds like a good idea, could use that traits as a BC layer, Thx.

Yeah we already run rector rules for the static data providers to make sure we not create unstatic data providers in our projects, thats why I thought forcing the property_exists is may something we could also already prepare 😄 :

    ->withRules([
        StaticDataProviderClassMethodRector::class, // prepare for PHPUnit >= 10
    ]);
alexander-schranz commented 3 months ago

Until I use a simple assertObjectHasAttribute\((.*), (.*)\) regex with assertTrue(property_exists($2, $1)) todo the replaces :)