在 Laravel PHPUnit 测试中使用内存中的 SQLite
Use in-memory SQLite in Laravel PHPUnit test
我正试图在我的新工作中获得一个 Laravel 项目 运行。离开的开发者在交接文件中没有说明代码是如何上来的运行,其他人也不知道。
SQLite 数据库是用 :memory:
创建的,php artisan migrate
没有错误。
...
Migrating: 2016_06_01_000004_create_oauth_clients_table
Migrated: 2016_06_01_000004_create_oauth_clients_table
...
正在执行特征(标准Laravel代码)中的方法beginDatabaseTransaction()
,但之后存在none个表。好像正在创建数据库但没有执行迁移。
<?php
namespace Illuminate\Foundation\Testing;
trait DatabaseTransactions
{
/**
* Handle database transactions on the specified connections.
*
* @return void
*/
public function beginDatabaseTransaction()
{
$database = $this->app->make('db');
foreach ($this->connectionsToTransact() as $name) {
$database->connection($name)->beginTransaction();
}
$this->beforeApplicationDestroyed(function () use ($database) {
foreach ($this->connectionsToTransact() as $name) {
$connection = $database->connection($name);
$connection->rollBack();
$connection->disconnect();
}
});
}
测试代码(最后显示的指令失败):
<?php
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Laravel\Passport\ClientRepository;
class PassportTest extends TestCase
{
use DatabaseTransactions;
public function test_basic_oauth_token_authentication(){
$clientRepository = new ClientRepository();
$client = $clientRepository->createPersonalAccessClient(
null, 'Test Personal Access Client', $this->baseUrl
);
命令行:
vendor/bin/phpunit tests/Feature/PassPortTest
错误信息:
Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1 no such table: oauth_clients
Caused by
Doctrine\DBAL\Driver\PDOException: SQLSTATE[HY000]: General error: 1 no such table: oauth_clients
Caused by
PDOException: SQLSTATE[HY000]: General error: 1 no such table: oauth_clients
如何确保表已创建为每个测试的一部分?
在您的 SQLite 配置中使用 :memory:
时需要注意。您的命令行迁移命中的内存位置 不是 测试试图与之交谈的内存位置。您的应用程序的每个实例基本上都会启动一个新的虚拟临时数据库。
你有两个选择。用显式文件名替换 :memory:
配置,或者使测试的 setUp()
方法实际启动 \Aritsan::call('migrate')
调用。不过我不推荐后者,只是因为它可能会很慢,而且我不完全确定如果您当时有活动的事务,迁移实际上会坚持下去。
使用 RefreshDatabase
特征而不是 DatabaseTransactions
特征(如果您尝试在同一个 class 中使用两者,它们会发生冲突)。
查看有关 resetting the database after each test 的文档。
我正试图在我的新工作中获得一个 Laravel 项目 运行。离开的开发者在交接文件中没有说明代码是如何上来的运行,其他人也不知道。
SQLite 数据库是用 :memory:
创建的,php artisan migrate
没有错误。
...
Migrating: 2016_06_01_000004_create_oauth_clients_table
Migrated: 2016_06_01_000004_create_oauth_clients_table
...
正在执行特征(标准Laravel代码)中的方法beginDatabaseTransaction()
,但之后存在none个表。好像正在创建数据库但没有执行迁移。
<?php
namespace Illuminate\Foundation\Testing;
trait DatabaseTransactions
{
/**
* Handle database transactions on the specified connections.
*
* @return void
*/
public function beginDatabaseTransaction()
{
$database = $this->app->make('db');
foreach ($this->connectionsToTransact() as $name) {
$database->connection($name)->beginTransaction();
}
$this->beforeApplicationDestroyed(function () use ($database) {
foreach ($this->connectionsToTransact() as $name) {
$connection = $database->connection($name);
$connection->rollBack();
$connection->disconnect();
}
});
}
测试代码(最后显示的指令失败):
<?php
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Laravel\Passport\ClientRepository;
class PassportTest extends TestCase
{
use DatabaseTransactions;
public function test_basic_oauth_token_authentication(){
$clientRepository = new ClientRepository();
$client = $clientRepository->createPersonalAccessClient(
null, 'Test Personal Access Client', $this->baseUrl
);
命令行:
vendor/bin/phpunit tests/Feature/PassPortTest
错误信息:
Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1 no such table: oauth_clients Caused by Doctrine\DBAL\Driver\PDOException: SQLSTATE[HY000]: General error: 1 no such table: oauth_clients Caused by PDOException: SQLSTATE[HY000]: General error: 1 no such table: oauth_clients
如何确保表已创建为每个测试的一部分?
在您的 SQLite 配置中使用 :memory:
时需要注意。您的命令行迁移命中的内存位置 不是 测试试图与之交谈的内存位置。您的应用程序的每个实例基本上都会启动一个新的虚拟临时数据库。
你有两个选择。用显式文件名替换 :memory:
配置,或者使测试的 setUp()
方法实际启动 \Aritsan::call('migrate')
调用。不过我不推荐后者,只是因为它可能会很慢,而且我不完全确定如果您当时有活动的事务,迁移实际上会坚持下去。
使用 RefreshDatabase
特征而不是 DatabaseTransactions
特征(如果您尝试在同一个 class 中使用两者,它们会发生冲突)。
查看有关 resetting the database after each test 的文档。