本文介绍了Laravel 5 应用程序始终使用“测试"环境配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

即使环境配置为 local,应用程序也只会加载 resources/config/testing 文件夹中的配置.我可以从 APP_ENV 环境变量中看到同一个应用程序中的环境,它是 local.

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?

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

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

推荐答案

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

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

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

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.

此文件未签入到 repo 以确保没有任何敏感内容被签入 repo.您应该为您的应用程序所处的每个环境创建一个单独的 .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.

测试

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

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>

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

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

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

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

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

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

这篇关于Laravel 5 应用程序始终使用“测试"环境配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-20 01:25