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 .env
files in your package. I created multiple Laravel package and php SDK, with PHPUnit and it never required .env
file.
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.dist
file. It's just a skeleton and it can be (git) versioned. (Just like .env.example
in 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:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit>
<other-setting-that-not-relevant-now />
<php>
<env name="MY_ENV_KEY" value="my_env_value" />
</php>
</phpunit>
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, then env()
will always return null
.
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.