Pestphp in developing a package laravel and .env vars

54 Views Asked by At

I am desperate because I am unable to load the values from the .env file, either .en or .env.testing in a development of a Laravel 10 package. I do the testing with PestPHP

Bine, the test, loads the configuration file, since in the debunbg that I created with ray(), I obtain its data, but with null values, since the values must be those of the .env file.

On ServiceProvider.php of my package development, i put

 public function register(): void
    {
        $this->mergeConfigFrom(
            self::CONFIG_PATH,
            'contawhmcs'
        );

        ray(config());

        $this->app->bind('contawhmcs', function () {
            return new Contawhmcs();
        });
    }

ray() show me:

Illuminate\Config\Repository {#824▶
  #items: array:15 [▶
    "hashing" => array:3 [▶]
    "auth" => array:5 [▶]
    "app" => array:14 [▶]
    "mail" => array:4 [▶]
    "services" => array:3 [▶]
    "database" => array:4 [▶]
    "cache" => array:3 [▶]
    "session" => array:15 [▶]
    "queue" => array:4 [▶]
    "broadcasting" => array:2 [▶]
    "view" => array:2 [▶]
    "cors" => array:8 [▶]
    "logging" => array:3 [▶]
    "filesystems" => array:3 [▶]
    "contawhmcs" => array:4 [▶
      "api_id" => null
      "api_secret" => null
      "api_key" => null
      "url" => null
    ]
  ]
}

Package charge in test, de file config/contawhmcs.php but this no charge .env's vars.

I have tried to locate information on how to do this upload, but nothing. Including Bard (it's a hot potato), ChatGPT, and none of them give me a solution.

Solution ??

I've tried and worked, a solution. But I think that is not correct

 public function register(): void
    {
        if ($this->app->environment('testing')) {
            $dotenv = Dotenv::createImmutable(__DIR__ . '/../'); 
            $dotenv->load();
        }

        $this->mergeConfigFrom(
            self::CONFIG_PATH,
            'contawhmcs'
        );

        $this->app->bind('contawhmcs', function () {
            return new Contawhmcs();
        });
    }
    ```
0

There are 0 best solutions below