|
16 | 16 | use CodeIgniter\Database\Postgre\Connection as PostgreConnection; |
17 | 17 | use CodeIgniter\Test\CIUnitTestCase; |
18 | 18 | use CodeIgniter\Test\ReflectionHelper; |
| 19 | +use CodeIgniter\Test\TestLogger; |
19 | 20 | use PHPUnit\Framework\Attributes\DataProvider; |
20 | 21 | use PHPUnit\Framework\Attributes\Group; |
| 22 | +use PHPUnit\Framework\MockObject\MockObject; |
| 23 | +use RuntimeException; |
21 | 24 |
|
22 | 25 | /** |
23 | 26 | * @internal |
@@ -119,9 +122,19 @@ final class ConfigTest extends CIUnitTestCase |
119 | 122 | 'port' => 5432, |
120 | 123 | ]; |
121 | 124 |
|
| 125 | + protected function setUp(): void |
| 126 | + { |
| 127 | + parent::setUp(); |
| 128 | + |
| 129 | + $this->setPrivateProperty(TestLogger::class, 'op_logs', []); |
| 130 | + } |
| 131 | + |
122 | 132 | protected function tearDown(): void |
123 | 133 | { |
| 134 | + parent::tearDown(); |
| 135 | + |
124 | 136 | $this->setPrivateProperty(Config::class, 'instances', []); |
| 137 | + $this->setPrivateProperty(TestLogger::class, 'op_logs', []); |
125 | 138 | } |
126 | 139 |
|
127 | 140 | public function testConnectionGroup(): void |
@@ -257,4 +270,94 @@ public static function provideConvertDSN(): iterable |
257 | 270 | ], |
258 | 271 | ]; |
259 | 272 | } |
| 273 | + |
| 274 | + public function testCleanupForWorkerModeRollsBackUncommittedTransactions(): void |
| 275 | + { |
| 276 | + $depth = 3; |
| 277 | + $connection = $this->createConnectionMock($depth); |
| 278 | + $connection->expects($this->exactly($depth)) |
| 279 | + ->method('transRollback') |
| 280 | + ->willReturnCallback(static function () use (&$depth): bool { |
| 281 | + $depth--; |
| 282 | + |
| 283 | + return true; |
| 284 | + }); |
| 285 | + |
| 286 | + $this->setPrivateProperty(Config::class, 'instances', ['tests' => $connection]); |
| 287 | + |
| 288 | + Config::cleanupForWorkerMode(); |
| 289 | + |
| 290 | + $this->assertSame(0, $depth); |
| 291 | + $this->assertFalse(TestLogger::didLog('error', 'Transaction cleanup aborted.', false)); |
| 292 | + } |
| 293 | + |
| 294 | + public function testCleanupForWorkerModeStopsWhenRollbackFails(): void |
| 295 | + { |
| 296 | + $depth = 2; |
| 297 | + $connection = $this->createConnectionMock($depth); |
| 298 | + $connection->expects($this->once()) |
| 299 | + ->method('transRollback') |
| 300 | + ->willReturn(false); |
| 301 | + $connection->expects($this->once()) |
| 302 | + ->method('resetTransStatus'); |
| 303 | + |
| 304 | + $this->setPrivateProperty(Config::class, 'instances', ['tests' => $connection]); |
| 305 | + |
| 306 | + Config::cleanupForWorkerMode(); |
| 307 | + |
| 308 | + $this->assertSame(2, $depth); |
| 309 | + $this->assertLogged('error', "Database: Failed to roll back an uncommitted transaction in database group 'tests'. Transaction cleanup aborted."); |
| 310 | + } |
| 311 | + |
| 312 | + public function testCleanupForWorkerModeStopsWhenRollbackMakesNoProgress(): void |
| 313 | + { |
| 314 | + $depth = 2; |
| 315 | + $connection = $this->createConnectionMock($depth); |
| 316 | + $connection->expects($this->once()) |
| 317 | + ->method('transRollback') |
| 318 | + ->willReturn(true); |
| 319 | + $connection->expects($this->once()) |
| 320 | + ->method('resetTransStatus'); |
| 321 | + |
| 322 | + $this->setPrivateProperty(Config::class, 'instances', ['tests' => $connection]); |
| 323 | + |
| 324 | + Config::cleanupForWorkerMode(); |
| 325 | + |
| 326 | + $this->assertSame(2, $depth); |
| 327 | + $this->assertLogged('error', "Database: Failed to roll back an uncommitted transaction in database group 'tests'. Transaction cleanup aborted."); |
| 328 | + } |
| 329 | + |
| 330 | + public function testCleanupForWorkerModeStopsWhenRollbackThrows(): void |
| 331 | + { |
| 332 | + $depth = 2; |
| 333 | + $connection = $this->createConnectionMock($depth); |
| 334 | + $connection->expects($this->once()) |
| 335 | + ->method('transRollback') |
| 336 | + ->willThrowException(new RuntimeException('MySQL server has gone away')); |
| 337 | + $connection->expects($this->once()) |
| 338 | + ->method('resetTransStatus'); |
| 339 | + |
| 340 | + $this->setPrivateProperty(Config::class, 'instances', ['tests' => $connection]); |
| 341 | + |
| 342 | + Config::cleanupForWorkerMode(); |
| 343 | + |
| 344 | + $this->assertSame(2, $depth); |
| 345 | + $this->assertLogged('error', "Database: Failed to roll back an uncommitted transaction in database group 'tests'. Transaction cleanup aborted: MySQL server has gone away"); |
| 346 | + } |
| 347 | + |
| 348 | + /** |
| 349 | + * @param int $depth Current transaction depth reported by the connection. |
| 350 | + * |
| 351 | + * @return BaseConnection&MockObject |
| 352 | + */ |
| 353 | + private function createConnectionMock(int &$depth): BaseConnection |
| 354 | + { |
| 355 | + $connection = $this->createMock(BaseConnection::class); |
| 356 | + $connection->method('__get')->willReturnCallback(static function (string $key) use (&$depth): ?int { |
| 357 | + return $key === 'transDepth' ? $depth : null; |
| 358 | + }); |
| 359 | + $connection->method('resetTransStatus')->willReturnSelf(); |
| 360 | + |
| 361 | + return $connection; |
| 362 | + } |
260 | 363 | } |
0 commit comments