fix: consolidate purchase confirmation emails
This commit is contained in:
@@ -142,7 +142,7 @@ class NotificationMailServiceTest extends TestCase
|
||||
});
|
||||
}
|
||||
|
||||
public function test_it_sends_purchase_and_ticket_emails_to_the_purchase_recipient(): void
|
||||
public function test_it_sends_one_purchase_confirmation_with_generated_tickets_attached(): void
|
||||
{
|
||||
$this->useWebsiteTypeBranding();
|
||||
$purchase = Purchase::query()->create([
|
||||
@@ -175,30 +175,23 @@ class NotificationMailServiceTest extends TestCase
|
||||
$ticket = Ticket::query()->create([
|
||||
'tenant_code' => $this->tenant->codigo,
|
||||
'ticket' => fake()->uuid(),
|
||||
'source_purchase_id' => $purchase->id,
|
||||
'source_catalog_item_id' => $catalogItem->id,
|
||||
'user_id' => $this->user->id,
|
||||
]);
|
||||
$service = app(NotificationMailService::class);
|
||||
|
||||
$service->sendPurchasePaid($purchase->id);
|
||||
$service->sendTicketsAvailable($purchase->id, [$ticket->id]);
|
||||
$service->sendPurchaseConfirmed($purchase->id);
|
||||
|
||||
Mail::assertSent(Mailable::class, 2);
|
||||
Mail::assertSent(Mailable::class, function (Mailable $mail) use ($purchase): bool {
|
||||
$mail->assertTo('checkout@example.com');
|
||||
|
||||
return $mail->subject === "Pago confirmado - Compra #{$purchase->id}"
|
||||
&& str_contains($mail->render(), 'Total pagado')
|
||||
&& str_contains($mail->render(), 'border-top: 4px solid #112233')
|
||||
&& ! str_contains($mail->render(), 'border-top: 4px solid #ff7006');
|
||||
});
|
||||
Mail::assertSent(Mailable::class, function (Mailable $mail) use ($ticket): bool {
|
||||
Mail::assertSent(Mailable::class, 1);
|
||||
Mail::assertSent(Mailable::class, function (Mailable $mail) use ($purchase, $ticket): bool {
|
||||
$mail->assertTo('checkout@example.com');
|
||||
$attachment = collect($mail->rawAttachments)->firstWhere('name', "tickets_{$ticket->id}.pdf");
|
||||
|
||||
return $mail->subject === 'Tus tickets ya están disponibles'
|
||||
&& str_contains($mail->render(), 'Entrada general')
|
||||
&& str_contains($mail->render(), 'El ticket está adjunto')
|
||||
return $mail->subject === "Compra confirmada - Compra #{$purchase->id}"
|
||||
&& str_contains($mail->render(), '¡Compra realizada con éxito!')
|
||||
&& str_contains($mail->render(), 'Total pagado')
|
||||
&& str_contains($mail->render(), 'Tus tickets ya están disponibles')
|
||||
&& $attachment !== null
|
||||
&& $attachment['options'] === ['mime' => 'application/pdf']
|
||||
&& str_starts_with($attachment['data'], '%PDF-')
|
||||
@@ -207,6 +200,26 @@ class NotificationMailServiceTest extends TestCase
|
||||
});
|
||||
}
|
||||
|
||||
public function test_purchase_confirmation_omits_ticket_content_and_attachment_without_tickets(): void
|
||||
{
|
||||
$purchase = Purchase::query()->create([
|
||||
'tenant_codigo' => $this->tenant->codigo,
|
||||
'user_id' => $this->user->id,
|
||||
'status' => Purchase::STATUS_PAID,
|
||||
'payment_method' => 'transfer',
|
||||
'total' => 25,
|
||||
]);
|
||||
|
||||
app(NotificationMailService::class)->sendPurchaseConfirmed($purchase->id);
|
||||
|
||||
Mail::assertSent(Mailable::class, function (Mailable $mail) use ($purchase): bool {
|
||||
return $mail->subject === "Compra confirmada - Compra #{$purchase->id}"
|
||||
&& str_contains($mail->render(), '¡Compra realizada con éxito!')
|
||||
&& ! str_contains($mail->render(), 'Tus tickets ya están disponibles')
|
||||
&& $mail->rawAttachments === [];
|
||||
});
|
||||
}
|
||||
|
||||
private function useWebsiteTypeBranding(): void
|
||||
{
|
||||
$websiteType = WebsiteType::query()->create([
|
||||
|
||||
@@ -2,9 +2,7 @@
|
||||
|
||||
namespace Tests\Feature\Notification;
|
||||
|
||||
use App\Domains\Notification\Events\TicketsAvailable;
|
||||
use App\Domains\Notification\Listeners\SendPurchasePaidEmail;
|
||||
use App\Domains\Notification\Listeners\SendTicketsAvailableEmail;
|
||||
use App\Domains\Notification\Listeners\SendPurchaseConfirmedEmail;
|
||||
use App\Domains\Notification\Services\NotificationMailService;
|
||||
use App\Domains\Purchase\Events\PurchasePaid;
|
||||
use Mockery;
|
||||
@@ -12,35 +10,21 @@ use Tests\TestCase;
|
||||
|
||||
class QueuedNotificationListenerTest extends TestCase
|
||||
{
|
||||
public function test_purchase_paid_email_delegates_with_the_purchase_id(): void
|
||||
public function test_purchase_confirmed_email_delegates_with_the_purchase_id(): void
|
||||
{
|
||||
$mailService = Mockery::mock(NotificationMailService::class);
|
||||
$mailService->shouldReceive('sendPurchasePaid')
|
||||
$mailService->shouldReceive('sendPurchaseConfirmed')
|
||||
->once()
|
||||
->with(123);
|
||||
$this->app->instance(NotificationMailService::class, $mailService);
|
||||
|
||||
(new SendPurchasePaidEmail)->handle(new PurchasePaid(123));
|
||||
(new SendPurchaseConfirmedEmail)->handle(new PurchasePaid(123));
|
||||
}
|
||||
|
||||
public function test_tickets_available_email_delegates_with_scalar_identifiers(): void
|
||||
{
|
||||
$mailService = Mockery::mock(NotificationMailService::class);
|
||||
$mailService->shouldReceive('sendTicketsAvailable')
|
||||
->once()
|
||||
->with(123, [10, 11]);
|
||||
$this->app->instance(NotificationMailService::class, $mailService);
|
||||
|
||||
(new SendTicketsAvailableEmail)->handle(new TicketsAvailable(123, [10, 11]));
|
||||
}
|
||||
|
||||
public function test_email_events_only_serialize_scalar_identifiers(): void
|
||||
public function test_purchase_paid_event_only_serializes_the_purchase_id(): void
|
||||
{
|
||||
$purchasePaid = unserialize(serialize(new PurchasePaid(123)));
|
||||
$ticketsAvailable = unserialize(serialize(new TicketsAvailable(123, [10, 11])));
|
||||
|
||||
$this->assertSame(123, $purchasePaid->purchaseId);
|
||||
$this->assertSame(123, $ticketsAvailable->purchaseId);
|
||||
$this->assertSame([10, 11], $ticketsAvailable->ticketIds);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user