Skip to content

Commit 78a34c9

Browse files
committed
tests: use expectException() for unit-tests, not try/catch
1 parent 77377a7 commit 78a34c9

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

tests/system/Database/Live/PreparedQueryTest.php

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -154,33 +154,37 @@ public function testExecuteRunsInvalidQuery()
154154
$this->query->execute('foo', '[email protected]', 'US');
155155
}
156156

157-
public function testDeallocatePreparedQuery()
157+
public function testDeallocatePreparedQueryThenTryToExecute()
158158
{
159159
$this->query = $this->db->prepare(static fn ($db) => $db->table('user')->insert([
160160
'name' => 'a',
161161
'email' => '[email protected]',
162162
'country' => 'x',
163163
]));
164164

165-
$this->query->execute('foo', '[email protected]', 'US');
166-
167165
$this->query->close();
168166

169167
// Try to execute a non-existing prepared statement
170-
try {
171-
$this->query->execute('bar', '[email protected]', 'GB');
172-
} catch (BadMethodCallException $e) {
173-
$this->assertSame('You must call prepare before trying to execute a prepared statement.', $e->getMessage());
174-
}
168+
$this->expectException(BadMethodCallException::class);
169+
$this->expectExceptionMessage('You must call prepare before trying to execute a prepared statement.');
170+
171+
$this->query->execute('bar', '[email protected]', 'GB');
172+
}
173+
174+
public function testDeallocatePreparedQueryThenTryToClose()
175+
{
176+
$this->query = $this->db->prepare(static fn ($db) => $db->table('user')->insert([
177+
'name' => 'a',
178+
'email' => '[email protected]',
179+
'country' => 'x',
180+
]));
181+
182+
$this->query->close();
175183

176184
// 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-
}
185+
$this->expectException(BadMethodCallException::class);
186+
$this->expectExceptionMessage('Cannot call close on a non-existing prepared statement.');
182187

183-
$this->seeInDatabase($this->db->DBPrefix . 'user', ['name' => 'foo', 'email' => '[email protected]']);
184-
$this->dontSeeInDatabase($this->db->DBPrefix . 'user', ['name' => 'bar', 'email' => '[email protected]']);
188+
$this->query->close();
185189
}
186190
}

0 commit comments

Comments
 (0)