Why phpunit is not getting the correct APP_ENV as specified in phpunit.xml?

I'm using Laravel and this is my ./phpunit.xml file

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="bootstrap/autoload.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false">
    <testsuites>
        <testsuite name="FeatureTests">
            <directory suffix="Test.php">./tests/Feature</directory>
        </testsuite>

        <testsuite name="UnitTests">
            <directory suffix="Test.php">./tests/Unit</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">./app</directory>
        </whitelist>
    </filter>
    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="QUEUE_DRIVER" value="sync"/>
        <env name="DB_CONNECTION" value="sqlite_testing" />
    </php>
</phpunit>

I'm firing one of my test suites with the following command:

./vendor/phpunit/phpunit/phpunit --testsuite UnitTests

Inside my test method I have:

public function testAllMandatoryData()
{
   dump(env('APP_ENV'));
   ....
}

It's displaying "local" I was expecting "testing" as specified in phpunit.xml

         <env name="APP_ENV" value="testing"/>

Edit: additional details I have this laravel application running in a Docker container

On the docker-compose.yml I set some environment variables like:

environment:
  - APP_ENV=local
  - DB_HOST=192.168.0.22
  - DB_PORT=33306
  - DB_DATABASE=mydatabase
  - DB_USERNAME=homestead
  - DB_PASSWORD=homestead

What I've noticed is that directives in phpunit.xml like:

    <env name="APP_ENV" value="testing"/>

have no effect when the name is there in the docker-compose already.

Instead if I add some not defined in docker-compose.yml will be correctly set at phpunit runtime, like:

    <env name="DB_CONNECTION" value="sqlite_test"/>

end Edit

What I'm missing?

Thanks


use

<env name="APP_ENV" value="testing" force="true"/>
<env name="CACHE_DRIVER" value="array" force="true"/>
<env name="SESSION_DRIVER" value="array" force="true"/>
<env name="QUEUE_DRIVER" value="sync" force="true"/>
<env name="DB_CONNECTION" value="sqlite_testing" force="true"/>

Without the force parameter, it won't work. See this issue: https://github.com/sebastianbergmann/phpunit/issues/2353 and the merged PR: https://github.com/sebastianbergmann/phpunit/pull/2723

phpunit.xml not being read, using variables from .env, Why phpunit is not getting the correct APP_ENV as specified in phpunit.xml? I'm using Laravel and this is my ./phpunit.xml file <?xml version="1.0"  Why phpunit is not getting the correct APP_ENV as specified in phpunit.xml? It's displaying "local" I was expecting "testing" as specified in phpunit.xml <env


I try to answer myself with the best option I found.

If you set ENV variables at docker-compose.yml file you won't be able to overwrite them with phpunit.xml directives such as:

  <env name="APP_ENV" value="testing"/>

Then you should opt for removing (like in this example) APP_ENV variable set from docker-compose.yml

And rely on .env Laravel file

APP_ENV=local

With this setup, phpunit will be able to overwrite the APP_ENV to "testing"

I'm still not 100% sure this arrangement is needed, with all docker agent versions. Another host I have with another Docker version behaves differently.

Reading .env in PHPUnit versus env variables in phpunit.xml.dist #251, I can't get tests to run from a testing environment, they always seem to run using the run using the 'local' variables set in the .env file rather than the settings in phpunit.xml But if I echo out the APP_ENV variable in testing somehow still being set and overriding the ones specified in the phpunit.xml file. Turned out the reason was that our CI provide Codeship defines APP_ENV=CODESHIP but our test suite has a common dependency on testing and doesn't need special treatment for the CI environment. TL;DR: I would have expected <env > defined in phpunit.xml to be authoritative and override any shell environments being inherited to phpunit.


Use config('APP_ENV') instead. env() values might be cached, in which case php artisan config:clear might help, though I've also had problems with accessing env values from command line environments.

More info here and here.

APP_ENV not used when running tests · Issue #28841 · laravel , It's displaying "local" I was expecting "testing" as specified in phpunit.xml <env name="APP_ENV" value="testing"/> Edit: additional details I have this laravel  That's because the environment variables you set in phpunit.xml.dist are not available for bin/console. B) There is duplication between .env and phpunit.xml.dist. Usually, 90% of my "dev" values for machine-specific config will be the same for dev and test (e.g. an API key to the sandbox of some service we use).


I had the same problem, .env variables seemed to be overriding the ones from my phpunit.xml, here are some of the solutions that could work:

  1. Specify which phpunit.xml file to use when running phpunit, it may be using a wrong one. vendor/bin/phpunit --configuration [path to your phpunit.xml]
  2. Clean the config cache. php artisan config:cache
  3. If you're in a Docker container, the above action may not work for you. What I did was to delete manually the bootstrap/cache files.

Hope it helps!

Testing: Getting Started - Laravel, This is because phpunit does not read .env. First, I don't completely get why DotEnv variables are only loaded if APP_ENV is not defined. it out I specified the ENV DATABASE_URL inside phpunit.xml.dist and it seems to  Keep in mind that this directory does not include your phpunit.xml file which means that your bootstrap/autoload.php file is not being included. This is probably why you don't get the fatal error when you run the tests in this way.


I run into the same problem when using Docker alongside docker-compose, and the fact was that my docker-compose.yml was loading Laravel's .env file. So, I solved it by creating a .env.docker file and setting (in docker-compose.yml):

  env_file:
    - .env.docker

It worked like a charm, and PHPUnit was able to select my .env.testing file insted of .env

laravel - How to make PHPUnit run all the tests?, This file will override the .env file when running PHPUnit tests I can't make this work a. If I set APP_ENV=testing in .env , it sets the env var correctly - but that With this config, PHPStorm does not explicitly load phpunit.xml for its However, in PHPStorm, if you do not specify a specific phpunit config file,  It's not intended to use any other file than the .env setting it up via the phpunit.xml is the correct way. GrahamCampbell closed this Jun 30, 2015 Sign up for free to join this conversation on GitHub .


Getting Started with PHPUnit in Laravel, After installing a new Laravel application, run phpunit on the command line to run and cache to the array driver while testing, meaning no session or cache data The testing environment variables may be configured in the phpunit.xml file,  13 Why phpunit is not getting the correct APP_ENV as specified in phpunit.xml? 13 Where to save files in Laravel 5 folder structure? 9 Apple keyboard remap keypad comma to dot?


This is an introduction to the basics of PHPUnit testing, using the basic PHPUnit the first time, it is worth pointing out the default phpunit.xml file that Laravel provides. which returns true or false when the specified item is in the box or not​. it is safe to say that our translation of this example test is correct. It seems that phpunit it is not getting the correct configuration. From the Symfony documentation: The -c option tells PHPUnit to look in the app/ directory for a configuration file. If you're curious about the PHPUnit options, check out the app/phpunit.xml.dist file.


Before we run our test suite for the first time, it is worth pointing out the default phpunit.xml file that Laravel provides. PHPUnit will automatically look for a file named phpunit.xml or phpunit.xml.dist in the current directory when it is run. This is where you configure the specific options for your tests.