如何正确更改测试环境变量 - Codeception 和 Laravel 5.1
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31061888/
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
How to properly change testing environment vars - Codeception and Laravel 5.1
提问by Joel Joel Binks
I am trying to use a sqlite db for testing purposes, as I don't want to touch my actual database:
我正在尝试使用 sqlite db 进行测试,因为我不想接触我的实际数据库:
In my unit.suite.yml file:
在我的 unit.suite.yml 文件中:
class_name: UnitTester
modules:
enabled: [Asserts, UnitHelper]
config:
Db:
dsn: 'sqlite:app/tests/_data/testdb.sqlite'
user: ''
password: ''
dump: app/tests/_data/dump.sql
In my TestCase.php file I have a function which resets and migrates and seeds the db for testing:
在我的 TestCase.php 文件中,我有一个函数可以重置、迁移和播种数据库以进行测试:
Artisan::call('migrate:reset');
Artisan::call('migrate');
Artisan::call('db:seed');
I also added the sqlite settings to app/config/testing/database.php:
我还在 app/config/testing/database.php 中添加了 sqlite 设置:
<?php
// Codeception testing environment uses SQLITE
return [
'default' => 'sqlite',
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => 'app/tests/_data/testdb.sqlite',
'prefix' => ''
],
]
];
I can verify that the database.php file is being read but something still prevents sqlite from being used, and I still end up hosing my REAL database (MySQL). How do I force Laravel/Codeception to use my test db?
我可以验证正在读取 database.php 文件,但仍然有一些东西阻止了 sqlite 的使用,而且我仍然最终使用了我的真实数据库 (MySQL)。如何强制 Laravel/Codeception 使用我的测试数据库?
采纳答案by Joel Joel Binks
So I got fed up and decided to do this hacky thing:
所以我受够了,决定做这件骇人听闻的事情:
In .env I added this:
在 .env 我添加了这个:
DB_DEFAULT=mysql
then I added my sqlite_testing config to the database.php file and I altered the default setting like so:
然后我将我的 sqlite_testing 配置添加到 database.php 文件中,并像这样更改了默认设置:
'default' => env('DB_DEFAULT', 'mysql'),
Lastly, I added this to the beginning of my TestCase.php:
最后,我将此添加到我的 TestCase.php 的开头:
putenv('DB_DEFAULT=sqlite_testing');
I'm sticking with this because it actually works, but any comment/suggestions are appreciated. I found this solution on laracasts site:
我坚持这一点,因为它确实有效,但任何评论/建议都值得赞赏。我在 laracasts 网站上找到了这个解决方案:
https://laracasts.com/discuss/channels/testing/how-to-specify-a-testing-database-in-laravel-5
https://laracasts.com/discuss/channels/testing/how-to-specify-a-testing-database-in-laravel-5
回答by Miha Trtnik
copying a part of my blog here ...
After searching around a bit, we suppose to include variables inside phpunit.xml. There are some preincluded ones
经过一番搜索,我们假设在 phpunit.xml 中包含变量。有一些预先包含的
<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 I wanted to use .env files. So there is always a way :)
但是我想使用 .env 文件。所以总有办法:)
All you need to do now is call applications loadEnvironmentFrom($file) with proper env file. Since I only needed it for testing I added .env.testing.example and .env.testing (that is excluded from git). I added a call to method in /tests/TestCase.php
您现在需要做的就是使用适当的 env 文件调用应用程序 loadEnvironmentFrom($file)。因为我只需要它进行测试,所以我添加了 .env.testing.example 和 .env.testing (从 git 中排除)。我在 /tests/TestCase.php 中添加了对方法的调用
public function createApplication()
{
$app = require __DIR__.'/../bootstrap/app.php';
//THIS IS IT :)
$app->loadEnvironmentFrom('.env.testing');
$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
return $app;
}
回答by carlosvini
TL;DR:
特尔;博士:
APP_ENV=testing php artisan serve
If you run "php artisan help" you will see:
如果您运行“php artisan help”,您将看到:
--env[=ENV] The environment the command should run under.
- --env=testing does not change the .env file anymore. So this information is incomplete.
- Files like config/testing/database.php worked on Laravel 4.2, won't work on Laravel 5
- PHPUnit sets environment variables through phpunit.xml, but it won't work with Codeception.
- Codeception has a module for Laravel 5, there you can set environment_fileto be .env.testingand then you can use this file for your tests. However it won't work on acceptance tests, since these tests open a browser.
In this case you could customize your app.php and add this before return $app:
- --env=testing 不再更改 .env 文件。所以这个信息是不完整的。
- 像 config/testing/database.php 这样的文件在 Laravel 4.2 上工作,在 Laravel 5 上不起作用
- PHPUnit 通过phpunit.xml设置环境变量,但它不适用于 Codeception。
- Codeception 有一个Laravel 5 模块,你可以将environment_file设置为.env.testing然后你可以使用这个文件进行测试。但是它不适用于验收测试,因为这些测试会打开浏览器。
在这种情况下,您可以自定义您的 app.php 并在 return $app 之前添加它:
if (!empty($argv) && in_array('--env=testing', $argv)) { $app->loadEnvironmentFrom('.env.testing'); }
And then you can start your server with "php artisan serve --env=testing"
This is a workaround and i hope soon enough someone from Laravel will define the right way to do this.
if (!empty($argv) && in_array('--env=testing', $argv)) { $app->loadEnvironmentFrom('.env.testing'); }
然后你可以用“php artisan serve --env=testing”启动你的服务器
这是一种解决方法,我希望 Laravel 的某个人很快就会定义正确的方法来做到这一点。
UPDATE: This way is less messy:
更新:这种方式不那么混乱:
APP_ENV=testing php artisan serve
And then:
进而:
if (getenv('APP_ENV') === 'testing') {
$app->loadEnvironmentFrom('.env.testing');
}
UPDATE 2: it seems you don't need the condition above in 5.2, or maybe never needed?
更新 2:您似乎不需要 5.2 中的上述条件,或者可能永远不需要?
回答by xelber
In codeception.yml file (see documentation at https://codeception.com/docs/06-ModulesAndHelpers#Dynamic-Configuration-With-Parameters) in your project root directory check the value for params, should be something like following
在项目根目录中的codeception.yml 文件(参见https://codeception.com/docs/06-ModulesAndHelpers#Dynamic-Configuration-With-Parameters中的文档)中,检查 params 的值,应该类似于以下内容
params:
- .env.testing
回答by mnv
I use tests/_bootstrap.php
for this purpose:
我tests/_bootstrap.php
为此目的使用:
<?php
// This is global bootstrap for autoloading
putenv('MY_ENV_NAME=my_env_valye');