Use symfony/clock component when available instead of non-testable time() #1204
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.
Motivation:
Tests are already hard enough to build so that they are testing, covering, isolated and in an expected way.
Therefore, managing external dependencies is an obligation. And time is an external dependency.
With the introduction of symfony/clock in 6.3, Symfony proposes a solution for managing time in an application, with a shared instance that is overridable during the tests.
Solution:
This PR looked over the time() usage, and replaced them with SymfonyClock::now()->getTimeStamp() when available.
Workaround:
It's possible to use Symfony/Clock in your own application without this PR by using a
Events::JWT_CREATED
event listener to update nbf, iat and exp accordingly.Retro-compatibility:
Since 2.x still support down to symfony 4, this dependency is optional, therefore it was only added in suggest key.
If there is a conflict with specific version of clock, we'll be able to restrict it by using the conflict key.
3.x:
This PR does not target 3.x because it's not released yet. But in a 3.x version, symfony/clock could become a requirement and allow code simplification overall.
Tests:
Current test suite still uses time(), but they could be improved to take advantage of this new testability.