9
9
10
10
namespace SymfonyCasts \Bundle \ResetPassword \Tests \UnitTests ;
11
11
12
+ use PHPUnit \Framework \MockObject \MockObject ;
12
13
use PHPUnit \Framework \TestCase ;
13
14
use SymfonyCasts \Bundle \ResetPassword \Exception \ExpiredResetPasswordTokenException ;
14
15
use SymfonyCasts \Bundle \ResetPassword \Exception \InvalidResetPasswordTokenException ;
27
28
class ResetPasswordHelperTest extends TestCase
28
29
{
29
30
/**
30
- * @var \PHPUnit\Framework\MockObject\ MockObject|ResetPasswordRequestRepositoryInterface
31
+ * @var MockObject|ResetPasswordRequestRepositoryInterface
31
32
*/
32
33
private $ mockRepo ;
33
34
34
35
/**
35
- * @var \PHPUnit\Framework\MockObject\ MockObject|ResetPasswordTokenGenerator
36
+ * @var MockObject|ResetPasswordTokenGenerator
36
37
*/
37
38
private $ mockTokenGenerator ;
38
39
39
40
/**
40
- * @var \PHPUnit\Framework\MockObject\ MockObject|ResetPasswordRequestInterface
41
+ * @var MockObject|ResetPasswordRequestInterface
41
42
*/
42
43
private $ mockResetRequest ;
43
44
44
45
/**
45
- * @var \PHPUnit\Framework\MockObject\ MockObject|ResetPasswordCleaner
46
+ * @var MockObject|ResetPasswordCleaner
46
47
*/
47
48
private $ mockCleaner ;
48
49
@@ -51,11 +52,6 @@ class ResetPasswordHelperTest extends TestCase
51
52
*/
52
53
private $ randomToken ;
53
54
54
- /**
55
- * @var object
56
- */
57
- private $ mockUser ;
58
-
59
55
/**
60
56
* {@inheritdoc}
61
57
*/
@@ -66,18 +62,6 @@ protected function setUp(): void
66
62
$ this ->mockCleaner = $ this ->createMock (ResetPasswordCleaner::class);
67
63
$ this ->mockResetRequest = $ this ->createMock (ResetPasswordRequestInterface::class);
68
64
$ this ->randomToken = \bin2hex (\random_bytes (20 ));
69
- $ this ->mockUser = new class () {};
70
- }
71
-
72
- private function getPasswordResetHelper (): ResetPasswordHelper
73
- {
74
- return new ResetPasswordHelper (
75
- $ this ->mockTokenGenerator ,
76
- $ this ->mockCleaner ,
77
- $ this ->mockRepo ,
78
- 99999999 ,
79
- 99999999
80
- );
81
65
}
82
66
83
67
/**
@@ -98,12 +82,13 @@ public function testHasUserThrottlingReturnsFalseWithNoLastRequestDate(): void
98
82
;
99
83
100
84
$ this ->mockRepo
85
+ ->expects ($ this ->once ())
101
86
->method ('createResetPasswordRequest ' )
102
87
->willReturn (new ResetPasswordTestFixtureRequest ())
103
88
;
104
89
105
90
$ helper = $ this ->getPasswordResetHelper ();
106
- $ helper ->generateResetToken ($ this -> mockUser );
91
+ $ helper ->generateResetToken (new \ stdClass () );
107
92
}
108
93
109
94
/**
@@ -131,12 +116,13 @@ public function testHasUserThrottlingReturnsFalseIfNotBeforeThrottleTime(): void
131
116
;
132
117
133
118
$ this ->mockRepo
119
+ ->expects ($ this ->once ())
134
120
->method ('createResetPasswordRequest ' )
135
121
->willReturn (new ResetPasswordTestFixtureRequest ())
136
122
;
137
123
138
124
$ helper = $ this ->getPasswordResetHelper ();
139
- $ helper ->generateResetToken ($ this -> mockUser );
125
+ $ helper ->generateResetToken (new \ stdClass () );
140
126
}
141
127
142
128
public function testExceptionThrownIfRequestBeforeThrottleLimit (): void
@@ -157,7 +143,7 @@ public function testExceptionThrownIfRequestBeforeThrottleLimit(): void
157
143
$ this ->expectException (TooManyPasswordRequestsException::class);
158
144
159
145
$ helper = $ this ->getPasswordResetHelper ();
160
- $ helper ->generateResetToken ($ this -> mockUser );
146
+ $ helper ->generateResetToken (new \ stdClass () );
161
147
}
162
148
163
149
public function testRemoveResetRequestThrowsExceptionWithEmptyToken (): void
@@ -184,6 +170,7 @@ public function testRemoveResetRequestRetrievesTokenFromRepository(): void
184
170
public function testRemoveResetRequestCallsRepositoryToRemoveResetRequestObject (): void
185
171
{
186
172
$ this ->mockRepo
173
+ ->expects ($ this ->once ())
187
174
->method ('findResetPasswordRequest ' )
188
175
->willReturn ($ this ->mockResetRequest )
189
176
;
@@ -252,7 +239,7 @@ public function testValidateTokenFetchesUserIfTokenNotExpired(): void
252
239
$ this ->mockResetRequest
253
240
->expects ($ this ->once ())
254
241
->method ('getUser ' )
255
- ->willReturn ($ this -> mockUser )
242
+ ->willReturn (new \ stdClass () )
256
243
;
257
244
258
245
$ this ->mockResetRequest
@@ -283,7 +270,7 @@ public function testValidateTokenThrowsExceptionIfTokenAndVerifierDoNotMatch():
283
270
$ this ->mockResetRequest
284
271
->expects ($ this ->once ())
285
272
->method ('getUser ' )
286
- ->willReturn ($ this -> mockUser )
273
+ ->willReturn (new \ stdClass () )
287
274
;
288
275
289
276
$ this ->mockResetRequest
@@ -306,26 +293,36 @@ public function testValidateTokenThrowsExceptionIfTokenAndVerifierDoNotMatch():
306
293
307
294
public function testGenerateResetTokenCallsGarbageCollector (): void
308
295
{
309
- $ this ->setMockCleanerExpectations ();
296
+ $ this ->mockCleaner
297
+ ->expects ($ this ->once ())
298
+ ->method ('handleGarbageCollection ' )
299
+ ;
310
300
311
301
$ helper = $ this ->getPasswordResetHelper ();
312
- $ helper ->generateResetToken ($ this -> mockUser );
302
+ $ helper ->generateResetToken (new \ stdClass () );
313
303
}
314
304
315
305
public function testGarbageCollectorCalledDuringValidation (): void
316
306
{
317
- $ this ->setMockCleanerExpectations ();
307
+ $ this ->mockCleaner
308
+ ->expects ($ this ->once ())
309
+ ->method ('handleGarbageCollection ' )
310
+ ;
311
+
318
312
$ this ->expectException (InvalidResetPasswordTokenException::class);
319
313
320
314
$ helper = $ this ->getPasswordResetHelper ();
321
315
$ helper ->validateTokenAndFetchUser ($ this ->randomToken );
322
316
}
323
317
324
- private function setMockCleanerExpectations (): void
318
+ private function getPasswordResetHelper (): ResetPasswordHelper
325
319
{
326
- $ this ->mockCleaner
327
- ->expects ($ this ->once ())
328
- ->method ('handleGarbageCollection ' )
329
- ;
320
+ return new ResetPasswordHelper (
321
+ $ this ->mockTokenGenerator ,
322
+ $ this ->mockCleaner ,
323
+ $ this ->mockRepo ,
324
+ 99999999 ,
325
+ 99999999
326
+ );
330
327
}
331
328
}
0 commit comments