Skip to content

[PHPUnit 11.5] Add AssertContainsOnlyMethodCallRector, bond deprecated generic asserts to phpunit >=11.5 - #738

Merged
TomasVotruba merged 2 commits into
mainfrom
phpunit-115-deprecated-generic-asserts
Jul 31, 2026
Merged

[PHPUnit 11.5] Add AssertContainsOnlyMethodCallRector, bond deprecated generic asserts to phpunit >=11.5#738
TomasVotruba merged 2 commits into
mainfrom
phpunit-115-deprecated-generic-asserts

Conversation

@TomasVotruba

@TomasVotruba TomasVotruba commented Jul 31, 2026

Copy link
Copy Markdown
Member

PHPUnit 11.5 deprecated the generic assertContainsOnly() and isType() assertions and added type-specific replacements for both (#6055, #6052).

Neither was handled for projects actually running PHPUnit 11.5:

  • there was no rule for assertContainsOnly() at all
  • AssertIsTypeMethodCallRector existed, but only in the phpunit120.php set. ComposerTriggeredSet matches with ^ + version, so on phpunit/phpunit 11.5.x only phpunit110.php is loaded and the rule never ran — despite the deprecation landing in 11.5.

New rule: AssertContainsOnlyMethodCallRector

 final class SomeTest extends TestCase
 {
     public function test(): void
     {
-        $this->assertContainsOnly('string', ['a', 'b']);
+        $this->assertContainsOnlyString(['a', 'b']);
-        self::assertContainsOnly('int', [1], null, 'some message');
+        self::assertContainsOnlyInt([1], 'some message');
     }
 }

The $isNativeType argument is dropped when it is absent, null or true. When it is false or an unresolvable expression the type may be a class name, so the call is skipped.

Version bonding

Both rules now implement ComposerPackageConstraintInterface (rectorphp/rector-src#7877):

public function provideComposerPackageConstraint(): ComposerPackageConstraint
{
    return new ComposerPackageConstraint('phpunit/phpunit', '>=11.5');
}

ComposerTriggeredSet matches with ^ + version and is not cumulative, so a project on PHPUnit 12 never loads phpunit110.php. Both rules are therefore registered in the phpunit110.php, phpunit120.php and phpunit130.php sets — someone upgrading straight from 11.4 to 12.0 still gets them. AssertIsTypeMethodCallRector keeps its current class name, so nothing breaks for existing PHPUnit 12 users.

The >=11.5 constraint is what keeps both rules from firing on 11.0–11.4, where the type-specific methods do not exist yet.

Needed rectorphp/rector-src#8237 (merged) — the autowired InstalledPackageResolver behind ComposerPackageConstraintFilter lost its cwd fallback, so every rule implementing ComposerPackageConstraintInterface threw The installed package json not found.

@TomasVotruba
TomasVotruba merged commit 9f7ae4a into main Jul 31, 2026
7 checks passed
@TomasVotruba
TomasVotruba deleted the phpunit-115-deprecated-generic-asserts branch July 31, 2026 11:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant