Unit testing Laravel password reset email - mailable not queued
I am using the auth that came with Laravel. I am testing the page where you put in your email and when you hit the submit button a password reset email will be sent to your email.
The password reset email is sent when I do it manually. But I created this test to make sure the password reset email is sent but it's not working.
There was 1 failure:
1)
The expected [IlluminateFoundationAuthResetPassword] mailable was not queued.
Failed asserting that false is true.
I am following this code:
https://github.com/JeffreyWay/council/blob/master/tests/Feature/Auth/RegisterUserTest.php
<?php
namespace TestsControllersUnit;
use TestsTestCase;
use IlluminateSupportFacadesMail;
use IlluminateAuthNotificationsResetPassword;
use IlluminateFoundationTestingRefreshDatabase;
class ResetPasswordEmailTest extends TestCase
{
use RefreshDatabase;
public function setUp()
{
parent::setUp();
Mail::fake();
}
/** @test */
public function does_send_password_reset_email()
{
$user = factory('AppUser')->create();
$this->post(route('password.email'), ['email' => $user->email])
Mail::assertQueued(ResetPassword::class);
}
}
php laravel unit-testing laravel-5 phpunit
add a comment |
I am using the auth that came with Laravel. I am testing the page where you put in your email and when you hit the submit button a password reset email will be sent to your email.
The password reset email is sent when I do it manually. But I created this test to make sure the password reset email is sent but it's not working.
There was 1 failure:
1)
The expected [IlluminateFoundationAuthResetPassword] mailable was not queued.
Failed asserting that false is true.
I am following this code:
https://github.com/JeffreyWay/council/blob/master/tests/Feature/Auth/RegisterUserTest.php
<?php
namespace TestsControllersUnit;
use TestsTestCase;
use IlluminateSupportFacadesMail;
use IlluminateAuthNotificationsResetPassword;
use IlluminateFoundationTestingRefreshDatabase;
class ResetPasswordEmailTest extends TestCase
{
use RefreshDatabase;
public function setUp()
{
parent::setUp();
Mail::fake();
}
/** @test */
public function does_send_password_reset_email()
{
$user = factory('AppUser')->create();
$this->post(route('password.email'), ['email' => $user->email])
Mail::assertQueued(ResetPassword::class);
}
}
php laravel unit-testing laravel-5 phpunit
add a comment |
I am using the auth that came with Laravel. I am testing the page where you put in your email and when you hit the submit button a password reset email will be sent to your email.
The password reset email is sent when I do it manually. But I created this test to make sure the password reset email is sent but it's not working.
There was 1 failure:
1)
The expected [IlluminateFoundationAuthResetPassword] mailable was not queued.
Failed asserting that false is true.
I am following this code:
https://github.com/JeffreyWay/council/blob/master/tests/Feature/Auth/RegisterUserTest.php
<?php
namespace TestsControllersUnit;
use TestsTestCase;
use IlluminateSupportFacadesMail;
use IlluminateAuthNotificationsResetPassword;
use IlluminateFoundationTestingRefreshDatabase;
class ResetPasswordEmailTest extends TestCase
{
use RefreshDatabase;
public function setUp()
{
parent::setUp();
Mail::fake();
}
/** @test */
public function does_send_password_reset_email()
{
$user = factory('AppUser')->create();
$this->post(route('password.email'), ['email' => $user->email])
Mail::assertQueued(ResetPassword::class);
}
}
php laravel unit-testing laravel-5 phpunit
I am using the auth that came with Laravel. I am testing the page where you put in your email and when you hit the submit button a password reset email will be sent to your email.
The password reset email is sent when I do it manually. But I created this test to make sure the password reset email is sent but it's not working.
There was 1 failure:
1)
The expected [IlluminateFoundationAuthResetPassword] mailable was not queued.
Failed asserting that false is true.
I am following this code:
https://github.com/JeffreyWay/council/blob/master/tests/Feature/Auth/RegisterUserTest.php
<?php
namespace TestsControllersUnit;
use TestsTestCase;
use IlluminateSupportFacadesMail;
use IlluminateAuthNotificationsResetPassword;
use IlluminateFoundationTestingRefreshDatabase;
class ResetPasswordEmailTest extends TestCase
{
use RefreshDatabase;
public function setUp()
{
parent::setUp();
Mail::fake();
}
/** @test */
public function does_send_password_reset_email()
{
$user = factory('AppUser')->create();
$this->post(route('password.email'), ['email' => $user->email])
Mail::assertQueued(ResetPassword::class);
}
}
php laravel unit-testing laravel-5 phpunit
php laravel unit-testing laravel-5 phpunit
edited Nov 16 '18 at 18:55
miken32
23.6k84872
23.6k84872
asked May 17 '18 at 3:44
johhnysiverjohhnysiver
112
112
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You received that error because the password reset email is a Notification and not a Mailable. Note that you must also save the fake user, so that the password reset code can look for it in the database. What worked for me is something like this:
<?php
namespace TestsControllersUnit;
use TestsTestCase;
use IlluminateSupportFacadesNotification;
use IlluminateAuthNotificationsResetPassword;
use IlluminateFoundationTestingRefreshDatabase;
class ResetPasswordEmailTest extends TestCase
{
use RefreshDatabase;
public function setUp()
{
parent::setUp();
Notification::fake();
}
/** @test */
public function does_send_password_reset_email()
{
$user = factory('AppUser')->create();
$user->save();
$this->post(route('password.email'), ['email' => $user->email])
Notification::assertSentTo($user, ResetPassword::class);
}
}
You could also check contents of the email using something like this:
Notification::assertSentTo(
$user,
ResetPassword::class,
function($notification, $channels) use ($user) {
$mail = $notification->toMail($user)->build();
$expected_subject = "Here's your password reset";
return $mail->subject === $expected_sub;
}
);
add a comment |
You can do this through your controller. Here is an example:
public function postforgotpassword(Request $request){
$this->validate($request, ['phone' => 'required']);
$user=User::where('phone',$request->get('phone'))->first();
if($user['email']==""){
return Redirect()->back()->with('status', 'You dont have email assigned. Contact admin for reset');
}
$response = Password::sendResetLink($request->only('phone'), function (Message $message) {
$message->subject($this->getEmailSubject());
});
switch ($response) {
case Password::RESET_LINK_SENT:
return redirect()->back()->with('status', trans($response));
case Password::INVALID_USER:
return redirect()->back()->withErrors(['phone' => trans($response)]);
}
}
protected function getEmailSubject(){
return isset($this->subject) ? $this->subject : 'Your Password Reset Link';
}
It will send a link with a token in it to your email. Then when you want to work with the link, you can try this:
public function getreset($token = null){
if (is_null($token)) {
throw new NotFoundHttpException;
}
return view('auth.reset')->with('token', $token);
}
public function postreset(Request $request){
$this->validate($request, [
'token' => 'required',
'phone' => 'required',
'password' => 'required|confirmed',
]);
$credentials = $request->only(
'phone', 'password', 'password_confirmation', 'token'
);
$response = Password::reset($credentials, function ($user, $password) {
$this->resetPassword($user, $password);
});
switch ($response) {
case Password::PASSWORD_RESET:
return redirect($this->redirectPath());
default:
return redirect()->back()
->withInput($request->only('phone'))
->withErrors(['phone' => trans($response)]);
}
}
protected function resetPassword($user, $password){
$user->password = bcrypt($password);
$user->save();
Auth::login($user);
}
And the routes should be:
Route::post('forgotpassword', array('as' => 'forgotpassword.post', 'uses' => 'YourController@postforgotpassword'));
Route::get('password/reset/{token}', array('as' => 'password.reset', 'uses' => 'YourController@getreset'));
Route::post('password/reset', array('as' => 'password.reset.post', 'uses' => 'YourController@postreset'));
Hope this helps, have a nice day.
This answer completely misses the point of the question. It's about unit testing the Mailable.
– miken32
Nov 15 '18 at 22:36
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50383016%2funit-testing-laravel-password-reset-email-mailable-not-queued%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You received that error because the password reset email is a Notification and not a Mailable. Note that you must also save the fake user, so that the password reset code can look for it in the database. What worked for me is something like this:
<?php
namespace TestsControllersUnit;
use TestsTestCase;
use IlluminateSupportFacadesNotification;
use IlluminateAuthNotificationsResetPassword;
use IlluminateFoundationTestingRefreshDatabase;
class ResetPasswordEmailTest extends TestCase
{
use RefreshDatabase;
public function setUp()
{
parent::setUp();
Notification::fake();
}
/** @test */
public function does_send_password_reset_email()
{
$user = factory('AppUser')->create();
$user->save();
$this->post(route('password.email'), ['email' => $user->email])
Notification::assertSentTo($user, ResetPassword::class);
}
}
You could also check contents of the email using something like this:
Notification::assertSentTo(
$user,
ResetPassword::class,
function($notification, $channels) use ($user) {
$mail = $notification->toMail($user)->build();
$expected_subject = "Here's your password reset";
return $mail->subject === $expected_sub;
}
);
add a comment |
You received that error because the password reset email is a Notification and not a Mailable. Note that you must also save the fake user, so that the password reset code can look for it in the database. What worked for me is something like this:
<?php
namespace TestsControllersUnit;
use TestsTestCase;
use IlluminateSupportFacadesNotification;
use IlluminateAuthNotificationsResetPassword;
use IlluminateFoundationTestingRefreshDatabase;
class ResetPasswordEmailTest extends TestCase
{
use RefreshDatabase;
public function setUp()
{
parent::setUp();
Notification::fake();
}
/** @test */
public function does_send_password_reset_email()
{
$user = factory('AppUser')->create();
$user->save();
$this->post(route('password.email'), ['email' => $user->email])
Notification::assertSentTo($user, ResetPassword::class);
}
}
You could also check contents of the email using something like this:
Notification::assertSentTo(
$user,
ResetPassword::class,
function($notification, $channels) use ($user) {
$mail = $notification->toMail($user)->build();
$expected_subject = "Here's your password reset";
return $mail->subject === $expected_sub;
}
);
add a comment |
You received that error because the password reset email is a Notification and not a Mailable. Note that you must also save the fake user, so that the password reset code can look for it in the database. What worked for me is something like this:
<?php
namespace TestsControllersUnit;
use TestsTestCase;
use IlluminateSupportFacadesNotification;
use IlluminateAuthNotificationsResetPassword;
use IlluminateFoundationTestingRefreshDatabase;
class ResetPasswordEmailTest extends TestCase
{
use RefreshDatabase;
public function setUp()
{
parent::setUp();
Notification::fake();
}
/** @test */
public function does_send_password_reset_email()
{
$user = factory('AppUser')->create();
$user->save();
$this->post(route('password.email'), ['email' => $user->email])
Notification::assertSentTo($user, ResetPassword::class);
}
}
You could also check contents of the email using something like this:
Notification::assertSentTo(
$user,
ResetPassword::class,
function($notification, $channels) use ($user) {
$mail = $notification->toMail($user)->build();
$expected_subject = "Here's your password reset";
return $mail->subject === $expected_sub;
}
);
You received that error because the password reset email is a Notification and not a Mailable. Note that you must also save the fake user, so that the password reset code can look for it in the database. What worked for me is something like this:
<?php
namespace TestsControllersUnit;
use TestsTestCase;
use IlluminateSupportFacadesNotification;
use IlluminateAuthNotificationsResetPassword;
use IlluminateFoundationTestingRefreshDatabase;
class ResetPasswordEmailTest extends TestCase
{
use RefreshDatabase;
public function setUp()
{
parent::setUp();
Notification::fake();
}
/** @test */
public function does_send_password_reset_email()
{
$user = factory('AppUser')->create();
$user->save();
$this->post(route('password.email'), ['email' => $user->email])
Notification::assertSentTo($user, ResetPassword::class);
}
}
You could also check contents of the email using something like this:
Notification::assertSentTo(
$user,
ResetPassword::class,
function($notification, $channels) use ($user) {
$mail = $notification->toMail($user)->build();
$expected_subject = "Here's your password reset";
return $mail->subject === $expected_sub;
}
);
answered Nov 16 '18 at 18:52
miken32miken32
23.6k84872
23.6k84872
add a comment |
add a comment |
You can do this through your controller. Here is an example:
public function postforgotpassword(Request $request){
$this->validate($request, ['phone' => 'required']);
$user=User::where('phone',$request->get('phone'))->first();
if($user['email']==""){
return Redirect()->back()->with('status', 'You dont have email assigned. Contact admin for reset');
}
$response = Password::sendResetLink($request->only('phone'), function (Message $message) {
$message->subject($this->getEmailSubject());
});
switch ($response) {
case Password::RESET_LINK_SENT:
return redirect()->back()->with('status', trans($response));
case Password::INVALID_USER:
return redirect()->back()->withErrors(['phone' => trans($response)]);
}
}
protected function getEmailSubject(){
return isset($this->subject) ? $this->subject : 'Your Password Reset Link';
}
It will send a link with a token in it to your email. Then when you want to work with the link, you can try this:
public function getreset($token = null){
if (is_null($token)) {
throw new NotFoundHttpException;
}
return view('auth.reset')->with('token', $token);
}
public function postreset(Request $request){
$this->validate($request, [
'token' => 'required',
'phone' => 'required',
'password' => 'required|confirmed',
]);
$credentials = $request->only(
'phone', 'password', 'password_confirmation', 'token'
);
$response = Password::reset($credentials, function ($user, $password) {
$this->resetPassword($user, $password);
});
switch ($response) {
case Password::PASSWORD_RESET:
return redirect($this->redirectPath());
default:
return redirect()->back()
->withInput($request->only('phone'))
->withErrors(['phone' => trans($response)]);
}
}
protected function resetPassword($user, $password){
$user->password = bcrypt($password);
$user->save();
Auth::login($user);
}
And the routes should be:
Route::post('forgotpassword', array('as' => 'forgotpassword.post', 'uses' => 'YourController@postforgotpassword'));
Route::get('password/reset/{token}', array('as' => 'password.reset', 'uses' => 'YourController@getreset'));
Route::post('password/reset', array('as' => 'password.reset.post', 'uses' => 'YourController@postreset'));
Hope this helps, have a nice day.
This answer completely misses the point of the question. It's about unit testing the Mailable.
– miken32
Nov 15 '18 at 22:36
add a comment |
You can do this through your controller. Here is an example:
public function postforgotpassword(Request $request){
$this->validate($request, ['phone' => 'required']);
$user=User::where('phone',$request->get('phone'))->first();
if($user['email']==""){
return Redirect()->back()->with('status', 'You dont have email assigned. Contact admin for reset');
}
$response = Password::sendResetLink($request->only('phone'), function (Message $message) {
$message->subject($this->getEmailSubject());
});
switch ($response) {
case Password::RESET_LINK_SENT:
return redirect()->back()->with('status', trans($response));
case Password::INVALID_USER:
return redirect()->back()->withErrors(['phone' => trans($response)]);
}
}
protected function getEmailSubject(){
return isset($this->subject) ? $this->subject : 'Your Password Reset Link';
}
It will send a link with a token in it to your email. Then when you want to work with the link, you can try this:
public function getreset($token = null){
if (is_null($token)) {
throw new NotFoundHttpException;
}
return view('auth.reset')->with('token', $token);
}
public function postreset(Request $request){
$this->validate($request, [
'token' => 'required',
'phone' => 'required',
'password' => 'required|confirmed',
]);
$credentials = $request->only(
'phone', 'password', 'password_confirmation', 'token'
);
$response = Password::reset($credentials, function ($user, $password) {
$this->resetPassword($user, $password);
});
switch ($response) {
case Password::PASSWORD_RESET:
return redirect($this->redirectPath());
default:
return redirect()->back()
->withInput($request->only('phone'))
->withErrors(['phone' => trans($response)]);
}
}
protected function resetPassword($user, $password){
$user->password = bcrypt($password);
$user->save();
Auth::login($user);
}
And the routes should be:
Route::post('forgotpassword', array('as' => 'forgotpassword.post', 'uses' => 'YourController@postforgotpassword'));
Route::get('password/reset/{token}', array('as' => 'password.reset', 'uses' => 'YourController@getreset'));
Route::post('password/reset', array('as' => 'password.reset.post', 'uses' => 'YourController@postreset'));
Hope this helps, have a nice day.
This answer completely misses the point of the question. It's about unit testing the Mailable.
– miken32
Nov 15 '18 at 22:36
add a comment |
You can do this through your controller. Here is an example:
public function postforgotpassword(Request $request){
$this->validate($request, ['phone' => 'required']);
$user=User::where('phone',$request->get('phone'))->first();
if($user['email']==""){
return Redirect()->back()->with('status', 'You dont have email assigned. Contact admin for reset');
}
$response = Password::sendResetLink($request->only('phone'), function (Message $message) {
$message->subject($this->getEmailSubject());
});
switch ($response) {
case Password::RESET_LINK_SENT:
return redirect()->back()->with('status', trans($response));
case Password::INVALID_USER:
return redirect()->back()->withErrors(['phone' => trans($response)]);
}
}
protected function getEmailSubject(){
return isset($this->subject) ? $this->subject : 'Your Password Reset Link';
}
It will send a link with a token in it to your email. Then when you want to work with the link, you can try this:
public function getreset($token = null){
if (is_null($token)) {
throw new NotFoundHttpException;
}
return view('auth.reset')->with('token', $token);
}
public function postreset(Request $request){
$this->validate($request, [
'token' => 'required',
'phone' => 'required',
'password' => 'required|confirmed',
]);
$credentials = $request->only(
'phone', 'password', 'password_confirmation', 'token'
);
$response = Password::reset($credentials, function ($user, $password) {
$this->resetPassword($user, $password);
});
switch ($response) {
case Password::PASSWORD_RESET:
return redirect($this->redirectPath());
default:
return redirect()->back()
->withInput($request->only('phone'))
->withErrors(['phone' => trans($response)]);
}
}
protected function resetPassword($user, $password){
$user->password = bcrypt($password);
$user->save();
Auth::login($user);
}
And the routes should be:
Route::post('forgotpassword', array('as' => 'forgotpassword.post', 'uses' => 'YourController@postforgotpassword'));
Route::get('password/reset/{token}', array('as' => 'password.reset', 'uses' => 'YourController@getreset'));
Route::post('password/reset', array('as' => 'password.reset.post', 'uses' => 'YourController@postreset'));
Hope this helps, have a nice day.
You can do this through your controller. Here is an example:
public function postforgotpassword(Request $request){
$this->validate($request, ['phone' => 'required']);
$user=User::where('phone',$request->get('phone'))->first();
if($user['email']==""){
return Redirect()->back()->with('status', 'You dont have email assigned. Contact admin for reset');
}
$response = Password::sendResetLink($request->only('phone'), function (Message $message) {
$message->subject($this->getEmailSubject());
});
switch ($response) {
case Password::RESET_LINK_SENT:
return redirect()->back()->with('status', trans($response));
case Password::INVALID_USER:
return redirect()->back()->withErrors(['phone' => trans($response)]);
}
}
protected function getEmailSubject(){
return isset($this->subject) ? $this->subject : 'Your Password Reset Link';
}
It will send a link with a token in it to your email. Then when you want to work with the link, you can try this:
public function getreset($token = null){
if (is_null($token)) {
throw new NotFoundHttpException;
}
return view('auth.reset')->with('token', $token);
}
public function postreset(Request $request){
$this->validate($request, [
'token' => 'required',
'phone' => 'required',
'password' => 'required|confirmed',
]);
$credentials = $request->only(
'phone', 'password', 'password_confirmation', 'token'
);
$response = Password::reset($credentials, function ($user, $password) {
$this->resetPassword($user, $password);
});
switch ($response) {
case Password::PASSWORD_RESET:
return redirect($this->redirectPath());
default:
return redirect()->back()
->withInput($request->only('phone'))
->withErrors(['phone' => trans($response)]);
}
}
protected function resetPassword($user, $password){
$user->password = bcrypt($password);
$user->save();
Auth::login($user);
}
And the routes should be:
Route::post('forgotpassword', array('as' => 'forgotpassword.post', 'uses' => 'YourController@postforgotpassword'));
Route::get('password/reset/{token}', array('as' => 'password.reset', 'uses' => 'YourController@getreset'));
Route::post('password/reset', array('as' => 'password.reset.post', 'uses' => 'YourController@postreset'));
Hope this helps, have a nice day.
edited May 17 '18 at 13:17
answered May 17 '18 at 13:09
Foysal NibirFoysal Nibir
18811
18811
This answer completely misses the point of the question. It's about unit testing the Mailable.
– miken32
Nov 15 '18 at 22:36
add a comment |
This answer completely misses the point of the question. It's about unit testing the Mailable.
– miken32
Nov 15 '18 at 22:36
This answer completely misses the point of the question. It's about unit testing the Mailable.
– miken32
Nov 15 '18 at 22:36
This answer completely misses the point of the question. It's about unit testing the Mailable.
– miken32
Nov 15 '18 at 22:36
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50383016%2funit-testing-laravel-password-reset-email-mailable-not-queued%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown