diff --git a/src/JWT.php b/src/JWT.php index dd9292a4..fa6565d4 100644 --- a/src/JWT.php +++ b/src/JWT.php @@ -445,6 +445,26 @@ public static function urlsafeB64Encode(string $input): string return \str_replace('=', '', \strtr(\base64_encode($input), '+/', '-_')); } + /** + * Set leeway to max. Useful for unit testing with fixtures. + * + * @return void + */ + public static function maxLeeway(): void + { + static::$leeway = \PHP_INT_MAX; + } + + /** + * Reset leeway and timestamp to their initial values. + * + * @return void + */ + public static function resetTime(): void + { + static::$leeway = 0; + static::$timestamp = null; + } /** * Determine if an algorithm has been provided for each Key diff --git a/tests/JWTTest.php b/tests/JWTTest.php index d09d43e3..53e235f3 100644 --- a/tests/JWTTest.php +++ b/tests/JWTTest.php @@ -546,4 +546,29 @@ public function testAdditionalHeaderOverrides() $this->assertEquals('my_key_id', $headers->kid, 'key param not overridden'); $this->assertEquals('HS256', $headers->alg, 'alg param not overridden'); } + + public function testReset() + { + JWT::$leeway = 100; + JWT::$timestamp = 7000; + + // Pre-conditions + $this->assertSame(100, JWT::$leeway); + $this->assertSame(7000, JWT::$timestamp); + + JWT::resetTime(); + + $this->assertSame(0, JWT::$leeway); + $this->assertNull(JWT::$timestamp); + } + + public function testMaxLeeway() + { + // Pre-condition + $this->assertSame(0, JWT::$leeway); + + JWT::maxLeeway(); + + $this->assertSame(\PHP_INT_MAX, JWT::$leeway); + } }