|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of CodeIgniter 4 framework. |
| 7 | + * |
| 8 | + * (c) CodeIgniter Foundation <admin@codeigniter.com> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view |
| 11 | + * the LICENSE file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace CodeIgniter\Database; |
| 15 | + |
| 16 | +use CodeIgniter\Database\Exceptions\DatabaseException; |
| 17 | +use CodeIgniter\Database\Exceptions\UniqueConstraintViolationException; |
| 18 | +use CodeIgniter\Database\MySQLi\Connection as MySQLiConnection; |
| 19 | +use CodeIgniter\Database\OCI8\Connection as OCI8Connection; |
| 20 | +use CodeIgniter\Database\Postgre\Connection as PostgreConnection; |
| 21 | +use CodeIgniter\Database\SQLite3\Connection as SQLite3Connection; |
| 22 | +use CodeIgniter\Database\SQLSRV\Connection as SQLSRVConnection; |
| 23 | +use CodeIgniter\Test\CIUnitTestCase; |
| 24 | +use CodeIgniter\Test\Mock\MockConnection; |
| 25 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 26 | +use PHPUnit\Framework\Attributes\Group; |
| 27 | +use RuntimeException; |
| 28 | + |
| 29 | +/** |
| 30 | + * @internal |
| 31 | + */ |
| 32 | +#[Group('Others')] |
| 33 | +final class RetryableTransactionExceptionTest extends CIUnitTestCase |
| 34 | +{ |
| 35 | + #[DataProvider('provideRecognizesRetryableTransactionExceptions')] |
| 36 | + public function testRecognizesRetryableTransactionExceptions(BaseConnection $db, int|string $code): void |
| 37 | + { |
| 38 | + $exception = new DatabaseException('Retryable transaction failure.', $code); |
| 39 | + |
| 40 | + $this->assertTrue($db->isRetryableTransactionException($exception)); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * @return iterable<string, array{BaseConnection, int|string}> |
| 45 | + */ |
| 46 | + public static function provideRecognizesRetryableTransactionExceptions(): iterable |
| 47 | + { |
| 48 | + yield 'MySQLi deadlock' => [self::connection(MySQLiConnection::class, 'MySQLi'), 1213]; |
| 49 | + |
| 50 | + yield 'Postgre serialization failure' => [self::connection(PostgreConnection::class, 'Postgre'), '40001']; |
| 51 | + |
| 52 | + yield 'Postgre deadlock' => [self::connection(PostgreConnection::class, 'Postgre'), '40P01']; |
| 53 | + |
| 54 | + yield 'SQLite busy' => [self::connection(SQLite3Connection::class, 'SQLite3'), 5]; |
| 55 | + |
| 56 | + yield 'SQLSRV deadlock' => [self::connection(SQLSRVConnection::class, 'SQLSRV'), '40001/1205']; |
| 57 | + |
| 58 | + yield 'SQLSRV vendor deadlock' => [self::connection(SQLSRVConnection::class, 'SQLSRV'), 1205]; |
| 59 | + |
| 60 | + yield 'SQLSRV snapshot isolation conflict' => [self::connection(SQLSRVConnection::class, 'SQLSRV'), 'HY000/3960']; |
| 61 | + |
| 62 | + if (defined('OCI_COMMIT_ON_SUCCESS')) { |
| 63 | + yield 'OCI8 deadlock' => [self::connection(OCI8Connection::class, 'OCI8'), 60]; |
| 64 | + |
| 65 | + yield 'OCI8 serialization failure' => [self::connection(OCI8Connection::class, 'OCI8'), 8177]; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + #[DataProvider('provideRejectsNonRetryableTransactionExceptions')] |
| 70 | + public function testRejectsNonRetryableTransactionExceptions(BaseConnection $db, int|string $code): void |
| 71 | + { |
| 72 | + $exception = new DatabaseException('Non-retryable transaction failure.', $code); |
| 73 | + |
| 74 | + $this->assertFalse($db->isRetryableTransactionException($exception)); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @return iterable<string, array{BaseConnection, int|string}> |
| 79 | + */ |
| 80 | + public static function provideRejectsNonRetryableTransactionExceptions(): iterable |
| 81 | + { |
| 82 | + yield 'Base connection default' => [self::connection(MockConnection::class, 'MockDriver'), 1213]; |
| 83 | + |
| 84 | + yield 'MySQLi lock wait timeout' => [self::connection(MySQLiConnection::class, 'MySQLi'), 1205]; |
| 85 | + |
| 86 | + yield 'MySQLi duplicate key' => [self::connection(MySQLiConnection::class, 'MySQLi'), 1062]; |
| 87 | + |
| 88 | + yield 'Postgre unique violation' => [self::connection(PostgreConnection::class, 'Postgre'), '23505']; |
| 89 | + |
| 90 | + yield 'Postgre exclusion violation' => [self::connection(PostgreConnection::class, 'Postgre'), '23P01']; |
| 91 | + |
| 92 | + yield 'SQLite locked' => [self::connection(SQLite3Connection::class, 'SQLite3'), 6]; |
| 93 | + |
| 94 | + yield 'SQLite busy snapshot extended code' => [self::connection(SQLite3Connection::class, 'SQLite3'), 517]; |
| 95 | + |
| 96 | + yield 'SQLite constraint' => [self::connection(SQLite3Connection::class, 'SQLite3'), 19]; |
| 97 | + |
| 98 | + yield 'SQLSRV lock timeout' => [self::connection(SQLSRVConnection::class, 'SQLSRV'), 'HYT00/1222']; |
| 99 | + |
| 100 | + yield 'SQLSRV SQLSTATE without vendor code' => [self::connection(SQLSRVConnection::class, 'SQLSRV'), '40001']; |
| 101 | + |
| 102 | + yield 'SQLSRV unique constraint' => [self::connection(SQLSRVConnection::class, 'SQLSRV'), '23000/2627']; |
| 103 | + |
| 104 | + yield 'SQLSRV unique index' => [self::connection(SQLSRVConnection::class, 'SQLSRV'), '23000/2601']; |
| 105 | + |
| 106 | + if (defined('OCI_COMMIT_ON_SUCCESS')) { |
| 107 | + yield 'OCI8 resource busy' => [self::connection(OCI8Connection::class, 'OCI8'), 54]; |
| 108 | + |
| 109 | + yield 'OCI8 unique constraint' => [self::connection(OCI8Connection::class, 'OCI8'), 1]; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + public function testRejectsNonDatabaseExceptions(): void |
| 114 | + { |
| 115 | + $db = self::connection(MySQLiConnection::class, 'MySQLi'); |
| 116 | + |
| 117 | + $this->assertFalse($db->isRetryableTransactionException(new RuntimeException('Not a database exception.'))); |
| 118 | + } |
| 119 | + |
| 120 | + public function testRejectsUniqueConstraintViolationExceptions(): void |
| 121 | + { |
| 122 | + $db = self::connection(MySQLiConnection::class, 'MySQLi'); |
| 123 | + |
| 124 | + $this->assertFalse($db->isRetryableTransactionException( |
| 125 | + new UniqueConstraintViolationException('Duplicate key.', 1062), |
| 126 | + )); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * @param class-string<BaseConnection> $connectionClass |
| 131 | + */ |
| 132 | + private static function connection(string $connectionClass, string $driver): BaseConnection |
| 133 | + { |
| 134 | + return new $connectionClass([ |
| 135 | + 'DSN' => '', |
| 136 | + 'hostname' => 'localhost', |
| 137 | + 'username' => '', |
| 138 | + 'password' => '', |
| 139 | + 'database' => 'test', |
| 140 | + 'DBDriver' => $driver, |
| 141 | + 'DBDebug' => true, |
| 142 | + 'charset' => 'utf8', |
| 143 | + 'DBCollat' => 'utf8_general_ci', |
| 144 | + 'swapPre' => '', |
| 145 | + 'encrypt' => false, |
| 146 | + 'compress' => false, |
| 147 | + 'failover' => [], |
| 148 | + ]); |
| 149 | + } |
| 150 | +} |
0 commit comments