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

[11.x] Application::inferBasePath returns incorrect path in tests #54118

Closed
calebdw opened this issue Jan 8, 2025 · 0 comments · Fixed by #54119
Closed

[11.x] Application::inferBasePath returns incorrect path in tests #54118

calebdw opened this issue Jan 8, 2025 · 0 comments · Fixed by #54119

Comments

@calebdw
Copy link
Contributor

calebdw commented Jan 8, 2025

Laravel Version

11.37.0

PHP Version

8.4.1

Database Driver & Version

No response

Description

Hello!

We just updated to Laravel 11.x and now our tests (running artisan test) are failing with:

Failed opening required 'phar://<app_path>/vendor/phpstan/phpstan/phpstan.phar/bootstrap/app.php' (include_path='.:/usr/local/lib/php')

which is coming from:

public function createApplication()
{
$app = require Application::inferBasePath().'/bootstrap/app.php';
$app->make(Kernel::class)->bootstrap();
return $app;
}

where Application::inferBasePath is given by:

public static function inferBasePath()
{
return match (true) {
isset($_ENV['APP_BASE_PATH']) => $_ENV['APP_BASE_PATH'],
default => dirname(array_keys(ClassLoader::getRegisteredLoaders())[0]),
};
}

Our codebase has several custom PHPStan rules for architecture tests and each of these rules have their own PHPUnit test---running feature tests after these tests fails, while feature tests executed before passes. This worked just fine on Laravel 10.x.

I realize that I can specify the APP_BASE_PATH env, but perhaps there's a more robust way of detecting the app path than just grabbing the first autoloader?

This seemed to work in local testing---it filters any paths that are inside the vendor/ directory:

     public static function inferBasePath()
    {
        return match (true) {
            isset($_ENV['APP_BASE_PATH']) => $_ENV['APP_BASE_PATH'],
            default => dirname(array_values(array_filter(
                array_keys(ClassLoader::getRegisteredLoaders()),
                fn ($path) => ! str_contains($path, 'vendor/'),
            ))[0]),
        };
    }

Thanks!

Steps To Reproduce

execute a PHPStan test with a feature test:

php artisan test <PHPStan test> <feature test>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant