Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 91ca67e

Browse files
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 91ca67e

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

‎src/Concerns/ManagesTransactions.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace MongoDB\Laravel\Concerns;
66

77
use Closure;
8+
use Illuminate\Tests\Validation\ValidationArrayRuleTest;
89
use MongoDB\Client;
910
use MongoDB\Driver\Exception\RuntimeException;
1011
use MongoDB\Driver\Session;
@@ -55,6 +56,8 @@ private function getSessionOrThrow(): Session
5556
*/
5657
public function beginTransaction(array $options = []): void
5758
{
59+
$this->transactionsManager?->begin($this->getName(), 0);
60+
5861
$this->getSessionOrCreate()->startTransaction($options);
5962
$this->transactions = 1;
6063
}
@@ -65,6 +68,8 @@ public function beginTransaction(array $options = []): void
6568
public function commit(): void
6669
{
6770
$this->getSessionOrThrow()->commitTransaction();
71+
72+
$this->transactionsManager?->commit($this->getName(), 0, 0);
6873
$this->transactions = 0;
6974
}
7075

@@ -88,6 +93,8 @@ public function transaction(Closure $callback, $attempts = 1, array $options = [
8893
$callbackResult = null;
8994
$throwable = null;
9095

96+
$this->transactionsManager?->begin($this->getName(), 0);
97+
9198
$callbackFunction = function (Session $session) use ($callback, &$attemptsLeft, &$callbackResult, &$throwable) {
9299
$attemptsLeft--;
93100

@@ -107,12 +114,15 @@ public function transaction(Closure $callback, $attempts = 1, array $options = [
107114
}
108115
};
109116

117+
110118
with_transaction($this->getSessionOrCreate(), $callbackFunction, $options);
111119

112120
if ($attemptsLeft < 0 && $throwable) {
113121
throw $throwable;
114122
}
115123

124+
$this->transactionsManager?->commit($this->getName(), 0, 0);
125+
116126
return $callbackResult;
117127
}
118128
}

‎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)

AltStyle によって変換されたページ (->オリジナル) /