What's the best way to set up a test environment in laravel 5?
I'm just getting into Laravel5 with some previous experience with L4. One of the problems I ran into was environment configuration, mainly in terms of testing.
I am trying to connect to a mysqlite database and after a lot of searching, the best I have come up with is to add conditional code to the config file like this:
'default' => $app->environment('testing') ? 'sqlite':'mysql'
And in my phpunit.xml file:
<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"/>
</php>
However, now when I try to run my test suite I get the following error:
ReflectionException: Class env does not exist.
At this point, my only option is to create two separate .env files, 'testing' and 'local', renaming them to '.env' as I need them. This is obviously ineffective or portable. Any advice?
source to share
As shown in the doc, when you run phpunit, APP_ENV is by default testing
and is installed at phpunit.xml
( http://laravel.com/docs/testing#introduction ). but lumen don't have any rule to load a config file like .env.testing
or .env.production
for us.
but laravel will provide us with callback
.
$app->detectEnvironment(function() use ($app){
if (file_exists(__DIR__.'/../'.'.env.' .getenv('APP_ENV') )) {
$app->loadEnvironmentFrom('.env.'. getenv('APP_ENV') );
}
});
run some command in the "enviroment" section:
APP_ENV=testing artisan serve
then the app will load the config from /.env.testing
Note:
if you accept my idea the -env option is invalid. another approach is to add the APP_ENV = testing prefix. I think this is a laravel bug because the DetectEnvironment Closure will not be called when the command is run in cli mode.
source to share