I have an application using laravel 8 that runs on laravel sail.
When I launch the tests with the command
sail artisan test
I noticed something strange. The config() function of laravel was getting the wrong value, meanwhile, the env() function returned the correct one.
I have a phpunit.xml
file for the tests to override some variables in the .env
.
Here is my PHP section of the PHPUnit file:
<php>
<server name="APP_ENV" value="testing" force="true"/>
<server name="BCRYPT_ROUNDS" value="4"/>
<server name="CACHE_DRIVER" value="array"/>
<server name="DB_CONNECTION" value="sqlite" force="true"/>
<server name="DB_DATABASE" value=":memory:"/>
<server name="DB_LEGACY_CONNECTION" value="sqlite" />
<server name="DB_LEGACY_DATABASE" value=":memory:" />
</php>
and in my .env
file:
APP_PORT=49200
APP_NAME=Laravel
APP_ENV=local
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
FORWARD_DB_PORT=33061
DB_DATABASE=lb_backend
DB_USERNAME=sail
DB_PASSWORD=sail
During the Pest test, I print the variables and this is what I found:
beforeEach(function () {
$env_vars = [
env('APP_ENV', 'production'), // return testing
config('app.env'), // return local
env('DB_CONNECTION', 'mysql'), // return sqlite
config('database.default') // return mysql
];
fwrite(STDERR, print_r($env_vars, TRUE));
$this->usingInMemoryDatabase(); // return false. it has a config('database.default') function inside
...
Is there a bug with laravel or with laravel sail? Or is something wrong with my configuration?
Below I put my config/app.php:
<?php
return [
...
/*
|--------------------------------------------------------------------------
| Application Environment
|--------------------------------------------------------------------------
|
| This value determines the "environment" your application is currently
| running in. This may determine how you prefer to configure various
| services the application utilizes. Set this in your ".env" file.
|
*/
'env' => env('APP_ENV', 'production'),
...
and config/database.php:
<?php
return [
/*
|--------------------------------------------------------------------------
| Default Database Connection Name
|--------------------------------------------------------------------------
|
| Here you may specify which of the database connections below you wish
| to use as your default connection for all database work. Of course
| you may use many connections at once using the Database library.
|
*/
'default' => env('DB_CONNECTION', 'mysql'),
...