rework of the e2e test suites
This commit is contained in:
109
app/Http/Controllers/Testing/TestCheckoutController.php
Normal file
109
app/Http/Controllers/Testing/TestCheckoutController.php
Normal file
@@ -0,0 +1,109 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Testing;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\CheckoutSession;
|
||||
use App\Services\Checkout\CheckoutWebhookService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class TestCheckoutController extends Controller
|
||||
{
|
||||
public function latest(Request $request): JsonResponse
|
||||
{
|
||||
abort_unless(app()->environment(['local', 'testing']), 404);
|
||||
|
||||
$validated = $request->validate([
|
||||
'email' => ['nullable', 'string', 'email'],
|
||||
'tenant_id' => ['nullable', 'integer'],
|
||||
'status' => ['nullable', 'string'],
|
||||
]);
|
||||
|
||||
$query = CheckoutSession::query()->latest();
|
||||
|
||||
if ($validated['email'] ?? null) {
|
||||
$query->whereHas('user', fn ($q) => $q->where('email', $validated['email']));
|
||||
}
|
||||
|
||||
if ($validated['tenant_id'] ?? null) {
|
||||
$query->where('tenant_id', $validated['tenant_id']);
|
||||
}
|
||||
|
||||
if ($validated['status'] ?? null) {
|
||||
$query->where('status', $validated['status']);
|
||||
}
|
||||
|
||||
$session = $query->first();
|
||||
|
||||
if (! $session) {
|
||||
return response()->json([
|
||||
'data' => null,
|
||||
], 404);
|
||||
}
|
||||
|
||||
$session->loadMissing(['user', 'tenant', 'package']);
|
||||
|
||||
return response()->json([
|
||||
'data' => [
|
||||
'id' => $session->id,
|
||||
'status' => $session->status,
|
||||
'provider' => $session->provider,
|
||||
'tenant_id' => $session->tenant_id,
|
||||
'package_id' => $session->package_id,
|
||||
'user_email' => $session->user?->email,
|
||||
'coupon_id' => $session->coupon_id,
|
||||
'amount_subtotal' => $session->amount_subtotal,
|
||||
'amount_total' => $session->amount_total,
|
||||
'created_at' => $session->created_at?->toIso8601String(),
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function simulatePaddle(
|
||||
Request $request,
|
||||
CheckoutWebhookService $webhooks,
|
||||
CheckoutSession $session
|
||||
): JsonResponse {
|
||||
abort_unless(app()->environment(['local', 'testing']), 404);
|
||||
|
||||
$validated = $request->validate([
|
||||
'event_type' => ['nullable', 'string'],
|
||||
'transaction_id' => ['nullable', 'string'],
|
||||
'status' => ['nullable', 'string'],
|
||||
'checkout_id' => ['nullable', 'string'],
|
||||
'metadata' => ['nullable', 'array'],
|
||||
]);
|
||||
|
||||
$eventType = $validated['event_type'] ?? 'transaction.completed';
|
||||
$metadata = array_merge([
|
||||
'tenant_id' => $session->tenant_id,
|
||||
'package_id' => $session->package_id,
|
||||
'checkout_session_id' => $session->id,
|
||||
], $validated['metadata'] ?? []);
|
||||
|
||||
$payload = [
|
||||
'event_type' => $eventType,
|
||||
'data' => array_filter([
|
||||
'id' => $validated['transaction_id'] ?? ('txn_'.Str::uuid()),
|
||||
'status' => $validated['status'] ?? 'completed',
|
||||
'metadata' => $metadata,
|
||||
'checkout_id' => $validated['checkout_id'] ?? $session->provider_metadata['paddle_checkout_id'] ?? 'chk_'.Str::uuid(),
|
||||
]),
|
||||
];
|
||||
|
||||
$handled = $webhooks->handlePaddleEvent($payload);
|
||||
|
||||
return response()->json([
|
||||
'data' => [
|
||||
'handled' => $handled,
|
||||
'session' => [
|
||||
'id' => $session->id,
|
||||
'status' => $session->fresh()->status,
|
||||
],
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
150
app/Http/Controllers/Testing/TestCouponController.php
Normal file
150
app/Http/Controllers/Testing/TestCouponController.php
Normal file
@@ -0,0 +1,150 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Testing;
|
||||
|
||||
use App\Enums\CouponStatus;
|
||||
use App\Enums\CouponType;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Coupon;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class TestCouponController extends Controller
|
||||
{
|
||||
public function store(Request $request): JsonResponse
|
||||
{
|
||||
abort_unless(app()->environment(['local', 'testing']), 404);
|
||||
|
||||
$payload = $request->input('coupons');
|
||||
$definitions = collect(is_array($payload) ? $payload : [])
|
||||
->map(fn ($definition) => $this->normalizeDefinition($definition))
|
||||
->filter()
|
||||
->values();
|
||||
|
||||
if ($definitions->isEmpty()) {
|
||||
$definitions = collect($this->defaultDefinitions());
|
||||
}
|
||||
|
||||
$created = $definitions->map(function (array $definition) {
|
||||
$coupon = Coupon::updateOrCreate(
|
||||
['code' => strtoupper($definition['code'])],
|
||||
[
|
||||
'name' => $definition['name'] ?? $definition['code'],
|
||||
'type' => CouponType::from($definition['type']),
|
||||
'status' => CouponStatus::from($definition['status'] ?? CouponStatus::ACTIVE->value),
|
||||
'amount' => $definition['amount'],
|
||||
'currency' => $definition['currency'],
|
||||
'description' => $definition['description'] ?? null,
|
||||
'enabled_for_checkout' => $definition['enabled_for_checkout'],
|
||||
'auto_apply' => $definition['auto_apply'],
|
||||
'is_stackable' => $definition['is_stackable'],
|
||||
'usage_limit' => $definition['usage_limit'],
|
||||
'per_customer_limit' => $definition['per_customer_limit'],
|
||||
'starts_at' => $definition['starts_at'],
|
||||
'ends_at' => $definition['ends_at'],
|
||||
]
|
||||
);
|
||||
|
||||
if (array_key_exists('packages', $definition)) {
|
||||
$coupon->packages()->sync($definition['packages']);
|
||||
}
|
||||
|
||||
return $coupon->fresh(['packages']);
|
||||
});
|
||||
|
||||
return response()->json([
|
||||
'data' => $created->map(fn (Coupon $coupon) => [
|
||||
'id' => $coupon->id,
|
||||
'code' => $coupon->code,
|
||||
'type' => $coupon->type->value,
|
||||
'status' => $coupon->status->value,
|
||||
]),
|
||||
]);
|
||||
}
|
||||
|
||||
private function normalizeDefinition(array $definition): array
|
||||
{
|
||||
$code = strtoupper((string) ($definition['code'] ?? ''));
|
||||
$type = $definition['type'] ?? CouponType::PERCENTAGE->value;
|
||||
|
||||
if ($code === '') {
|
||||
throw ValidationException::withMessages(['code' => 'Coupon code is required.']);
|
||||
}
|
||||
|
||||
$amount = (float) ($definition['amount'] ?? 0);
|
||||
if ($amount <= 0) {
|
||||
throw ValidationException::withMessages(['amount' => 'Coupon amount must be greater than zero.']);
|
||||
}
|
||||
|
||||
$currency = $definition['currency'] ?? null;
|
||||
if ($type !== CouponType::PERCENTAGE->value && ! $currency) {
|
||||
$currency = 'EUR';
|
||||
}
|
||||
|
||||
return [
|
||||
'code' => $code,
|
||||
'name' => $definition['name'] ?? null,
|
||||
'type' => $type,
|
||||
'status' => $definition['status'] ?? CouponStatus::ACTIVE->value,
|
||||
'amount' => $amount,
|
||||
'currency' => $currency ? strtoupper((string) $currency) : null,
|
||||
'description' => $definition['description'] ?? null,
|
||||
'enabled_for_checkout' => Arr::get($definition, 'enabled_for_checkout', true),
|
||||
'auto_apply' => Arr::get($definition, 'auto_apply', false),
|
||||
'is_stackable' => Arr::get($definition, 'is_stackable', false),
|
||||
'usage_limit' => Arr::get($definition, 'usage_limit'),
|
||||
'per_customer_limit' => Arr::get($definition, 'per_customer_limit'),
|
||||
'starts_at' => $this->parseDate($definition['starts_at'] ?? null),
|
||||
'ends_at' => $this->parseDate($definition['ends_at'] ?? null),
|
||||
'packages' => Arr::get($definition, 'packages'),
|
||||
];
|
||||
}
|
||||
|
||||
private function parseDate(mixed $value): ?Carbon
|
||||
{
|
||||
if (! $value) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($value instanceof Carbon) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
return Carbon::parse($value);
|
||||
}
|
||||
|
||||
private function defaultDefinitions(): array
|
||||
{
|
||||
return [
|
||||
[
|
||||
'code' => 'PERCENT10',
|
||||
'name' => '10% off',
|
||||
'type' => CouponType::PERCENTAGE->value,
|
||||
'amount' => 10,
|
||||
'description' => '10% discount for package flows',
|
||||
'usage_limit' => 500,
|
||||
],
|
||||
[
|
||||
'code' => 'FLAT50',
|
||||
'name' => '50 EUR off',
|
||||
'type' => CouponType::FLAT->value,
|
||||
'amount' => 50,
|
||||
'currency' => 'EUR',
|
||||
'description' => '50€ discount on qualifying packages',
|
||||
'usage_limit' => 200,
|
||||
],
|
||||
[
|
||||
'code' => 'EXPIRED25',
|
||||
'name' => 'Expired 25%',
|
||||
'type' => CouponType::PERCENTAGE->value,
|
||||
'amount' => 25,
|
||||
'description' => 'Expired coupon for error handling tests',
|
||||
'starts_at' => now()->subWeeks(2),
|
||||
'ends_at' => now()->subDays(2),
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
91
app/Http/Controllers/Testing/TestEventController.php
Normal file
91
app/Http/Controllers/Testing/TestEventController.php
Normal file
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Testing;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Event;
|
||||
use App\Models\EventJoinToken;
|
||||
use App\Services\EventJoinTokenService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use SimpleSoftwareIO\QrCode\Facades\QrCode;
|
||||
|
||||
class TestEventController extends Controller
|
||||
{
|
||||
public function joinToken(Request $request, EventJoinTokenService $tokens): JsonResponse
|
||||
{
|
||||
abort_unless(app()->environment(['local', 'testing']), 404);
|
||||
|
||||
$validated = $request->validate([
|
||||
'event_id' => ['nullable', 'integer'],
|
||||
'slug' => ['nullable', 'string'],
|
||||
'ensure_active' => ['sometimes', 'boolean'],
|
||||
]);
|
||||
|
||||
$eventId = $validated['event_id'] ?? null;
|
||||
$slug = $validated['slug'] ?? null;
|
||||
|
||||
if (! $eventId && ! $slug) {
|
||||
throw ValidationException::withMessages([
|
||||
'event_id' => 'Provide either event_id or slug.',
|
||||
]);
|
||||
}
|
||||
|
||||
$eventQuery = Event::query();
|
||||
|
||||
if ($eventId) {
|
||||
$eventQuery->whereKey($eventId);
|
||||
} else {
|
||||
$eventQuery->where('slug', $slug);
|
||||
}
|
||||
|
||||
/** @var Event|null $event */
|
||||
$event = $eventQuery->first();
|
||||
|
||||
if (! $event) {
|
||||
return response()->json([
|
||||
'data' => null,
|
||||
], 404);
|
||||
}
|
||||
|
||||
/** @var EventJoinToken|null $token */
|
||||
$token = $event->joinTokens()->latest()->first();
|
||||
|
||||
if (! $token || (($validated['ensure_active'] ?? true) && ! $token->isActive())) {
|
||||
$token = $tokens->createToken($event, [
|
||||
'label' => 'Automation',
|
||||
'metadata' => [
|
||||
'generated_for' => 'testing_api',
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
$plainToken = $token->token;
|
||||
|
||||
if (! $plainToken) {
|
||||
throw ValidationException::withMessages([
|
||||
'token' => 'Failed to resolve token value.',
|
||||
]);
|
||||
}
|
||||
|
||||
$joinUrl = route('guest.event', ['token' => $plainToken]);
|
||||
|
||||
$qrSvg = QrCode::format('svg')
|
||||
->size(256)
|
||||
->generate($joinUrl);
|
||||
|
||||
return response()->json([
|
||||
'data' => [
|
||||
'event_id' => $event->id,
|
||||
'token_id' => $token->id,
|
||||
'token' => $plainToken,
|
||||
'join_url' => $joinUrl,
|
||||
'qr_svg' => $qrSvg,
|
||||
'expires_at' => $token->expires_at?->toIso8601String(),
|
||||
'usage_count' => $token->usage_count,
|
||||
'usage_limit' => $token->usage_limit,
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
192
app/Http/Controllers/Testing/TestGuestEventController.php
Normal file
192
app/Http/Controllers/Testing/TestGuestEventController.php
Normal file
@@ -0,0 +1,192 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Testing;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Event;
|
||||
use App\Models\EventType;
|
||||
use App\Models\Task;
|
||||
use App\Models\Tenant;
|
||||
use App\Models\User;
|
||||
use App\Services\EventJoinTokenService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class TestGuestEventController extends Controller
|
||||
{
|
||||
public function store(Request $request, EventJoinTokenService $joinTokens): JsonResponse
|
||||
{
|
||||
abort_unless(app()->environment(['local', 'testing']), 404);
|
||||
|
||||
$validated = $request->validate([
|
||||
'slug' => ['nullable', 'string', 'max:100'],
|
||||
'name' => ['nullable', 'string', 'max:255'],
|
||||
'date' => ['nullable', 'date'],
|
||||
'event_type' => ['nullable', 'string', 'max:100'],
|
||||
'tasks' => ['nullable', 'array'],
|
||||
'tasks.*.slug' => ['required_with:tasks', 'string', 'max:120'],
|
||||
'tasks.*.title' => ['required_with:tasks', 'string', 'max:255'],
|
||||
'tasks.*.description' => ['nullable', 'string', 'max:1000'],
|
||||
]);
|
||||
|
||||
$slug = Str::slug($validated['slug'] ?? 'pwa-demo-event');
|
||||
|
||||
[$event, $tokenValue] = DB::transaction(function () use ($validated, $slug, $joinTokens) {
|
||||
$tenant = $this->ensureTenant();
|
||||
$eventType = $this->ensureEventType($validated['event_type'] ?? 'wedding');
|
||||
|
||||
$event = Event::updateOrCreate(
|
||||
['slug' => $slug],
|
||||
[
|
||||
'tenant_id' => $tenant->id,
|
||||
'event_type_id' => $eventType->id,
|
||||
'name' => [
|
||||
'de' => $validated['name'] ?? 'PWA Demo Event',
|
||||
'en' => $validated['name'] ?? 'PWA Demo Event',
|
||||
],
|
||||
'description' => [
|
||||
'de' => 'Automatisches Demo-Event für Playwright.',
|
||||
'en' => 'Automated demo event for Playwright.',
|
||||
],
|
||||
'default_locale' => 'de',
|
||||
'status' => 'published',
|
||||
'is_active' => true,
|
||||
'date' => ($validated['date'] ?? Carbon::now()->addWeeks(2)->toDateString()),
|
||||
'settings' => [
|
||||
'branding' => [
|
||||
'primary_color' => '#f43f5e',
|
||||
'secondary_color' => '#fb7185',
|
||||
'font_family' => 'Inter, sans-serif',
|
||||
],
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
$taskIds = $this->ensureTasks($tenant->id, $eventType->id, $validated['tasks'] ?? null);
|
||||
if ($taskIds !== []) {
|
||||
$event->tasks()->syncWithoutDetaching($taskIds);
|
||||
}
|
||||
|
||||
$token = $event->joinTokens()->latest()->first();
|
||||
if (! $token || ! $token->isActive()) {
|
||||
$token = $joinTokens->createToken($event, [
|
||||
'label' => 'Testing Automation',
|
||||
'metadata' => ['generator' => 'testing_api'],
|
||||
]);
|
||||
}
|
||||
|
||||
return [$event, $token?->token];
|
||||
});
|
||||
|
||||
return response()->json([
|
||||
'data' => [
|
||||
'event_id' => $event->id,
|
||||
'slug' => $event->slug,
|
||||
'name' => $event->name,
|
||||
'join_token' => $tokenValue,
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
private function ensureTenant(): Tenant
|
||||
{
|
||||
$user = User::firstOrCreate(
|
||||
['email' => 'guest-suite@example.com'],
|
||||
[
|
||||
'name' => 'Guest Suite Admin',
|
||||
'first_name' => 'Guest',
|
||||
'last_name' => 'Suite',
|
||||
'password' => Hash::make('Password123!'),
|
||||
'role' => 'tenant_admin',
|
||||
]
|
||||
);
|
||||
|
||||
$tenant = Tenant::firstOrCreate(
|
||||
['slug' => 'guest-suite-tenant'],
|
||||
[
|
||||
'user_id' => $user->id,
|
||||
'name' => 'Guest Suite Tenant',
|
||||
'email' => $user->email,
|
||||
'is_active' => true,
|
||||
]
|
||||
);
|
||||
|
||||
if (! $user->tenant_id) {
|
||||
$user->forceFill(['tenant_id' => $tenant->id])->save();
|
||||
}
|
||||
|
||||
return $tenant;
|
||||
}
|
||||
|
||||
private function ensureEventType(string $slug): EventType
|
||||
{
|
||||
$slug = Str::slug($slug) ?: 'wedding';
|
||||
|
||||
return EventType::updateOrCreate(
|
||||
['slug' => $slug],
|
||||
[
|
||||
'name' => [
|
||||
'de' => Str::title($slug),
|
||||
'en' => Str::title($slug),
|
||||
],
|
||||
'settings' => [],
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, int>
|
||||
*/
|
||||
private function ensureTasks(int $tenantId, int $eventTypeId, ?array $payload): array
|
||||
{
|
||||
$definitions = $payload ?: [
|
||||
[
|
||||
'slug' => 'guest-demo-rings',
|
||||
'title' => 'Ringe im Fokus',
|
||||
'description' => 'Halte die Ringe oder eure Hände mit einem kreativen Hintergrund fest.',
|
||||
],
|
||||
[
|
||||
'slug' => 'guest-demo-dancefloor',
|
||||
'title' => 'Tanzfläche in Bewegung',
|
||||
'description' => 'Zeigt uns eure besten Moves – gerne mit Motion Blur.',
|
||||
],
|
||||
[
|
||||
'slug' => 'guest-demo-cheers',
|
||||
'title' => 'Cheers!',
|
||||
'description' => 'Ein Toast-Moment mit Gläsern oder Konfetti.',
|
||||
],
|
||||
];
|
||||
|
||||
$ids = [];
|
||||
foreach ($definitions as $index => $definition) {
|
||||
$task = Task::updateOrCreate(
|
||||
['slug' => $definition['slug']],
|
||||
[
|
||||
'tenant_id' => $tenantId,
|
||||
'event_type_id' => $eventTypeId,
|
||||
'title' => [
|
||||
'de' => $definition['title'],
|
||||
'en' => $definition['title'],
|
||||
],
|
||||
'description' => [
|
||||
'de' => $definition['description'] ?? $definition['title'],
|
||||
'en' => $definition['description'] ?? $definition['title'],
|
||||
],
|
||||
'instructions' => [
|
||||
'de' => 'Creatives Willkommen',
|
||||
'en' => 'Get creative',
|
||||
],
|
||||
'priority' => $index === 0 ? 'high' : 'medium',
|
||||
]
|
||||
);
|
||||
|
||||
$ids[] = $task->id;
|
||||
}
|
||||
|
||||
return $ids;
|
||||
}
|
||||
}
|
||||
30
app/Http/Controllers/Testing/TestMailboxController.php
Normal file
30
app/Http/Controllers/Testing/TestMailboxController.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Testing;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Testing\Mailbox;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
class TestMailboxController extends Controller
|
||||
{
|
||||
public function index(): JsonResponse
|
||||
{
|
||||
abort_unless(app()->environment(['local', 'testing']), 404);
|
||||
|
||||
return response()->json([
|
||||
'data' => Mailbox::all(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function destroy(): JsonResponse
|
||||
{
|
||||
abort_unless(app()->environment(['local', 'testing']), 404);
|
||||
|
||||
Mailbox::flush();
|
||||
|
||||
return response()->json([
|
||||
'status' => 'ok',
|
||||
]);
|
||||
}
|
||||
}
|
||||
42
app/Http/Middleware/HandleInvalidSignedUrl.php
Normal file
42
app/Http/Middleware/HandleInvalidSignedUrl.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Routing\Exceptions\InvalidSignatureException;
|
||||
|
||||
class HandleInvalidSignedUrl
|
||||
{
|
||||
public function handle(Request $request, Closure $next)
|
||||
{
|
||||
try {
|
||||
return $next($request);
|
||||
} catch (InvalidSignatureException $exception) {
|
||||
if ($request->expectsJson()) {
|
||||
throw $exception;
|
||||
}
|
||||
|
||||
if ($this->isVerificationRoute($request)) {
|
||||
$request->session()->flash('verification', [
|
||||
'status' => 'error',
|
||||
'title' => __('auth.verification.expired_title'),
|
||||
'message' => __('auth.verification.expired_message'),
|
||||
]);
|
||||
|
||||
return redirect()->route('verification.notice');
|
||||
}
|
||||
|
||||
throw $exception;
|
||||
}
|
||||
}
|
||||
|
||||
private function isVerificationRoute(Request $request): bool
|
||||
{
|
||||
if ($request->route()?->getName() === 'verification.verify') {
|
||||
return true;
|
||||
}
|
||||
|
||||
return str_starts_with($request->path(), 'verify-email/');
|
||||
}
|
||||
}
|
||||
@@ -35,13 +35,16 @@ use App\Services\Checkout\CheckoutSessionService;
|
||||
use App\Services\Security\PhotoSecurityScanner;
|
||||
use App\Services\Storage\EventStorageManager;
|
||||
use App\Services\Storage\StorageHealthService;
|
||||
use App\Testing\Mailbox;
|
||||
use Illuminate\Cache\RateLimiting\Limit;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Mail\Events\MessageSent;
|
||||
use Illuminate\Queue\Events\JobFailed;
|
||||
use Illuminate\Support\Facades\Event as EventFacade;
|
||||
use Illuminate\Support\Facades\Notification;
|
||||
use Illuminate\Support\Facades\Queue;
|
||||
use Illuminate\Support\Facades\RateLimiter;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Illuminate\Support\Facades\URL;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Inertia\Inertia;
|
||||
@@ -70,6 +73,8 @@ class AppServiceProvider extends ServiceProvider
|
||||
URL::forceScheme('https');
|
||||
}
|
||||
|
||||
Route::aliasMiddleware('signed', \App\Http\Middleware\ValidateSignature::class);
|
||||
|
||||
$this->app->make(EventStorageManager::class)->registerDynamicDisks();
|
||||
|
||||
EventFacade::listen(
|
||||
@@ -137,6 +142,13 @@ class AppServiceProvider extends ServiceProvider
|
||||
[DispatchGuestNotificationPush::class, 'handle']
|
||||
);
|
||||
|
||||
if ($this->app->environment(['local', 'testing'])) {
|
||||
EventFacade::listen(
|
||||
MessageSent::class,
|
||||
[Mailbox::class, 'record']
|
||||
);
|
||||
}
|
||||
|
||||
RateLimiter::for('tenant-api', function (Request $request) {
|
||||
$tenantId = $request->attributes->get('tenant_id')
|
||||
?? $request->user()?->tenant_id
|
||||
|
||||
91
app/Testing/Mailbox.php
Normal file
91
app/Testing/Mailbox.php
Normal file
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
namespace App\Testing;
|
||||
|
||||
use Illuminate\Mail\Events\MessageSent;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
use Symfony\Component\Mime\Address;
|
||||
|
||||
class Mailbox
|
||||
{
|
||||
private const string STORAGE_PATH = 'testing/mailbox.json';
|
||||
|
||||
public static function record(MessageSent $event): void
|
||||
{
|
||||
if (! app()->environment(['local', 'testing'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
$messages = self::read();
|
||||
$messages[] = [
|
||||
'id' => (string) Str::uuid(),
|
||||
'subject' => (string) $event->message->getSubject(),
|
||||
'from' => self::formatAddresses($event->message->getFrom()),
|
||||
'to' => self::formatAddresses($event->message->getTo()),
|
||||
'cc' => self::formatAddresses($event->message->getCc()),
|
||||
'bcc' => self::formatAddresses($event->message->getBcc()),
|
||||
'html' => method_exists($event->message, 'getHtmlBody') ? $event->message->getHtmlBody() : null,
|
||||
'text' => method_exists($event->message, 'getTextBody') ? $event->message->getTextBody() : null,
|
||||
'sent_at' => now()->toIso8601String(),
|
||||
'headers' => (string) $event->message->getHeaders(),
|
||||
];
|
||||
|
||||
self::write($messages);
|
||||
}
|
||||
|
||||
public static function all(): array
|
||||
{
|
||||
return self::read();
|
||||
}
|
||||
|
||||
public static function flush(): void
|
||||
{
|
||||
self::write([]);
|
||||
}
|
||||
|
||||
private static function read(): array
|
||||
{
|
||||
$disk = Storage::disk('local');
|
||||
|
||||
if (! $disk->exists(self::STORAGE_PATH)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$decoded = json_decode($disk->get(self::STORAGE_PATH), true);
|
||||
|
||||
return is_array($decoded) ? $decoded : [];
|
||||
}
|
||||
|
||||
private static function write(array $messages): void
|
||||
{
|
||||
$disk = Storage::disk('local');
|
||||
|
||||
$disk->put(self::STORAGE_PATH, json_encode($messages, JSON_PRETTY_PRINT));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Address[]|array|null $addresses
|
||||
*/
|
||||
private static function formatAddresses(?array $addresses): array
|
||||
{
|
||||
return Collection::make($addresses)
|
||||
->filter()
|
||||
->map(function ($address) {
|
||||
if ($address instanceof Address) {
|
||||
return [
|
||||
'name' => $address->getName() ?: null,
|
||||
'email' => $address->getAddress(),
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
'name' => null,
|
||||
'email' => (string) $address,
|
||||
];
|
||||
})
|
||||
->values()
|
||||
->all();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user