rector icon indicating copy to clipboard operation
rector copied to clipboard

Incorrect behavior of ParentTestClassConstructorRector

Open gisostallenberg opened this issue 3 months ago • 3 comments

Bug Report

Subject Details
Rector version last dev-main
Installed as composer dependency

Minimal PHP Code Causing Issue

See https://getrector.com/demo/919b7847-fa96-4779-8160-37d400c35745

<?php

 use PHPUnit\Framework\TestCase;

 final class SomeHelper extends TestCase
 {
 }

Responsible rules

  • ParentTestClassConstructorRector

Description

Because in PHPUnit 11 the constructor of \PHPUnit\Framework\TestCase is marked final, applying this rule results in a PHPStan error 'Method SomeHelper::__construct() overrides @final method PHPUnit\Framework\TestCase::__construct()

Could the rule detect if the parent method is final?

gisostallenberg avatar Dec 02 '25 11:12 gisostallenberg

The added value of this rule is also very low. PHPUnit does not detect classes without *Test when a filter or directory is given. When a direct test on the class (file) is ran, PHPUnit executes the test cases in the class, even without the constructor.

I would suggest a rule that checks if child classes of TestCase have a Test suffix, which would add them to the detected classes. Otherwise removing this rule from (at least PHPUnit 11 set) would be better.

gisostallenberg avatar Dec 02 '25 11:12 gisostallenberg

@TomasVotruba this is why I was suggesting to remove this rule:

  • https://github.com/rectorphp/rector-phpunit/pull/490

that you closed, as you want this to upgrade phpunit 9 to 10,

so we add special rule on phpunit 12 to remove override final construct:

  • https://github.com/rectorphp/rector-phpunit/pull/491

samsonasik avatar Dec 02 '25 11:12 samsonasik

This rule should only trigger, if these is a __construct method in the helper class.

What is the expeced content per PHPUnit version from 9 through 12? The class should be always initializable via new SomeHelper()

TomasVotruba avatar Dec 05 '25 17:12 TomasVotruba

This rule should only trigger, if these is a __construct method in the helper class

It now adds the __construct method if there isn't one and the class is not named *Test.

gisostallenberg avatar Dec 12 '25 10:12 gisostallenberg