Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ForgotPasswordPostTest.php
Go to the documentation of this file.
1 <?php
7 
15 use Magento\Framework\Controller\Result\RedirectFactory as ResultRedirectFactory;
19 
23 class ForgotPasswordPostTest extends \PHPUnit\Framework\TestCase
24 {
28  protected $controller;
29 
33  protected $context;
34 
38  protected $session;
39 
43  protected $accountManagement;
44 
48  protected $escaper;
49 
53  protected $resultRedirect;
54 
59 
63  protected $request;
64 
68  protected $messageManager;
69 
70  protected function setUp()
71  {
72  $this->prepareContext();
73 
74  $this->session = $this->getMockBuilder(\Magento\Customer\Model\Session::class)
75  ->disableOriginalConstructor()
76  ->getMock();
77 
78  $this->accountManagement = $this->getMockBuilder(\Magento\Customer\Api\AccountManagementInterface::class)
79  ->getMockForAbstractClass();
80 
81  $this->escaper = $this->getMockBuilder(\Magento\Framework\Escaper::class)
82  ->disableOriginalConstructor()
83  ->getMock();
84 
85  $this->controller = new ForgotPasswordPost(
86  $this->context,
87  $this->session,
88  $this->accountManagement,
89  $this->escaper
90  );
91  }
92 
93  public function testExecuteEmptyEmail()
94  {
95  $this->request->expects($this->once())
96  ->method('getPost')
97  ->with('email')
98  ->willReturn(null);
99 
100  $this->messageManager->expects($this->once())
101  ->method('addErrorMessage')
102  ->with(__('Please enter your email.'))
103  ->willReturnSelf();
104 
105  $this->resultRedirect->expects($this->once())
106  ->method('setPath')
107  ->with('*/*/forgotpassword')
108  ->willReturnSelf();
109 
110  $this->assertSame($this->resultRedirect, $this->controller->execute());
111  }
112 
113  public function testExecute()
114  {
116 
117  $this->request->expects($this->once())
118  ->method('getPost')
119  ->with('email')
120  ->willReturn($email);
121 
122  $this->accountManagement->expects($this->once())
123  ->method('initiatePasswordReset')
125  ->willReturnSelf();
126 
127  $this->escaper->expects($this->once())
128  ->method('escapeHtml')
129  ->with($email)
130  ->willReturn($email);
131 
132  $message = __(
133  'If there is an account associated with %1 you will receive an email with a link to reset your password.',
134  $email
135  );
136  $this->messageManager->expects($this->once())
137  ->method('addSuccessMessage')
138  ->with($message)
139  ->willReturnSelf();
140 
141  $this->resultRedirect->expects($this->once())
142  ->method('setPath')
143  ->with('*/*/')
144  ->willReturnSelf();
145 
146  $this->controller->execute();
147  }
148 
150  {
152 
153  $this->request->expects($this->once())
154  ->method('getPost')
155  ->with('email')
156  ->willReturn($email);
157 
158  $this->accountManagement->expects($this->once())
159  ->method('initiatePasswordReset')
161  ->willThrowException(new NoSuchEntityException(__('NoSuchEntityException')));
162 
163  $this->escaper->expects($this->once())
164  ->method('escapeHtml')
165  ->with($email)
166  ->willReturn($email);
167 
168  $message = __(
169  'If there is an account associated with %1 you will receive an email with a link to reset your password.',
170  $email
171  );
172  $this->messageManager->expects($this->once())
173  ->method('addSuccessMessage')
174  ->with($message)
175  ->willReturnSelf();
176 
177  $this->resultRedirect->expects($this->once())
178  ->method('setPath')
179  ->with('*/*/')
180  ->willReturnSelf();
181 
182  $this->controller->execute();
183  }
184 
185  public function testExecuteException()
186  {
188  $exception = new \Exception(__('Exception'));
189 
190  $this->request->expects($this->once())
191  ->method('getPost')
192  ->with('email')
193  ->willReturn($email);
194 
195  $this->accountManagement->expects($this->once())
196  ->method('initiatePasswordReset')
198  ->willThrowException($exception);
199 
200  $this->messageManager->expects($this->once())
201  ->method('addExceptionMessage')
202  ->with($exception, __('We\'re unable to send the password reset email.'))
203  ->willReturnSelf();
204 
205  $this->resultRedirect->expects($this->once())
206  ->method('setPath')
207  ->with('*/*/forgotpassword')
208  ->willReturnSelf();
209 
210  $this->controller->execute();
211  }
212 
213  protected function prepareContext()
214  {
215  $this->resultRedirect = $this->getMockBuilder(\Magento\Framework\Controller\Result\Redirect::class)
216  ->disableOriginalConstructor()
217  ->getMock();
218 
219  $this->resultRedirectFactory = $this->getMockBuilder(
220  \Magento\Framework\Controller\Result\RedirectFactory::class
221  )
222  ->disableOriginalConstructor()
223  ->getMock();
224 
225  $this->context = $this->getMockBuilder(\Magento\Framework\App\Action\Context::class)
226  ->disableOriginalConstructor()
227  ->getMock();
228 
229  $this->request = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
230  ->disableOriginalConstructor()
231  ->setMethods([
232  'getPost',
233  ])
234  ->getMock();
235 
236  $this->messageManager = $this->getMockBuilder(\Magento\Framework\Message\ManagerInterface::class)
237  ->getMockForAbstractClass();
238 
239  $this->resultRedirectFactory->expects($this->any())
240  ->method('create')
241  ->willReturn($this->resultRedirect);
242 
243  $this->context->expects($this->any())
244  ->method('getResultRedirectFactory')
245  ->willReturn($this->resultRedirectFactory);
246 
247  $this->context->expects($this->any())
248  ->method('getRequest')
249  ->willReturn($this->request);
250 
251  $this->context->expects($this->any())
252  ->method('getMessageManager')
253  ->willReturn($this->messageManager);
254  }
255 }
testExecuteException()
testExecute()
$email
Definition: details.phtml:13
testExecuteEmptyEmail()
$session
__()
Definition: __.php:13
$message
$accountManagement
$context
$escaper
$resultRedirect
prepareContext()
setUp()
$controller
testExecuteNoSuchEntityException()
$messageManager
$resultRedirectFactory
$request