Skip to content

Commit 67a9263

Browse files
committed
Use DB tx manager in ManagesTransactions trait
`DatabaseTransactionsManager` was introduced in Laravel to keep track of things like pending transactions and fire transaction events, so that e.g. dispatches could hook into transaction state. Our trait was not using it yet, resulting in missing `afterCommit` behavior (see PHPLIB-373)
1 parent c483b99 commit 67a9263

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

src/Concerns/ManagesTransactions.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ private function getSessionOrThrow(): Session
5555
*/
5656
public function beginTransaction(array $options = []): void
5757
{
58+
$this->transactionsManager?->begin($this->getName(), 0);
59+
5860
$this->getSessionOrCreate()->startTransaction($options);
5961
$this->transactions = 1;
6062
}
@@ -65,6 +67,8 @@ public function beginTransaction(array $options = []): void
6567
public function commit(): void
6668
{
6769
$this->getSessionOrThrow()->commitTransaction();
70+
71+
$this->transactionsManager?->commit($this->getName(), 0, 0);
6872
$this->transactions = 0;
6973
}
7074

@@ -88,6 +92,8 @@ public function transaction(Closure $callback, $attempts = 1, array $options = [
8892
$callbackResult = null;
8993
$throwable = null;
9094

95+
$this->transactionsManager?->begin($this->getName(), 0);
96+
9197
$callbackFunction = function (Session $session) use ($callback, &$attemptsLeft, &$callbackResult, &$throwable) {
9298
$attemptsLeft--;
9399

@@ -113,6 +119,8 @@ public function transaction(Closure $callback, $attempts = 1, array $options = [
113119
throw $throwable;
114120
}
115121

122+
$this->transactionsManager?->commit($this->getName(), 0, 0);
123+
116124
return $callbackResult;
117125
}
118126
}

tests/Ticket/GH3328Test.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
namespace MongoDB\Laravel\Tests\Ticket;
4+
5+
use Exception;
6+
use Illuminate\Contracts\Events\ShouldDispatchAfterCommit;
7+
use Illuminate\Foundation\Events\Dispatchable;
8+
use Illuminate\Support\Facades\DB;
9+
use Illuminate\Support\Facades\Event;
10+
use MongoDB\Laravel\Tests\TestCase;
11+
12+
use function event;
13+
14+
/**
15+
* @see https://github.com/mongodb/laravel-mongodb/issues/3328
16+
* @see https://jira.mongodb.org/browse/PHPORM-373
17+
*/
18+
class GH3328Test extends TestCase
19+
{
20+
public function testAfterCommitOnSuccessfulTransaction(): void
21+
{
22+
Event::fake();
23+
24+
try {
25+
DB::transaction(static function (): void {
26+
event(new RegularEvent());
27+
event(new AfterCommitEvent());
28+
});
29+
} catch (Exception) {
30+
}
31+
32+
Event::assertDispatched(RegularEvent::class);
33+
Event::assertDispatched(AfterCommitEvent::class);
34+
}
35+
36+
public function testAfterCommitOnFailedTransaction(): void
37+
{
38+
Event::fake();
39+
40+
try {
41+
DB::transaction(static function (): void {
42+
event(new RegularEvent());
43+
event(new AfterCommitEvent());
44+
45+
throw new Exception('Transaction failed; after commit event should not be dispatched');
46+
});
47+
} catch (Exception) {
48+
}
49+
50+
Event::assertDispatched(RegularEvent::class);
51+
Event::assertNotDispatched(AfterCommitEvent::class);
52+
}
53+
54+
public function testAfterCommitOnSuccessfulManualTransaction(): void
55+
{
56+
Event::fake();
57+
58+
DB::beginTransaction();
59+
60+
event(new RegularEvent());
61+
event(new AfterCommitEvent());
62+
63+
DB::commit();
64+
65+
Event::assertDispatched(RegularEvent::class);
66+
Event::assertDispatched(AfterCommitEvent::class);
67+
}
68+
69+
public function testAfterCommitOnUncommitedManualTransaction(): void
70+
{
71+
Event::fake();
72+
73+
DB::beginTransaction();
74+
75+
event(new RegularEvent());
76+
event(new AfterCommitEvent());
77+
78+
Event::assertDispatched(RegularEvent::class);
79+
Event::assertNotDispatched(AfterCommitEvent::class);
80+
}
81+
}
82+
83+
class AfterCommitEvent implements ShouldDispatchAfterCommit
84+
{
85+
use Dispatchable;
86+
}
87+
88+
class RegularEvent
89+
{
90+
use Dispatchable;
91+
}

0 commit comments

Comments
 (0)