Skip to content

Commit 0d17e0a

Browse files
committed
Add a new test case, refactoring
1 parent 17990cb commit 0d17e0a

File tree

1 file changed

+56
-15
lines changed

1 file changed

+56
-15
lines changed

app/code/Magento/Captcha/Test/Unit/Observer/CheckUserForgotPasswordBackendObserverTest.php

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
*/
3030
class CheckUserForgotPasswordBackendObserverTest extends TestCase
3131
{
32+
const STUB_EMAIL = 'stub@test.mail';
33+
const STUB_REQUEST_PARAMS = ['STUB_PARAM'];
34+
3235
/**
3336
* @var MockObject|DataHelper
3437
*/
@@ -60,7 +63,7 @@ class CheckUserForgotPasswordBackendObserverTest extends TestCase
6063
private $observer;
6164

6265
/**
63-
* @var MockObject
66+
* @var MockObject|CaptchaInterface
6467
*/
6568
private $captchaMock;
6669

@@ -79,14 +82,17 @@ class CheckUserForgotPasswordBackendObserverTest extends TestCase
7982
*/
8083
private $httpResponseMock;
8184

85+
/**
86+
* @var MockObject|HttpRequest
87+
*/
88+
private $requestMock;
89+
8290
/**
8391
* @inheritDoc
8492
*/
8593
protected function setUp()
8694
{
8795
$formId = 'backend_forgotpassword';
88-
$email = 'stub@test.mail';
89-
$requestParams = ['STUB_PARAM'];
9096

9197
$this->helperMock = $this->createMock(DataHelper::class);
9298
$this->captchaStringResolverMock = $this->createMock(CaptchaStringResolver::class);
@@ -116,14 +122,7 @@ protected function setUp()
116122
->with($formId)
117123
->willReturn($this->captchaMock);
118124

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);
127126
$this->httpResponseMock = $this->createMock(HttpResponse::class);
128127

129128
$this->controllerMock = $this->getMockBuilder(Action::class)
@@ -132,7 +131,7 @@ protected function setUp()
132131
->getMockForAbstractClass();
133132
$this->controllerMock->expects($this->any())
134133
->method('getRequest')
135-
->willReturn($requestMock);
134+
->willReturn($this->requestMock);
136135
$this->controllerMock->expects($this->any())
137136
->method('getResponse')
138137
->willReturn($this->httpResponseMock);
@@ -148,19 +147,19 @@ protected function setUp()
148147
*/
149148
public function testExecuteWhenCaptchaIsCorrect()
150149
{
150+
$this->configureRequestMockWithStubValues();
151151
$this->captchaMock->expects($this->once())->method('isRequired')->willReturn(true);
152152
$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');
155153

156-
$this->observer->execute($this->eventObserverMock);
154+
$this->executeOriginalMethodExpectsNoError();
157155
}
158156

159157
/**
160158
* Test case when Captcha is required and was entered incorrectly.
161159
*/
162160
public function testExecuteWhenCaptchaIsIncorrect()
163161
{
162+
$this->configureRequestMockWithStubValues();
164163
$this->captchaMock->expects($this->once())->method('isRequired')->willReturn(true);
165164
$this->captchaMock->expects($this->once())->method('isCorrect')->willReturn(false);
166165

@@ -180,7 +179,49 @@ public function testExecuteWhenCaptchaIsIncorrect()
180179
*/
181180
public function testExecuteWhenCaptchaIsNotRequired()
182181
{
182+
$this->configureRequestMockWithStubValues();
183183
$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+
{
184225
$this->messageManagerMock->expects($this->never())->method('addErrorMessage');
185226
$this->httpResponseMock->expects($this->never())->method('setRedirect');
186227

0 commit comments

Comments
 (0)