Skip to content

[11.x] Make mailables tappable #53788

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 8, 2024

Conversation

kevinb1989
Copy link
Contributor

@kevinb1989 kevinb1989 commented Dec 7, 2024

I have been enjoying using the tap method from the Illuminate\Testing\TestResponse class, when I got a little logics around assertions.

The Illuminate\Mail\Mailable class contains assertion methods. It would be nice to also make it tappable.

I have the following real-life example from my organization:

use App\Mails\OrderCompleted;
use App\Models\Customer;
use App\Models\Order;

public function test_emails_containing_correct_addresses(country $countryCode)
{
    $order = Order::factory()
        ->for(Customer::factory()->create(['country' => $countryCode]))
        ->create();

    $mail = new OrderCompleted($order);

    $mail->assertHasSubject('Your Order Has Been Fulfilled!');

    match (true) {
        $countryCode === 'AU' => $mail->assertSeeInHtml('64 Barker Street REDMOND WA 6327'),
        $countryCode === 'US' => $mail->assertSeeInHtml('2142 Doctors Drive, Burbank, CA 91505'),
        $order->customer->is_from_europe => $mail->assertSeeInHtml('Gerrit van der Veenstraat 1 1077 DX  Amsterdam'),
    };
}

As a global business, we want to display correct branch address in our emails depending on the customer's location.

I would prefer to do all assertions in one go:

$mail->assertHasSubject('Your Order Has Been Fulfilled!')
    ->tap(function ($mailable) use ($countryCode, $order) {
        match (true) {
            $countryCode === 'AU' => $mailable->assertSeeInHtml('64 Barker Street REDMOND WA 6327'),
            $countryCode === 'US' => $mailable->assertSeeInHtml('2142 Doctors Drive, Burbank, CA 91505'),
            $order->customer->is_from_europe => $mailable->assertSeeInHtml('Gerrit van der Veenstraat 1 1077 DX Amsterdam'),
        };
    });

@taylorotwell taylorotwell merged commit 36b09b7 into laravel:11.x Dec 8, 2024
40 checks passed
browner12 pushed a commit to browner12/framework that referenced this pull request Dec 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants