Skip to content

Commit 77377a7

Browse files
committed
fix: return BadMethodCallException when trying to close a non-existing prepared statement
1 parent 3b7465f commit 77377a7

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

system/Database/BasePreparedQuery.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ abstract public function _getResult();
152152
public function close(): bool
153153
{
154154
if (! isset($this->statement)) {
155-
return true;
155+
throw new BadMethodCallException('Cannot call close on a non-existing prepared statement.');
156156
}
157157

158158
try {

tests/system/Database/Live/PreparedQueryTest.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ protected function tearDown(): void
4343
parent::tearDown();
4444

4545
if ($this->query !== null) {
46-
$this->query->close();
46+
try {
47+
$this->query->close();
48+
} catch (BadMethodCallException $e) {
49+
$this->query = null;
50+
}
4751
}
4852
}
4953

@@ -162,12 +166,20 @@ public function testDeallocatePreparedQuery()
162166

163167
$this->query->close();
164168

169+
// Try to execute a non-existing prepared statement
165170
try {
166171
$this->query->execute('bar', '[email protected]', 'GB');
167172
} catch (BadMethodCallException $e) {
168173
$this->assertSame('You must call prepare before trying to execute a prepared statement.', $e->getMessage());
169174
}
170175

176+
// Try to close a non-existing prepared statement
177+
try {
178+
$this->query->close();
179+
} catch (BadMethodCallException $e) {
180+
$this->assertSame('Cannot call close on a non-existing prepared statement.', $e->getMessage());
181+
}
182+
171183
$this->seeInDatabase($this->db->DBPrefix . 'user', ['name' => 'foo', 'email' => '[email protected]']);
172184
$this->dontSeeInDatabase($this->db->DBPrefix . 'user', ['name' => 'bar', 'email' => '[email protected]']);
173185
}

0 commit comments

Comments
 (0)