Note that there are some explanatory texts on larger screens.

plurals
  1. POCakephp mock Email Utility
    primarykey
    data
    text
    <p>i'm a little stuck trying to test my users-controller in cakephp, i have an action wich sends an email to a certain email address. The email utility works, no problem whatsoever.</p> <p>I want to mock the email utility so that when i test the action (with "testAction"), no email will be sent. I already searched all over stackoverflow and tried a lot of solutions, the current code is like follows:</p> <p>UsersController:</p> <pre><code> /** * Get Email Utility, use method so that unit testing is possible * @return object */ public function _getEmailer() { return new CakeEmail(); } public function lostPassword() { if($this-&gt;request-&gt;is('post')) { $email = $this-&gt;request-&gt;data['User']['email']; $this-&gt;User-&gt;recursive = -1; $user = $this-&gt;User-&gt;find('first', array('conditions' =&gt; array('email' =&gt; $email))); if(!$user) { $this-&gt;Session-&gt;setFlash('Error: user not found'); return $this-&gt;render(); } $recoverTrials = $this-&gt;User-&gt;Recover-&gt;find('count', array('conditions' =&gt; array('email' =&gt; $email))); if($recoverTrials &gt; 3) { $this-&gt;Session-&gt;setFlash(__('message.recover-too-many')); return $this-&gt;redirect('/'); } // Generate random key to reset password $key = md5(microtime().rand()); $data = array('user_id' =&gt; $user['User']['id'], 'key' =&gt; $key, 'active' =&gt; 1, 'created' =&gt; date('Y-m-d H:i:s')); if(!$this-&gt;User-&gt;Recover-&gt;save($data)) { $this-&gt;Session-&gt;setFlash('Error while sending the recovery-mail'); return $this-&gt;redirect('/'); } $this-&gt;Email = $this-&gt;_getEmailer(); $this-&gt;Email-&gt;emailFormat('html'); $this-&gt;Email-&gt;to($email); $this-&gt;Email-&gt;subject('Password recover'); $this-&gt;Email-&gt;replyTo('noreply@domain.com'); $this-&gt;Email-&gt;from(array('noreply@domain.com' =&gt; 'Sender ID')); $this-&gt;Email-&gt;template('recover'); $this-&gt;Email-&gt;viewVars(array('key' =&gt; $key)); // Set variables for template try { $this-&gt;Email-&gt;send(); } catch(Exception $e) { $this-&gt;Session-&gt;setFlash('Error while sending the recovery-mail'); return $this-&gt;render(); } $this-&gt;Session-&gt;setFlash('The recovery mail with instructions to reset your password has been sent. Please note that the link will only remain active for 2 hours.'); return $this-&gt;redirect('/'); } } </code></pre> <p>And my test class looks like (excerpt):</p> <pre><code>public function testPostLostPassword() { $this-&gt;Controller = $this-&gt;generate('Users', array( 'methods' =&gt; array( '_getEmailer' ), 'components' =&gt; array('Security') )); $emailer = $this-&gt;getMock('CakeEmail', array( 'to', 'emailFormat', 'subject', 'replyTo', 'from', 'template', 'viewVars', 'send' )); $emailer-&gt;expects($this-&gt;any()) -&gt;method('send') -&gt;will($this-&gt;returnValue(true)); $this-&gt;Controller-&gt;expects($this-&gt;any()) -&gt;method('_getEmailer') -&gt;will($this-&gt;returnValue($emailer)); Correct email $data = array('User' =&gt; array('email' =&gt; 'me@domain.com')); $result = $this-&gt;testAction('/lostPassword', array('method' =&gt; 'post', 'data' =&gt; $data, 'return' =&gt; 'contents')); } </code></pre> <p>What am i doing wrong? The Email utility still sends out the email to my address, even though i mocked it...</p> <p>Thanks in advance!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload