From e0ab415828262fc2971228662f4a56b7fc4edce7 Mon Sep 17 00:00:00 2001 From: Andrii Date: Wed, 22 Oct 2025 15:49:48 +0200 Subject: [PATCH 1/4] Add posibility to close the connection --- src/Database.php | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/Database.php b/src/Database.php index 5b54aea..41f8b8b 100644 --- a/src/Database.php +++ b/src/Database.php @@ -44,9 +44,9 @@ class Database /** * PDO connection * - * @var \PDO $connection + * @var \PDO|null $connection */ - private \PDO $connection; + private ?\PDO $connection; /** * Driver @@ -128,9 +128,26 @@ public static function create( */ public function getConnection(): \PDO { + if ($this->connection === null) { + throw new Exception('Connection is null'); + } + return $this->connection; } + /** + * Close the PDO connection by setting the property to null + * + * Note that in order for the connection to be closed, all it's instances + * must be set to null + * + * @return void + */ + public function closeConnection(): void + { + $this->connection = null; + } + /** * Set a new PDO connection * From b15a770f71dc8d6c43ff9312bb27a8311f40db01 Mon Sep 17 00:00:00 2001 From: Andrii Date: Wed, 22 Oct 2025 15:53:52 +0200 Subject: [PATCH 2/4] Add test for closeConnection --- tests/base/BaseDatabaseTest.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/base/BaseDatabaseTest.php b/tests/base/BaseDatabaseTest.php index 73428fb..e136b16 100644 --- a/tests/base/BaseDatabaseTest.php +++ b/tests/base/BaseDatabaseTest.php @@ -325,4 +325,18 @@ public function testWithIncludeSoftDeleted(): void $user = $db->findOne(User::class, 1); $this->assertNotNull($user); } + + public function testCloseConnection(): void + { + $db = static::createDatabaseWithDummyData(); + + $connection = $db->getConnection(); + self::assertInstanceOf(PDO::class, $connection); + + $db->closeConnection(); + + self::expectException(Exception::class); + self::expectExceptionMessage('Connection is null'); + $db->getConnection(); + } } From 78731eff922f4550880ba722f4d761f522ff1269 Mon Sep 17 00:00:00 2001 From: Andrii Date: Wed, 22 Oct 2025 16:19:21 +0200 Subject: [PATCH 3/4] Introduce ClosedConnectionException class --- src/Database.php | 3 ++- src/Exception/ClosedConnectionException.php | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 src/Exception/ClosedConnectionException.php diff --git a/src/Database.php b/src/Database.php index 41f8b8b..f49b260 100644 --- a/src/Database.php +++ b/src/Database.php @@ -19,6 +19,7 @@ use Access\Driver\Sqlite; use Access\Entity; use Access\Exception; +use Access\Exception\ClosedConnectionException; use Access\Lock; use Access\Presenter; use Access\Presenter\EntityPresenter; @@ -129,7 +130,7 @@ public static function create( public function getConnection(): \PDO { if ($this->connection === null) { - throw new Exception('Connection is null'); + throw new ClosedConnectionException(); } return $this->connection; diff --git a/src/Exception/ClosedConnectionException.php b/src/Exception/ClosedConnectionException.php new file mode 100644 index 0000000..c716303 --- /dev/null +++ b/src/Exception/ClosedConnectionException.php @@ -0,0 +1,13 @@ + Date: Wed, 22 Oct 2025 16:20:36 +0200 Subject: [PATCH 4/4] Change the test --- tests/base/BaseDatabaseTest.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/base/BaseDatabaseTest.php b/tests/base/BaseDatabaseTest.php index e136b16..db8f09b 100644 --- a/tests/base/BaseDatabaseTest.php +++ b/tests/base/BaseDatabaseTest.php @@ -15,6 +15,7 @@ use Access\Database; use Access\Exception; +use Access\Exception\ClosedConnectionException; use Access\Query; use PDO; use PHPUnit\Framework\TestCase; @@ -335,8 +336,8 @@ public function testCloseConnection(): void $db->closeConnection(); - self::expectException(Exception::class); - self::expectExceptionMessage('Connection is null'); + self::expectException(ClosedConnectionException::class); + self::expectExceptionMessage('Connection is closed'); $db->getConnection(); } }