Skip to content

Commit 2e5fa9d

Browse files
committed
test: tighten constraint exception assertions
Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
1 parent 0b8ba3d commit 2e5fa9d

2 files changed

Lines changed: 15 additions & 7 deletions

File tree

tests/system/Database/ConstraintViolationExceptionTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,10 @@ public function testCreatesConstraintViolationExceptions(
5252
string $message,
5353
string $expectedException,
5454
): void {
55-
$exception = $db->createDatabaseException($message, $code);
55+
$exception = $db->createDatabaseException($message, $code);
56+
$actualException = $exception::class;
5657

57-
$this->assertSame($exception::class, $expectedException);
58+
$this->assertSame($expectedException, $actualException);
5859
$this->assertInstanceOf(ConstraintViolationException::class, $exception);
5960
$this->assertSame($code, $exception->getDatabaseCode());
6061
}

tests/system/Database/Live/ConstraintViolationExceptionTest.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace CodeIgniter\Database\Live;
1515

1616
use CodeIgniter\Database\Exceptions\ConstraintViolationException;
17+
use CodeIgniter\Database\Exceptions\DatabaseException;
1718
use CodeIgniter\Database\Exceptions\ForeignKeyConstraintViolationException;
1819
use CodeIgniter\Database\Exceptions\NotNullConstraintViolationException;
1920
use CodeIgniter\Database\Forge;
@@ -80,12 +81,18 @@ public function testThrowsConstraintViolationExceptionForForeignKeyWithDebugEnab
8081
? ConstraintViolationException::class
8182
: ForeignKeyConstraintViolationException::class;
8283

83-
$this->expectException($expectedException);
84+
try {
85+
$this->db->table('cv_child')->insert([
86+
'id' => 1,
87+
'parent_id' => 999,
88+
]);
8489

85-
$this->db->table('cv_child')->insert([
86-
'id' => 1,
87-
'parent_id' => 999,
88-
]);
90+
$this->fail('Expected database exception was not thrown.');
91+
} catch (DatabaseException $e) {
92+
$actualException = $e::class;
93+
94+
$this->assertSame($expectedException, $actualException);
95+
}
8996
}
9097

9198
public function testStoresNotNullConstraintViolationExceptionWithDebugDisabled(): void

0 commit comments

Comments
 (0)