将SQLite设置为Laravel 5.1中进行单元测试的数据库 [英] Set SQLite as database for unit testing in Laravel 5.1

查看:157
本文介绍了将SQLite设置为Laravel 5.1中进行单元测试的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Laravel 5.1中设置单元测试.在 文档 之后,我看到了:

I'm trying to set-up unit testing in Laravel 5.1. Following the documentation I see this:

Laravel在构建时就考虑了测试.其实支持测试 PHPUnit包含在开箱即用中

Laravel is built with testing in mind. In fact, support for testing with PHPUnit is included out of the box

运行测试时,Laravel将自动设置配置 测试环境. Laravel自动配置会话 并在测试时缓存到阵列驱动程序,这意味着没有会话或 缓存数据将在测试期间保留.

When running tests, Laravel will automatically set the configuration environment to testing. Laravel automatically configures the session and cache to the array driver while testing, meaning no session or cache data will be persisted while testing.

太棒了.但是... 如何在运行测试时告诉Laravel使用SQLite数据库连接?

which is awesome. But... how do I tell Laravel to use SQLite database connection when tests are ran?

config/database.php中只有这个注释的代码:

Only thing there is in config/database.php is this commented code:

/* We might use this connection for unit tests
'sqlite' => [
    'driver'   => 'sqlite',
    'database' => storage_path().'/database.sqlite',
    'prefix'   => '',
],
*/

我们可能会使用此:)

是否有一种方法可以对所有测试进行全局设置?我需要在每个测试用例中设置连接子吗?

Is there a way to set this globally for all test? Do I need to set the connecton in every test case?

任何帮助表示赞赏.

推荐答案

实际上很简单.

storage/文件夹上创建一个测试数据库,其名称为database.sqlite,或者如果您想使用其他名称或其他位置,则必须更改config/database.php文件上的配置,这些是默认配置:

Create a testing database on your storage/ folder, with the name database.sqlite or if you want another name or another location you have to change the configs on the config/database.php file, these are the default configs:

'sqlite' => [
    'driver'   => 'sqlite',
    'database' => storage_path('database.sqlite'),
    'prefix'   => '',
],

您有两个选择,您可以编辑.env或仅指定要在其中运行迁移的数据库的名称:

You have two options, you either edit your .env or you just specify the name of the database where you want to run your migrations:

php artisan migrate --database=sqlite

如果您希望编辑.env文件,那么我们必须添加一个新变量:

If you prefer editing your .env file, then we have to add a new variable:

DB_CONNECTION=sqlite

这是因为.env文件中不存在此变量时,Laravel默认为MySQL:

That's because Laravel defaults to MySQL when this variable is absent from .env file:

//config/database.php file
'default' => env('DB_CONNECTION', 'mysql'),

现在,我们的应用程序指向了我们的sqlite数据库,我们现在可以运行我们的迁移和播种了.之后,如果您只想继续运行MySQL,请从.env文件中删除DB_CONNECTION=sqlite.

Now that our app is pointing into our sqlite database we can now run our migrations and seeding. After that if you just want to keep running MySQL remove DB_CONNECTION=sqlite from the .env file.

现在,您可以在测试数据库上进行迁移了,最后一步就是指定SQLite作为测试的默认数据库.

Now that you have your migrations on your testing database the last step is just to specify, your SQLite as your default database for tests.

在您的根文件夹中,有一个phpunit.xml文件,将其打开,并在<php>节点下添加一个新变量:

On your root folder you have a phpunit.xml file, open it and a new variable under <php> node:

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

现在您的应用程序使用MySQL,而 phpunit 使用SQLite.

Now your application is using MySQL and phpunit is using SQLite.

记住,如果您更改了.env并将其更改回默认数据库;

Remember that if you change the .env to change it back to your default database;

在运行迁移时要小心,如果您在迁移本身上指定了一个连接,它将尝试在该连接上运行它.

Be careful when running your migrations, if you specified a connection on the migration itself, it will try to run it on that connection.

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AdminUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::connection('manage')->create('admin_users', function (Blueprint $t) {
            $t->increments('id');
            $t->string('name');
            $t->softDeletes();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::connection('manage')->dropIfExists('admin_users');
    }
}

无论您在.env文件上还是在迁移命令中指定了什么内容,该迁移都将始终在连接管理上运行

This migration will always run on the connection manage no matter what you specify on the .env file or in the migration command

这篇关于将SQLite设置为Laravel 5.1中进行单元测试的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