Skip to content

Commit

Permalink
Merge pull request #479 from range-of-motion/create-tests-for-mails
Browse files Browse the repository at this point in the history
Create tests for mails
  • Loading branch information
range-of-motion authored Dec 29, 2023
2 parents 189428e + bf7b37b commit f7f7c4e
Show file tree
Hide file tree
Showing 11 changed files with 174 additions and 32 deletions.
9 changes: 4 additions & 5 deletions app/Mail/InvitedToSpace.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ class InvitedToSpace extends Mailable
use Queueable;
use SerializesModels;

protected $invite;

public function __construct(SpaceInvite $invite)
{
$this->invite = $invite;
public function __construct(
protected SpaceInvite $invite,
) {
//
}

public function build()
Expand Down
9 changes: 4 additions & 5 deletions app/Mail/PasswordChanged.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ class PasswordChanged extends Mailable
use Queueable;
use SerializesModels;

protected $updated_at;

public function __construct($updated_at)
{
$this->updated_at = $updated_at;
public function __construct(
protected $updated_at,
) {
//
}

public function build()
Expand Down
9 changes: 4 additions & 5 deletions app/Mail/ResetPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ class ResetPassword extends Mailable
use Queueable;
use SerializesModels;

protected $token;

public function __construct($token)
{
$this->token = $token;
public function __construct(
protected $token,
) {
//
}

public function build()
Expand Down
9 changes: 4 additions & 5 deletions app/Mail/VerifyRegistration.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ class VerifyRegistration extends Mailable
use Queueable;
use SerializesModels;

protected $user;

public function __construct(User $user)
{
$this->user = $user;
public function __construct(
protected User $user,
) {
//
}

public function build()
Expand Down
18 changes: 7 additions & 11 deletions app/Mail/WeeklyReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,13 @@ class WeeklyReport extends Mailable
use Queueable;
use SerializesModels;

protected $space;
protected $week;
protected $totalSpent;
protected $largestSpendingWithTag;

public function __construct(Space $space, $week, $totalSpent, $largestSpendingWithTag)
{
$this->space = $space;
$this->week = $week;
$this->totalSpent = $totalSpent;
$this->largestSpendingWithTag = $largestSpendingWithTag;
public function __construct(
protected Space $space,
protected $week,
protected $totalSpent,
protected $largestSpendingWithTag
) {
//
}

public function build()
Expand Down
2 changes: 1 addition & 1 deletion resources/views/emails/invited_to_space_plain.blade.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{ $invite->inviter->name }} has invited you to "{{ $invite->space->name }}"
{{ $invite->inviter->name }} has invited you to "{{ $invite->space->name }}".

Use the link below to check out your invite.

Expand Down
37 changes: 37 additions & 0 deletions tests/Unit/Mail/InvitedToSpaceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Tests\Unit\Mail;

use App\Mail\InvitedToSpace;
use App\Models\Space;
use App\Models\SpaceInvite;
use App\Models\User;
use Tests\TestCase;

class InvitedToSpaceTest extends TestCase
{
public function testMailable(): void
{
$space = Space::factory()
->create([
'name' => 'Foo Bar',
]);

$inviterUser = User::factory()
->create([
'name' => 'John Doe',
]);

$spaceInvite = SpaceInvite::factory()
->create([
'space_id' => $space->id,
'inviter_user_id' => $inviterUser->id,
]);

$mailable = new InvitedToSpace($spaceInvite);

$mailable
->assertSeeInText('John Doe has invited you to "Foo Bar".')
->assertSeeInHtml('<a href="' . config('app.url') . '/spaces/' . $space->id . '/invites/' . $spaceInvite->id . '">Check out your invite</a>', false); // phpcs:ignore
}
}
16 changes: 16 additions & 0 deletions tests/Unit/Mail/PasswordChangedTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Tests\Unit\Mail;

use App\Mail\PasswordChanged;
use Tests\TestCase;

class PasswordChangedTest extends TestCase
{
public function testMailable(): void
{
$mailable = new PasswordChanged('2023-12-05 18:00:00');

$mailable->assertSeeInText('Heads up! Your password has been changed (2023-12-05 18:00:00 CEST).');
}
}
16 changes: 16 additions & 0 deletions tests/Unit/Mail/ResetPasswordTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Tests\Unit\Mail;

use App\Mail\ResetPassword;
use Tests\TestCase;

class ResetPasswordTest extends TestCase
{
public function testMailable(): void
{
$mailable = new ResetPassword('123456789');

$mailable->assertSeeInHtml('<a href="' . config('app.url') . '/reset_password?token=123456789">Click here to change your password</a>', false); // phpcs:ignore
}
}
27 changes: 27 additions & 0 deletions tests/Unit/Mail/VerifyRegistrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Tests\Unit\Mail;

use App\Mail\VerifyRegistration;
use App\Models\User;
use Tests\TestCase;

class VerifyRegistrationTest extends TestCase
{
public function testMailable(): void
{
$user = User::factory()
->create([
'name' => 'John Doe',
'verification_token' => 'abc123',
]);

$mailable = new VerifyRegistration($user);

$mailable
->assertSeeInText('Welcome aboard, John Doe')
->assertSeeInText('We\'re going to help you get insight into your personal finances.')
->assertSeeInText('No more dealing with pesky, half-assed spreadsheets.')
->assertSeeInHtml('<a href="' . config('app.url') . '/verify/abc123">Verify</a>', false);
}
}
54 changes: 54 additions & 0 deletions tests/Unit/Mail/WeeklyReportTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Tests\Unit\Mail;

use App\Mail\WeeklyReport;
use App\Models\Space;
use Tests\TestCase;

class WeeklyReportTest extends TestCase
{
public function testMailableUsingSpaceWithoutTransactions(): void
{
$space = Space::factory()
->create([
'currency_id' => 2, // Should be USD
'name' => 'Foo Bar',
]);

$mailable = new WeeklyReport(
space: $space,
week: 5,
totalSpent: 0,
largestSpendingWithTag: [],
);

$mailable
->assertSeeInText('Here\'s your weekly report for Foo Bar')
->assertSeeInText('This week (#5) you\'ve')
->assertSeeInText('- Spent &dollar; 0.00')
->assertDontSeeInText('Most of which you\'ve spent on');
}

public function testMailableUsingSpaceWithTransactions(): void
{
$space = Space::factory()
->create([
'currency_id' => 2, // Should be USD
'name' => 'Foo Bar',
]);

$mailable = new WeeklyReport(
space: $space,
week: 5,
totalSpent: 10000,
largestSpendingWithTag: [(object) ['amount' => 7000, 'tag_name' => 'Insurance']],
);

$mailable
->assertSeeInText('Here\'s your weekly report for Foo Bar')
->assertSeeInText('This week (#5) you\'ve')
->assertSeeInText('- Spent &dollar; 100.00')
->assertSeeInText('- Most of which you\'ve spent on Insurance (&dollar; 70.00)');
}
}

0 comments on commit f7f7c4e

Please sign in to comment.