Skip to content

Commit 104d49b

Browse files
committed
test(QueryBuilderMixin): add test for quoted raw sql handling in toRawSql
- Extend `replaceSqlBindings` function docblock with additional PHP functions for context. - Add a new test to verify quoting and escaping of raw SQL in `toRawSql`. - Test deals with special characters, strings, numbers, nulls, and booleans. - Ensures expected SQL output is correctly formatted. - Maintain grouping in the test file for better organization.
1 parent 27b3837 commit 104d49b

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

src/Support/Utils.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,15 @@ public static function toRawSql(EloquentBuilder|QueryBuilder|QueryExecuted|Relat
9494
* @noinspection DebugFunctionUsageInspection
9595
*
9696
* @see \Laravel\Telescope\Watchers\QueryWatcher::replaceBindings()
97+
* @see addslashes()
98+
* @see addcslashes()
99+
* @see stripslashes()
100+
* @see stripcslashes()
101+
* @see quotemeta()
102+
* @see mysqli_real_escape_string()
103+
* @see PDO::quote()
104+
* @see var_export()
105+
* @see json_encode()
97106
*/
98107
public static function replaceSqlBindings(string $sql, array $bindings, Connection $connection): string
99108
{

tests/Mixins/QueryBuilderMixinTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,37 @@
3030
])->each->toBe('select * from "users" where "id" = 1 and "name" = \'soar\'');
3131
})->group(__DIR__, __FILE__);
3232

33+
it('can return quoted raw sql for `toRawSql`', function (): void {
34+
expect(User::query())
35+
->whereIn('name', $names = [
36+
"O'Reilly",
37+
'He said "Hello"',
38+
'C:\\Users\\Admin',
39+
'100%',
40+
'user_name',
41+
'`backtick`',
42+
'$dollar$',
43+
"line1\nline2",
44+
'comma,semicolon;',
45+
'特殊字符:★☆!@#',
46+
null,
47+
111100,
48+
1.11100,
49+
false,
50+
true,
51+
])
52+
->toRawSql()
53+
->toBe(
54+
$rawSql = <<<'SQL'
55+
select * from "users" where "name" in ('O''Reilly', 'He said "Hello"', 'C:\Users\Admin', '100%', 'user_name', '`backtick`', '$dollar$', 'line1
56+
line2', 'comma,semicolon;', '特殊字符:★☆!@#', NULL, 111100, 1.111, 0, 1)
57+
SQL
58+
);
59+
60+
User::query()->whereIn('name', $names)->first();
61+
DB::scalar($rawSql);
62+
})->group(__DIR__, __FILE__);
63+
3364
it('can dump raw sql for `dumpRawSql`', function (): void {
3465
expect(User::query()->where('id', 1)->where('name', 'soar')->dumpRawSql())->toBeInstanceOf(Builder::class);
3566
})->group(__DIR__, __FILE__);

0 commit comments

Comments
 (0)