Skip to content

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

Closed
@calebdw

Description

@calebdw

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>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions