2

I am running phpUnit tests using Laravel Framework Version 9.52.20. The tests work just fine in my local environment and in my CI/CD pipeline. But when I try to run the same tests in my dev environment it throws this error repeatedly:

Call to undefined function Illuminate\Foundation\laravel_cloud()

  at vendor/laravel/framework/src/Illuminate/Foundation/Application.php:237
    233▕      * @return void
    234▕      */
    235▕     protected function registerLaravelCloudServices()
    236▕     {
  ➜ 237▕         if (! laravel_cloud()) {
    238▕             return;
    239▕         }
    240▕ 
    241▕         $this['events']->listen(

I can see this function being defined in vendor/laravel/framework/src/Illuminate/Support/helpers.php

if (! function_exists('laravel_cloud')) {
    /**
     * Determine if the application is running on Laravel Cloud.
     *
     * @return bool
     */
    function laravel_cloud()
    {
        return ($_ENV['LARAVEL_CLOUD'] ?? false) === '1' ||
               ($_SERVER['LARAVEL_CLOUD'] ?? false) === '1';
    }
}

The oddity is that my phpUnit tests run just fine in my local environment and in my CI/CD pipeline. It is only in my separate dev environment that I experience this issue.

Additional Information

My Tests namespace uses a trait named CreatesApplication() to set up my test environment. Should I manually reference the Illuminate/Support/helpers class in my TestCase class in order to make this function available to the phpUnit tests? I had assumed that the Laravel framework would have automatically included such a dependency. But that doesn't explain why this particular environment has the problem while other identical environments do not.

Let me know if you have run into this situation also, and how you were able to fix this reference error.

Addendum

I went into artisan tinker and queried the runtime environment to see if the laravel_cloud function was defined. It said it was:

~$ php artisan tinker
Psy Shell v0.12.7 (PHP 8.3.19 — cli) by Justin Hileman
> function_exists('my_fake_function');
= false

> function_exists('laravel_cloud');
= true

It looks like the laravel_cloud function is defined and available to the Laravel engine in general. However, something about my environment is causing the phpUnit runtime to not find that function within the list of global environment functions.

2 Answers 2

0

Since this is currently blocking me, I have altered my phpUnit bootstrap.php file and mocked the missing function so it can be available while testing:

<?php

// include the composer autoloader
require __DIR__ . '/../vendor/autoload.php';

// dev environment reports that global function laravel_cloud() is missing
if (! function_exists('laravel_cloud')) {
    function laravel_cloud()
    {
        return false;
    }
}

This doesn't solve the issue with why the Laravel framework is choking on the backported laravel_cloud() method. It simply allows me to bypass the error when I run my tests.

Sign up to request clarification or add additional context in comments.

Comments

0

I ran into the same problem, and figured it out, maybe your case will be similar.

tl;dr - if you run artisan test from within the Laravel app's directory, the problem no longer occurs.

cd path/to/app
php artisan test

In my case, this is why:

  • I was trying to run artisan test from a different directory (like php path/to/app/artisan test).

  • And in this other directory, I also happened to have a vendor directory with phpunit installed, and Laravel, but a slightly older version of Laravel that does not have laravel_cloud defined. artisan chose to use phpunit from this directory, rather than the one from the vendor directory in the Laravel app (path/to/app/vendor).

  • When phpunit started up, it loaded vendor/autoload.php, which brought in vendor/laravel/framework/src/Illuminate/Support/helpers.php (from the older Laravel package), with laravel_cloud not defined.

  • When phpunit then tried to load the bootstrap file (in phpunit.xml), which was set to vendor/autoload.php (and resolves to path/to/app/vendor/autoload.php), path/to/app/vendor/laravel/framework/src/Illuminate/Support/helpers.php was not loaded. (Somehow, the $fileIdentifier hash string looking thing that composer generates ended up being the same for the older helpers.php and the newer helpers.php.)

Entering the path/to/app directory, and running artisan there, fixed the issue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.