|
| 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\MySQLi\Connection as MySQLiConnection; |
| 18 | +use CodeIgniter\Database\OCI8\Connection as OCI8Connection; |
| 19 | +use CodeIgniter\Test\CIUnitTestCase; |
| 20 | +use PHPUnit\Framework\Attributes\Group; |
| 21 | + |
| 22 | +/** |
| 23 | + * @internal |
| 24 | + */ |
| 25 | +#[Group('Others')] |
| 26 | +final class ReconnectLostTransactionTest extends CIUnitTestCase |
| 27 | +{ |
| 28 | + public function testReconnectClearsTransactionStateWhenInitializeFails(): void |
| 29 | + { |
| 30 | + $db = new class (['DBDriver' => 'MySQLi']) extends MySQLiConnection { |
| 31 | + public function initialize(): never |
| 32 | + { |
| 33 | + throw new DatabaseException('database unavailable'); |
| 34 | + } |
| 35 | + }; |
| 36 | + |
| 37 | + $this->setPrivateProperty($db, 'transDepth', 1); |
| 38 | + |
| 39 | + try { |
| 40 | + $db->reconnect(); |
| 41 | + $this->fail('initialize() should have thrown.'); |
| 42 | + } catch (DatabaseException $exception) { |
| 43 | + $this->assertSame('database unavailable', $exception->getMessage()); |
| 44 | + } |
| 45 | + |
| 46 | + $this->assertSame(0, $this->getPrivateProperty($db, 'transDepth')); |
| 47 | + $this->assertFalse($db->transStatus()); |
| 48 | + } |
| 49 | + |
| 50 | + public function testReconnectRestoresOci8CommitModeWhenTransactionIsLost(): void |
| 51 | + { |
| 52 | + if (! defined('OCI_COMMIT_ON_SUCCESS')) { |
| 53 | + define('OCI_COMMIT_ON_SUCCESS', 32); |
| 54 | + } |
| 55 | + |
| 56 | + if (! defined('OCI_NO_AUTO_COMMIT')) { |
| 57 | + define('OCI_NO_AUTO_COMMIT', 0); |
| 58 | + } |
| 59 | + |
| 60 | + $db = new class (['DBDriver' => 'OCI8']) extends OCI8Connection { |
| 61 | + public function initialize(): void |
| 62 | + { |
| 63 | + } |
| 64 | + }; |
| 65 | + |
| 66 | + $db->commitMode = OCI_NO_AUTO_COMMIT; |
| 67 | + $this->setPrivateProperty($db, 'transDepth', 1); |
| 68 | + |
| 69 | + $db->reconnect(); |
| 70 | + |
| 71 | + $this->assertSame(OCI_COMMIT_ON_SUCCESS, $db->commitMode); |
| 72 | + $this->assertSame(0, $this->getPrivateProperty($db, 'transDepth')); |
| 73 | + $this->assertFalse($db->transStatus()); |
| 74 | + } |
| 75 | +} |
0 commit comments