diff --git a/test/functional/HmacTokenTest.php b/test/functional/HmacTokenTest.php index d87771a65..de4ff146e 100644 --- a/test/functional/HmacTokenTest.php +++ b/test/functional/HmacTokenTest.php @@ -3,6 +3,7 @@ namespace Lcobucci\JWT\FunctionalTests; +use DateTimeImmutable; use Lcobucci\JWT\Configuration; use Lcobucci\JWT\Signer\Hmac\Sha256; use Lcobucci\JWT\Signer\Hmac\Sha512; @@ -131,4 +132,28 @@ public function everythingShouldWorkWhenUsingATokenGeneratedByOtherLibs(): void self::assertTrue($this->config->validator()->validate($token, $constraint)); self::assertEquals('world', $token->claims()->get('hello')); } + + /** + * @test + * @dataProvider datesWithPotentialRoundingIssues + */ + public function timeFractionsPrecisionsAreRespected(DateTimeImmutable $issuedAt): void + { + $token = $this->config->builder() + ->issuedAt($issuedAt) + ->getToken($this->config->signer(), $this->config->signingKey()); + + $parsedToken = $this->config->parser()->parse($token->toString()); + + self::assertInstanceOf(Token\Plain::class, $parsedToken); + self::assertSame($issuedAt->format('U.u'), $parsedToken->claims()->get('iat')->format('U.u')); + } + + public function datesWithPotentialRoundingIssues(): iterable + { + yield [DateTimeImmutable::createFromFormat('U.u', '1613938511.017448')]; + yield [DateTimeImmutable::createFromFormat('U.u', '1613938511.023691')]; + yield [DateTimeImmutable::createFromFormat('U.u', '1613938511.018045')]; + yield [DateTimeImmutable::createFromFormat('U.u', '1616074725.008455')]; + } }