rework of the e2e test suites

This commit is contained in:
Codex Agent
2025-11-19 22:23:33 +01:00
parent 8d2075bdd2
commit 0127114e59
32 changed files with 1593 additions and 124 deletions

91
app/Testing/Mailbox.php Normal file
View 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();
}
}