Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: preg_replace evaluate double backslash for mysql driver and keep… #1488

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/DataCollector/QueryCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ public function addQuery($query, $bindings, $time, $connection)
if ($pdo) {
try {
$binding = $pdo->quote((string) $binding);

// Double escape backslashes because preg_replace evaluate backslashes
$binding = addcslashes($binding, "\\");
} catch (\Exception $e) {
$binding = $this->emulateQuote($binding);
}
Expand Down
51 changes: 51 additions & 0 deletions tests/DataCollector/QueryCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace Barryvdh\Debugbar\Tests\DataCollector;

use Barryvdh\Debugbar\Tests\Models\Person;
use Barryvdh\Debugbar\Tests\Models\User;
use Barryvdh\Debugbar\Tests\TestCase;
use Illuminate\Database\DatabaseManager;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Arr;

Expand Down Expand Up @@ -58,4 +61,52 @@ public function testDollarBindingsArePresentedCorrectly()
);
});
}

public function testModelBindingsArePresentedCorrectlyWithSqliteDriver()
{
// Default connection "testing" running on sqlite pdo driver

debugbar()->boot();

/** @var \Barryvdh\Debugbar\DataCollector\QueryCollector $collector */
$collector = debugbar()->getCollector('queries');
$collector->addQuery(
"SELECT a FROM b WHERE c = ? AND d = ? AND e = :user",
[User::class, Person::class, 'user' => User::class],
0,
$this->app['db']->connection()
);

tap(Arr::first($collector->collect()['statements']), function (array $statement) {
$this->assertEquals(<<<SQL
SELECT a FROM b WHERE c = 'Barryvdh\Debugbar\Tests\Models\User' AND d = 'Barryvdh\Debugbar\Tests\Models\Person' AND e = 'Barryvdh\Debugbar\Tests\Models\User'
SQL,
$statement['sql']
);
});
}

public function testModelBindingsArePresentedCorrectlyWithMysqlDriver()
{
$this->markTestSkipped('Unknown how to run only MySQL driver test (I dont know how to use pdo_mysql driver)');

debugbar()->boot();

/** @var \Barryvdh\Debugbar\DataCollector\QueryCollector $collector */
$collector = debugbar()->getCollector('queries');
$collector->addQuery(
"SELECT a FROM b WHERE c = ? AND d = ? AND e = :user",
[User::class, Person::class, 'user' => User::class],
0,
$this->app['db']->connection()
);

tap(Arr::first($collector->collect()['statements']), function (array $statement) {
$this->assertEquals(<<<SQL
SELECT a FROM b WHERE c = 'Barryvdh\\\\Debugbar\\\\Tests\\\\Models\\\\User' AND d = 'Barryvdh\\\\Debugbar\\\\Tests\\\\Models\\\\Person' AND e = 'Barryvdh\\\\Debugbar\\\\Tests\\\\Models\\\\User'
SQL,
$statement['sql']
);
});
}
}
Loading