0

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'),

    ...
C. Celora
  • 429
  • 1
  • 3
  • 12
  • 1
    I think it should be like this: 1. Add a custom env profile. ( Example: .env.davis 2. Specify the environment ID when executing the command. ( Example: APP_ENV=davis php artisan test 3. default get .env file phpunit is only required for unit testing, i think – Davis Sep 16 '22 at 09:29
  • 1
    You don't need to do what the previous comment says, so your set up is correct, BUT, did you run `php artisan config:cache` on that machine? Try running `php artisan config:clear` and run the test again. Also, have in mind that you should be using ` – matiaslauriti Sep 16 '22 at 16:51
  • Those are good answers. I tried both of them but they did not work out. To resolve this issue I substituited the sail container with a nginx and a php-fpm containers – C. Celora Oct 02 '22 at 16:04

0 Answers0