Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Composer 2.2 _composer_autoload_path global #4835

Closed
Seldaek opened this issue Nov 29, 2021 · 3 comments
Closed

Support for Composer 2.2 _composer_autoload_path global #4835

Seldaek opened this issue Nov 29, 2021 · 3 comments
Labels
installation/composer type/enhancement A new idea that should be implemented

Comments

@Seldaek
Copy link
Contributor

Seldaek commented Nov 29, 2021

Composer 2.2.0 (releasing early December hopefully) will introduce a new $_composer_autoload_path global variable which is defined when running a Composer binary. See docs for the new feature.

Essentially when running vendor/bin/phpunit, instead of running a symlink to vendor/phpunit/phpunit/phpunit, Composer will add an intermediate PHP proxy file which looks like this:

#!/usr/bin/env php
<?php

/**
 * Proxy PHP file generated by Composer
 *
 * This file includes the referenced bin path (../phpunit/phpunit/phpunit)
 *
 * @generated
 */

$GLOBALS['_composer_autoload_path'] = __DIR__ . '/..'.'/autoload.php';

include __DIR__ . '/..'.'/phpunit/phpunit/phpunit';

This means these lines could be changed to support the new variable if present:

phpunit/phpunit

Lines 61 to 67 in 9b18457

foreach (array(__DIR__ . '/../../autoload.php', __DIR__ . '/../vendor/autoload.php', __DIR__ . '/vendor/autoload.php') as $file) {
if (file_exists($file)) {
define('PHPUNIT_COMPOSER_INSTALL', $file);
break;
}
}

But the main reason I wanted to point this out is that this will define a new global variable, so any test relying on a given global state might see differences. I'm hoping there aren't too many of these out there, but who knows.

One thing PHPUnit could do is unset the global variable in the bootstrap to ensure it's back to a clean state.

@Seldaek Seldaek added the type/enhancement A new idea that should be implemented label Nov 29, 2021
@sebastianbergmann
Copy link
Owner

Initial idea (untested):

diff --git a/phpunit b/phpunit
index d9e4f7250..360bffbe7 100755
--- a/phpunit
+++ b/phpunit
@@ -58,15 +58,21 @@ if (!ini_get('date.timezone')) {
     ini_set('date.timezone', 'UTC');
 }
 
-foreach (array(__DIR__ . '/../../autoload.php', __DIR__ . '/../vendor/autoload.php', __DIR__ . '/vendor/autoload.php') as $file) {
-    if (file_exists($file)) {
-        define('PHPUNIT_COMPOSER_INSTALL', $file);
+if (isset($GLOBALS['_composer_autoload_path'])) {
+    define('PHPUNIT_COMPOSER_INSTALL', $GLOBALS['_composer_autoload_path']);
 
-        break;
+    unset($GLOBALS['_composer_autoload_path']);
+} else {
+    foreach (array(__DIR__ . '/../../autoload.php', __DIR__ . '/../vendor/autoload.php', __DIR__ . '/vendor/autoload.php') as $file) {
+        if (file_exists($file)) {
+            define('PHPUNIT_COMPOSER_INSTALL', $file);
+
+            break;
+        }
     }
-}
 
-unset($file);
+    unset($file);
+}
 
 if (!defined('PHPUNIT_COMPOSER_INSTALL')) {
     fwrite(

@Seldaek
Copy link
Contributor Author

Seldaek commented Nov 29, 2021

Yup that looks good enough :)

@sebastianbergmann
Copy link
Owner

Thank you for the quick feedback, Jordi.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
installation/composer type/enhancement A new idea that should be implemented
Projects
None yet
Development

No branches or pull requests

2 participants