Skip to content

Commit

Permalink
Cover possible no-db related throw before touching the db
Browse files Browse the repository at this point in the history
  • Loading branch information
simPod committed Oct 12, 2024
1 parent f0e53dc commit 88879b3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1291,7 +1291,13 @@ public function transactional(Closure $func)
}
}

$this->commit();
try {
$this->commit();
} finally {
if ($this->isTransactionActive()) {
$this->rollBack();
}
}

return $res;
}
Expand Down
22 changes: 22 additions & 0 deletions tests/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,28 @@ public function rollBack(): void
self::assertSame('Original exception', $e->getPrevious()->getMessage());
}
}

/**
* We are not sure if this can happen in real life scenario
*/
public function testItFailsDuringCommitBeforeTouchingDb(): void
{
$connection = new class (['memory' => true], new Driver\SQLite3\Driver()) extends Connection {
public function commit(): void
{
throw new \Exception('Fail before touching the db');
}

public function rollBack(): void
{
throw new \Exception('Rollback got triggered');
}
};

$this->expectExceptionMessage('Rollback got triggered');
$connection->transactional(static function (): void {
});
}
}

interface ConnectDispatchEventListener
Expand Down

0 comments on commit 88879b3

Please sign in to comment.