Files
UNIT3D-Community-Edition/tests/TestCase.php
Ben Johnson 38d8b3cf79 feat: Reduce test suite execution time considerably
Given the sheer volume of migrations, it takes about 40 seconds on an average system to execute them all prior to running the test suite.

Loading one flat SQL file instead of executing each migration reduces this overhead significantly.

This is implemented in a way that degrades gracefully; if the flat file is not specified, all migrations will run as normal.
2020-02-18 15:45:54 -05:00

68 lines
1.8 KiB
PHP
Executable File

<?php
namespace Tests;
use App\Console\Kernel;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\RefreshDatabaseState;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use JMac\Testing\Traits\HttpTestAssertions;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
use HttpTestAssertions;
use RefreshDatabase;
/**
* {@inheritdoc}
*/
protected function setUp(): void
{
parent::setUp();
$this->artisan('config:clear');
$this->artisan('route:clear');
$this->artisan('cache:clear');
}
/**
* Overrides the method in the default trait.
*
* @see https://alexvanderbist.com/posts/2019/how-migrations-might-be-slowing-down-your-laravel-tests
*/
protected function refreshTestDatabase()
{
if (!RefreshDatabaseState::$migrated) {
if (config('database.pristine-db-file')) {
// If a flat file is defined, load it.
$this->artisan('db:load --quiet');
$this->artisan('migrate');
} else {
// Otherwise, proceed using default strategy.
$this->artisan('migrate:fresh', [
'--drop-views' => $this->shouldDropViews(),
'--drop-types' => $this->shouldDropTypes(),
]);
}
$this->app[Kernel::class]->setArtisan(null);
RefreshDatabaseState::$migrated = true;
}
$connectionsNotToTransact = ['sqlite', 'pgsql', 'sqlsrv'];
$this->connectionsToTransact = array_keys(
array_diff_key(config('database.connections'), array_flip($connectionsNotToTransact))
);
$this->beginDatabaseTransaction();
}
}