【发布时间】:2019-05-05 18:06:30
【问题描述】:
在我的 Lumen 应用中,当我执行时
php artisan migrate --seed
效果很好。
但是当我尝试使用 phpunit 运行测试时,它不会从我编码的 Laravel 包运行迁移,因此所有测试都失败
我在我的测试中运行我的迁移:
Artisan::call('migrate');
我用于内存测试以加快运行速度。
这是我的 Lumen 应用 Testcase.php
abstract class TestCase extends Laravel\Lumen\Testing\TestCase
{
/** @var array */
protected $dispatchedNotifications = [];
protected static $applicationRefreshed = false;
/**
* Creates the application.
*
* @return \Laravel\Lumen\Application
*/
/**
* Creates the application.
*
*/
public function createApplication()
{
return self::initialize();
}
private static $configurationApp = null;
public static function initialize()
{
$app = require __DIR__ . '/../bootstrap/app.php';
if (is_null(self::$configurationApp)) {
$app->environment('testing');
if (config('database.default') == 'sqlite') {
$db = app()->make('db');
$db->connection()->getPdo()->exec("pragma foreign_keys=1");
}
Artisan::call('migrate');
Artisan::call('db:seed');
self::$configurationApp = $app;
}
return $app;
}
/**
* Refresh the application instance.
*
* @return void
*/
protected function forceRefreshApplication()
{
if (!is_null($this->app)) {
$this->app->flush();
}
$this->app = null;
self::$configurationApp = null;
self::$applicationRefreshed = true;
parent::refreshApplication();
}
...
在我的包中,我在服务提供者的启动方法中使用:
$this->loadMigrationsFrom(__DIR__.'/../database/migrations');
然后是一个测试示例:
class TournamentsTest extends TestCase
{
use DatabaseTransactions, AttachJwtToken;
protected $initialTournamentNum = 6;
protected $defaultPagintation = 25;
protected $user;
/** @test */
public function user_can_see_tournament_list()
{
$response = $this
->call('GET', '/tournaments');
$this->assertEquals(HttpResponse::HTTP_OK, $response->status());
}
...
我所有的测试都失败了:
PDOException: SQLSTATE[HY000]: General error: 1 no such table: ken_venue
ken_venue 是一张来自 laravel 包的表格
事实上,我有同样的包在 Laravel 5.7 应用程序中运行良好。但我正在将此应用程序迁移到 Lumen 应用程序。
知道为什么会这样吗?
【问题讨论】:
-
您是否将包中的迁移文件发布到您的迁移文件夹中?
-
没有。我认为目前没有必要