Laravel 5:使用不同的数据库进行测试和本地

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35227226/
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 13:11:12  来源:igfitidea点击:

Laravel 5 : Use different database for testing and local

phplaravel

提问by James Okpe George

How does one change database for development and testing on local system without editing the .envfile each time?

如何在本地系统上更改数据库以进行开发和测试,而无需.env每次都编辑文件?

I have found it quite inconvenient to practice TDDbecause of this.

因此,我发现实践TDD非常不方便。

Is it possible for a Laravelapplication to differentiate between normal development and testing so that it can choose the appropriate database?

是否有可能为一个Laravel应用程序才能正常发育和测试,以便它可以选择合适的数据库区分?

回答by Tijmen

Create a testing database configuration in Laravel

在 Laravel 中创建测试数据库配置

Edit the config\database.phpfile and add a testing- array into the connectionsarray:

编辑config\database.php文件并将testing- 数组添加到connections数组中:

'connections' => [
    'testing' => [
        'driver' => env('DB_TEST_DRIVER'),
        // more details on your testing database
    ]
]

Then add the necessary variables to your .env-file.

然后将必要的变量添加到您的.env-file 中。

Edit PHPUnit configuration

编辑 PHPUnit 配置

Open your phpunit.xml-file and add the following within your <php>-tag:

打开您的phpunit.xml-file 并在您的<php>-tag 中添加以下内容:

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

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

Now PHPUnit will run with the tests on the database you defined in the testing- array.

现在 PHPUnit 将在您在testing- 数组中定义的数据库上运行测试。

回答by Jed Lynch

For Laravel 5.5, the proper way to do this is create a testing environment file called .env.testing. Here, you can define your testing environment, including the database you want to use for testing...

对于 Laravel 5.5,正确的做法是创建一个名为.env.testing. 在这里,您可以定义您的测试环境,包括您要用于测试的数据库...

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=test DB
DB_USERNAME=test DB user
DB_PASSWORD=test DB password

Then run this command...

然后运行这个命令...

php artisan config:cache --env=testing

This will configure the cache to the .env.testing file parameters.

这会将缓存配置为 .env.testing 文件参数。

Hereis a link to the documentation.

是文档的链接。

回答by vivanov

If your testing database uses the same configuration but only it's name is different, you could only change the selected database name by adding

如果您的测试数据库使用相同的配置但只是名称不同,则只能通过添加更改选定的数据库名称

<env name="DB_DATABASE" value="testing_db_name"/>

to the file phpunit.xmlin the <php>node

该文件phpunit.xml<php>节点

回答by Yevgeniy Afanasyev

if you need a specific database for one of your unit tests, add this code at the start of your unit test

如果您的单元测试之一需要特定的数据库,请在单元测试开始时添加此代码

    $connections                                    = config('database.connections');
    $connections['single_test_database_connection'] = [
        'driver'      => 'mysql',
        'host'        => env('DB_HOST', '127.0.0.1'),
        'port'        => env('DB_PORT', '3306'),
        'database'    => 'single_test_database',
        'username'    => env('DB_USERNAME', 'forge'),
        'password'    => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset'     => 'utf8mb4',
        'collation'   => 'utf8mb4_unicode_ci',
        'prefix'      => '',
        'strict'      => true,
        'engine'      => null,
    ];
    config()->set('database.connections', $connections);
    config()->set('database.default', 'single_test_database_connection');

回答by Pitchinnate

You can use a different .env file for each type of test. You can modify your tests/functional.suite.yml and tests/unit.suite.yml something like this:

您可以为每种类型的测试使用不同的 .env 文件。你可以像这样修改你的 tests/functional.suite.yml 和 tests/unit.suite.yml :

class_name: FunctionalTester
modules:
    enabled: [Laravel5, FunctionalHelper, Asserts]
    config:
        Laravel5:
            environment_file: .env.testing

And this:

和这个:

class_name: UnitTester
modules:
    enabled: [Asserts, UnitHelper, Laravel5]
    config:
        Laravel5:
            environment_file: .env.unittesting

Or you can simply modify your phpunit.xml and add environment vars like @Tijmen did above.

或者你可以简单地修改你的 phpunit.xml 并添加环境变量,就像上面@Tijmen 所做的那样。