feat(mail): introduce MailService for tenant-specific SMTP handling and update related tests
This commit is contained in:
@@ -9,6 +9,7 @@ use App\Domains\Integration\Models\TenantIntegration;
|
||||
use App\Domains\MailTest\Mailables\TestMail;
|
||||
use App\Domains\Tenant\Models\Tenant;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Mail\Mailable;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use Tests\TestCase;
|
||||
|
||||
@@ -31,14 +32,14 @@ class MailTestControllerTest extends TestCase
|
||||
->assertJsonPath('message', 'Correo de prueba enviado correctamente.')
|
||||
->assertJsonPath('recipient', 'recipient@example.com')
|
||||
->assertJsonPath('tenant_code', 'acme')
|
||||
->assertJsonPath('mailer', 'array')
|
||||
->assertJsonPath('mailer', 'tenant-smtp')
|
||||
->assertJsonStructure(['sent_at']);
|
||||
|
||||
Mail::assertSent(TestMail::class, function (TestMail $mail) use ($tenant): bool {
|
||||
Mail::assertSent(Mailable::class, function (Mailable $mail) use ($tenant): bool {
|
||||
return $mail->hasTo('recipient@example.com')
|
||||
&& $mail->mailSubject === 'SMTP test'
|
||||
&& $mail->mailMessage === 'Test message'
|
||||
&& $mail->tenant->is($tenant);
|
||||
&& $mail->subject === 'SMTP test'
|
||||
&& str_contains($mail->render(), 'Test message')
|
||||
&& str_contains($mail->render(), $tenant->nombre);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -51,42 +52,12 @@ class MailTestControllerTest extends TestCase
|
||||
'to' => 'recipient@example.com',
|
||||
])->assertOk();
|
||||
|
||||
Mail::assertSent(TestMail::class, function (TestMail $mail): bool {
|
||||
return $mail->mailSubject === 'Prueba de correo de Shopit'
|
||||
&& $mail->mailMessage === 'Este es un correo de prueba enviado desde Shopit.';
|
||||
Mail::assertSent(Mailable::class, function (Mailable $mail): bool {
|
||||
return $mail->subject === 'Prueba de correo de Shopit'
|
||||
&& str_contains($mail->render(), 'Este es un correo de prueba enviado desde Shopit.');
|
||||
});
|
||||
}
|
||||
|
||||
public function test_it_uses_the_tenant_email_integration_when_configured(): void
|
||||
{
|
||||
Mail::fake();
|
||||
$tenant = $this->createTenant();
|
||||
|
||||
Integration::create([
|
||||
'integration_code' => 'email',
|
||||
'name' => 'Email',
|
||||
]);
|
||||
TenantIntegration::create([
|
||||
'tenant_code' => $tenant->codigo,
|
||||
'integration_code' => 'email',
|
||||
'integration_data' => [
|
||||
'MAIL_SCHEME' => 'smtp',
|
||||
'MAIL_HOST' => 'smtp.example.com',
|
||||
'MAIL_PORT' => 587,
|
||||
'MAIL_USERNAME' => 'mailer@example.com',
|
||||
'MAIL_PASSWORD' => 'secret',
|
||||
'MAIL_FROM_ADDRESS' => 'store@example.com',
|
||||
],
|
||||
]);
|
||||
|
||||
$this->postJson('/api/acme/mail-test/send', [
|
||||
'to' => 'recipient@example.com',
|
||||
])->assertOk()
|
||||
->assertJsonPath('mailer', 'tenant-smtp');
|
||||
|
||||
Mail::assertSent(TestMail::class);
|
||||
}
|
||||
|
||||
public function test_it_validates_the_recipient(): void
|
||||
{
|
||||
Mail::fake();
|
||||
@@ -182,7 +153,7 @@ class MailTestControllerTest extends TestCase
|
||||
'extension' => 'png',
|
||||
]);
|
||||
|
||||
return Tenant::create([
|
||||
$tenant = Tenant::create([
|
||||
'codigo' => 'acme',
|
||||
'nombre' => 'Acme Store',
|
||||
'dominio' => 'acme.example.com',
|
||||
@@ -195,5 +166,24 @@ class MailTestControllerTest extends TestCase
|
||||
'header_logo_id' => $headerLogo->id,
|
||||
'footer_logo_id' => $footerLogo->id,
|
||||
]);
|
||||
|
||||
Integration::create([
|
||||
'integration_code' => 'email',
|
||||
'name' => 'Email',
|
||||
]);
|
||||
TenantIntegration::create([
|
||||
'tenant_code' => $tenant->codigo,
|
||||
'integration_code' => 'email',
|
||||
'integration_data' => [
|
||||
'MAIL_SCHEME' => 'smtp',
|
||||
'MAIL_HOST' => 'smtp.example.com',
|
||||
'MAIL_PORT' => 587,
|
||||
'MAIL_USERNAME' => 'mailer@example.com',
|
||||
'MAIL_PASSWORD' => 'secret',
|
||||
'MAIL_FROM_ADDRESS' => 'store@example.com',
|
||||
],
|
||||
]);
|
||||
|
||||
return $tenant;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user