feat(mail): introduce MailService for tenant-specific SMTP handling and update related tests
This commit is contained in:
147
tests/Feature/Integration/MailServiceTest.php
Normal file
147
tests/Feature/Integration/MailServiceTest.php
Normal file
@@ -0,0 +1,147 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Integration;
|
||||
|
||||
use App\Domains\Attachable\Enums\AttachmentType;
|
||||
use App\Domains\Attachable\Models\Attachment;
|
||||
use App\Domains\Integration\Models\Integration;
|
||||
use App\Domains\Integration\Models\TenantIntegration;
|
||||
use App\Domains\Integration\Services\MailService;
|
||||
use App\Domains\Integration\Services\TenantIntegrationService;
|
||||
use App\Domains\Tenant\Models\Tenant;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Mail\Mailable;
|
||||
use Illuminate\Mail\Mailer;
|
||||
use Illuminate\Mail\MailManager;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use Mockery;
|
||||
use Tests\TestCase;
|
||||
|
||||
class MailServiceTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_it_builds_an_isolated_smtp_mailer_from_the_tenant_integration(): void
|
||||
{
|
||||
$tenant = $this->createTenant();
|
||||
$this->createEmailIntegration();
|
||||
TenantIntegration::create([
|
||||
'tenant_code' => $tenant->codigo,
|
||||
'integration_code' => 'email',
|
||||
'integration_data' => $this->emailData(),
|
||||
]);
|
||||
|
||||
$mailer = Mockery::mock(Mailer::class);
|
||||
$mailer->shouldReceive('alwaysFrom')
|
||||
->once()
|
||||
->with('store@example.com', 'Acme Mail');
|
||||
|
||||
$manager = Mockery::mock(MailManager::class);
|
||||
$manager->shouldReceive('build')
|
||||
->once()
|
||||
->with(Mockery::on(fn (array $config): bool => $config === [
|
||||
'name' => 'tenant-smtp-acme',
|
||||
'transport' => 'smtp',
|
||||
'scheme' => 'smtp',
|
||||
'host' => 'smtp.example.com',
|
||||
'port' => 587,
|
||||
'username' => 'mailer@example.com',
|
||||
'password' => 'secret',
|
||||
'timeout' => null,
|
||||
'local_domain' => null,
|
||||
]))
|
||||
->andReturn($mailer);
|
||||
|
||||
$service = (new MailService($manager))->forTenant($tenant->codigo);
|
||||
|
||||
$this->assertSame('tenant-smtp', $service->mailerName());
|
||||
}
|
||||
|
||||
public function test_on_setup_sends_a_branded_test_email_to_the_configured_sender(): void
|
||||
{
|
||||
Mail::fake();
|
||||
$tenant = $this->createTenant();
|
||||
$this->createEmailIntegration();
|
||||
TenantIntegration::create([
|
||||
'tenant_code' => $tenant->codigo,
|
||||
'integration_code' => 'email',
|
||||
'integration_data' => $this->emailData(),
|
||||
]);
|
||||
|
||||
(new MailService)->forTenant($tenant->codigo)->onSetup();
|
||||
|
||||
Mail::assertSent(Mailable::class, function (Mailable $mail) use ($tenant): bool {
|
||||
$html = $mail->render();
|
||||
|
||||
return $mail->hasTo('store@example.com')
|
||||
&& $mail->subject === 'Configuración de correo validada'
|
||||
&& str_contains($html, $tenant->nombre)
|
||||
&& str_contains($html, 'background-color: #112233')
|
||||
&& str_contains($html, 'background-color: #445566');
|
||||
});
|
||||
}
|
||||
|
||||
public function test_configuring_the_email_integration_runs_its_setup_hook(): void
|
||||
{
|
||||
Mail::fake();
|
||||
$tenant = $this->createTenant();
|
||||
$integration = $this->createEmailIntegration();
|
||||
|
||||
app(TenantIntegrationService::class)->updateOrCreateIntegration(
|
||||
$tenant->codigo,
|
||||
$integration,
|
||||
$this->emailData(),
|
||||
);
|
||||
|
||||
Mail::assertSent(Mailable::class, 1);
|
||||
}
|
||||
|
||||
private function createTenant(): Tenant
|
||||
{
|
||||
$logo = Attachment::create([
|
||||
'path' => 'tenants/logo.png',
|
||||
'filename' => 'logo.png',
|
||||
'type' => AttachmentType::Image,
|
||||
'mime_type' => 'image/png',
|
||||
'extension' => 'png',
|
||||
]);
|
||||
|
||||
return Tenant::create([
|
||||
'codigo' => 'acme',
|
||||
'nombre' => 'Acme Store',
|
||||
'dominio' => 'acme.example.com',
|
||||
'primary_color' => '#778899',
|
||||
'secondary_color' => '#64748b',
|
||||
'danger_color' => '#dc2626',
|
||||
'success_color' => '#16a34a',
|
||||
'header_bg_color' => '#112233',
|
||||
'footer_bg_color' => '#445566',
|
||||
'header_logo_id' => $logo->id,
|
||||
'footer_logo_id' => $logo->id,
|
||||
]);
|
||||
}
|
||||
|
||||
private function createEmailIntegration(): Integration
|
||||
{
|
||||
return Integration::create([
|
||||
'integration_code' => 'email',
|
||||
'name' => 'Email',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string|int>
|
||||
*/
|
||||
private function emailData(): array
|
||||
{
|
||||
return [
|
||||
'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',
|
||||
'MAIL_FROM_NAME' => 'Acme Mail',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -1,86 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Mail;
|
||||
|
||||
use App\Domains\Attachable\Enums\AttachmentType;
|
||||
use App\Domains\Attachable\Models\Attachment;
|
||||
use App\Domains\Integration\Models\Integration;
|
||||
use App\Domains\Integration\Models\TenantIntegration;
|
||||
use App\Domains\Mail\Services\MailService;
|
||||
use App\Domains\Tenant\Models\Tenant;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Mail\Mailer;
|
||||
use Illuminate\Mail\MailManager;
|
||||
use Mockery;
|
||||
use Tests\TestCase;
|
||||
|
||||
class MailServiceTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_it_builds_an_isolated_smtp_mailer_from_the_tenant_integration(): void
|
||||
{
|
||||
$logo = Attachment::create([
|
||||
'path' => 'tenants/logo.png',
|
||||
'filename' => 'logo.png',
|
||||
'type' => AttachmentType::Image,
|
||||
'mime_type' => 'image/png',
|
||||
'extension' => 'png',
|
||||
]);
|
||||
$tenant = Tenant::create([
|
||||
'codigo' => 'acme',
|
||||
'nombre' => 'Acme Store',
|
||||
'dominio' => 'acme.example.com',
|
||||
'primary_color' => '#778899',
|
||||
'secondary_color' => '#64748b',
|
||||
'danger_color' => '#dc2626',
|
||||
'success_color' => '#16a34a',
|
||||
'header_bg_color' => '#112233',
|
||||
'footer_bg_color' => '#445566',
|
||||
'header_logo_id' => $logo->id,
|
||||
'footer_logo_id' => $logo->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',
|
||||
'MAIL_FROM_NAME' => 'Acme Mail',
|
||||
],
|
||||
]);
|
||||
|
||||
$mailer = Mockery::mock(Mailer::class);
|
||||
$mailer->shouldReceive('alwaysFrom')
|
||||
->once()
|
||||
->with('store@example.com', 'Acme Mail');
|
||||
|
||||
$manager = Mockery::mock(MailManager::class);
|
||||
$manager->shouldReceive('build')
|
||||
->once()
|
||||
->with(Mockery::on(fn (array $config): bool => $config === [
|
||||
'name' => 'tenant-smtp-acme',
|
||||
'transport' => 'smtp',
|
||||
'scheme' => 'smtp',
|
||||
'host' => 'smtp.example.com',
|
||||
'port' => 587,
|
||||
'username' => 'mailer@example.com',
|
||||
'password' => 'secret',
|
||||
'timeout' => null,
|
||||
'local_domain' => null,
|
||||
]))
|
||||
->andReturn($mailer);
|
||||
|
||||
$service = new MailService($tenant, $manager);
|
||||
|
||||
$this->assertSame('tenant-smtp', $service->mailerName());
|
||||
}
|
||||
}
|
||||
@@ -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