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

chore: add ability to exclude user email #28

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
Expand Down
2 changes: 1 addition & 1 deletion config/sql-commenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
Expand Down
6 changes: 5 additions & 1 deletion src/Commenters/CurrentUserCommenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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,
];
}
}
21 changes: 21 additions & 0 deletions tests/Commenters/CurrentUserCommenterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -20,3 +21,23 @@

User::all();
});


it('can exclude the user email', function () {
$user = User::create([
'name' => 'John',
'password' => 'dummy',
'email' => '[email protected]',
]);

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)->not->toContainComment('user_email', $user->email);
});

User::all();
});
Loading