Skip to content

Commit

Permalink
Interact with Mailable
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalbaljet committed Mar 24, 2022
1 parent 763b646 commit 6ef74dc
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to `laravel-verify-new-email` will be documented in this file

## 1.6.0 - 2022-02-24

- Interact with the Mailable before sending

## 1.5.0 - 2022-02-04

- Support for Laravel 9
Expand Down
14 changes: 10 additions & 4 deletions src/MustVerifyNewEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@ trait MustVerifyNewEmail
* to the new email address.
*
* @param string $email
* @param callable $withMailable
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function newEmail(string $email): ?Model
public function newEmail(string $email, callable $withMailable = null): ?Model
{
if ($this->getEmailForVerification() === $email && $this->hasVerifiedEmail()) {
return null;
}

return $this->createPendingUserEmailModel($email)->tap(function ($model) {
$this->sendPendingEmailVerificationMail($model);
return $this->createPendingUserEmailModel($email)->tap(function ($model) use ($withMailable) {
$this->sendPendingEmailVerificationMail($model, $withMailable);
});
}

Expand Down Expand Up @@ -80,9 +81,10 @@ public function clearPendingEmail()
* Sends the VerifyNewEmail Mailable to the new email address.
*
* @param \Illuminate\Database\Eloquent\Model $pendingUserEmail
* @param callable $withMailable
* @return mixed
*/
public function sendPendingEmailVerificationMail(Model $pendingUserEmail)
public function sendPendingEmailVerificationMail(Model $pendingUserEmail, callable $withMailable = null)
{
$mailableClass = config('verify-new-email.mailable_for_first_verification');

Expand All @@ -92,6 +94,10 @@ public function sendPendingEmailVerificationMail(Model $pendingUserEmail)

$mailable = new $mailableClass($pendingUserEmail);

if ($withMailable) {
$withMailable($mailable, $pendingUserEmail);
}

return Mail::to($pendingUserEmail->email)->send($mailable);
}

Expand Down
30 changes: 30 additions & 0 deletions tests/MustVerifyNewEmailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,36 @@ public function it_can_regenerate_a_token_and_mail_it()
});
}

/** @test */
public function it_can_interact_with_the_mailable()
{
Mail::fake();

$user = $this->user();

$user->email_verified_at = now();
$user->save();

$pendingUserEmail = $user->newEmail('new@example.com', function (VerifyNewEmail $mailable, PendingUserEmail $model) {
$mailable->bcc('test@test.com');
$this->assertTrue($model->exists);
});

Mail::assertQueued(VerifyNewEmail::class, function (Mailable $mailable) use ($pendingUserEmail) {
$this->assertTrue($mailable->pendingUserEmail->is($pendingUserEmail));

$this->assertTrue($mailable->hasTo('new@example.com'));

$this->assertFalse($mailable->hasTo('old@example.com'));
$this->assertFalse($mailable->hasCc('old@example.com'));
$this->assertFalse($mailable->hasBcc('old@example.com'));

$this->assertTrue($mailable->hasBcc('test@test.com'));

return true;
});
}

/** @test */
public function it_deletes_previous_attempts_of_the_user_trying_to_verify_a_new_email()
{
Expand Down

0 comments on commit 6ef74dc

Please sign in to comment.