aboutsummaryrefslogtreecommitdiffhomepage
path: root/main/app/tests/DatabaseTransactions.php
diff options
context:
space:
mode:
Diffstat (limited to 'main/app/tests/DatabaseTransactions.php')
-rwxr-xr-xmain/app/tests/DatabaseTransactions.php48
1 files changed, 48 insertions, 0 deletions
diff --git a/main/app/tests/DatabaseTransactions.php b/main/app/tests/DatabaseTransactions.php
new file mode 100755
index 0000000..ed2225b
--- /dev/null
+++ b/main/app/tests/DatabaseTransactions.php
@@ -0,0 +1,48 @@
+<?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];
+ }
+} \ No newline at end of file