Laravel 5 app always using 'testing' envir

2019-03-13 02:53发布

I have a Laravel 5 app with two environments and two configurations: testing (for PHPUnit configuration, in-memory db) and local (my development configuration).

Even when the environment is configured to be local, the application only loads the configuration in the resources/config/testing folder. I can see the environment in the same app from the APP_ENV environment variable, and it is local.

  • Should I just not be using a testing configuration directory for configuring my tests?

  • What's a better way to configure my testing environment in Laravel 5?

3条回答
祖国的老花朵
2楼-- · 2019-03-13 03:26

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

查看更多
一夜七次
3楼-- · 2019-03-13 03:34

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.

查看更多
看我几分像从前
4楼-- · 2019-03-13 03:34

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:',
查看更多
登录 后发表回答