Skip to content

Commit c23f5e7

Browse files
authored
Merge pull request #3 from pnlinh/fix/wrong-format
Fix wrong origin sql format
2 parents 885c37c + 3b3ec52 commit c23f5e7

File tree

4 files changed

+36
-9
lines changed

4 files changed

+36
-9
lines changed

docker-compose.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
version: '2'
2+
3+
services:
4+
app:
5+
image: shinsenter/laravel:php8.0
6+
volumes:
7+
- ./:/var/www/html
8+
networks:
9+
- localnet

src/QueryLoggerServiceProvider.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,12 @@ protected function writeMySqlLog(QueryExecuted $query, LogManager $logger)
7272
}
7373
}
7474

75+
$tempStr = sprintf('@%s@', date('Y'));
76+
7577
$sql = $query->sql;
76-
$sql = str_replace(['%', '?'], ['**', '"%s"'], $sql);
78+
$sql = str_replace(['%', '?'], [$tempStr, '"%s"'], $sql);
7779
$sql = vsprintf(str_replace('?', '"%s"', $sql), $query->bindings);
78-
$sql = str_replace('**', '%', $sql);
80+
$sql = str_replace($tempStr, '%', $sql);
7981

8082
$messages = [
8183
'[SQL]',

tests/Feature/QueryLoggerServiceProviderTest.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,28 @@ public function it_can_be_constructed()
1818
/** @test */
1919
public function it_can_be_write_log()
2020
{
21-
User::create(['email' => '[email protected]']);
21+
User::query()->create(['email' => '[email protected]']);
2222

2323
$path = storage_path('logs/laravel.log');
2424
$logContent = file_get_contents($path);
2525

2626
$this->assertFileExists($path);
2727
$this->assertNotEmpty($logContent);
2828
}
29+
30+
/** @test */
31+
public function it_must_match_origin_query()
32+
{
33+
$phone = '60********85';
34+
35+
User::query()->where('phone', $phone)->first();
36+
37+
$path = storage_path('logs/laravel.log');
38+
$logContent = file_get_contents($path);
39+
40+
$this->assertNotEmpty($logContent);
41+
$this->assertStringContainsString($phone, $logContent);
42+
}
2943
}
3044

3145
class User extends Model
@@ -37,5 +51,5 @@ class User extends Model
3751
public $timestamps = false;
3852

3953
/** @var string[] */
40-
protected $fillable = ['email'];
54+
protected $fillable = ['email', 'phone'];
4155
}

tests/TestCase.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,20 @@ public function getPackageProviders($app): array
2222

2323
public function getEnvironmentSetUp($app)
2424
{
25-
config()->set('database.default', 'sqlite');
26-
config()->set('database.connections.sqlite', [
25+
$app['config']->set('database.default', 'sqlite');
26+
$app['config']->set('database.connections.sqlite', [
2727
'driver' => 'sqlite',
2828
'database' => ':memory:',
2929
'prefix' => '',
3030
]);
31-
config()->set('app.debug', true);
32-
config()->set('app.logging.default', 'single');
31+
$app['config']->set('app.debug', true);
32+
$app['config']->set('app.logging.default', 'single');
3333

3434
Schema::create('users', function (Blueprint $table) {
3535
$table->increments('id');
36-
$table->string('email')->nullable();
36+
$table->string('email')->unique();
37+
$table->string('phone')->nullable();
38+
$table->timestamps();
3739
});
3840
}
3941
}

0 commit comments

Comments
 (0)