30

I created a .env.testing file with my credentials, everything the same as .env besides a different table name for the database.

I tried php artisan config:clear which deletes the cached config file in bootstrap/cache/config.php and it broke the database connection. If I re-cache the file by running php artisan config:cache the cached file is back, but without the credentials in the .env.testing file. When I rerun PHPUnit, it connects to the wrong DB, the DB name that is stored in .env not .env.testing.

Is this for real? Did the latest Laravel release break test environments?

Here is what the docs read: (found here: https://laravel.com/docs/5.8/testing)

"You are free to define other testing environment configuration values as necessary. The testing environment variables can be configured in the phpunit.xml file, but make sure to clear your configuration cache using the config:clear Artisan command before running your tests!

Also, you may create a .env.testing file in the root of your project. This file will override the .env file when running PHPUnit tests or executing Artisan commands with the --env=testing option."

UPDATE

I finally was able to override the database that is stated in the .env by adding the database override in the phpunit.xml file; the .env.testing is still not working. The docs are misleading at the least.

5
  • I deleted this because I thought I was just being an idiot and solved the problem, but nope. I still haven't solved this issue. Any one out there have custom .en.testing files working? Jun 14, 2019 at 22:58
  • How / where do you tell the Laravel test-suite to use .env.testing instead of .env (apart from clearing the config cache)? From your question it sounds like you expect it to work out of the box, but from other questions here I can read this is not the case. It's perhaps worth to ensure that first, the other problems just seem to describe an after-effect of thtat.
    – hakre
    Jul 1, 2019 at 0:25
  • Are you using PHPStorm? Oct 31, 2019 at 20:14
  • Try these docs hub.docker.com/r/chilio/laravel-dusk-ci it should help you on enabling local and remote tests.
    – Bart
    Nov 1, 2019 at 0:32
  • For other devs who have similar issue, I tried all above but didnt work. instead I used php artisan config:clear --env=testing (not config:cache) ha suddenly it loads .env.testing file and works fine.
    – Alex
    Jul 14 at 19:03

10 Answers 10

53

I was having this same problem with Laravel v5.6. I set up a .env.testing file with a different username, password, and database but my tests were always running in the main .env database.

It looks like you need to specify the test environment when running the config cache command. After running this command, it worked as described in the docs:

php artisan config:cache --env=testing
3
  • 5
    If I run php artisan config:cache --env=testing, then the local server will use the testing environment. But if I run php artisan config:cache --env=local then phpunit will use the .env instead of .env.testing
    – Iter Ator
    Nov 4, 2019 at 10:47
  • 4
    Note that as soon as you run php artisan config:cache again (without env flag) you'll be back to using regular environment until you do php artisan config:cache --env=testing again. It's whatever is currently in bootstrap/cache/config.php.
    – hello_luke
    Jun 9, 2020 at 14:23
  • 1
    For other devs who have similar issue, I tried all above but didnt work. instead I used php artisan config:clear --env=testing (not config:cache) ha suddenly it loads .env.testing file and works fine. I'm using Laravel 10
    – Alex
    Jul 14 at 19:04
14

This is what worked for me:

In phpunit.xml I had to define <env name="APP_ENV" value="testing"/> inside the <php> block to make PhpUnit load .env.testing instead of .env


It seems like the Laravel docs are wrong and .env.testing is not hard-coded anywhere in Laravel or PhpUnit, it reads the environment file for whatever APP_ENV is specified in phpunit.xml. You could even rename this to .env.phpunit or anything else if you define it in phpunit.xml in this format: <env name="APP_ENV" value="phpunit"/>

2
  • 1
    This was the right answer for me, as you say the Laravel docs are missleading here. My "testing" environment is called "local" so none of the other suggestions were working, changing the other names to "local" too did work! Thanks
    – Novocaine
    Sep 3, 2020 at 13:20
  • 1
    Oh man can't believe I missed this file. My APP_ENV in phpunit.xml was set to an empty string. What is weird is I was calling my console commands with the --env=testing argument but I guess phpunit.xml overrides this (when calling artisan command from a unit test). Thanks for this!
    – Skylord123
    Sep 14 at 18:23
8

I was having the same issue with Laravel v7. Had my hands in my hair for quite some time, but I found a workaround.

The .env.testing was a lost cause for me. I never got it to load, so I just put all the variables within phpunit.xml. To make the testing environment (php artisan test) actually load those variables, I had to do this:

First I had to set APP_ENV in phpunit.xml to force="true":

<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"/>
    <server name="DB_DATABASE" value=":memory:"/>
    <server name="MAIL_MAILER" value="array"/>
    <server name="QUEUE_CONNECTION" value="sync"/>
    <server name="SESSION_DRIVER" value="array"/>
    <server name="TELESCOPE_ENABLED" value="false"/>
</php>

Then I had to run php artisan config:clear, NOT :cache. When I ran php artisan config:cache it actually would not work. Probably because it caches the env once and never loads the new settings based on the different environment.

2
  • 2
    The php artisan config:clear worked for me. Thanks!
    – shoxton
    May 4, 2021 at 19:20
  • config:cache clears the cache and then repopulates it. This is why the accepted answer includes the --env flag.
    – miken32
    Dec 16, 2021 at 20:52
7

You need to set APP_CONFIG_CACHE variable in phpunit.xml to avoid testing and local configurations overwrites each other.

<server name="APP_ENV" value="testing"/>
<server name="APP_CONFIG_CACHE" value="cache/config-testing.php" force="true"/>
4
  • 1
    I can't believe I've searched so long for this answer. Do you have any idea why it seems to work for other people? Because IMHO it is not mentioned anywhere.
    – badluck
    Sep 17, 2021 at 12:14
  • Laravel only reload environment variables if config file bootstrap/cache/config.php is not cached. Since Laravel uses same config file for every APP_ENV type (production, local, testing), when you run php artisan config:cache and change your environment, a new config.php is created with your new variables, and this will work right until you change to other environment. To avoid run php artisan config:cache every time you change your environment, you can isolate your cache config file at env level by using APP_CONFIG_CACHE. Oct 3, 2021 at 11:40
  • For me the path was "bootstrap/cache/config-testing.php". Maybe this will help somebody. Oct 30, 2022 at 9:03
  • 2023: it still works! I had to use "env" in place of "server" May 18 at 20:58
4

It seems that the variables defined in phpunit.xml is overriding the variables defined in .env.testing file, not the other way around as we all expect.

I recommend keeping only APP_ENV in phpunit.xml and then cloning .env.testing from current .env. And it will work as expected.

phpunit.xml

<php>
  <server name="APP_ENV" value="testing"/>
  <!-- Remove all others and define them in .env.testing -->
</php>
1
  • 1
    I'm pretty sure this was my issue, as soon as I removed the other <env name= it worked :D Jun 2, 2021 at 4:18
2

In order to run .env.testing in test runs only and to run .env in default behavior. You will need to execute:

./vendor/bin/phpunit.bat // For windows
./vendor/bin/phpunit // For Linux

Good luck

1

When you use a local docker container, make sure you don't set the "env_file" key in docker-compose.yml to point to your .env file (or have it overwritten with an environment key). These container-level environment variables take precedence over the values loaded by Laravel (i.e. from .env.testing).

So don't do the following in your docker-compose.yml:

phpfpm:
  env_file: .env

or

phpfpm 
  environment:
    APP_ENV: "testing"

or otherwise unset them before running tests: unset DB_CONNECTION OR_OTHERS && php artisan config:cache --env=testing && php artisan test

0

I had this problem too, and it turns out none of the other answers were on point at all.

I'm on Laravel 9 for this, so it is possible that my answer is too far into the future, but this question is the one that pops up on Google when I search for "laravel .env.testing not working"

Here's how I fixed it:

  1. Restored Tests/TestCase.php to the "new project" content (it contained some crud)

    It's a short file, so I can paste it here too:

    <?php
    
    namespace Tests;
    
    use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
    
    abstract class TestCase extends BaseTestCase
    {
        use CreatesApplication;
    }
    
  2. Make sure to extend the right class in your tests

    • ✅ YES: extends Tests\TestCase
    • ❌ NO: extends PHPUnit\Framework\TestCase
  3. Override the setUp method

     private string $emailDomain;
     public function setUp(): void
     {
         parent::setUp();
         $this->emailDomain = env('EMAIL_DOMAIN');
     }
    

And make damn sure you call parent::setUp too.

Then everything works as described in the manual and there's no need to touch the phpunit.xml file. Just leave it be.

Extra credits: my phpunit.xml file used <server ...> in the <php> section, but it should use <env ...> instead, like the one provided by Laravel by default.

2
  • As expected, I got a minus 1 :) Wouldn't be Stack Overflow otherwise.
    – Radu C
    Mar 4 at 12:27
  • 1
    I am giving it an upvote now sorry that happened! Mar 16 at 22:36
