Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel PHPUnit loading .env.testing file

I have some problems with getting Laravel to load the proper .env file for my testcases. I'm using PHPUnit with the following var set in phpunit.xml:

<?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="Application Test Suite">
            <directory suffix="Test.php">./tests</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">./app</directory>
            <exclude>
                <file>./app/Http/routes.php</file>
            </exclude>
        </whitelist>
    </filter>
    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="DB_CONNECTION" value="sqlite"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="QUEUE_DRIVER" value="sync"/>
    </php>
</phpunit>

In my .env.testing file i have:

APP_ENV=testing
DB_CONNECTION=sqlite

And i have his connection set up under config/database.php:

'sqlite' => [
     'driver' => 'sqlite',
     'database' => ':memory:',
     'prefix' => ''
]

It just isn't loading the .env.testing file. If i do this in my TestCase.php:

dd(env('APP_ENV'));

I still get "development" from my .env file

I have also tried using:

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

Like suggested in the thread here

Does anyone have an idea to what could be wrong?

like image 393
bsgrd Avatar asked Jun 05 '17 16:06

bsgrd


1 Answers

My problem was quite similar. Env file .env.testing was not read at all. I tried to put some var_dump everywhere in my test file, everything was ok but even a var_dump(env("APP_NAME")) was null.

I figured to type a "php artisan config:clear" and everything was back to normal. Not sure what I've done but it worked for me :)

like image 174
Tyteck Avatar answered Nov 20 '22 11:11

Tyteck