File tree Expand file tree Collapse file tree 3 files changed +43
-0
lines changed
tests/Integration/Database Expand file tree Collapse file tree 3 files changed +43
-0
lines changed Original file line number Diff line number Diff line change @@ -353,4 +353,21 @@ public function afterCommit($callback)
353
353
354
354
throw new RuntimeException ('Transactions Manager has not been set. ' );
355
355
}
356
+
357
+ /**
358
+ * Execute the callback after a transaction rolls back.
359
+ *
360
+ * @param callable $callback
361
+ * @return void
362
+ *
363
+ * @throws \RuntimeException
364
+ */
365
+ public function afterRollBack ($ callback )
366
+ {
367
+ if ($ this ->transactionsManager ) {
368
+ return $ this ->transactionsManager ->addCallbackForRollback ($ callback );
369
+ }
370
+
371
+ throw new RuntimeException ('Transactions Manager has not been set. ' );
372
+ }
356
373
}
Original file line number Diff line number Diff line change 114
114
* @method static void rollBack(int|null $toLevel = null)
115
115
* @method static int transactionLevel()
116
116
* @method static void afterCommit(callable $callback)
117
+ * @method static void afterRollBack(callable $callback)
117
118
*
118
119
* @see \Illuminate\Database\DatabaseManager
119
120
*/
Original file line number Diff line number Diff line change @@ -105,6 +105,31 @@ public function testTransactionsDoNotAffectDifferentConnections()
105
105
$ this ->assertTrue ($ secondObject ->ran );
106
106
$ this ->assertFalse ($ thirdObject ->ran );
107
107
}
108
+
109
+ public function testAfterRollbackCallbacksAreExecuted ()
110
+ {
111
+ $ afterCommitRan = false ;
112
+ $ afterRollbackRan = false ;
113
+
114
+ try {
115
+ DB ::transaction (function () use (&$ afterCommitRan , &$ afterRollbackRan ) {
116
+ DB ::afterCommit (function () use (&$ afterCommitRan ) {
117
+ $ afterCommitRan = true ;
118
+ });
119
+
120
+ DB ::afterRollBack (function () use (&$ afterRollbackRan ) {
121
+ $ afterRollbackRan = true ;
122
+ });
123
+
124
+ throw new \RuntimeException ('rollback ' );
125
+ });
126
+ } catch (\RuntimeException ) {
127
+ // Ignore the expected rollback exception.
128
+ }
129
+
130
+ $ this ->assertFalse ($ afterCommitRan );
131
+ $ this ->assertTrue ($ afterRollbackRan );
132
+ }
108
133
}
109
134
110
135
class TestObjectForTransactions
You can’t perform that action at this time.
0 commit comments