Laravel 5 app always using 'testing' environment configuration

风格不统一 提交于 2019-12-02 23:29:07

Laravel 5 doesn't cascade config files correctly anymore so your testing config file is overriding anything you have in your local config file.

Now you aren't supposed to have any subfolders for each environment, but rather set configuration settings inside the .env file in the root folder.

This file isn't checked in to the repo to ensure that nothing sensitive is checked into the repo. You should have a separate .env file for each environment your application is living.

TESTING

For php unit (functional) you can set env variables in the phpunit.xml file e.g..

<php>
    <env name="APP_ENV" value="testing"/>
    <env name="CACHE_DRIVER" value="array"/>
    <env name="SESSION_DRIVER" value="array"/>
</php>

For behat (acceptance) testing the Laracasts Laravel Behat extension allows you to create a .env.behat file to change the environment variables.

For phpspec (unit) testing well the environment shouldn't matter as your testing individual methods in isolation and mock everything else.

For selenium (integration / system / e2e) testing the environment variables should come from the .env file on the server wherever you are doing this testing.

Additional points I've just had to use to get tests running on sqlite, while the main app normally defaults to mysql:

Add something like this to phpunit.xml, as explained by craig.michael.morris:

<env name="DB_DRIVER" value="sqlite"/>

And this change to config/database.php

'default' => env('DB_DRIVER', 'mysql'),

And in the 'sqlite' section of config/database.php

'database' => ':memory:',

1) open the config file database.php and add the following inside the 'connections' => [

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

this in memory sqlite will be used for your testing

2) open the .env file and make sure it has the following:

DB_CONNECTION=mysql

3) open phpunit.xml and add the following inside the <php> tag:

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

here’s where you can add all the testing related env variables

4) in your tests were you use a database connection use the Database Migration Trait:

use DatabaseMigrations;

for more details about the DatabaseMigrations refer to the documentation https://laravel.com/docs/5.1/testing#resetting-the-database-after-each-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!