diff --git a/README.md b/README.md index c70d6a3..9e5f9a1 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,7 @@ return [ 'excludePathSegments' => [], 'useRelativePath' => false, ], - Spatie\SqlCommenter\Commenters\CurrentUserCommenter::class, + Spatie\SqlCommenter\Commenters\CurrentUserCommenter::class => ['includeEmail' => true], // Spatie\SqlCommenter\Commenters\FrameworkVersionCommenter::class, // Spatie\SqlCommenter\Commenters\DbDriverCommenter::class, ], diff --git a/config/sql-commenter.php b/config/sql-commenter.php index 0089cab..560dce9 100644 --- a/config/sql-commenter.php +++ b/config/sql-commenter.php @@ -18,7 +18,7 @@ 'excludePathSegments' => [], 'useRelativePath' => false, ], - Spatie\SqlCommenter\Commenters\CurrentUserCommenter::class, + Spatie\SqlCommenter\Commenters\CurrentUserCommenter::class => ['includeEmail' => true], // Spatie\SqlCommenter\Commenters\FrameworkVersionCommenter::class, // Spatie\SqlCommenter\Commenters\DbDriverCommenter::class, ], diff --git a/src/Commenters/CurrentUserCommenter.php b/src/Commenters/CurrentUserCommenter.php index 684feb4..5b4c4c0 100644 --- a/src/Commenters/CurrentUserCommenter.php +++ b/src/Commenters/CurrentUserCommenter.php @@ -8,6 +8,10 @@ class CurrentUserCommenter implements Commenter { + public function __construct(protected bool $includeEmail = false) + { + } + public function comments(string $query, Connection $connection): Comment|array|null { SqlCommenter::disable(); @@ -23,7 +27,7 @@ public function comments(string $query, Connection $connection): Comment|array|n return [ Comment::make('user_id', $user->getKey()), - Comment::make('user_email', $user->email ?? ''), + $this->includeEmail ? Comment::make('user_email', $user->email ?? '') : null, ]; } } diff --git a/tests/Commenters/CurrentUserCommenterTest.php b/tests/Commenters/CurrentUserCommenterTest.php index 8818029..b0fcda0 100644 --- a/tests/Commenters/CurrentUserCommenterTest.php +++ b/tests/Commenters/CurrentUserCommenterTest.php @@ -2,6 +2,7 @@ use Illuminate\Database\Events\QueryExecuted; use Illuminate\Support\Facades\Event; +use Spatie\SqlCommenter\Commenters\CurrentUserCommenter; use Spatie\SqlCommenter\Tests\TestSupport\TestClasses\User; it('logs the current user', function () { @@ -20,3 +21,23 @@ User::all(); }); + + +it('can exclude the user email', function () { + $user = User::create([ + 'name' => 'John', + 'password' => 'dummy', + 'email' => 'johndoe@example.com', + ]); + + auth()->login($user); + + $this->addCommenterToConfig(CurrentUserCommenter::class, ['includeEmail' => false]); + + Event::listen(QueryExecuted::class, function (QueryExecuted $event) use ($user) { + expect($event->sql)->toContainComment('user_id', $user->id) + ->and($event->sql)->notToContainComment('user_email', $user->email); + }); + + User::all(); +}); diff --git a/tests/Pest.php b/tests/Pest.php index ec60f62..2434a9e 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -10,3 +10,9 @@ return $this; }); + +expect()->extend('notToContainComment', function (string $key, string|int|null $value) { + expect($this->value)->not->toContain((string)Comment::make($key, $value)); + + return $this; +});