-
-
Notifications
You must be signed in to change notification settings - Fork 165
perf(auth): memoize current authenticatable #2144
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
Open
xHeaven
wants to merge
1
commit into
tempestphp:3.x
Choose a base branch
from
xHeaven:fix/auth-current-cache
base: 3.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+227
−7
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,189 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tempest\Auth\Tests; | ||
|
|
||
| use PHPUnit\Framework\Attributes\Test; | ||
| use PHPUnit\Framework\TestCase; | ||
| use Tempest\Auth\Authentication\Authenticatable; | ||
| use Tempest\Auth\Authentication\AuthenticatableResolver; | ||
| use Tempest\Auth\Authentication\SessionAuthenticator; | ||
| use Tempest\DateTime\DateTime; | ||
| use Tempest\Http\Session\Session; | ||
| use Tempest\Http\Session\SessionId; | ||
| use Tempest\Http\Session\SessionManager; | ||
|
|
||
| final class SessionAuthenticatorTest extends TestCase | ||
| { | ||
| #[Test] | ||
| public function current_memoizes_the_resolved_authenticatable_for_the_current_session_identity(): void | ||
| { | ||
| $authenticatable = new MemoizedAuthenticatable(id: 1); | ||
| $resolver = new CountingAuthenticatableResolver($authenticatable); | ||
| $session = $this->createSession(); | ||
| $session->set(SessionAuthenticator::AUTHENTICATABLE_KEY, 1); | ||
| $session->set(SessionAuthenticator::AUTHENTICATABLE_CLASS, MemoizedAuthenticatable::class); | ||
|
|
||
| $authenticator = new SessionAuthenticator( | ||
| sessionManager: new TestingSessionManager(), | ||
| session: $session, | ||
| authenticatableResolver: $resolver, | ||
| ); | ||
|
|
||
| $this->assertSame($authenticatable, $authenticator->current()); | ||
| $this->assertSame($authenticatable, $authenticator->current()); | ||
| $this->assertSame(1, $resolver->resolveCalls); | ||
| } | ||
|
|
||
| #[Test] | ||
| public function current_memoizes_a_missing_authenticatable_for_the_current_session_identity(): void | ||
| { | ||
| $resolver = new CountingAuthenticatableResolver(); | ||
| $session = $this->createSession(); | ||
| $session->set(SessionAuthenticator::AUTHENTICATABLE_KEY, 1); | ||
| $session->set(SessionAuthenticator::AUTHENTICATABLE_CLASS, MemoizedAuthenticatable::class); | ||
|
|
||
| $authenticator = new SessionAuthenticator( | ||
| sessionManager: new TestingSessionManager(), | ||
| session: $session, | ||
| authenticatableResolver: $resolver, | ||
| ); | ||
|
|
||
| $this->assertNull($authenticator->current()); | ||
| $this->assertNull($authenticator->current()); | ||
| $this->assertSame(1, $resolver->resolveCalls); | ||
| } | ||
|
|
||
| #[Test] | ||
| public function current_re_resolves_when_the_session_identity_changes(): void | ||
| { | ||
| $resolver = new CountingAuthenticatableResolver( | ||
| new MemoizedAuthenticatable(id: 1), | ||
| new MemoizedAuthenticatable(id: 2), | ||
| ); | ||
| $session = $this->createSession(); | ||
| $session->set(SessionAuthenticator::AUTHENTICATABLE_KEY, 1); | ||
| $session->set(SessionAuthenticator::AUTHENTICATABLE_CLASS, MemoizedAuthenticatable::class); | ||
|
|
||
| $authenticator = new SessionAuthenticator( | ||
| sessionManager: new TestingSessionManager(), | ||
| session: $session, | ||
| authenticatableResolver: $resolver, | ||
| ); | ||
|
|
||
| $current = $authenticator->current(); | ||
| $this->assertInstanceOf(MemoizedAuthenticatable::class, $current); | ||
| $this->assertSame(1, $current->id); | ||
|
|
||
| $session->set(SessionAuthenticator::AUTHENTICATABLE_KEY, 2); | ||
|
|
||
| $current = $authenticator->current(); | ||
| $this->assertInstanceOf(MemoizedAuthenticatable::class, $current); | ||
| $this->assertSame(2, $current->id); | ||
| $this->assertSame(2, $resolver->resolveCalls); | ||
| } | ||
|
|
||
| #[Test] | ||
| public function authenticate_replaces_a_cached_current_authenticatable(): void | ||
| { | ||
| $resolver = new CountingAuthenticatableResolver( | ||
| new MemoizedAuthenticatable(id: 1), | ||
| new MemoizedAuthenticatable(id: 2), | ||
| ); | ||
| $session = $this->createSession(); | ||
| $session->set(SessionAuthenticator::AUTHENTICATABLE_KEY, 1); | ||
| $session->set(SessionAuthenticator::AUTHENTICATABLE_CLASS, MemoizedAuthenticatable::class); | ||
|
|
||
| $authenticator = new SessionAuthenticator( | ||
| sessionManager: new TestingSessionManager(), | ||
| session: $session, | ||
| authenticatableResolver: $resolver, | ||
| ); | ||
|
|
||
| $current = $authenticator->current(); | ||
| $this->assertInstanceOf(MemoizedAuthenticatable::class, $current); | ||
| $this->assertSame(1, $current->id); | ||
|
|
||
| $authenticator->authenticate(new MemoizedAuthenticatable(id: 2)); | ||
|
|
||
| $current = $authenticator->current(); | ||
| $this->assertInstanceOf(MemoizedAuthenticatable::class, $current); | ||
| $this->assertSame(2, $current->id); | ||
| } | ||
|
|
||
| private function createSession(): Session | ||
| { | ||
| $now = DateTime::now(); | ||
|
|
||
| return new Session( | ||
| id: new SessionId('test-session'), | ||
| createdAt: $now, | ||
| lastActiveAt: $now, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| final readonly class MemoizedAuthenticatable implements Authenticatable | ||
| { | ||
| public function __construct( | ||
| public int $id, | ||
| ) {} | ||
| } | ||
|
|
||
| final class CountingAuthenticatableResolver implements AuthenticatableResolver | ||
| { | ||
| public int $resolveCalls = 0; | ||
|
|
||
| /** @var array<int|string, MemoizedAuthenticatable> */ | ||
| private array $authenticatables = []; | ||
|
|
||
| public function __construct(MemoizedAuthenticatable ...$authenticatables) | ||
| { | ||
| foreach ($authenticatables as $authenticatable) { | ||
| $this->authenticatables[$authenticatable->id] = $authenticatable; | ||
| } | ||
| } | ||
|
|
||
| public function resolve(int|string $id, string $class): ?Authenticatable | ||
| { | ||
| $this->resolveCalls++; | ||
|
|
||
| if ($class !== MemoizedAuthenticatable::class) { | ||
| return null; | ||
| } | ||
|
|
||
| return $this->authenticatables[$id] ?? null; | ||
| } | ||
|
|
||
| public function resolveId(Authenticatable $authenticatable): int | ||
| { | ||
| return $authenticatable instanceof MemoizedAuthenticatable ? $authenticatable->id : 0; | ||
| } | ||
| } | ||
|
|
||
| final class TestingSessionManager implements SessionManager | ||
| { | ||
| public int $savedSessions = 0; | ||
|
|
||
| public function getOrCreate(SessionId $id): Session | ||
| { | ||
| $now = DateTime::now(); | ||
|
|
||
| return new Session($id, $now, $now); | ||
| } | ||
|
|
||
| public function save(Session $session): void | ||
| { | ||
| $this->savedSessions++; | ||
| } | ||
|
|
||
| public function delete(Session $session): void {} | ||
|
|
||
| public function isValid(Session $session): bool | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| public function deleteExpiredSessions(): void {} | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you also implement
Resettable? That way this one is already prepared for FrankenPHP support :)