connecteev's avatar

access base_path() from within a unit test?

Trying to call base_path from setupBeforeClass()

php artisan test --filter=PaymentsAndSubscriptionsTest

    public static function setUpBeforeClass(): void
    {
        $path = base_path('tests') . "/Feature/listen_for_stripe_webhooks.sh";
        exec("bash $path");

and it throws this error:

  • Tests\Feature\PaymentsAndSubscriptionsTest > can reseed database
   Error

  Call to undefined method Illuminate\Container\Container::path()

  at vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:132
    128▕      * @return string
    129▕      */
    130▕     function app_path($path = '')
    131▕     {
  ➜ 132▕         return app()->path($path);
    133▕     }
    134▕ }
    135▕
    136▕ if (! function_exists('asset')) {

Why can't I access base_path from within a unit test? I am on an app that was converted from laravel 7 -> laravel 8

0 likes
4 replies
divspace's avatar

That app isn't actually available yet. You can do something like:

$path = __DIR__.'/listen_for_stripe_webhooks.sh';
connecteev's avatar

@divspace thanks, but the strange thing is, base_path() used to work. I dont know what changed.. Your solution works, but it relies on relative paths... For this reason I was hoping to get base_path() working.

1 like
Regi44's avatar

@connecteev Did you find a solution for that problem? I am struggeling to solve this issue this excact moment :D

Please or to participate in this conversation.