Skip to content

Commit 16fb507

Browse files
committed
Fix
1 parent cf16d3c commit 16fb507

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

packages/database/src/Connection/ConnectionReset.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,15 @@ public function __construct(
1515

1616
public function reset(): void
1717
{
18+
// Manually looping over the connection singletons so that we can check whether they still have an active transaction
1819
if ($this->container instanceof GenericContainer) {
19-
/** @var Connection[] $connections */
2020
$connections = $this->container->getSingletons(Connection::class);
2121

2222
foreach ($connections as $connection) {
23+
if (! $connection instanceof Connection) {
24+
continue;
25+
}
26+
2327
if ($connection->inTransaction()) {
2428
throw new CouldNotResetConnection("There's still an active transaction, make sure to close it before ending the request");
2529
}

tests/Integration/Database/Connection/ConnectionResetTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Tests\Tempest\Integration\Database\Connection;
44

5+
use Exception;
56
use PHPUnit\Framework\Attributes\Test;
67
use Tempest\Database\Connection\Connection;
78
use Tempest\Database\Exceptions\CouldNotResetConnection;
@@ -38,4 +39,22 @@ public function test_properly_closed_transaction_can_reset_connection(): void
3839
$newConnection = $this->container->get(Connection::class);
3940
$this->assertNotSame($connection, $newConnection);
4041
}
42+
43+
#[Test]
44+
public function test_reset_with_uninstantiated_singletons(): void
45+
{
46+
$this->container->singleton(
47+
Connection::class,
48+
fn () => throw new Exception('Should not happen'),
49+
tag: 'other',
50+
);
51+
52+
/** @var Connection $connection */
53+
$connection = $this->container->get(Connection::class);
54+
55+
$this->container->reset();
56+
57+
$newConnection = $this->container->get(Connection::class);
58+
$this->assertNotSame($connection, $newConnection);
59+
}
4160
}

0 commit comments

Comments
 (0)