Last active
August 29, 2015 14:21
-
-
Save jatubio/09d4ca187aed91560a09 to your computer and use it in GitHub Desktop.
Laravel 5.1 - Using DatabaseTransactions with custom connections on Testing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace JaTubio\Testing; | |
trait DatabaseTransactions { | |
protected function getConnectionName() | |
{ | |
$model = $this->getModel(); | |
if ( null !== $model ) | |
{ | |
return $model->getConnectionName(); | |
} | |
return null; | |
} | |
/** | |
* @return \Portal\Entities\Model | |
*/ | |
protected function getModel() | |
{ | |
return null; | |
} | |
/** | |
* @before | |
*/ | |
public function beginDatabaseTransaction() | |
{ | |
$this->app->make('db')->setDefaultConnection($this->getConnectionName()); | |
$this->app->make('db')->beginTransaction(); | |
$this->beforeApplicationDestroyed(function () | |
{ | |
$this->app->make('db')->rollBack(); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use samples: