Skip to content

Commit

Permalink
[5.6] Fixes starting of two database transactions in tests (#23132)
Browse files Browse the repository at this point in the history
* Fixes starting of two database transactions in tests

* indentation fixed
  • Loading branch information
skollro authored and taylorotwell committed Feb 12, 2018
1 parent 06c7160 commit f97e57b
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions src/Illuminate/Foundation/Testing/RefreshDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

trait RefreshDatabase
{
use DatabaseTransactions;

/**
* Define hooks to migrate the database before and after each test.
*
Expand Down Expand Up @@ -61,4 +59,38 @@ protected function refreshTestDatabase()

$this->beginDatabaseTransaction();
}

/**
* Begin a database transaction on the testing database.
*
* @return void
*/
public function beginDatabaseTransaction()
{
$database = $this->app->make('db');

foreach ($this->connectionsToTransact() as $name) {
$database->connection($name)->beginTransaction();
}

$this->beforeApplicationDestroyed(function () use ($database) {
foreach ($this->connectionsToTransact() as $name) {
$connection = $database->connection($name);

$connection->rollBack();
$connection->disconnect();
}
});
}

/**
* The database connections that should have transactions.
*
* @return array
*/
protected function connectionsToTransact()
{
return property_exists($this, 'connectionsToTransact')
? $this->connectionsToTransact : [null];
}
}

0 comments on commit f97e57b

Please sign in to comment.