在 Laravel 5.1 中将 SQLite 设置为用于单元测试的数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31132270/
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
Set SQLite as database for unit testing in Laravel 5.1
提问by lesssugar
I'm trying to set-up unit testing in Laravel 5.1. Following the documentationI see this:
我正在尝试在 Laravel 5.1 中设置单元测试。按照文档,我看到了这一点:
Laravel is built with testing in mind. In fact, support for testing with PHPUnit is included out of the box
Laravel 在构建时就考虑到了测试。事实上,对使用 PHPUnit 进行测试的支持是开箱即用的
and
和
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 会自动将配置环境设置为测试。Laravel 在测试时自动将会话和缓存配置到数组驱动程序,这意味着测试时不会持久化会话或缓存数据。
which is awesome. But... how do I tell Laravel to use SQLite database connection when tests are ran?
这太棒了。但是......我如何告诉 Laravel 在运行测试时使用 SQLite 数据库连接?
Only thing there is in config/database.php is this commented code:
config/database.php 中唯一的内容是以下注释代码:
/* We might use this connection for unit tests
'sqlite' => [
'driver' => 'sqlite',
'database' => storage_path().'/database.sqlite',
'prefix' => '',
],
*/
We might use this:)
我们可能会使用这个:)
Is there a way to set this globally for all test? Do I need to set the connecton in every test case?
有没有办法为所有测试全局设置它?我是否需要在每个测试用例中设置连接?
Any help appreciated.
任何帮助表示赞赏。
回答by Fabio Antunes
Actually it's pretty simple.
其实很简单。
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:
在您的storage/
文件夹上创建一个测试数据库,使用名称database.sqlite
或者如果您想要其他名称或其他位置,您必须更改config/database.php
文件上的配置,这些是默认配置:
'sqlite' => [
'driver' => 'sqlite',
'database' => storage_path('database.sqlite'),
'prefix' => '',
],
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:
您有两个选择,您可以编辑.env
或指定要运行迁移的数据库的名称:
php artisan migrate --database=sqlite
If you prefer editing your .env
file, then we have to add a new variable:
如果您更喜欢编辑.env
文件,那么我们必须添加一个新变量:
DB_CONNECTION=sqlite
That's because Laravel defaults to MySQL when this variable is absent from .env
file:
那是因为当.env
文件中缺少此变量时,Laravel 默认使用 MySQL :
//config/database.php file
'default' => env('DB_CONNECTION', 'mysql'),
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 数据库,我们现在可以运行我们的迁移和播种。之后,如果您只想继续运行 MySQL,请DB_CONNECTION=sqlite
从.env
文件中删除。
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.
现在您已经在测试数据库上进行了迁移,最后一步就是指定您的 SQLite 作为测试的默认数据库。
On your root folder you have a phpunit.xml
file, open it and a new variable under <php>
node:
在您的根文件夹中,您有一个phpunit.xml
文件,打开它并在<php>
node下添加一个新变量:
<env name="DB_CONNECTION" value="sqlite"/>
Now your application is using MySQL and phpunitis using SQLite.
现在您的应用程序正在使用 MySQL,而phpunit正在使用 SQLite。
Rememberthat if you change the .env
to change it back to your default database;
请记住,如果您更改 将.env
其更改回您的默认数据库;
PS
聚苯乙烯
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');
}
}
This migration will always run on the connection manageno matter what you specify on the .env
file or in the migration command
无论您在文件或迁移命令中指定什么,此迁移都将始终在连接管理器上运行.env
回答by cw24
I have a bit of a different solution. I am using an in memory sqlite database.
我有一些不同的解决方案。我正在使用内存中的 sqlite 数据库。
first I change the default and then add a new connection to the database.php in config
首先我更改默认值,然后在配置中添加一个到 database.php 的新连接
'default' => env('DB_CONNECTION', 'mysql'),
...
'testing' => [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
],
then I add the following line to the phpunit.xml
然后我将以下行添加到 phpunit.xml
<env name="DB_CONNECTION" value="testing"/>
I leave the .env file untouched. That way the database connection will default to 'mysql'
我保持 .env 文件不变。这样数据库连接将默认为'mysql'
Here is the full phpunit.xml for more clarity
为了更清晰,这里是完整的 phpunit.xml
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="bootstrap/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false">
<testsuites>
<testsuite name="Application Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">app/</directory>
</whitelist>
</filter>
<logging>
<log type="coverage-html" target="./report/" charset="UTF-8"
yui="true" highlight="false"
lowUpperBound="50" highLowerBound="80"/>
</logging>
<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"/>
<env name="DB_CONNECTION" value="testing"/>
</php>
</phpunit>