Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failure to set APP_ENV to 'testing' for unit testing in Laravel 5.5

I am using Laravel 5.5, phpunit 6.5.5 on Homestead-7 (I think).

I am trying this tutorial: https://laravel-news.com/your-first-laravel-application (And this should tell you a lot about my experience with the framework)

Testing fails (due to TokenMismatchException) and I have managed to track down the root cause to the APP_ENV variable being set to local, although I've tried many ways of setting it to testing.

At the end, what allowed me to overcome the problem was to set the variable like this:

APP_ENV=testing vendor/bin/phpunit

Then, the tests completed successfully.

My question is, what am I doing wrong? The above hack is obviously not the best way to do it. There must be a way to properly do this.

Update

Contents of phpunit.xml:

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

        <testsuite name="Unit">
            <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"/>
        <env name="DB_DATABASE" value=":memory:"/>
    </php>
</phpunit>

Thank you in advance for your time.

like image 631
mirnis Avatar asked Dec 02 '25 09:12

mirnis


1 Answers

From the Laravel docs:

When running tests via phpunit, Laravel will automatically set the configuration environment to testing because of the environment variables defined in the phpunit.xml file.

...so I would bet that your phpunit.xml file is missing or misconfigured. It should be in your project's root directory and should contain the following:

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

If you do have a phpunit.xml file in the right place and it does contain the part above, and it's still not working, then try clearing your config cache:

php artisan config:clear

If that still doesn't fix it, then I'd check for something odd with the phpunit.xml file, such as being misnamed or containing a syntax error.

And here's a link to the original phpunit.xml file, to help you restore it if needed.

like image 108
jdunk Avatar answered Dec 06 '25 17:12

jdunk