-1

What does your .env.testing look like?

is your environment set to APP_ENV=testing?

When I do tests and I do not have that and only have my database details in the file it does not work but when I make a carbon copy of the original .env file and edit the env to testing mine works.

3
  • I run a dev server, which uses the .env file and connects to a local mysql server. In the same time, I would like to run unit tests. I created a .env.testing, and defined a :memory: sqlite connection in it. For some reason, the .env is used during the testing, as if .env.testing would not be there.
    – Iter Ator
    Oct 31, 2019 at 10:29
  • Yes I read the question :) I was asking is your APP_ENV set in that file. Nov 1, 2019 at 3:29
  • It was: APP_ENV=local. I changed it to APP_ENV=testing, but the error is the same
    – Iter Ator
    Nov 4, 2019 at 10:42
-1

If you are likely using phpstorm,then,

go to settings of your phpstorm or press Ctrl + Alt + S.

then selectt Languages & Frameworks ->PHP ->Test Frameworks Under Test Runner tab, click Default configuration file and select (by clicking the folder icon) the path of your project's PHPUnit.xml file.

it will make all your changes in the xml file take effect.Then create the .env.testing file, create your preferred DB config variables for test and running your tests again!

2
  • I am not using phpstorm. I run the local server and the tests from the terminal
    – Iter Ator
    Nov 5, 2019 at 18:07
  • I am using VIM for life! Nov 7, 2019 at 22:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.