dg / bypass-finals

Removes `final` and `readonly` keywords from source code on-the-fly and allows mocking of final methods and classes. It can be used together with any test tool such as PHPUnit or Mockery.
Other
477 stars 30 forks source link

PHPUnit 11 incompatibility #53

Closed alebedev80 closed 4 months ago

alebedev80 commented 6 months ago

I have set up PHPUnit 11 and started test:

PHP Fatal error:  Non-readonly class SebastianBergmann\CodeUnit\ClassUnit cannot extend readonly class SebastianBergmann\CodeUnit\CodeUnit in /Users/alm/code/api_client/vendor/sebastian/code-unit/src/ClassUnit.php on line 15

With PHPUnit 10.5.13 everything works fine.

dg commented 6 months ago

Isn't it possible that SebastianBergmann\CodeUnit\CodeUnit is loaded before BypassFinals is enabled?

alebedev80 commented 6 months ago

I just used this bootstrap.php:

<?php

use DG\BypassFinals;

require dirname(__DIR__).'/vendor/autoload.php';

BypassFinals::enable();
$cacheDir = dirname(__DIR__).'.phpunit.bypass.cache';
if (!file_exists($cacheDir) && !mkdir($cacheDir, 0777, true) && !is_dir($cacheDir)) {
    throw new \RuntimeException(sprintf('Directory "%s" was not created', $cacheDir));
}
BypassFinals::setCacheDirectory($cacheDir);
dg commented 4 months ago

This error occurred because the cache was created with a different bypassReadOnly parameter setting than was used afterwards.

finpension-aless commented 3 months ago

This still seem to be an issue with PHPUnit 11 (11.2.5). Even if you use the Extension class recommended in the README I get the error "PHP Fatal error: Non-readonly class PHPUnit\Event\TestSuite\TestSuiteForTestClass cannot extend readonly class PHPUnit\Event\TestSuite\TestSuite in /builds/finpension/finpension-admins/vendor/phpunit/phpunit/src/Event/Value/TestSuite/TestSuiteForTestClass.php on line 19"

The only warkaround that works for me is, if I call "BypassFinals::enable(false, true);", with the readonly removal it won't work.

dg commented 3 months ago

@finpension-aless it was caused by something else. I tried to modify the extension class to skip the PHPUnit code altogether. Please try the dev version to see if it works for you.