|
15 | 15 |
|
16 | 16 | use CodeIgniter\Database\Exceptions\DatabaseException; |
17 | 17 | use CodeIgniter\Database\Exceptions\RetryableTransactionException; |
| 18 | +use CodeIgniter\Database\Exceptions\UniqueConstraintViolationException; |
18 | 19 | use CodeIgniter\Database\MySQLi\Connection as MySQLiConnection; |
19 | 20 | use CodeIgniter\Database\OCI8\Connection as OCI8Connection; |
20 | 21 | use CodeIgniter\Database\Postgre\Connection as PostgreConnection; |
|
32 | 33 | #[Group('Others')] |
33 | 34 | final class RetryableTransactionExceptionTest extends CIUnitTestCase |
34 | 35 | { |
| 36 | + #[DataProvider('provideCreatesUniqueConstraintViolationExceptions')] |
| 37 | + public function testCreatesUniqueConstraintViolationExceptions( |
| 38 | + BaseConnection $db, |
| 39 | + int|string $code, |
| 40 | + string $message, |
| 41 | + ): void { |
| 42 | + $exception = self::createDatabaseException($db, $message, $code); |
| 43 | + |
| 44 | + $this->assertInstanceOf(UniqueConstraintViolationException::class, $exception); |
| 45 | + $this->assertSame($code, $exception->getDatabaseCode()); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @return iterable<string, array{BaseConnection, int|string, string}> |
| 50 | + */ |
| 51 | + public static function provideCreatesUniqueConstraintViolationExceptions(): iterable |
| 52 | + { |
| 53 | + yield 'MySQLi duplicate key' => [ |
| 54 | + self::connection(MySQLiConnection::class, 'MySQLi'), |
| 55 | + 1062, |
| 56 | + 'Duplicate entry.', |
| 57 | + ]; |
| 58 | + |
| 59 | + yield 'Postgre unique violation' => [ |
| 60 | + self::connection(PostgreConnection::class, 'Postgre'), |
| 61 | + '23505', |
| 62 | + 'Unique violation.', |
| 63 | + ]; |
| 64 | + |
| 65 | + yield 'SQLite unique constraint' => [ |
| 66 | + self::connection(SQLite3Connection::class, 'SQLite3'), |
| 67 | + 19, |
| 68 | + 'UNIQUE constraint failed: table.column', |
| 69 | + ]; |
| 70 | + |
| 71 | + yield 'SQLite legacy unique constraint' => [ |
| 72 | + self::connection(SQLite3Connection::class, 'SQLite3'), |
| 73 | + 19, |
| 74 | + 'column email is not unique', |
| 75 | + ]; |
| 76 | + |
| 77 | + if (defined('OCI_COMMIT_ON_SUCCESS')) { |
| 78 | + yield 'OCI8 unique constraint' => [ |
| 79 | + self::connection(OCI8Connection::class, 'OCI8'), |
| 80 | + 1, |
| 81 | + 'Unique constraint violated.', |
| 82 | + ]; |
| 83 | + } |
| 84 | + } |
| 85 | + |
35 | 86 | #[DataProvider('provideCreatesRetryableTransactionExceptions')] |
36 | 87 | public function testCreatesRetryableTransactionExceptions(BaseConnection $db, int|string $code): void |
37 | 88 | { |
|
0 commit comments