Skip to content

Conversation

@samsonasik
Copy link
Member

@TomasVotruba per #7451 (comment)

This is alternative that it only load preload.php when on phpunit < 12. As PHPUnit 12+ load vendor/autoload.php too early.

I will need help on verify on community rules to test the manual patch :)

@samsonasik
Copy link
Member Author

@nikophil I tested on your repo with steps:

  1. Clone and install dependencies
git clone git@github.com:zenstruck/foundry.git
cd foundry
composer install
composer bin rector install
composer bin phpstan install
  1. Manually update file bin/tools/rector/vendor/rector/rector/src/Testing/PHPUnit/AbstractLazyTestCase.php with this PR patch https://github.com/rectorphp/rector-src/pull/7452/files#diff-cb464ba580e5b47644d6f1062f03d797d3bfc6d7e540e5efded419ceb004e5b0

  2. Remove preload require at utils/rector/tests/bootstrap.php

-require \dirname(__DIR__).'/../../bin/tools/rector/vendor/rector/rector/preload.php';
  1. Run PHPUnit:
➜  foundry git:(2.x) php84 vendor/bin/phpunit -c phpunit-rector.xml.dist     
PHPUnit 12.4.0 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.4.0RC1
Configuration: /Users/samsonasik/www/foundry/phpunit-rector.xml.dist

.......................................                           39 / 39 (100%)

Time: 00:00.367, Memory: 76.50 MB

OK (39 tests, 49 assertions)

which seems working ok 👍

@samsonasik
Copy link
Member Author

@ostrolucky I tested on your repo with the following steps:

  1. Clone and install dependencies
git clone https://github.com/ostrolucky/rector-rules
cd rector-rules
composer install
  1. Manually update file vendor/rector/rector/src/Testing/PHPUnit/AbstractLazyTestCase.php with this PR patch https://github.com/rectorphp/rector-src/pull/7452/files#diff-cb464ba580e5b47644d6f1062f03d797d3bfc6d7e540e5efded419ceb004e5b0

  2. Remove preload require at tests/bootstrap.php

-require dirname(__DIR__) . '/vendor/rector/rector/preload.php';
  1. Run PHPUnit:
vendor/bin/phpunit
PHPUnit 12.4.0 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.3.12
Configuration: /Users/samsonasik/www/rector-rules/phpunit.xml.dist


WARNING: On fixture file "array_access_fixture.php.inc" for test "Ostrolucky\Test\RectorRules\Rector\RemoveUnnecessaryEmptyRector\RemoveUnnecessaryEmptyRectorTest"
File not changed but some Rector rules applied:
 * Ostrolucky\RectorRules\RemoveUnnecessaryEmptyRector
.....
WARNING: On fixture file "superglobal_variable_fixture.php.inc" for test "Ostrolucky\Test\RectorRules\Rector\RemoveUnnecessaryEmptyRector\RemoveUnnecessaryEmptyRectorTest"
File not changed but some Rector rules applied:
 * Ostrolucky\RectorRules\RemoveUnnecessaryEmptyRector
.                                                              6 / 6 (100%)

Time: 00:00.273, Memory: 80.50 MB

OK (6 tests, 7 assertions)

which seems working ok 👍 (warning notice is unrelated, due to not return null on no change on the custom rules)

@samsonasik
Copy link
Member Author

samsonasik commented Oct 9, 2025

/cc @Androoha1 @staabm @megawubs @ivastly @nikophil @devnix @smnandre I need help to test this PR patch:

  1. Remove custom patch on composer.json -> files handling tweak of rector preload.php when exists.

  2. Run composer update

  3. Manually update vendor/rector/rector/src/Testing/PHPUnit/AbstractLazyTestCase.php with this PR patch https://github.com/rectorphp/rector-src/pull/7452/files#diff-cb464ba580e5b47644d6f1062f03d797d3bfc6d7e540e5efded419ceb004e5b0

  4. Remove custom autoload that include rector preload.php when exists, eg, if you define:

bootstrap="./tests/bootstrap.php"

in phpunit.xml, then in tests/bootstrap.php, remove the preload.php autoload.

-require dirname(__DIR__) . '/vendor/rector/rector/preload.php';
  1. run PHPUnit 👍

@samsonasik
Copy link
Member Author

samsonasik commented Oct 9, 2025

@TomasVotruba I manually test this patch PR https://github.com/rectorphp/rector-src/pull/7452/files#diff-cb464ba580e5b47644d6f1062f03d797d3bfc6d7e540e5efded419ceb004e5b0 on https://github.com/laminas/laminas-servicemanager-migration repo with requirre PHPUnit 12+ and it seems working OK, the node check with

var_dump($node instanceof StmtsAwareInterface);

and run phpunit show true:

vendor/bin/phpunit test/Rector/Class_/ImplementsFactoryInterfaceToPsrFactoryRector/AutoImportRenameUseTest.php
PHPUnit 12.4.0 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.3.12
Configuration: /Users/samsonasik/www/laminas-servicemanager-migration/phpunit.xml.dist

bool(true)

which seems working OK as our own node type detected as "true" 👍

@samsonasik
Copy link
Member Author

All checks have passed 🎉 @TomasVotruba it is ready for review.

@nikophil
Copy link
Contributor

nikophil commented Oct 9, 2025

@samsonasik this is awesome, thank you for your work!

@samsonasik
Copy link
Member Author

@TomasVotruba this can possibly better solution :), this only modify the AbstractLazyTestCase.php without forcing autoload preload early, as scoped autoload later already loaded.

Tested in community rules and seems more our php-parser vendor already loaded early, see above tests.

@TomasVotruba
Copy link
Member

I prefer the other one, because it's single place used for all race-condition preloads 👍
Putting the preload hacking into too many places might lead to various results in various places.

If something loads PHPUnit and then Rector test case, we have the same bug. This would happen e.g. while PHPStan test case is run → PHPUnit is loaded → then php-parser of PHPUnit is loaded and crash.

@samsonasik
Copy link
Member Author

@TomasVotruba that seems issue on custom vendor directory for rector, so autolad loaded later, see foundry repo usecase

#7451 (comment)

On foundry repo usecase, the rector vendor is on different location vs project vendor, while later I recommend to use dedicated single location for rector vendor and project vendor, but still

#7451 (comment)

@TomasVotruba TomasVotruba deleted the let-preload branch October 9, 2025 15:26
@samsonasik
Copy link
Member Author

@nikophil since the approved solution is the patch on rector bootstrap

The only way to work on your use case on your foundry repo is by use rector/rector on your require-dev, and remove the autoload of rector autoload, since it already loaded early, I created PR on your foundry repo :)

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.

4 participants