执行 phpunit 时 Lumen 不迁移包表
Lumen is not migrating package tables when executing phpunit
在我的 Lumen 应用程序中,当我执行
php artisan migrate --seed
效果不错。
但是当我尝试 运行 我的测试 phpunit
时,它不会 运行 从我编码的 Laravel 包迁移,所以所有测试都失败了
我 运行 我的迁移在我的测试中使用 :
Artisan::call('migrate');
我在内存测试中使用更快运行ning。
这是我的 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 包的 table
事实上,我在 Laravel 5.7 应用程序中使用了同样的软件包。但我正在将此应用迁移到 Lumen 应用。
知道为什么会这样吗?
先说几点:
你的测试函数不是以 test
开头的,对我来说 Laravel 这样的测试不会执行。
第二次尝试 运行 种子而不是手动调用 migrate 和 seed
namespace Tests\Unit;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Tests\TestCase;
class ShippingCostTest extends TestCase
{
use DatabaseMigrations; // call migrations this way
public function testDoesApply() // start your function with the word 'test'
$this->assertTrue(true); // call assert functions
}
protected function setUp() // use this function for setup
{
parent::setUp();
$this->seed(); // call this for seeding
}
}
第三:你是否使用相同的数据库类型(例如MySQL在这两种情况下),因为你使用sqlite进行测试,语法可能会因为系统之间的差异而突然中断。
在我的 Lumen 应用程序中,当我执行
php artisan migrate --seed
效果不错。
但是当我尝试 运行 我的测试 phpunit
时,它不会 运行 从我编码的 Laravel 包迁移,所以所有测试都失败了
我 运行 我的迁移在我的测试中使用 :
Artisan::call('migrate');
我在内存测试中使用更快运行ning。
这是我的 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 包的 table
事实上,我在 Laravel 5.7 应用程序中使用了同样的软件包。但我正在将此应用迁移到 Lumen 应用。
知道为什么会这样吗?
先说几点:
你的测试函数不是以 test
开头的,对我来说 Laravel 这样的测试不会执行。
第二次尝试 运行 种子而不是手动调用 migrate 和 seed
namespace Tests\Unit;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Tests\TestCase;
class ShippingCostTest extends TestCase
{
use DatabaseMigrations; // call migrations this way
public function testDoesApply() // start your function with the word 'test'
$this->assertTrue(true); // call assert functions
}
protected function setUp() // use this function for setup
{
parent::setUp();
$this->seed(); // call this for seeding
}
}
第三:你是否使用相同的数据库类型(例如MySQL在这两种情况下),因为你使用sqlite进行测试,语法可能会因为系统之间的差异而突然中断。