MichaelAndish's avatar

Phpunit Variables are not loading on Test Environment

Hi

I've created this issue on Laravel's Github repository and I think it's a bug! You can find it here: https://github.com/laravel/framework/issues/43409

Description: Phpunit.xml variables are not loaded on abstract TestCase! After clearing cache, config, etc., php artisan config:clear, php artisan optimize:clear,... I was unable to create a test environment with different DB. It is not possible for me to get the test environment variable.

Steps To Reproduce: There are 2 ways to see the bug: after changing you phpunit.xml file in your Abstract TestCase add this method and you can see the result:

    protected function setUp(): void
    {
        parent::setUp();

        dump(env('APP_NAME'));
        dd(env('APP_ENV'));
     }

After clearing cache, run test command (php artisan test)

change your bootstrap/app.php: Create .env.testing file and change your app file:

$app = new Illuminate\Foundation\Application(
    $_ENV['APP_BASE_PATH'] ?? dirname(__DIR__)
);

$app->loadEnvironmentFrom('.env.testing');

dump(env('APP_NAME'));  // still load from .env
dd(env('APP_ENV'));  // still load from .env

I have change the env tag in phpunit.xml to server it works!

        <server name="APP_NAME" value="testing"/>
        <server name="APP_ENV" value="testing"/>

But still I can not load variables from .env.testing

Update:

here also you can see someone in docker environment solved the problem! https://stackoverflow.com/questions/45922358/why-phpunit-is-not-getting-the-correct-app-env-as-specified-in-phpunit-xml

0 likes
8 replies
Sinnbeck's avatar

What about if you load from config? If this was a general bug, I dont think anyone would be able to do testing on laravel, as we all use env values (through config.. never directly from env)

    protected function setUp(): void
    {
        parent::setUp();

        dd(config('app.name'));
     }
Sinnbeck's avatar

I just tested it myself, and it works for me with both env() and config(). Can you share a project on github with the issue so we can help inspect and solve the problem?

1 like
MichaelAndish's avatar

@Sinnbeck Thanks Sinnbeck I used Docker container and on container it works! and also I've cloned a new Laravel project to test and I have the same issue on a new Laravel Repository!

Sinnbeck's avatar

@Michael.Andish Ok interesting. I use docker as well. So the problem happens when you run laravel directory on your computer then using your native php installtion?

MichaelAndish's avatar

@Sinnbeck when I run Laravel directory on my computer!

On the container I have defined variables like this:

environment:
  - APP_ENV=testing
  - DB_HOST=127.0.0.1
  - DB_PORT=3308
  - DB_DATABASE=db-test
  - DB_USERNAME=homestead
  - DB_PASSWORD=homestead

then executing the test on it:

docker exec -it CONTAINER_NAME php artisan test

and it works. but when I try to do that right on my computer without container, it's not working.

Sinnbeck's avatar

@Michael.Andish What is your OS? I run linux myself. I will happily try to recreate it locally if you can give me some hints on how :)

Sinnbeck's avatar

@Michael.Andish sadly I don't own a Mac. Maybe someone else can try?

And the setup is just

  1. New laravel project
  2. Add .env.testing with some env variables to test
  3. See if you can get them in phpunit
1 like

Please or to participate in this conversation.