Skip to content

Commit

Permalink
Add event TransactionCommitting (#44608)
Browse files Browse the repository at this point in the history
  • Loading branch information
rez1dent3 authored Oct 19, 2022
1 parent 80ba056 commit 3b26f7a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Illuminate/Database/Concerns/ManagesTransactions.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function transaction(Closure $callback, $attempts = 1)

try {
if ($this->transactions == 1) {
$this->fireConnectionEvent('committing');
$this->getPdo()->commit();
}

Expand Down Expand Up @@ -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();
}

Expand Down
2 changes: 2 additions & 0 deletions src/Illuminate/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
});
Expand Down
8 changes: 8 additions & 0 deletions src/Illuminate/Database/Events/TransactionCommitting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Illuminate\Database\Events;

class TransactionCommitting extends ConnectionEvent
{
//
}
13 changes: 13 additions & 0 deletions tests/Database/DatabaseConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Illuminate\Database\Events\QueryExecuted;
use Illuminate\Database\Events\TransactionBeginning;
use Illuminate\Database\Events\TransactionCommitted;
use Illuminate\Database\Events\TransactionCommitting;
use Illuminate\Database\Events\TransactionRolledBack;
use Illuminate\Database\MultipleColumnsSelectedException;
use Illuminate\Database\Query\Builder as BaseBuilder;
Expand Down Expand Up @@ -247,6 +248,18 @@ public function testCommittedFiresEventsIfSet()
$connection->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);
Expand Down

0 comments on commit 3b26f7a

Please sign in to comment.