29
29
*/
30
30
class CheckUserForgotPasswordBackendObserverTest extends TestCase
31
31
{
32
+ const STUB_EMAIL = 'stub@test.mail ' ;
33
+ const STUB_REQUEST_PARAMS = ['STUB_PARAM ' ];
34
+
32
35
/**
33
36
* @var MockObject|DataHelper
34
37
*/
@@ -60,7 +63,7 @@ class CheckUserForgotPasswordBackendObserverTest extends TestCase
60
63
private $ observer ;
61
64
62
65
/**
63
- * @var MockObject
66
+ * @var MockObject|CaptchaInterface
64
67
*/
65
68
private $ captchaMock ;
66
69
@@ -79,14 +82,17 @@ class CheckUserForgotPasswordBackendObserverTest extends TestCase
79
82
*/
80
83
private $ httpResponseMock ;
81
84
85
+ /**
86
+ * @var MockObject|HttpRequest
87
+ */
88
+ private $ requestMock ;
89
+
82
90
/**
83
91
* @inheritDoc
84
92
*/
85
93
protected function setUp ()
86
94
{
87
95
$ formId = 'backend_forgotpassword ' ;
88
- $ email = 'stub@test.mail ' ;
89
- $ requestParams = ['STUB_PARAM ' ];
90
96
91
97
$ this ->helperMock = $ this ->createMock (DataHelper::class);
92
98
$ this ->captchaStringResolverMock = $ this ->createMock (CaptchaStringResolver::class);
@@ -116,14 +122,7 @@ protected function setUp()
116
122
->with ($ formId )
117
123
->willReturn ($ this ->captchaMock );
118
124
119
- $ requestMock = $ this ->createMock (HttpRequest::class);
120
- $ requestMock ->expects ($ this ->any ())
121
- ->method ('getParam ' )
122
- ->with ('email ' )
123
- ->willReturn ($ email );
124
- $ requestMock ->expects ($ this ->any ())
125
- ->method ('getParams ' )
126
- ->willReturn ($ requestParams );
125
+ $ this ->requestMock = $ this ->createMock (HttpRequest::class);
127
126
$ this ->httpResponseMock = $ this ->createMock (HttpResponse::class);
128
127
129
128
$ this ->controllerMock = $ this ->getMockBuilder (Action::class)
@@ -132,7 +131,7 @@ protected function setUp()
132
131
->getMockForAbstractClass ();
133
132
$ this ->controllerMock ->expects ($ this ->any ())
134
133
->method ('getRequest ' )
135
- ->willReturn ($ requestMock );
134
+ ->willReturn ($ this -> requestMock );
136
135
$ this ->controllerMock ->expects ($ this ->any ())
137
136
->method ('getResponse ' )
138
137
->willReturn ($ this ->httpResponseMock );
@@ -148,19 +147,19 @@ protected function setUp()
148
147
*/
149
148
public function testExecuteWhenCaptchaIsCorrect ()
150
149
{
150
+ $ this ->configureRequestMockWithStubValues ();
151
151
$ this ->captchaMock ->expects ($ this ->once ())->method ('isRequired ' )->willReturn (true );
152
152
$ this ->captchaMock ->expects ($ this ->once ())->method ('isCorrect ' )->willReturn (true );
153
- $ this ->messageManagerMock ->expects ($ this ->never ())->method ('addErrorMessage ' );
154
- $ this ->httpResponseMock ->expects ($ this ->never ())->method ('setRedirect ' );
155
153
156
- $ this ->observer -> execute ( $ this -> eventObserverMock );
154
+ $ this ->executeOriginalMethodExpectsNoError ( );
157
155
}
158
156
159
157
/**
160
158
* Test case when Captcha is required and was entered incorrectly.
161
159
*/
162
160
public function testExecuteWhenCaptchaIsIncorrect ()
163
161
{
162
+ $ this ->configureRequestMockWithStubValues ();
164
163
$ this ->captchaMock ->expects ($ this ->once ())->method ('isRequired ' )->willReturn (true );
165
164
$ this ->captchaMock ->expects ($ this ->once ())->method ('isCorrect ' )->willReturn (false );
166
165
@@ -180,7 +179,49 @@ public function testExecuteWhenCaptchaIsIncorrect()
180
179
*/
181
180
public function testExecuteWhenCaptchaIsNotRequired ()
182
181
{
182
+ $ this ->configureRequestMockWithStubValues ();
183
183
$ this ->captchaMock ->expects ($ this ->once ())->method ('isRequired ' )->willReturn (false );
184
+
185
+ $ this ->executeOriginalMethodExpectsNoError ();
186
+ }
187
+
188
+ /**
189
+ * Test case when email is not provided
190
+ */
191
+ public function testExecuteWhenEmailParamIsNotPresent ()
192
+ {
193
+ $ this ->requestMock ->expects ($ this ->any ())
194
+ ->method ('getParam ' )
195
+ ->with ('email ' )
196
+ ->willReturn (null );
197
+ $ this ->requestMock ->expects ($ this ->any ())
198
+ ->method ('getParams ' )
199
+ ->willReturn (self ::STUB_REQUEST_PARAMS );
200
+ $ this ->captchaMock ->expects ($ this ->never ())->method ('isRequired ' );
201
+ $ this ->captchaMock ->expects ($ this ->never ())->method ('isCorrect ' );
202
+
203
+ $ this ->executeOriginalMethodExpectsNoError ();
204
+ }
205
+
206
+ /**
207
+ * Stub params for Request Mock
208
+ */
209
+ private function configureRequestMockWithStubValues ()
210
+ {
211
+ $ this ->requestMock ->expects ($ this ->any ())
212
+ ->method ('getParam ' )
213
+ ->with ('email ' )
214
+ ->willReturn (self ::STUB_EMAIL );
215
+ $ this ->requestMock ->expects ($ this ->any ())
216
+ ->method ('getParams ' )
217
+ ->willReturn (self ::STUB_REQUEST_PARAMS );
218
+ }
219
+
220
+ /**
221
+ * Run original method, expect there is no error
222
+ */
223
+ private function executeOriginalMethodExpectsNoError ()
224
+ {
184
225
$ this ->messageManagerMock ->expects ($ this ->never ())->method ('addErrorMessage ' );
185
226
$ this ->httpResponseMock ->expects ($ this ->never ())->method ('setRedirect ' );
186
227
0 commit comments