Skip to content

Commit

Permalink
Adds "setUpTestDatabase" support to Parallel Testing (#36301)
Browse files Browse the repository at this point in the history
  • Loading branch information
nunomaduro authored Feb 18, 2021
1 parent a48d4b8 commit aeb0bf5
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 14 deletions.
36 changes: 22 additions & 14 deletions src/Illuminate/Testing/Concerns/TestDatabases.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,17 @@ protected function bootTestDatabase()

if (Arr::hasAny($uses, $databaseTraits)) {
$this->whenNotUsingInMemoryDatabase(function ($database) use ($uses) {
$testDatabase = $this->ensureTestDatabaseExists($database);
[$testDatabase, $created] = $this->ensureTestDatabaseExists($database);

$this->switchToDatabase($testDatabase);

if (isset($uses[Testing\DatabaseTransactions::class])) {
$this->ensureSchemaIsUpToDate();
}

if ($created) {
ParallelTesting::callSetUpTestDatabaseCallbacks($testDatabase);
}
});
}
});
Expand All @@ -64,22 +68,26 @@ protected function bootTestDatabase()
*
* @param string $database
*
* @return string
* @return array
*/
protected function ensureTestDatabaseExists($database)
{
return tap($this->testDatabase($database), function ($testDatabase) use ($database) {
try {
$this->usingDatabase($testDatabase, function () {
Schema::hasTable('dummy');
});
} catch (QueryException $e) {
$this->usingDatabase($database, function () use ($testDatabase) {
Schema::dropDatabaseIfExists($testDatabase);
Schema::createDatabase($testDatabase);
});
}
});
$testDatabase = $this->testDatabase($database);

try {
$this->usingDatabase($testDatabase, function () {
Schema::hasTable('dummy');
});
} catch (QueryException $e) {
$this->usingDatabase($database, function () use ($testDatabase) {
Schema::dropDatabaseIfExists($testDatabase);
Schema::createDatabase($testDatabase);
});

return [$testDatabase, true];
}

return [$testDatabase, false];
}

/**
Expand Down
36 changes: 36 additions & 0 deletions src/Illuminate/Testing/ParallelTesting.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ class ParallelTesting
*/
protected $setUpTestCaseCallbacks = [];

/**
* All of the registered "setUp" test database callbacks.
*
* @var array
*/
protected $setUpTestDatabaseCallbacks = [];

/**
* All of the registered "tearDown" process callbacks.
*
Expand Down Expand Up @@ -111,6 +118,17 @@ public function setUpTestCase($callback)
$this->setUpTestCaseCallbacks[] = $callback;
}

/**
* Register a "setUp" test database callback.
*
* @param callable $callback
* @return void
*/
public function setUpTestDatabase($callback)
{
$this->setUpTestDatabaseCallbacks[] = $callback;
}

/**
* Register a "tearDown" process callback.
*
Expand Down Expand Up @@ -167,6 +185,24 @@ public function callSetUpTestCaseCallbacks($testCase)
});
}

/**
* Call all of the "setUp" test database callbacks.
*
* @param string $database
* @return void
*/
public function callSetUpTestDatabaseCallbacks($database)
{
$this->whenRunningInParallel(function () use ($database) {
foreach ($this->setUpTestDatabaseCallbacks as $callback) {
$this->container->call($callback, [
'database' => $database,
'token' => $this->token(),
]);
}
});
}

/**
* Call all of the "tearDown" process callbacks.
*
Expand Down
1 change: 1 addition & 0 deletions tests/Testing/ParallelTestingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public function callbacks()
return [
['setUpProcess'],
['setUpTestCase'],
['setUpTestDatabase'],
['tearDownTestCase'],
['tearDownProcess'],
];
Expand Down

0 comments on commit aeb0bf5

Please sign in to comment.