-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: tracer can use custom resolver for user ID (#37)
Allow changing config('xray.user-resolver') with a custom class
- Loading branch information
Showing
7 changed files
with
177 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Napp\Xray\Resolvers; | ||
|
||
use Illuminate\Support\Facades\Auth; | ||
use Napp\Xray\Resolvers\Contracts\ResolvesUser; | ||
|
||
class AuthIdentifier implements ResolvesUser | ||
{ | ||
public function getUser(): ?string | ||
{ | ||
if (app()->bound('auth') && Auth::check()) { | ||
return (string) Auth::user()->getAuthIdentifier(); | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Napp\Xray\Resolvers\Contracts; | ||
|
||
interface ResolvesUser | ||
{ | ||
public function getUser(): ?string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Napp\Xray\Tests; | ||
|
||
use Illuminate\Support\Facades\Auth; | ||
use Napp\Xray\Tests\Stubs\CustomUserResolver; | ||
use Napp\Xray\Tests\Stubs\User; | ||
use Napp\Xray\Collectors\SegmentCollector; | ||
use Napp\Xray\XrayServiceProvider; | ||
use Orchestra\Testbench\TestCase; | ||
|
||
class SegmentCollectorTest extends TestCase | ||
{ | ||
/** | ||
* @param \Illuminate\Foundation\Application $app | ||
* @return string[] | ||
*/ | ||
protected function getPackageProviders($app) | ||
{ | ||
return [ | ||
XrayServiceProvider::class, | ||
]; | ||
} | ||
|
||
public function test_get_user_when_logged_in() | ||
{ | ||
Auth::login(new User()); | ||
|
||
$collector = new SegmentCollector(); | ||
|
||
$this->assertSame('1', $collector->getUser()); | ||
} | ||
|
||
public function test_get_user_when_guest() | ||
{ | ||
$collector = new SegmentCollector(); | ||
|
||
$this->assertNull($collector->getUser()); | ||
} | ||
|
||
public function test_get_user_when_config_is_missing() | ||
{ | ||
$config = config('xray'); | ||
unset($config['user-resolver']); | ||
config()->set('xray', $config); | ||
|
||
Auth::login(new User()); | ||
|
||
$collector = new SegmentCollector(); | ||
|
||
$this->assertSame('1', $collector->getUser()); | ||
} | ||
|
||
public function test_get_user_when_custom_resolver_is_configured() | ||
{ | ||
config()->set('xray.user-resolver', CustomUserResolver::class); | ||
|
||
$collector = new SegmentCollector(); | ||
|
||
$this->assertSame('foo', $collector->getUser()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Napp\Xray\Tests\Stubs; | ||
|
||
use Napp\Xray\Resolvers\Contracts\ResolvesUser; | ||
|
||
class CustomUserResolver implements ResolvesUser | ||
{ | ||
public function getUser(): ?string | ||
{ | ||
return 'foo'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Napp\Xray\Tests\Stubs; | ||
|
||
use Illuminate\Contracts\Auth\Authenticatable; | ||
|
||
class User implements Authenticatable | ||
{ | ||
public function getAuthIdentifierName(): string | ||
{ | ||
return 'id'; | ||
} | ||
|
||
public function getAuthIdentifier(): int | ||
{ | ||
return 1; | ||
} | ||
|
||
public function getAuthPassword(): string | ||
{ | ||
return 'foo'; | ||
} | ||
|
||
public function getRememberToken(): string | ||
{ | ||
return 'bar'; | ||
} | ||
|
||
public function setRememberToken($value): void | ||
{ | ||
} | ||
|
||
public function getRememberTokenName(): string | ||
{ | ||
return 'baz'; | ||
} | ||
} |