22
33namespace Mvdnbrk \DhlParcel \Tests \Unit \Resources ;
44
5- use Lcobucci \JWT \Builder ;
5+ use DateInterval ;
6+ use DateTimeImmutable ;
7+ use Lcobucci \JWT \Configuration ;
68use Mvdnbrk \DhlParcel \Resources \AccessToken ;
79use Mvdnbrk \DhlParcel \Tests \TestCase ;
810
911class AccessTokenTest extends TestCase
1012{
13+ /** @var \Lcobucci\JWT\Configuration */
14+ protected $ config ;
15+
16+ protected function setUp (): void
17+ {
18+ parent ::setUp ();
19+
20+ $ this ->config = Configuration::forUnsecuredSigner ();
21+ }
22+
1123 /** @test */
1224 public function create_a_new_access_token ()
1325 {
26+ $ expires = (new DateTimeImmutable ('1970-01-01 00:00:00 ' ))->add (new DateInterval ('PT9S ' ));
1427 $ accessToken = new AccessToken (
15- ( new Builder )->setExpiration ( 9 )->set ('accounts ' , [])->set ('roles ' , [])->getToken ()-> __toString ()
28+ $ this -> config -> builder ( )->expiresAt ( $ expires )->withClaim ('accounts ' , [])->withClaim ('roles ' , [])->getToken ($ this -> config -> signer (), $ this -> config -> signingKey ())-> toString ()
1629 );
1730
1831 $ this ->assertSame ('eyJ0eXAiOiJKV1QiLCJhbGciOiJub25lIn0.eyJleHAiOjksImFjY291bnRzIjpbXSwicm9sZXMiOltdfQ. ' , $ accessToken ->token );
@@ -22,14 +35,16 @@ public function create_a_new_access_token()
2235 /** @test */
2336 public function it_can_determine_if_the_access_token_has_expired ()
2437 {
38+ $ expires = (new DateTimeImmutable ())->sub (new DateInterval ('PT1S ' ));
2539 $ accessToken = new AccessToken (
26- ( new Builder )->setExpiration ( 0 )->set ('accounts ' , [])->set ('roles ' , [])->getToken ()-> __toString ()
40+ $ this -> config -> builder ( )->expiresAt ( $ expires )->withClaim ('accounts ' , [])->withClaim ('roles ' , [])->getToken ($ this -> config -> signer (), $ this -> config -> signingKey ())-> toString ()
2741 );
2842
2943 $ this ->assertTrue ($ accessToken ->isExpired ());
3044
45+ $ expires = (new DateTimeImmutable ())->add (new DateInterval ('PT9S ' ));
3146 $ accessToken = new AccessToken (
32- ( new Builder )->setExpiration ( time () + 999 )-> set ('accounts ' , [])->set ('roles ' , [])->getToken ()-> __toString ()
47+ $ this -> config -> builder ( )->expiresAt ( $ expires )-> withClaim ('accounts ' , [])->withClaim ('roles ' , [])->getToken ($ this -> config -> signer (), $ this -> config -> signingKey ())-> toString ()
3348 );
3449
3550 $ this ->assertFalse ($ accessToken ->isExpired ());
@@ -38,8 +53,9 @@ public function it_can_determine_if_the_access_token_has_expired()
3853 /** @test */
3954 public function it_can_retrieve_the_account_id_from_the_token ()
4055 {
56+ $ expires = new DateTimeImmutable ();
4157 $ accessToken = new AccessToken (
42- ( new Builder )->setExpiration ( 0 )->set ('accounts ' , ['123456 ' ])->set ('roles ' , [])->getToken ()-> __toString ()
58+ $ this -> config -> builder ( )->expiresAt ( $ expires )->withClaim ('accounts ' , ['123456 ' ])->withClaim ('roles ' , [])->getToken ($ this -> config -> signer (), $ this -> config -> signingKey ())-> toString ()
4359 );
4460
4561 $ this ->assertEquals ('123456 ' , $ accessToken ->getAccountId ());
@@ -48,8 +64,9 @@ public function it_can_retrieve_the_account_id_from_the_token()
4864 /** @test */
4965 public function it_can_set_the_account_id ()
5066 {
67+ $ expires = new DateTimeImmutable ();
5168 $ accessToken = new AccessToken (
52- ( new Builder )->setExpiration ( 0 )->set ('accounts ' , ['1111 ' , '2222 ' ])->set ('roles ' , [])->getToken ()-> __toString ()
69+ $ this -> config -> builder ( )->expiresAt ( $ expires )->withClaim ('accounts ' , ['1111 ' , '2222 ' ])->withClaim ('roles ' , [])->getToken ($ this -> config -> signer (), $ this -> config -> signingKey ())-> toString ()
5370 );
5471
5572 $ accessToken ->setAccountId ('does-not-exist ' );
0 commit comments