From 3b26f7a1ef2f04a3c1dfd6454eed4385e2399152 Mon Sep 17 00:00:00 2001 From: Maxim Babichev Date: Wed, 19 Oct 2022 17:05:52 +0300 Subject: [PATCH] Add event TransactionCommitting (#44608) --- .../Database/Concerns/ManagesTransactions.php | 4 +++- src/Illuminate/Database/Connection.php | 2 ++ .../Database/Events/TransactionCommitting.php | 8 ++++++++ tests/Database/DatabaseConnectionTest.php | 13 +++++++++++++ 4 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 src/Illuminate/Database/Events/TransactionCommitting.php diff --git a/src/Illuminate/Database/Concerns/ManagesTransactions.php b/src/Illuminate/Database/Concerns/ManagesTransactions.php index 4d232f2f3c8a..2f316a393930 100644 --- a/src/Illuminate/Database/Concerns/ManagesTransactions.php +++ b/src/Illuminate/Database/Concerns/ManagesTransactions.php @@ -43,6 +43,7 @@ public function transaction(Closure $callback, $attempts = 1) try { if ($this->transactions == 1) { + $this->fireConnectionEvent('committing'); $this->getPdo()->commit(); } @@ -188,7 +189,8 @@ protected function handleBeginTransactionException(Throwable $e) */ public function commit() { - if ($this->transactions == 1) { + if ($this->transactionLevel() == 1) { + $this->fireConnectionEvent('committing'); $this->getPdo()->commit(); } diff --git a/src/Illuminate/Database/Connection.php b/src/Illuminate/Database/Connection.php index 5eaf728effbf..b1c4c99360c3 100755 --- a/src/Illuminate/Database/Connection.php +++ b/src/Illuminate/Database/Connection.php @@ -13,6 +13,7 @@ use Illuminate\Database\Events\StatementPrepared; use Illuminate\Database\Events\TransactionBeginning; use Illuminate\Database\Events\TransactionCommitted; +use Illuminate\Database\Events\TransactionCommitting; use Illuminate\Database\Events\TransactionRolledBack; use Illuminate\Database\Query\Builder as QueryBuilder; use Illuminate\Database\Query\Expression; @@ -978,6 +979,7 @@ protected function fireConnectionEvent($event) return $this->events?->dispatch(match ($event) { 'beganTransaction' => new TransactionBeginning($this), 'committed' => new TransactionCommitted($this), + 'committing' => new TransactionCommitting($this), 'rollingBack' => new TransactionRolledBack($this), default => null, }); diff --git a/src/Illuminate/Database/Events/TransactionCommitting.php b/src/Illuminate/Database/Events/TransactionCommitting.php new file mode 100644 index 000000000000..9b8179d32d0b --- /dev/null +++ b/src/Illuminate/Database/Events/TransactionCommitting.php @@ -0,0 +1,8 @@ +commit(); } + public function testCommittingFiresEventsIfSet() + { + $pdo = $this->createMock(DatabaseConnectionTestMockPDO::class); + $connection = $this->getMockConnection(['getName', 'transactionLevel'], $pdo); + $connection->expects($this->any())->method('getName')->willReturn('name'); + $connection->expects($this->any())->method('transactionLevel')->willReturn(1); + $connection->setEventDispatcher($events = m::mock(Dispatcher::class)); + $events->shouldReceive('dispatch')->once()->with(m::type(TransactionCommitting::class)); + $events->shouldReceive('dispatch')->once()->with(m::type(TransactionCommitted::class)); + $connection->commit(); + } + public function testRollBackedFiresEventsIfSet() { $pdo = $this->createMock(DatabaseConnectionTestMockPDO::class);