|
| 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\Live; |
| 15 | + |
| 16 | +use CodeIgniter\Test\CIUnitTestCase; |
| 17 | +use CodeIgniter\Test\TestLogger; |
| 18 | +use Config\Database; |
| 19 | +use PHPUnit\Framework\Attributes\Group; |
| 20 | + |
| 21 | +/** |
| 22 | + * @internal |
| 23 | + */ |
| 24 | +#[Group('DatabaseLive')] |
| 25 | +final class ExecuteLogMessageFormatTest extends CIUnitTestCase |
| 26 | +{ |
| 27 | + protected function setUp(): void |
| 28 | + { |
| 29 | + parent::setUp(); |
| 30 | + |
| 31 | + self::setPrivateProperty(TestLogger::class, 'op_logs', []); |
| 32 | + } |
| 33 | + |
| 34 | + protected function tearDown(): void |
| 35 | + { |
| 36 | + parent::tearDown(); |
| 37 | + |
| 38 | + self::setPrivateProperty(TestLogger::class, 'op_logs', []); |
| 39 | + |
| 40 | + // reset the instances array as it now holds a modified connection under 'tests' group |
| 41 | + // @todo remove once 4.7 is updated with latest develop |
| 42 | + self::setPrivateProperty(Database::class, 'instances', []); |
| 43 | + } |
| 44 | + |
| 45 | + public function testLogMessageWhenExecuteFailsShowFullStructuredBacktrace(): void |
| 46 | + { |
| 47 | + $db = Database::connect('tests', false); |
| 48 | + self::setPrivateProperty($db, 'DBDebug', false); |
| 49 | + |
| 50 | + $sql = 'SELECT * FROM some_table WHERE id = ? AND status = ? AND author = ?'; |
| 51 | + $db->query($sql, [3, 'live', 'Rick']); |
| 52 | + |
| 53 | + $pattern = match ($db->DBDriver) { |
| 54 | + 'MySQLi' => '/Table \'test\.some_table\' doesn\'t exist/', |
| 55 | + 'Postgre' => '/pg_query\(\): Query failed: ERROR: relation "some_table" does not exist/', |
| 56 | + 'SQLite3' => '/Unable to prepare statement:(\d+, )?no such table: some_table/', |
| 57 | + 'OCI8' => '/oci_execute\(\): ORA-00942: table or view does not exist/', |
| 58 | + 'SQLSRV' => '/\[Microsoft\]\[ODBC Driver \d+ for SQL Server\]\[SQL Server\]Invalid object name \'some_table\'/', |
| 59 | + default => '/Unknown DB error/', |
| 60 | + }; |
| 61 | + $messageFromLogs = explode("\n", self::getPrivateProperty(TestLogger::class, 'op_logs')[0]['message']); |
| 62 | + |
| 63 | + $this->assertMatchesRegularExpression($pattern, array_shift($messageFromLogs)); |
| 64 | + |
| 65 | + if ($db->DBDriver === 'Postgre') { |
| 66 | + $messageFromLogs = array_slice($messageFromLogs, 2); |
| 67 | + } elseif ($db->DBDriver === 'OCI8') { |
| 68 | + $messageFromLogs = array_slice($messageFromLogs, 1); |
| 69 | + } |
| 70 | + |
| 71 | + $this->assertMatchesRegularExpression('/^in \S+ on line \d+\.$/', array_shift($messageFromLogs)); |
| 72 | + |
| 73 | + foreach ($messageFromLogs as $line) { |
| 74 | + $this->assertMatchesRegularExpression('/^\s*\d* .+(?:\(\d+\))?: \S+(?:(?:\->|::)\S+)?\(.*\)$/', $line); |
| 75 | + } |
| 76 | + } |
| 77 | +} |
0 commit comments