Skip to content

Commit becab3f

Browse files
Better fix for #4740
Instead of replacing static loading with autoloading we now combine the best of both worlds. The PHAR now loads all sourcecode files bundled in it on startup (again), but assisted by an autoloader so that PHP's compiler can find code units that have not been loaded yet for perming method compatibility checks, for instance.
1 parent d396026 commit becab3f

File tree

7 files changed

+17
-64
lines changed

7 files changed

+17
-64
lines changed

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ jobs:
246246
java-version: 1.8
247247

248248
- name: Build unscoped PHAR for testing
249-
run: ant unscoped-phar-snapshot generate-autoloader-for-test-fixture
249+
run: ant unscoped-phar-snapshot
250250

251251
- name: Run regular tests with unscoped PHAR
252252
run: ./build/artifacts/phpunit-snapshot.phar

ChangeLog-8.5.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ All notable changes of the PHPUnit 8.5 release series are documented in this fil
44

55
## [8.5.19] - 2021-MM-DD
66

7-
### Changed
7+
### Fixed
88

9-
* [#4740](https://github.com/sebastianbergmann/phpunit/issues/4740): The PHPUnit PHAR no longer imports all code units on startup
9+
* [#4740](https://github.com/sebastianbergmann/phpunit/issues/4740): `phpunit.phar` does not work with PHP 8.1
1010

1111
## [8.5.18] - 2021-07-19
1212

build.xml

-10
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,6 @@
1919
<property name="prepare.done" value="true"/>
2020
</target>
2121

22-
<target name="generate-autoloader-for-test-fixture" description="Generate autoloader for test fixture">
23-
<exec executable="${basedir}/tools/phpab" taskname="phpab" failonerror="true">
24-
<arg value="--output" />
25-
<arg path="${basedir}/tests/autoload.php" />
26-
<arg path="${basedir}/tests/_files" />
27-
<arg path="${basedir}/tests/end-to-end/execution-order/_files" />
28-
<arg path="${basedir}/tests/unit" />
29-
</exec>
30-
</target>
31-
3222
<target name="validate-composer-json" depends="clean" unless="validate-composer-json.done" description="Validate composer.json">
3323
<exec executable="${basedir}/tools/composer" failonerror="true" taskname="composer">
3424
<arg value="validate"/>

build/templates/binary-phar-autoload.php.in

+7-5
Original file line numberDiff line numberDiff line change
@@ -67,25 +67,27 @@ unset($options);
6767
define('__PHPUNIT_PHAR__', str_replace(DIRECTORY_SEPARATOR, '/', __FILE__));
6868
define('__PHPUNIT_PHAR_ROOT__', 'phar://___PHAR___');
6969

70+
Phar::mapPhar('___PHAR___');
71+
7072
spl_autoload_register(
7173
function ($class) {
7274
static $classes = null;
7375

7476
if ($classes === null) {
75-
$classes = [
76-
___CLASSLIST___
77-
];
77+
$classes = [___CLASSLIST___];
7878
}
7979

8080
if (isset($classes[$class])) {
81-
require 'phar://___PHAR___' . $classes[$class];
81+
require_once 'phar://___PHAR___' . $classes[$class];
8282
}
8383
},
8484
___EXCEPTION___,
8585
___PREPEND___
8686
);
8787

88-
Phar::mapPhar('___PHAR___');
88+
foreach ([___CLASSLIST___] as $file) {
89+
require_once 'phar://___PHAR___' . $file;
90+
}
8991

9092
if ($execute) {
9193
if (isset($printManifest)) {

build/templates/library-phar-autoload.php.in

+7-5
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,26 @@
22
define('__PHPUNIT_PHAR__', str_replace(DIRECTORY_SEPARATOR, '/', __FILE__));
33
define('__PHPUNIT_PHAR_ROOT__', 'phar://___PHAR___');
44

5+
Phar::mapPhar('___PHAR___');
6+
57
spl_autoload_register(
68
function ($class) {
79
static $classes = null;
810

911
if ($classes === null) {
10-
$classes = [
11-
___CLASSLIST___
12-
];
12+
$classes = [___CLASSLIST___];
1313
}
1414

1515
if (isset($classes[$class])) {
16-
require 'phar://___PHAR___' . $classes[$class];
16+
require_once 'phar://___PHAR___' . $classes[$class];
1717
}
1818
},
1919
___EXCEPTION___,
2020
___PREPEND___
2121
);
2222

23-
Phar::mapPhar('___PHAR___');
23+
foreach ([___CLASSLIST___] as $file) {
24+
require_once 'phar://___PHAR___' . $file;
25+
}
2426

2527
__HALT_COMPILER();

src/TextUI/Command.php

-22
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
use function array_keys;
1616
use function assert;
1717
use function class_exists;
18-
use function defined;
19-
use function dirname;
2018
use function explode;
2119
use function extension_loaded;
2220
use function fgets;
@@ -37,7 +35,6 @@
3735
use function sprintf;
3836
use function str_replace;
3937
use function stream_resolve_include_path;
40-
use function strpos;
4138
use function strrpos;
4239
use function substr;
4340
use function trim;
@@ -48,7 +45,6 @@
4845
use PharIo\Version\Version as PharIoVersion;
4946
use PHPUnit\Framework\Exception;
5047
use PHPUnit\Framework\Test;
51-
use PHPUnit\Framework\TestCase;
5248
use PHPUnit\Framework\TestListener;
5349
use PHPUnit\Framework\TestSuite;
5450
use PHPUnit\Runner\StandardTestSuiteLoader;
@@ -1131,24 +1127,6 @@ protected function handleBootstrap(string $filename): void
11311127
} catch (Exception $e) {
11321128
$this->exitWithErrorMessage($e->getMessage());
11331129
}
1134-
1135-
if (!defined('__PHPUNIT_PHAR__') || !defined('__PHPUNIT_PHAR_ROOT__')) {
1136-
return;
1137-
}
1138-
1139-
$testCaseSource = (new ReflectionClass(TestCase::class))->getFileName();
1140-
1141-
if (strpos($testCaseSource, 'phar://' . __PHPUNIT_PHAR__) !== 0) {
1142-
$this->exitWithErrorMessage(
1143-
sprintf(
1144-
'Mixed installation detected, exiting.' . PHP_EOL . PHP_EOL .
1145-
'PHPUnit was invoked from %s.' . PHP_EOL .
1146-
'PHPUnit\'s code was loaded from %s.',
1147-
__PHPUNIT_PHAR__,
1148-
dirname($testCaseSource, 2)
1149-
)
1150-
);
1151-
}
11521130
}
11531131

11541132
protected function handleVersionCheck(): void

tests/bootstrap.php

-19
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,6 @@
99
*/
1010
const TEST_FILES_PATH = __DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR;
1111

12-
if (defined('__PHPUNIT_PHAR__')) {
13-
if (!file_exists(__DIR__ . '/autoload.php')) {
14-
print __DIR__ . '/autoload.php does not exist' . PHP_EOL;
15-
16-
exit(1);
17-
}
18-
19-
require_once __DIR__ . '/autoload.php';
20-
21-
$jsonFile = realpath(__DIR__ . '/../composer.json');
22-
$base = dirname($jsonFile);
23-
24-
foreach (json_decode(file_get_contents($jsonFile), true)['autoload-dev']['files'] as $file) {
25-
require_once $base . DIRECTORY_SEPARATOR . $file;
26-
}
27-
28-
return;
29-
}
30-
3112
if (!defined('PHPUNIT_COMPOSER_INSTALL')) {
3213
define('PHPUNIT_COMPOSER_INSTALL', dirname(__DIR__) . '/vendor/autoload.php');
3314
}

0 commit comments

Comments
 (0)