-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add structured trace for all DB drivers
- Loading branch information
1 parent
d393960
commit 319a3ec
Showing
6 changed files
with
98 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
tests/system/Database/Live/ExecuteLogMessageFormatTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* This file is part of CodeIgniter 4 framework. | ||
* | ||
* (c) CodeIgniter Foundation <admin@codeigniter.com> | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace CodeIgniter\Database\Live; | ||
|
||
use CodeIgniter\Test\CIUnitTestCase; | ||
use CodeIgniter\Test\TestLogger; | ||
use Config\Database; | ||
use PHPUnit\Framework\Attributes\Group; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
#[Group('DatabaseLive')] | ||
final class ExecuteLogMessageFormatTest extends CIUnitTestCase | ||
{ | ||
public function testLogMessageWhenExecuteFailsShowFullStructuredBacktrace(): void | ||
{ | ||
$db = Database::connect(); | ||
$this->setPrivateProperty($db, 'DBDebug', false); | ||
|
||
$sql = 'SELECT * FROM some_table WHERE id = ? AND status = ? AND author = ?'; | ||
$db->query($sql, [3, 'live', 'Rick']); | ||
|
||
$message = match ($db->DBDriver) { | ||
'MySQLi' => 'Table \'test.some_table\' doesn\'t exist', | ||
'Postgre' => 'pg_query(): Query failed: ERROR: relation "some_table" does not exist', | ||
'SQLite3' => 'Unable to prepare statement: no such table: some_table', | ||
'OCI8' => 'oci_execute(): ORA-00942: table or view does not exist', | ||
'SQLSRV' => '[Microsoft][ODBC Driver 18 for SQL Server][SQL Server]Invalid object name \'some_table\'.', | ||
}; | ||
$messageFromLogs = explode("\n", $this->getPrivateProperty(TestLogger::class, 'op_logs')[0]['message']); | ||
|
||
$this->assertSame($message, array_shift($messageFromLogs)); | ||
|
||
if ($db->DBDriver === 'Postgre') { | ||
$messageFromLogs = array_slice($messageFromLogs, 2); | ||
} elseif ($db->DBDriver === 'OCI8') { | ||
$messageFromLogs = array_slice($messageFromLogs, 1); | ||
} | ||
|
||
$this->assertMatchesRegularExpression('/^in \S+ on line \d+\.$/', array_shift($messageFromLogs)); | ||
|
||
foreach ($messageFromLogs as $line) { | ||
$this->assertMatchesRegularExpression('/\s?\d*\s\S+\(\d+\)\:\s.+/', $line); | ||
} | ||
} | ||
} |