I am using Spatie Package Skelton to develop a package. It is using pest under the hood.
I created a .env.testing file and checked the environment used while running a pest test with dd(app()->environment));, which is testing.
Anyhow, when I use env('TEST'); in my test and having TEST=test in my .env.testing file, is it returning null.
What am I missing?
I don't see any reason to use
.envfiles in your package. I created multiple Laravel package and php SDK, with PHPUnit and it never required.envfile.If you are fully commited to use Pest, my answer won't be useful.
However if you want to use PHPUnit, then you can create a
phpunit.xml.distfile. It's just a skeleton and it can be (git) versioned. (Just like.env.examplein Laravel.)Make a copy of it and name it
phpunit.xml. This file should not versioned.By default PHPunit will looking for this file, or fall back for the dist file.
In this file you can provide environment variables for your test:
This example code might be out-of-date.
Also you should not use
env()in your package, since Laravel can cache the configs. If there is a config cache file, thenenv()will always returnnull.Register your own config file, you can use
env()here, and merge it in your provider.This way you can use
config()function, but way better solution if you collect the config in your provider, then pass it down to your service class.