这就是我这样做的方式,以防其他人也在为同样的事情而苦苦挣扎,我创建了一个从Laravel的基类继承并执行以下操作:testClase
/**
* Creates the application.
*
* @return \Illuminate\Foundation\Application
*/
public function createApplication()
{
return self::initialize();
}
private static $configurationApp = null;
public static function initialize(){
if(is_null(self::$configurationApp)){
$app = require __DIR__.'/../bootstrap/app.php';
$app->loadEnvironmentFrom('.env.testing');
$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
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;
}
return self::$configurationApp;
}
public function tearDown()
{
if ($this->app) {
foreach ($this->beforeApplicationDestroyedCallbacks as $callback) {
call_user_func($callback);
}
}
$this->setUpHasRun = false;
if (property_exists($this, 'serverVariables')) {
$this->serverVariables = [];
}
if (class_exists('Mockery')) {
Mockery::close();
}
$this->afterApplicationCreatedCallbacks = [];
$this->beforeApplicationDestroyedCallbacks = [];
}
我覆盖了 和 方法。我更改了第一个以使用相同的配置,并删除了它冲洗的部分。createApplication()
tearDown()
$app
teardown()
$this->app
我的所有其他测试都必须从这个TestClass继承,仅此而已。
其他一切都不起作用。即使在内存数据库中也可以使用,它的速度也快了100倍。
如果您正在处理用户会话,一旦您登录了用户,您将不得不在拆卸中注销他,否则用户将被登录,因为应用程序环境永远不会重建,或者您可以执行类似如下操作来刷新应用程序每次您想要:
protected static $applicationRefreshed = false;
/**
* 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();
}
并将其添加到 :tearDown()
$this->setUphasRun = false;
if (self::$applicationRefreshed) {
self::$applicationRefreshed = false;
$this->app->flush();
$this->app = null;
self::$configurationApp = null;
}