Tests: Always start transaction before seeding

This commit is contained in:
Franz Liedke 2020-05-21 22:35:06 +02:00 committed by Alexander Skvortsov
parent 663f7a5d32
commit 6ca9191f42

View File

@ -23,13 +23,6 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase
{
use BuildsHttpRequests;
protected function setUp(): void
{
parent::setUp();
$this->database()->beginTransaction();
}
protected function tearDown(): void
{
parent::tearDown();
@ -61,6 +54,10 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase
$site->extendWith($this->extenders);
$this->app = $site->bootApp();
$this->database()->beginTransaction();
$this->populateDatabase();
}
return $this->app;
@ -103,13 +100,23 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase
return $this->database;
}
protected $databaseContent = [];
protected function prepareDatabase(array $tableData)
{
$this->databaseContent = array_merge_recursive(
$this->databaseContent,
$tableData
);
}
protected function populateDatabase()
{
// We temporarily disable foreign key checks to simplify this process.
$this->database()->getSchemaBuilder()->disableForeignKeyConstraints();
// Then, insert all rows required for this test case.
foreach ($tableData as $table => $rows) {
foreach ($this->databaseContent as $table => $rows) {
foreach ($rows as $row) {
if ($table === 'settings') {
$this->database()->table($table)->updateOrInsert(