Laravel 5 应用程序始终使用“测试”环境配置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27438817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:32:57  来源:igfitidea点击:

Laravel 5 app always using 'testing' environment configuration

phplaravellaravel-5phpunitenvironment

提问by Simon Fredsted

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

我有一个 Laravel 5 应用程序,有两个环境和两个配置:测试(用于 PHPUnit 配置,内存数据库)和本地(我的开发配置)。

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

即使环境配置为local,应用程序也只加载resources/config/testing文件夹中的配置。我可以从APP_ENV环境变量中看到同一个应用程序中的环境,它是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?

  • 我应该不使用测试配置目录来配置我的测试吗?

  • 在 Laravel 5 中配置我的测试环境的更好方法是什么?

回答by morrislaptop

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

Laravel 5 不再正确级联配置文件,因此您的测试配置文件会覆盖本地配置文件中的任何内容。

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.

现在您不应该为每个环境设置任何子文件夹,而是在根文件夹的 .env 文件中设置配置设置。

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.

此文件未签入存储库以确保未将任何敏感内容签入存储库。对于您的应用程序所在的每个环境,您都应该有一个单独的 .env 文件。

TESTING

测试

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

对于 php 单元(功能),您可以在 phpunit.xml 文件中设置环境变量,例如。

<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.

对于 behat(验收)测试,Laracasts Laravel Behat 扩展允许您创建一个 .env.behat 文件来更改环境变量。

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

对于 phpspec(单元)测试,环境应该无关紧要,因为您单独测试各个方法并模拟其他所有方法。

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

对于 selenium (integration / system / e2e) 测试,环境变量应该来自服务器上的 .env 文件,无论您在何处进行此测试。

回答by markdwhite

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

我只需要用来在 sqlite 上运行测试的其他要点,而主应用程序通常默认为 mysql:

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

像 craig.michael.morris 解释的那样,向 phpunit.xml 添加类似的内容:

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

And this change to config/database.php

对 config/database.php 的更改

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

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

在 config/database.php 的“sqlite”部分

'database' => ':memory:',

回答by Mahmoud Zalt

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

1)打开配置文件 database.php,在里面添加以下内容'connections' => [

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

this in memory sqlite will be used for your testing

这个在内存中的 sqlite 将用于您的测试

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

2)打开.env文件并确保它具有以下内容:

DB_CONNECTION=mysql

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

3)打开phpunit.xml并在<php>标签中添加以下内容:

<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:

4) 在您的测试中,您是否使用数据库连接使用数据库迁移特性:

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

有关 DatabaseMigrations 的更多详细信息,请参阅文档https://laravel.com/docs/5.1/testing#resetting-the-database-after-each-test