0

Most of my classes include loading env variables for configuration. When I was coding I thought vlucas/phpdotenv ignored the .env file on testing environments since there is already a conf file like phpunit.xml where you define them.

Is it possible to make dotenv ignore .env when on testing environment?

miken32
  • 42,008
  • 16
  • 111
  • 154
Christopher Francisco
  • 15,672
  • 28
  • 94
  • 206
  • 1
    You can't ignore the .env file but if the environment variable is set before the .env file is loaded, the predefined variable is used, unless the .env file is called using the `$dotenv->overload` method. I don't think Laravel does the latter. This means that the environment variables defined in phpunit.xml will not be overriden. – David Barker Jul 01 '15 at 16:23
  • I was trying to use the default value `env('myVar', $defaultValue)` so that I didn't have to set it on `phpunit.xml`, but it seems like it can't be done :( – Christopher Francisco Jul 01 '15 at 16:47
  • You could declare the environment variables within your testcase before the application is booted. But that won't let you use the default values still. I usually reserve the default values for my production server so I never need a .env file on my live. – David Barker Jul 01 '15 at 17:11

1 Answers1

1

Testing will use your .env file, but you can override any of its settings in phpunit.xml.

<php>
    <env name="APP_ENV" value="testing"/>
    <env name="CACHE_DRIVER" value="array"/>
    <env name="SESSION_DRIVER" value="array"/>
    <env name="DB_DRIVER" value="sqlite"/>
    <env name="CACHE_HOST" value="localhost"/>
    <env name="SESSION_HOST" value="localhost"/>
</php>
ceejayoz
  • 176,543
  • 40
  • 303
  • 368