1010
1111class AccessTokenTest extends TestCase
1212{
13+ /**
14+ * BC teardown.
15+ *
16+ * This is for backwards compatibility of older PHP versions. Ideally we would just implement a tearDown() here but
17+ * older PHP versions this library supports don't have return typehint support, so this is the workaround.
18+ *
19+ * @return void
20+ */
21+ private static function tearDownForBackwardsCompatibility ()
22+ {
23+ /* reset the test double time if it was set */
24+ AccessToken::resetTimeNow ();
25+ }
26+
1327 public function testInvalidRefreshToken ()
1428 {
1529 $ this ->expectException (InvalidArgumentException::class);
1630
1731 $ token = $ this ->getAccessToken (['invalid_access_token ' => 'none ' ]);
32+
33+ self ::tearDownForBackwardsCompatibility ();
1834 }
1935
2036 protected function getAccessToken ($ options = [])
@@ -32,6 +48,49 @@ public function testExpiresInCorrection()
3248 $ this ->assertNotNull ($ expires );
3349 $ this ->assertGreaterThan (time (), $ expires );
3450 $ this ->assertLessThan (time () + 200 , $ expires );
51+
52+ self ::tearDownForBackwardsCompatibility ();
53+ }
54+
55+ public function testExpiresInCorrectionUsingSetTimeNow ()
56+ {
57+ /* set fake time at 2020-01-01 00:00:00 */
58+ AccessToken::setTimeNow (1577836800 );
59+ $ options = ['access_token ' => 'access_token ' , 'expires_in ' => 100 ];
60+ $ token = $ this ->getAccessToken ($ options );
61+
62+ $ expires = $ token ->getExpires ();
63+
64+ $ this ->assertNotNull ($ expires );
65+ $ this ->assertEquals (1577836900 , $ expires );
66+
67+ self ::tearDownForBackwardsCompatibility ();
68+ }
69+
70+ public function testSetTimeNow ()
71+ {
72+ AccessToken::setTimeNow (1577836800 );
73+ $ timeNow = $ this ->getAccessToken (['access_token ' => 'asdf ' ])->getTimeNow ();
74+
75+ $ this ->assertEquals (1577836800 , $ timeNow );
76+
77+ self ::tearDownForBackwardsCompatibility ();
78+ }
79+
80+ public function testResetTimeNow ()
81+ {
82+ AccessToken::setTimeNow (1577836800 );
83+ $ token = $ this ->getAccessToken (['access_token ' => 'asdf ' ]);
84+
85+ $ this ->assertEquals (1577836800 , $ token ->getTimeNow ());
86+ AccessToken::resetTimeNow ();
87+
88+ $ this ->assertNotEquals (1577836800 , $ token ->getTimeNow ());
89+
90+ $ timeBeforeAssertion = time ();
91+ $ this ->assertGreaterThanOrEqual ($ timeBeforeAssertion , $ token ->getTimeNow ());
92+
93+ self ::tearDownForBackwardsCompatibility ();
3594 }
3695
3796 public function testExpiresPastTimestamp ()
@@ -45,6 +104,8 @@ public function testExpiresPastTimestamp()
45104 $ token = $ this ->getAccessToken ($ options );
46105
47106 $ this ->assertFalse ($ token ->hasExpired ());
107+
108+ self ::tearDownForBackwardsCompatibility ();
48109 }
49110
50111 public function testGetRefreshToken ()
@@ -58,6 +119,8 @@ public function testGetRefreshToken()
58119 $ refreshToken = $ token ->getRefreshToken ();
59120
60121 $ this ->assertEquals ($ options ['refresh_token ' ], $ refreshToken );
122+
123+ self ::tearDownForBackwardsCompatibility ();
61124 }
62125
63126 public function testHasNotExpiredWhenPropertySetInFuture ()
@@ -75,6 +138,8 @@ public function testHasNotExpiredWhenPropertySetInFuture()
75138 ->andReturn ($ expectedExpires );
76139
77140 $ this ->assertFalse ($ token ->hasExpired ());
141+
142+ self ::tearDownForBackwardsCompatibility ();
78143 }
79144
80145 public function testHasExpiredWhenPropertySetInPast ()
@@ -92,6 +157,8 @@ public function testHasExpiredWhenPropertySetInPast()
92157 ->andReturn ($ expectedExpires );
93158
94159 $ this ->assertTrue ($ token ->hasExpired ());
160+
161+ self ::tearDownForBackwardsCompatibility ();
95162 }
96163
97164 public function testCannotReportExpiredWhenNoExpirationSet ()
@@ -104,6 +171,8 @@ public function testCannotReportExpiredWhenNoExpirationSet()
104171 $ this ->expectException (RuntimeException::class);
105172
106173 $ hasExpired = $ token ->hasExpired ();
174+
175+ self ::tearDownForBackwardsCompatibility ();
107176 }
108177
109178 public function testInvalidExpiresIn ()
@@ -116,6 +185,8 @@ public function testInvalidExpiresIn()
116185 $ this ->expectException (InvalidArgumentException::class);
117186
118187 $ token = $ this ->getAccessToken ($ options );
188+
189+ self ::tearDownForBackwardsCompatibility ();
119190 }
120191
121192
@@ -132,6 +203,8 @@ public function testJsonSerializable()
132203 $ jsonToken = json_encode ($ token );
133204
134205 $ this ->assertEquals ($ options , json_decode ($ jsonToken , true ));
206+
207+ self ::tearDownForBackwardsCompatibility ();
135208 }
136209
137210 public function testValues ()
@@ -151,5 +224,7 @@ public function testValues()
151224 $ this ->assertTrue (is_array ($ values ));
152225 $ this ->assertArrayHasKey ('custom_thing ' , $ values );
153226 $ this ->assertSame ($ options ['custom_thing ' ], $ values ['custom_thing ' ]);
227+
228+ self ::tearDownForBackwardsCompatibility ();
154229 }
155230}
0 commit comments