Skip to content

Commit

Permalink
Add DatabaseTransactions trait (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
mfn authored and fntneves committed Sep 4, 2019
1 parent aa41f4e commit 42a27bf
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/Neves/Testing/DatabaseTransactions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Neves\Testing;

use Illuminate\Foundation\Testing\DatabaseTransactions as BaseDatabaseTransactions;

trait DatabaseTransactions
{
use BaseDatabaseTransactions;

public function beginDatabaseTransaction()
{
$emptyDispatcher = new \Illuminate\Events\Dispatcher;
$database = $this->app->make('db');

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

$currentDispatcher = $connection->getEventDispatcher();
$connection->setEventDispatcher($emptyDispatcher);
$connection->beginTransaction();
$connection->setEventDispatcher($currentDispatcher);
}

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

$currentDispatcher = $connection->getEventDispatcher();
$connection->setEventDispatcher($emptyDispatcher);
$connection->rollBack();
$connection->setEventDispatcher($currentDispatcher);

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

0 comments on commit 42a27bf

Please sign in to comment.