<?php /** * UserFrosting (http://www.userfrosting.com) * * @link https://github.com/userfrosting/UserFrosting * @license https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License) */ namespace UserFrosting\Tests; /** * Trait enabling wrapping of each test case in a database transaction * Based on Laravel `DatabaseTransactions` Traits * * @author Louis Charette */ trait DatabaseTransactions { /** * Handle database transactions on the specified connections. * * @return void */ public function beginDatabaseTransaction() { $database = $this->ci['db']; foreach ($this->connectionsToTransact() as $name) { $database->connection($name)->beginTransaction(); } $this->beforeApplicationDestroyed(function () use ($database) { foreach ($this->connectionsToTransact() as $name) { $database->connection($name)->rollBack(); } }); } /** * The database connections that should have transactions. * * @return array */ protected function connectionsToTransact() { return property_exists($this, 'connectionsToTransact') ? $this->connectionsToTransact : [null]; } }