10
10
11
11
class AccessTokenTest extends TestCase
12
12
{
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
+
13
27
public function testInvalidRefreshToken ()
14
28
{
15
29
$ this ->expectException (InvalidArgumentException::class);
16
30
17
31
$ token = $ this ->getAccessToken (['invalid_access_token ' => 'none ' ]);
32
+
33
+ self ::tearDownForBackwardsCompatibility ();
18
34
}
19
35
20
36
protected function getAccessToken ($ options = [])
@@ -32,6 +48,49 @@ public function testExpiresInCorrection()
32
48
$ this ->assertNotNull ($ expires );
33
49
$ this ->assertGreaterThan (time (), $ expires );
34
50
$ 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 ();
35
94
}
36
95
37
96
public function testExpiresPastTimestamp ()
@@ -45,6 +104,8 @@ public function testExpiresPastTimestamp()
45
104
$ token = $ this ->getAccessToken ($ options );
46
105
47
106
$ this ->assertFalse ($ token ->hasExpired ());
107
+
108
+ self ::tearDownForBackwardsCompatibility ();
48
109
}
49
110
50
111
public function testGetRefreshToken ()
@@ -58,6 +119,8 @@ public function testGetRefreshToken()
58
119
$ refreshToken = $ token ->getRefreshToken ();
59
120
60
121
$ this ->assertEquals ($ options ['refresh_token ' ], $ refreshToken );
122
+
123
+ self ::tearDownForBackwardsCompatibility ();
61
124
}
62
125
63
126
public function testHasNotExpiredWhenPropertySetInFuture ()
@@ -75,6 +138,8 @@ public function testHasNotExpiredWhenPropertySetInFuture()
75
138
->andReturn ($ expectedExpires );
76
139
77
140
$ this ->assertFalse ($ token ->hasExpired ());
141
+
142
+ self ::tearDownForBackwardsCompatibility ();
78
143
}
79
144
80
145
public function testHasExpiredWhenPropertySetInPast ()
@@ -92,6 +157,8 @@ public function testHasExpiredWhenPropertySetInPast()
92
157
->andReturn ($ expectedExpires );
93
158
94
159
$ this ->assertTrue ($ token ->hasExpired ());
160
+
161
+ self ::tearDownForBackwardsCompatibility ();
95
162
}
96
163
97
164
public function testCannotReportExpiredWhenNoExpirationSet ()
@@ -104,6 +171,8 @@ public function testCannotReportExpiredWhenNoExpirationSet()
104
171
$ this ->expectException (RuntimeException::class);
105
172
106
173
$ hasExpired = $ token ->hasExpired ();
174
+
175
+ self ::tearDownForBackwardsCompatibility ();
107
176
}
108
177
109
178
public function testInvalidExpiresIn ()
@@ -116,6 +185,8 @@ public function testInvalidExpiresIn()
116
185
$ this ->expectException (InvalidArgumentException::class);
117
186
118
187
$ token = $ this ->getAccessToken ($ options );
188
+
189
+ self ::tearDownForBackwardsCompatibility ();
119
190
}
120
191
121
192
@@ -132,6 +203,8 @@ public function testJsonSerializable()
132
203
$ jsonToken = json_encode ($ token );
133
204
134
205
$ this ->assertEquals ($ options , json_decode ($ jsonToken , true ));
206
+
207
+ self ::tearDownForBackwardsCompatibility ();
135
208
}
136
209
137
210
public function testValues ()
@@ -151,5 +224,7 @@ public function testValues()
151
224
$ this ->assertTrue (is_array ($ values ));
152
225
$ this ->assertArrayHasKey ('custom_thing ' , $ values );
153
226
$ this ->assertSame ($ options ['custom_thing ' ], $ values ['custom_thing ' ]);
227
+
228
+ self ::tearDownForBackwardsCompatibility ();
154
229
}
155
230
}
0 commit comments