feat(mail-test): implement email testing functionality
- Created MailTestController to handle email sending requests. - Added SendTestMailRequest for validating email input. - Developed MailTestService to manage email sending logic. - Introduced TestMail Mailable for formatting test emails. - Defined API route for sending test emails. - Created EmailIntegrationSeeder to seed email integration settings. - Updated DatabaseSeeder to include EmailIntegrationSeeder. - Added MailTestControllerTest to ensure email sending functionality works as expected.
This commit is contained in:
@@ -51,10 +51,10 @@ REDIS_HOST=127.0.0.1
|
||||
REDIS_PASSWORD=null
|
||||
REDIS_PORT=6379
|
||||
|
||||
MAIL_MAILER=log
|
||||
MAIL_SCHEME=null
|
||||
MAIL_HOST=127.0.0.1
|
||||
MAIL_PORT=2525
|
||||
MAIL_MAILER=smtp
|
||||
MAIL_SCHEME=smtp
|
||||
MAIL_HOST=smtp.gmail.com
|
||||
MAIL_PORT=587
|
||||
MAIL_USERNAME=null
|
||||
MAIL_PASSWORD=null
|
||||
MAIL_FROM_ADDRESS="hello@example.com"
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
26
app/Domains/MailTest/Controllers/MailTestController.php
Normal file
26
app/Domains/MailTest/Controllers/MailTestController.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\MailTest\Controllers;
|
||||
|
||||
use App\Domains\MailTest\Requests\SendTestMailRequest;
|
||||
use App\Domains\MailTest\Services\MailTestService;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
class MailTestController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
protected MailTestService $mailTestService,
|
||||
) {}
|
||||
|
||||
public function __invoke(SendTestMailRequest $request): JsonResponse
|
||||
{
|
||||
return response()->json(
|
||||
$this->mailTestService->send(
|
||||
$request->validated('to'),
|
||||
$request->validated('subject'),
|
||||
$request->validated('message'),
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
37
app/Domains/MailTest/Mailables/TestMail.php
Normal file
37
app/Domains/MailTest/Mailables/TestMail.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\MailTest\Mailables;
|
||||
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Mail\Mailable;
|
||||
use Illuminate\Mail\Mailables\Content;
|
||||
use Illuminate\Mail\Mailables\Envelope;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class TestMail extends Mailable
|
||||
{
|
||||
use Queueable, SerializesModels;
|
||||
|
||||
public function __construct(
|
||||
public readonly string $mailSubject,
|
||||
public readonly string $mailMessage,
|
||||
) {}
|
||||
|
||||
public function envelope(): Envelope
|
||||
{
|
||||
return new Envelope(subject: $this->mailSubject);
|
||||
}
|
||||
|
||||
public function content(): Content
|
||||
{
|
||||
$message = nl2br(e($this->mailMessage));
|
||||
|
||||
return new Content(
|
||||
htmlString: <<<HTML
|
||||
<h1>Prueba de correo de Shopit</h1>
|
||||
<p>{$message}</p>
|
||||
<p><small>Si recibiste este mensaje, la configuracion de correo funciona correctamente.</small></p>
|
||||
HTML,
|
||||
);
|
||||
}
|
||||
}
|
||||
25
app/Domains/MailTest/Requests/SendTestMailRequest.php
Normal file
25
app/Domains/MailTest/Requests/SendTestMailRequest.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\MailTest\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class SendTestMailRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, array<int, string>>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'to' => ['required', 'string', 'email', 'max:255'],
|
||||
'subject' => ['nullable', 'string', 'max:255'],
|
||||
'message' => ['nullable', 'string', 'max:5000'],
|
||||
];
|
||||
}
|
||||
}
|
||||
27
app/Domains/MailTest/Services/MailTestService.php
Normal file
27
app/Domains/MailTest/Services/MailTestService.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\MailTest\Services;
|
||||
|
||||
use App\Domains\MailTest\Mailables\TestMail;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
|
||||
class MailTestService
|
||||
{
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function send(string $recipient, ?string $subject = null, ?string $message = null): array
|
||||
{
|
||||
$subject ??= 'Prueba de correo de Shopit';
|
||||
$message ??= 'Este es un correo de prueba enviado desde Shopit.';
|
||||
|
||||
Mail::to($recipient)->send(new TestMail($subject, $message));
|
||||
|
||||
return [
|
||||
'message' => 'Correo de prueba enviado correctamente.',
|
||||
'recipient' => $recipient,
|
||||
'mailer' => (string) config('mail.default'),
|
||||
'sent_at' => now()->toIso8601String(),
|
||||
];
|
||||
}
|
||||
}
|
||||
6
app/Domains/MailTest/routes/api.php
Normal file
6
app/Domains/MailTest/routes/api.php
Normal file
@@ -0,0 +1,6 @@
|
||||
<?php
|
||||
|
||||
use App\Domains\MailTest\Controllers\MailTestController;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
||||
Route::post('mail-test/send', MailTestController::class);
|
||||
@@ -30,6 +30,7 @@ class DatabaseSeeder extends Seeder
|
||||
ProductCatalogFromImagesSeeder::class,
|
||||
FiestaFutbolInfantilProductSeeder::class,
|
||||
TelepagosIntegrationSeeder::class,
|
||||
EmailIntegrationSeeder::class,
|
||||
MenuSeeder::class,
|
||||
]);
|
||||
}
|
||||
|
||||
32
database/seeders/EmailIntegrationSeeder.php
Normal file
32
database/seeders/EmailIntegrationSeeder.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use App\Domains\Integration\Models\Integration;
|
||||
use Illuminate\Database\Seeder;
|
||||
|
||||
class EmailIntegrationSeeder extends Seeder
|
||||
{
|
||||
/**
|
||||
* Run the database seeds.
|
||||
*/
|
||||
public function run(): void
|
||||
{
|
||||
Integration::updateOrCreate(
|
||||
['integration_code' => 'email'],
|
||||
[
|
||||
'name' => 'Email',
|
||||
'url' => null,
|
||||
'integration_data_schema' => [
|
||||
'MAIL_MAILER' => 'required|string|in:smtp',
|
||||
'MAIL_SCHEME' => 'required|string|in:smtp',
|
||||
'MAIL_HOST' => 'required|string|in:smtp.gmail.com',
|
||||
'MAIL_PORT' => 'required|integer|in:587',
|
||||
'MAIL_USERNAME' => 'required|email',
|
||||
'MAIL_PASSWORD' => 'required|string',
|
||||
'MAIL_FROM_ADDRESS' => 'required|email',
|
||||
],
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ require __DIR__.'/../app/Domains/Auth/routes/api.php';
|
||||
require __DIR__.'/../app/Domains/Catalog/routes/api.php';
|
||||
require __DIR__.'/../app/Domains/Cart/routes/api.php';
|
||||
require __DIR__.'/../app/Domains/StorageTest/routes/api.php';
|
||||
require __DIR__.'/../app/Domains/MailTest/routes/api.php';
|
||||
require __DIR__.'/../app/Domains/Purchase/routes/api.php';
|
||||
require __DIR__.'/../app/Domains/Tenant/routes/api.php';
|
||||
require __DIR__.'/../app/Domains/Integration/routes/api.php';
|
||||
|
||||
59
tests/Feature/MailTest/MailTestControllerTest.php
Normal file
59
tests/Feature/MailTest/MailTestControllerTest.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\MailTest;
|
||||
|
||||
use App\Domains\MailTest\Mailables\TestMail;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use Tests\TestCase;
|
||||
|
||||
class MailTestControllerTest extends TestCase
|
||||
{
|
||||
public function test_it_sends_a_test_email(): void
|
||||
{
|
||||
Mail::fake();
|
||||
|
||||
$response = $this->postJson('/api/mail-test/send', [
|
||||
'to' => 'recipient@example.com',
|
||||
'subject' => 'SMTP test',
|
||||
'message' => 'Test message',
|
||||
]);
|
||||
|
||||
$response->assertOk()
|
||||
->assertJsonPath('message', 'Correo de prueba enviado correctamente.')
|
||||
->assertJsonPath('recipient', 'recipient@example.com')
|
||||
->assertJsonPath('mailer', 'array')
|
||||
->assertJsonStructure(['sent_at']);
|
||||
|
||||
Mail::assertSent(TestMail::class, function (TestMail $mail): bool {
|
||||
return $mail->hasTo('recipient@example.com')
|
||||
&& $mail->mailSubject === 'SMTP test'
|
||||
&& $mail->mailMessage === 'Test message';
|
||||
});
|
||||
}
|
||||
|
||||
public function test_it_uses_default_content_when_optional_fields_are_omitted(): void
|
||||
{
|
||||
Mail::fake();
|
||||
|
||||
$this->postJson('/api/mail-test/send', [
|
||||
'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.';
|
||||
});
|
||||
}
|
||||
|
||||
public function test_it_validates_the_recipient(): void
|
||||
{
|
||||
Mail::fake();
|
||||
|
||||
$this->postJson('/api/mail-test/send', [
|
||||
'to' => 'invalid-email',
|
||||
])->assertUnprocessable()
|
||||
->assertJsonValidationErrors(['to']);
|
||||
|
||||
Mail::assertNothingSent();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user